FormatTest.cpp 308 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #define DEBUG_TYPE "format-test"
  10. #include "FormatTestUtils.h"
  11. #include "clang/Format/Format.h"
  12. #include "llvm/Support/Debug.h"
  13. #include "gtest/gtest.h"
  14. namespace clang {
  15. namespace format {
  16. FormatStyle getGoogleStyle() {
  17. return getGoogleStyle(FormatStyle::LK_Cpp);
  18. }
  19. class FormatTest : public ::testing::Test {
  20. protected:
  21. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
  22. const FormatStyle &Style) {
  23. DEBUG(llvm::errs() << "---\n");
  24. DEBUG(llvm::errs() << Code << "\n\n");
  25. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  26. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  27. ReplacementCount = Replaces.size();
  28. std::string Result = applyAllReplacements(Code, Replaces);
  29. EXPECT_NE("", Result);
  30. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  31. return Result;
  32. }
  33. std::string
  34. format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
  35. return format(Code, 0, Code.size(), Style);
  36. }
  37. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  38. FormatStyle Style = getLLVMStyle();
  39. Style.ColumnLimit = ColumnLimit;
  40. return Style;
  41. }
  42. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  43. FormatStyle Style = getGoogleStyle();
  44. Style.ColumnLimit = ColumnLimit;
  45. return Style;
  46. }
  47. void verifyFormat(llvm::StringRef Code,
  48. const FormatStyle &Style = getLLVMStyle()) {
  49. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  50. }
  51. void verifyGoogleFormat(llvm::StringRef Code) {
  52. verifyFormat(Code, getGoogleStyle());
  53. }
  54. void verifyIndependentOfContext(llvm::StringRef text) {
  55. verifyFormat(text);
  56. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  57. }
  58. int ReplacementCount;
  59. };
  60. TEST_F(FormatTest, MessUp) {
  61. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  62. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  63. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  64. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  65. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  66. }
  67. //===----------------------------------------------------------------------===//
  68. // Basic function tests.
  69. //===----------------------------------------------------------------------===//
  70. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  71. EXPECT_EQ(";", format(";"));
  72. }
  73. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  74. EXPECT_EQ("int i;", format(" int i;"));
  75. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  76. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  77. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  78. }
  79. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  80. EXPECT_EQ("int i;", format("int\ni;"));
  81. }
  82. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  83. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  84. }
  85. TEST_F(FormatTest, FormatsNestedCall) {
  86. verifyFormat("Method(f1, f2(f3));");
  87. verifyFormat("Method(f1(f2, f3()));");
  88. verifyFormat("Method(f1(f2, (f3())));");
  89. }
  90. TEST_F(FormatTest, NestedNameSpecifiers) {
  91. verifyFormat("vector<::Type> v;");
  92. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  93. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  94. }
  95. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  96. EXPECT_EQ("if (a) {\n"
  97. " f();\n"
  98. "}",
  99. format("if(a){f();}"));
  100. EXPECT_EQ(4, ReplacementCount);
  101. EXPECT_EQ("if (a) {\n"
  102. " f();\n"
  103. "}",
  104. format("if (a) {\n"
  105. " f();\n"
  106. "}"));
  107. EXPECT_EQ(0, ReplacementCount);
  108. }
  109. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  110. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  111. EXPECT_EQ("int a;", format("int a; "));
  112. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  113. EXPECT_EQ("int a;\nint b; ",
  114. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  115. }
  116. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  117. EXPECT_EQ("int b;\nint a;",
  118. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  119. EXPECT_EQ("int b;\n int a;",
  120. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  121. EXPECT_EQ("#define A \\\n"
  122. " int a; \\\n"
  123. " int b;",
  124. format("#define A \\\n"
  125. " int a; \\\n"
  126. " int b;",
  127. 26, 0, getLLVMStyleWithColumns(12)));
  128. EXPECT_EQ("#define A \\\n"
  129. " int a; \\\n"
  130. " int b;",
  131. format("#define A \\\n"
  132. " int a; \\\n"
  133. " int b;",
  134. 25, 0, getLLVMStyleWithColumns(12)));
  135. }
  136. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  137. EXPECT_EQ("int a;\n\n int b;",
  138. format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
  139. EXPECT_EQ("int a;\n\n int b;",
  140. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  141. }
  142. TEST_F(FormatTest, RemovesEmptyLines) {
  143. EXPECT_EQ("class C {\n"
  144. " int i;\n"
  145. "};",
  146. format("class C {\n"
  147. " int i;\n"
  148. "\n"
  149. "};"));
  150. // Don't remove empty lines at the start of namespaces.
  151. EXPECT_EQ("namespace N {\n"
  152. "\n"
  153. "int i;\n"
  154. "}",
  155. format("namespace N {\n"
  156. "\n"
  157. "int i;\n"
  158. "}",
  159. getGoogleStyle()));
  160. // Remove empty lines at the beginning and end of blocks.
  161. EXPECT_EQ("void f() {\n"
  162. "\n"
  163. " if (a) {\n"
  164. "\n"
  165. " f();\n"
  166. " }\n"
  167. "}",
  168. format("void f() {\n"
  169. "\n"
  170. " if (a) {\n"
  171. "\n"
  172. " f();\n"
  173. "\n"
  174. " }\n"
  175. "\n"
  176. "}",
  177. getLLVMStyle()));
  178. EXPECT_EQ("void f() {\n"
  179. " if (a) {\n"
  180. " f();\n"
  181. " }\n"
  182. "}",
  183. format("void f() {\n"
  184. "\n"
  185. " if (a) {\n"
  186. "\n"
  187. " f();\n"
  188. "\n"
  189. " }\n"
  190. "\n"
  191. "}",
  192. getGoogleStyle()));
  193. // Don't remove empty lines in more complex control statements.
  194. EXPECT_EQ("void f() {\n"
  195. " if (a) {\n"
  196. " f();\n"
  197. "\n"
  198. " } else if (b) {\n"
  199. " f();\n"
  200. " }\n"
  201. "}",
  202. format("void f() {\n"
  203. " if (a) {\n"
  204. " f();\n"
  205. "\n"
  206. " } else if (b) {\n"
  207. " f();\n"
  208. "\n"
  209. " }\n"
  210. "\n"
  211. "}"));
  212. // FIXME: This is slightly inconsistent.
  213. EXPECT_EQ("namespace {\n"
  214. "int i;\n"
  215. "}",
  216. format("namespace {\n"
  217. "int i;\n"
  218. "\n"
  219. "}"));
  220. EXPECT_EQ("namespace {\n"
  221. "int i;\n"
  222. "\n"
  223. "} // namespace",
  224. format("namespace {\n"
  225. "int i;\n"
  226. "\n"
  227. "} // namespace"));
  228. }
  229. TEST_F(FormatTest, ReformatsMovedLines) {
  230. EXPECT_EQ(
  231. "template <typename T> T *getFETokenInfo() const {\n"
  232. " return static_cast<T *>(FETokenInfo);\n"
  233. "}\n"
  234. " int a; // <- Should not be formatted",
  235. format(
  236. "template<typename T>\n"
  237. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  238. " int a; // <- Should not be formatted",
  239. 9, 5, getLLVMStyle()));
  240. }
  241. //===----------------------------------------------------------------------===//
  242. // Tests for control statements.
  243. //===----------------------------------------------------------------------===//
  244. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  245. verifyFormat("if (true)\n f();\ng();");
  246. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  247. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  248. FormatStyle AllowsMergedIf = getLLVMStyle();
  249. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  250. verifyFormat("if (a)\n"
  251. " // comment\n"
  252. " f();",
  253. AllowsMergedIf);
  254. verifyFormat("if (a)\n"
  255. " ;",
  256. AllowsMergedIf);
  257. verifyFormat("if (a)\n"
  258. " if (b) return;",
  259. AllowsMergedIf);
  260. verifyFormat("if (a) // Can't merge this\n"
  261. " f();\n",
  262. AllowsMergedIf);
  263. verifyFormat("if (a) /* still don't merge */\n"
  264. " f();",
  265. AllowsMergedIf);
  266. verifyFormat("if (a) { // Never merge this\n"
  267. " f();\n"
  268. "}",
  269. AllowsMergedIf);
  270. verifyFormat("if (a) {/* Never merge this */\n"
  271. " f();\n"
  272. "}",
  273. AllowsMergedIf);
  274. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  275. EXPECT_EQ("if (a) return; // comment",
  276. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  277. AllowsMergedIf.ColumnLimit = 14;
  278. verifyFormat("if (a) return;", AllowsMergedIf);
  279. verifyFormat("if (aaaaaaaaa)\n"
  280. " return;",
  281. AllowsMergedIf);
  282. AllowsMergedIf.ColumnLimit = 13;
  283. verifyFormat("if (a)\n return;", AllowsMergedIf);
  284. }
  285. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  286. FormatStyle AllowsMergedLoops = getLLVMStyle();
  287. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  288. verifyFormat("while (true) continue;", AllowsMergedLoops);
  289. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  290. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  291. verifyFormat("while (true)\n"
  292. " ;",
  293. AllowsMergedLoops);
  294. verifyFormat("for (;;)\n"
  295. " ;",
  296. AllowsMergedLoops);
  297. verifyFormat("for (;;)\n"
  298. " for (;;) continue;",
  299. AllowsMergedLoops);
  300. verifyFormat("for (;;) // Can't merge this\n"
  301. " continue;",
  302. AllowsMergedLoops);
  303. verifyFormat("for (;;) /* still don't merge */\n"
  304. " continue;",
  305. AllowsMergedLoops);
  306. }
  307. TEST_F(FormatTest, ParseIfElse) {
  308. verifyFormat("if (true)\n"
  309. " if (true)\n"
  310. " if (true)\n"
  311. " f();\n"
  312. " else\n"
  313. " g();\n"
  314. " else\n"
  315. " h();\n"
  316. "else\n"
  317. " i();");
  318. verifyFormat("if (true)\n"
  319. " if (true)\n"
  320. " if (true) {\n"
  321. " if (true)\n"
  322. " f();\n"
  323. " } else {\n"
  324. " g();\n"
  325. " }\n"
  326. " else\n"
  327. " h();\n"
  328. "else {\n"
  329. " i();\n"
  330. "}");
  331. verifyFormat("void f() {\n"
  332. " if (a) {\n"
  333. " } else {\n"
  334. " }\n"
  335. "}");
  336. }
  337. TEST_F(FormatTest, ElseIf) {
  338. verifyFormat("if (a) {\n} else if (b) {\n}");
  339. verifyFormat("if (a)\n"
  340. " f();\n"
  341. "else if (b)\n"
  342. " g();\n"
  343. "else\n"
  344. " h();");
  345. verifyFormat("if (a) {\n"
  346. " f();\n"
  347. "}\n"
  348. "// or else ..\n"
  349. "else {\n"
  350. " g()\n"
  351. "}");
  352. }
  353. TEST_F(FormatTest, FormatsForLoop) {
  354. verifyFormat(
  355. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  356. " ++VeryVeryLongLoopVariable)\n"
  357. " ;");
  358. verifyFormat("for (;;)\n"
  359. " f();");
  360. verifyFormat("for (;;) {\n}");
  361. verifyFormat("for (;;) {\n"
  362. " f();\n"
  363. "}");
  364. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  365. verifyFormat(
  366. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  367. " E = UnwrappedLines.end();\n"
  368. " I != E; ++I) {\n}");
  369. verifyFormat(
  370. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  371. " ++IIIII) {\n}");
  372. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  373. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  374. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  375. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  376. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  377. " E = FD->getDeclsInPrototypeScope().end();\n"
  378. " I != E; ++I) {\n}");
  379. // FIXME: Not sure whether we want extra identation in line 3 here:
  380. verifyFormat(
  381. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  382. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  383. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  384. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  385. " ++aaaaaaaaaaa) {\n}");
  386. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  387. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  388. "}");
  389. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  390. " aaaaaaaaaa);\n"
  391. " iter; ++iter) {\n"
  392. "}");
  393. FormatStyle NoBinPacking = getLLVMStyle();
  394. NoBinPacking.BinPackParameters = false;
  395. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  396. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  397. " aaaaaaaaaaaaaaaa,\n"
  398. " aaaaaaaaaaaaaaaa,\n"
  399. " aaaaaaaaaaaaaaaa);\n"
  400. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  401. "}",
  402. NoBinPacking);
  403. verifyFormat(
  404. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  405. " E = UnwrappedLines.end();\n"
  406. " I != E;\n"
  407. " ++I) {\n}",
  408. NoBinPacking);
  409. }
  410. TEST_F(FormatTest, RangeBasedForLoops) {
  411. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  412. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  413. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  414. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  415. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  416. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  417. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  418. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  419. }
  420. TEST_F(FormatTest, FormatsWhileLoop) {
  421. verifyFormat("while (true) {\n}");
  422. verifyFormat("while (true)\n"
  423. " f();");
  424. verifyFormat("while () {\n}");
  425. verifyFormat("while () {\n"
  426. " f();\n"
  427. "}");
  428. }
  429. TEST_F(FormatTest, FormatsDoWhile) {
  430. verifyFormat("do {\n"
  431. " do_something();\n"
  432. "} while (something());");
  433. verifyFormat("do\n"
  434. " do_something();\n"
  435. "while (something());");
  436. }
  437. TEST_F(FormatTest, FormatsSwitchStatement) {
  438. verifyFormat("switch (x) {\n"
  439. "case 1:\n"
  440. " f();\n"
  441. " break;\n"
  442. "case kFoo:\n"
  443. "case ns::kBar:\n"
  444. "case kBaz:\n"
  445. " break;\n"
  446. "default:\n"
  447. " g();\n"
  448. " break;\n"
  449. "}");
  450. verifyFormat("switch (x) {\n"
  451. "case 1: {\n"
  452. " f();\n"
  453. " break;\n"
  454. "}\n"
  455. "}");
  456. verifyFormat("switch (x) {\n"
  457. "case 1: {\n"
  458. " f();\n"
  459. " {\n"
  460. " g();\n"
  461. " h();\n"
  462. " }\n"
  463. " break;\n"
  464. "}\n"
  465. "}");
  466. verifyFormat("switch (x) {\n"
  467. "case 1: {\n"
  468. " f();\n"
  469. " if (foo) {\n"
  470. " g();\n"
  471. " h();\n"
  472. " }\n"
  473. " break;\n"
  474. "}\n"
  475. "}");
  476. verifyFormat("switch (x) {\n"
  477. "case 1: {\n"
  478. " f();\n"
  479. " g();\n"
  480. "} break;\n"
  481. "}");
  482. verifyFormat("switch (test)\n"
  483. " ;");
  484. verifyFormat("switch (x) {\n"
  485. "default: {\n"
  486. " // Do nothing.\n"
  487. "}\n"
  488. "}");
  489. verifyFormat("switch (x) {\n"
  490. "// comment\n"
  491. "// if 1, do f()\n"
  492. "case 1:\n"
  493. " f();\n"
  494. "}");
  495. verifyFormat("switch (x) {\n"
  496. "case 1:\n"
  497. " // Do amazing stuff\n"
  498. " {\n"
  499. " f();\n"
  500. " g();\n"
  501. " }\n"
  502. " break;\n"
  503. "}");
  504. verifyFormat("#define A \\\n"
  505. " switch (x) { \\\n"
  506. " case a: \\\n"
  507. " foo = b; \\\n"
  508. " }", getLLVMStyleWithColumns(20));
  509. verifyFormat("#define OPERATION_CASE(name) \\\n"
  510. " case OP_name: \\\n"
  511. " return operations::Operation##name\n",
  512. getLLVMStyleWithColumns(40));
  513. verifyGoogleFormat("switch (x) {\n"
  514. " case 1:\n"
  515. " f();\n"
  516. " break;\n"
  517. " case kFoo:\n"
  518. " case ns::kBar:\n"
  519. " case kBaz:\n"
  520. " break;\n"
  521. " default:\n"
  522. " g();\n"
  523. " break;\n"
  524. "}");
  525. verifyGoogleFormat("switch (x) {\n"
  526. " case 1: {\n"
  527. " f();\n"
  528. " break;\n"
  529. " }\n"
  530. "}");
  531. verifyGoogleFormat("switch (test)\n"
  532. " ;");
  533. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  534. " case OP_name: \\\n"
  535. " return operations::Operation##name\n");
  536. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  537. " // Get the correction operation class.\n"
  538. " switch (OpCode) {\n"
  539. " CASE(Add);\n"
  540. " CASE(Subtract);\n"
  541. " default:\n"
  542. " return operations::Unknown;\n"
  543. " }\n"
  544. "#undef OPERATION_CASE\n"
  545. "}");
  546. verifyFormat("DEBUG({\n"
  547. " switch (x) {\n"
  548. " case A:\n"
  549. " f();\n"
  550. " break;\n"
  551. " // On B:\n"
  552. " case B:\n"
  553. " g();\n"
  554. " break;\n"
  555. " }\n"
  556. "});");
  557. }
  558. TEST_F(FormatTest, CaseRanges) {
  559. verifyFormat("switch (x) {\n"
  560. "case 'A' ... 'Z':\n"
  561. "case 1 ... 5:\n"
  562. " break;\n"
  563. "}");
  564. }
  565. TEST_F(FormatTest, FormatsLabels) {
  566. verifyFormat("void f() {\n"
  567. " some_code();\n"
  568. "test_label:\n"
  569. " some_other_code();\n"
  570. " {\n"
  571. " some_more_code();\n"
  572. " another_label:\n"
  573. " some_more_code();\n"
  574. " }\n"
  575. "}");
  576. verifyFormat("some_code();\n"
  577. "test_label:\n"
  578. "some_other_code();");
  579. }
  580. //===----------------------------------------------------------------------===//
  581. // Tests for comments.
  582. //===----------------------------------------------------------------------===//
  583. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  584. verifyFormat("//* */");
  585. verifyFormat("// line 1\n"
  586. "// line 2\n"
  587. "void f() {}\n");
  588. verifyFormat("void f() {\n"
  589. " // Doesn't do anything\n"
  590. "}");
  591. verifyFormat("SomeObject\n"
  592. " // Calling someFunction on SomeObject\n"
  593. " .someFunction();");
  594. verifyFormat("auto result = SomeObject\n"
  595. " // Calling someFunction on SomeObject\n"
  596. " .someFunction();");
  597. verifyFormat("void f(int i, // some comment (probably for i)\n"
  598. " int j, // some comment (probably for j)\n"
  599. " int k); // some comment (probably for k)");
  600. verifyFormat("void f(int i,\n"
  601. " // some comment (probably for j)\n"
  602. " int j,\n"
  603. " // some comment (probably for k)\n"
  604. " int k);");
  605. verifyFormat("int i // This is a fancy variable\n"
  606. " = 5; // with nicely aligned comment.");
  607. verifyFormat("// Leading comment.\n"
  608. "int a; // Trailing comment.");
  609. verifyFormat("int a; // Trailing comment\n"
  610. " // on 2\n"
  611. " // or 3 lines.\n"
  612. "int b;");
  613. verifyFormat("int a; // Trailing comment\n"
  614. "\n"
  615. "// Leading comment.\n"
  616. "int b;");
  617. verifyFormat("int a; // Comment.\n"
  618. " // More details.\n"
  619. "int bbbb; // Another comment.");
  620. verifyFormat(
  621. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  622. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  623. "int cccccccccccccccccccccccccccccc; // comment\n"
  624. "int ddd; // looooooooooooooooooooooooong comment\n"
  625. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  626. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  627. "int ccccccccccccccccccc; // comment");
  628. verifyFormat("#include \"a\" // comment\n"
  629. "#include \"a/b/c\" // comment");
  630. verifyFormat("#include <a> // comment\n"
  631. "#include <a/b/c> // comment");
  632. EXPECT_EQ("#include \"a\" // comment\n"
  633. "#include \"a/b/c\" // comment",
  634. format("#include \\\n"
  635. " \"a\" // comment\n"
  636. "#include \"a/b/c\" // comment"));
  637. verifyFormat("enum E {\n"
  638. " // comment\n"
  639. " VAL_A, // comment\n"
  640. " VAL_B\n"
  641. "};");
  642. verifyFormat(
  643. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  644. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  645. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  646. " // Comment inside a statement.\n"
  647. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  648. verifyFormat(
  649. "bool aaaaaaaaaaaaa = // comment\n"
  650. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  651. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  652. verifyFormat("int aaaa; // aaaaa\n"
  653. "int aa; // aaaaaaa",
  654. getLLVMStyleWithColumns(20));
  655. EXPECT_EQ("void f() { // This does something ..\n"
  656. "}\n"
  657. "int a; // This is unrelated",
  658. format("void f() { // This does something ..\n"
  659. " }\n"
  660. "int a; // This is unrelated"));
  661. EXPECT_EQ("class C {\n"
  662. " void f() { // This does something ..\n"
  663. " } // awesome..\n"
  664. "\n"
  665. " int a; // This is unrelated\n"
  666. "};",
  667. format("class C{void f() { // This does something ..\n"
  668. " } // awesome..\n"
  669. " \n"
  670. "int a; // This is unrelated\n"
  671. "};"));
  672. EXPECT_EQ("int i; // single line trailing comment",
  673. format("int i;\\\n// single line trailing comment"));
  674. verifyGoogleFormat("int a; // Trailing comment.");
  675. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  676. " parameter));");
  677. verifyGoogleFormat("#endif // HEADER_GUARD");
  678. verifyFormat("const char *test[] = {\n"
  679. " // A\n"
  680. " \"aaaa\",\n"
  681. " // B\n"
  682. " \"aaaaa\"};");
  683. verifyGoogleFormat(
  684. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  685. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  686. EXPECT_EQ("D(a, {\n"
  687. " // test\n"
  688. " int a;\n"
  689. "});",
  690. format("D(a, {\n"
  691. "// test\n"
  692. "int a;\n"
  693. "});"));
  694. EXPECT_EQ("lineWith(); // comment\n"
  695. "// at start\n"
  696. "otherLine();",
  697. format("lineWith(); // comment\n"
  698. "// at start\n"
  699. "otherLine();"));
  700. EXPECT_EQ("lineWith(); // comment\n"
  701. " // at start\n"
  702. "otherLine();",
  703. format("lineWith(); // comment\n"
  704. " // at start\n"
  705. "otherLine();"));
  706. EXPECT_EQ("lineWith(); // comment\n"
  707. "// at start\n"
  708. "otherLine(); // comment",
  709. format("lineWith(); // comment\n"
  710. "// at start\n"
  711. "otherLine(); // comment"));
  712. EXPECT_EQ("lineWith();\n"
  713. "// at start\n"
  714. "otherLine(); // comment",
  715. format("lineWith();\n"
  716. " // at start\n"
  717. "otherLine(); // comment"));
  718. EXPECT_EQ("// first\n"
  719. "// at start\n"
  720. "otherLine(); // comment",
  721. format("// first\n"
  722. " // at start\n"
  723. "otherLine(); // comment"));
  724. EXPECT_EQ("f();\n"
  725. "// first\n"
  726. "// at start\n"
  727. "otherLine(); // comment",
  728. format("f();\n"
  729. "// first\n"
  730. " // at start\n"
  731. "otherLine(); // comment"));
  732. verifyFormat("f(); // comment\n"
  733. "// first\n"
  734. "// at start\n"
  735. "otherLine();");
  736. EXPECT_EQ("f(); // comment\n"
  737. "// first\n"
  738. "// at start\n"
  739. "otherLine();",
  740. format("f(); // comment\n"
  741. "// first\n"
  742. " // at start\n"
  743. "otherLine();"));
  744. EXPECT_EQ("f(); // comment\n"
  745. " // first\n"
  746. "// at start\n"
  747. "otherLine();",
  748. format("f(); // comment\n"
  749. " // first\n"
  750. "// at start\n"
  751. "otherLine();"));
  752. }
  753. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  754. EXPECT_EQ("SomeFunction(a,\n"
  755. " b, // comment\n"
  756. " c);",
  757. format("SomeFunction(a,\n"
  758. " b, // comment\n"
  759. " c);"));
  760. EXPECT_EQ("SomeFunction(a, b,\n"
  761. " // comment\n"
  762. " c);",
  763. format("SomeFunction(a,\n"
  764. " b,\n"
  765. " // comment\n"
  766. " c);"));
  767. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  768. " c);",
  769. format("SomeFunction(a, b, // comment (unclear relation)\n"
  770. " c);"));
  771. EXPECT_EQ("SomeFunction(a, // comment\n"
  772. " b,\n"
  773. " c); // comment",
  774. format("SomeFunction(a, // comment\n"
  775. " b,\n"
  776. " c); // comment"));
  777. }
  778. TEST_F(FormatTest, CanFormatCommentsLocally) {
  779. EXPECT_EQ("int a; // comment\n"
  780. "int b; // comment",
  781. format("int a; // comment\n"
  782. "int b; // comment",
  783. 0, 0, getLLVMStyle()));
  784. EXPECT_EQ("int a; // comment\n"
  785. " // line 2\n"
  786. "int b;",
  787. format("int a; // comment\n"
  788. " // line 2\n"
  789. "int b;",
  790. 28, 0, getLLVMStyle()));
  791. EXPECT_EQ("int aaaaaa; // comment\n"
  792. "int b;\n"
  793. "int c; // unrelated comment",
  794. format("int aaaaaa; // comment\n"
  795. "int b;\n"
  796. "int c; // unrelated comment",
  797. 31, 0, getLLVMStyle()));
  798. EXPECT_EQ("int a; // This\n"
  799. " // is\n"
  800. " // a",
  801. format("int a; // This\n"
  802. " // is\n"
  803. " // a",
  804. 0, 0, getLLVMStyle()));
  805. EXPECT_EQ("int a; // This\n"
  806. " // is\n"
  807. " // a\n"
  808. "// This is b\n"
  809. "int b;",
  810. format("int a; // This\n"
  811. " // is\n"
  812. " // a\n"
  813. "// This is b\n"
  814. "int b;",
  815. 0, 0, getLLVMStyle()));
  816. EXPECT_EQ("int a; // This\n"
  817. " // is\n"
  818. " // a\n"
  819. "\n"
  820. " // This is unrelated",
  821. format("int a; // This\n"
  822. " // is\n"
  823. " // a\n"
  824. "\n"
  825. " // This is unrelated",
  826. 0, 0, getLLVMStyle()));
  827. EXPECT_EQ("int a;\n"
  828. "// This is\n"
  829. "// not formatted. ",
  830. format("int a;\n"
  831. "// This is\n"
  832. "// not formatted. ",
  833. 0, 0, getLLVMStyle()));
  834. }
  835. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  836. EXPECT_EQ("// comment", format("// comment "));
  837. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  838. format("int aaaaaaa, bbbbbbb; // comment ",
  839. getLLVMStyleWithColumns(33)));
  840. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  841. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  842. }
  843. TEST_F(FormatTest, UnderstandsBlockComments) {
  844. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  845. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  846. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  847. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  848. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  849. "/* Trailing comment for aa... */\n"
  850. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  851. EXPECT_EQ(
  852. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  853. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  854. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  855. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  856. EXPECT_EQ(
  857. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  858. " aaaaaaaaaaaaaaaaaa,\n"
  859. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  860. "}",
  861. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  862. " aaaaaaaaaaaaaaaaaa ,\n"
  863. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  864. "}"));
  865. FormatStyle NoBinPacking = getLLVMStyle();
  866. NoBinPacking.BinPackParameters = false;
  867. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  868. " /* parameter 2 */ aaaaaa,\n"
  869. " /* parameter 3 */ aaaaaa,\n"
  870. " /* parameter 4 */ aaaaaa);",
  871. NoBinPacking);
  872. // Aligning block comments in macros.
  873. verifyGoogleFormat("#define A \\\n"
  874. " int i; /*a*/ \\\n"
  875. " int jjj; /*b*/");
  876. }
  877. TEST_F(FormatTest, AlignsBlockComments) {
  878. EXPECT_EQ("/*\n"
  879. " * Really multi-line\n"
  880. " * comment.\n"
  881. " */\n"
  882. "void f() {}",
  883. format(" /*\n"
  884. " * Really multi-line\n"
  885. " * comment.\n"
  886. " */\n"
  887. " void f() {}"));
  888. EXPECT_EQ("class C {\n"
  889. " /*\n"
  890. " * Another multi-line\n"
  891. " * comment.\n"
  892. " */\n"
  893. " void f() {}\n"
  894. "};",
  895. format("class C {\n"
  896. "/*\n"
  897. " * Another multi-line\n"
  898. " * comment.\n"
  899. " */\n"
  900. "void f() {}\n"
  901. "};"));
  902. EXPECT_EQ("/*\n"
  903. " 1. This is a comment with non-trivial formatting.\n"
  904. " 1.1. We have to indent/outdent all lines equally\n"
  905. " 1.1.1. to keep the formatting.\n"
  906. " */",
  907. format(" /*\n"
  908. " 1. This is a comment with non-trivial formatting.\n"
  909. " 1.1. We have to indent/outdent all lines equally\n"
  910. " 1.1.1. to keep the formatting.\n"
  911. " */"));
  912. EXPECT_EQ("/*\n"
  913. "Don't try to outdent if there's not enough indentation.\n"
  914. "*/",
  915. format(" /*\n"
  916. " Don't try to outdent if there's not enough indentation.\n"
  917. " */"));
  918. EXPECT_EQ("int i; /* Comment with empty...\n"
  919. " *\n"
  920. " * line. */",
  921. format("int i; /* Comment with empty...\n"
  922. " *\n"
  923. " * line. */"));
  924. }
  925. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  926. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  927. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  928. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  930. EXPECT_EQ(
  931. "void ffffffffffff(\n"
  932. " int aaaaaaaa, int bbbbbbbb,\n"
  933. " int cccccccccccc) { /*\n"
  934. " aaaaaaaaaa\n"
  935. " aaaaaaaaaaaaa\n"
  936. " bbbbbbbbbbbbbb\n"
  937. " bbbbbbbbbb\n"
  938. " */\n"
  939. "}",
  940. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  941. "{ /*\n"
  942. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  943. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  944. " */\n"
  945. "}",
  946. getLLVMStyleWithColumns(40)));
  947. }
  948. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  949. EXPECT_EQ("void\n"
  950. "ffffffffff(int aaaaa /* test */);",
  951. format("void ffffffffff(int aaaaa /* test */);",
  952. getLLVMStyleWithColumns(35)));
  953. }
  954. TEST_F(FormatTest, SplitsLongCxxComments) {
  955. EXPECT_EQ("// A comment that\n"
  956. "// doesn't fit on\n"
  957. "// one line",
  958. format("// A comment that doesn't fit on one line",
  959. getLLVMStyleWithColumns(20)));
  960. EXPECT_EQ("// a b c d\n"
  961. "// e f g\n"
  962. "// h i j k",
  963. format("// a b c d e f g h i j k",
  964. getLLVMStyleWithColumns(10)));
  965. EXPECT_EQ("// a b c d\n"
  966. "// e f g\n"
  967. "// h i j k",
  968. format("\\\n// a b c d e f g h i j k",
  969. getLLVMStyleWithColumns(10)));
  970. EXPECT_EQ("if (true) // A comment that\n"
  971. " // doesn't fit on\n"
  972. " // one line",
  973. format("if (true) // A comment that doesn't fit on one line ",
  974. getLLVMStyleWithColumns(30)));
  975. EXPECT_EQ("// Don't_touch_leading_whitespace",
  976. format("// Don't_touch_leading_whitespace",
  977. getLLVMStyleWithColumns(20)));
  978. EXPECT_EQ("// Add leading\n"
  979. "// whitespace",
  980. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  981. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  982. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  983. "// limit",
  984. format("//Even if it makes the line exceed the column limit",
  985. getLLVMStyleWithColumns(51)));
  986. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  987. EXPECT_EQ("// aa bb cc dd",
  988. format("// aa bb cc dd ",
  989. getLLVMStyleWithColumns(15)));
  990. EXPECT_EQ("// A comment before\n"
  991. "// a macro\n"
  992. "// definition\n"
  993. "#define a b",
  994. format("// A comment before a macro definition\n"
  995. "#define a b",
  996. getLLVMStyleWithColumns(20)));
  997. EXPECT_EQ("void\n"
  998. "ffffff(int aaaaaaaaa, // wwww\n"
  999. " int bbbbbbbbbb, // xxxxxxx\n"
  1000. " // yyyyyyyyyy\n"
  1001. " int c, int d, int e) {}",
  1002. format("void ffffff(\n"
  1003. " int aaaaaaaaa, // wwww\n"
  1004. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1005. " int c, int d, int e) {}",
  1006. getLLVMStyleWithColumns(40)));
  1007. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1008. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1009. getLLVMStyleWithColumns(20)));
  1010. EXPECT_EQ(
  1011. "#define XXX // a b c d\n"
  1012. " // e f g h",
  1013. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1014. EXPECT_EQ(
  1015. "#define XXX // q w e r\n"
  1016. " // t y u i",
  1017. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1018. }
  1019. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1020. EXPECT_EQ("// A comment\n"
  1021. "// that doesn't\n"
  1022. "// fit on one\n"
  1023. "// line",
  1024. format("// A comment that doesn't fit on one line",
  1025. getLLVMStyleWithColumns(20)));
  1026. EXPECT_EQ("/// A comment\n"
  1027. "/// that doesn't\n"
  1028. "/// fit on one\n"
  1029. "/// line",
  1030. format("/// A comment that doesn't fit on one line",
  1031. getLLVMStyleWithColumns(20)));
  1032. }
  1033. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1034. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1035. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1036. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1037. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1038. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1039. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1040. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1041. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1042. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1043. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1044. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1045. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1046. getLLVMStyleWithColumns(50)));
  1047. // FIXME: One day we might want to implement adjustment of leading whitespace
  1048. // of the consecutive lines in this kind of comment:
  1049. EXPECT_EQ("int\n"
  1050. "a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1051. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1052. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1053. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1054. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1055. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1056. getLLVMStyleWithColumns(49)));
  1057. }
  1058. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1059. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1060. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1061. EXPECT_EQ(
  1062. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1063. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1064. EXPECT_EQ(
  1065. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1066. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1067. }
  1068. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1069. EXPECT_EQ("if (xxx ==\n"
  1070. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1071. " zzz)\n"
  1072. " q();",
  1073. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1074. " zzz) q();",
  1075. getLLVMStyleWithColumns(40)));
  1076. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1077. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1078. " zzz)\n"
  1079. " q();",
  1080. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1081. " zzz) q();",
  1082. getLLVMStyleWithColumns(40)));
  1083. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1084. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1085. " zzz)\n"
  1086. " q();",
  1087. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1088. " zzz) q();",
  1089. getLLVMStyleWithColumns(40)));
  1090. EXPECT_EQ("fffffffff(\n"
  1091. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1092. " zzz);",
  1093. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1094. " zzz);",
  1095. getLLVMStyleWithColumns(40)));
  1096. }
  1097. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1098. EXPECT_EQ("#define A(x) /* \\\n"
  1099. " a comment \\\n"
  1100. " inside */ \\\n"
  1101. " f();",
  1102. format("#define A(x) /* \\\n"
  1103. " a comment \\\n"
  1104. " inside */ \\\n"
  1105. " f();",
  1106. getLLVMStyleWithColumns(17)));
  1107. EXPECT_EQ("#define A( \\\n"
  1108. " x) /* \\\n"
  1109. " a comment \\\n"
  1110. " inside */ \\\n"
  1111. " f();",
  1112. format("#define A( \\\n"
  1113. " x) /* \\\n"
  1114. " a comment \\\n"
  1115. " inside */ \\\n"
  1116. " f();",
  1117. getLLVMStyleWithColumns(17)));
  1118. }
  1119. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1120. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1121. format("namespace {}\n // Test\n#define A"));
  1122. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1123. format("namespace {}\n /* Test */\n#define A"));
  1124. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1125. format("namespace {}\n /* Test */ #define A"));
  1126. }
  1127. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1128. EXPECT_EQ("/* This is a long\n"
  1129. " * comment that\n"
  1130. " * doesn't\n"
  1131. " * fit on one line.\n"
  1132. " */",
  1133. format("/* "
  1134. "This is a long "
  1135. "comment that "
  1136. "doesn't "
  1137. "fit on one line. */",
  1138. getLLVMStyleWithColumns(20)));
  1139. EXPECT_EQ("/* a b c d\n"
  1140. " * e f g\n"
  1141. " * h i j k\n"
  1142. " */",
  1143. format("/* a b c d e f g h i j k */",
  1144. getLLVMStyleWithColumns(10)));
  1145. EXPECT_EQ("/* a b c d\n"
  1146. " * e f g\n"
  1147. " * h i j k\n"
  1148. " */",
  1149. format("\\\n/* a b c d e f g h i j k */",
  1150. getLLVMStyleWithColumns(10)));
  1151. EXPECT_EQ("/*\n"
  1152. "This is a long\n"
  1153. "comment that doesn't\n"
  1154. "fit on one line.\n"
  1155. "*/",
  1156. format("/*\n"
  1157. "This is a long "
  1158. "comment that doesn't "
  1159. "fit on one line. \n"
  1160. "*/", getLLVMStyleWithColumns(20)));
  1161. EXPECT_EQ("/*\n"
  1162. " * This is a long\n"
  1163. " * comment that\n"
  1164. " * doesn't fit on\n"
  1165. " * one line.\n"
  1166. " */",
  1167. format("/* \n"
  1168. " * This is a long "
  1169. " comment that "
  1170. " doesn't fit on "
  1171. " one line. \n"
  1172. " */", getLLVMStyleWithColumns(20)));
  1173. EXPECT_EQ("/*\n"
  1174. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1175. " * so_it_should_be_broken\n"
  1176. " * wherever_a_space_occurs\n"
  1177. " */",
  1178. format("/*\n"
  1179. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1180. " so_it_should_be_broken "
  1181. " wherever_a_space_occurs \n"
  1182. " */",
  1183. getLLVMStyleWithColumns(20)));
  1184. EXPECT_EQ("/*\n"
  1185. " * This_comment_can_not_be_broken_into_lines\n"
  1186. " */",
  1187. format("/*\n"
  1188. " * This_comment_can_not_be_broken_into_lines\n"
  1189. " */",
  1190. getLLVMStyleWithColumns(20)));
  1191. EXPECT_EQ("{\n"
  1192. " /*\n"
  1193. " This is another\n"
  1194. " long comment that\n"
  1195. " doesn't fit on one\n"
  1196. " line 1234567890\n"
  1197. " */\n"
  1198. "}",
  1199. format("{\n"
  1200. "/*\n"
  1201. "This is another "
  1202. " long comment that "
  1203. " doesn't fit on one"
  1204. " line 1234567890\n"
  1205. "*/\n"
  1206. "}", getLLVMStyleWithColumns(20)));
  1207. EXPECT_EQ("{\n"
  1208. " /*\n"
  1209. " * This i s\n"
  1210. " * another comment\n"
  1211. " * t hat doesn' t\n"
  1212. " * fit on one l i\n"
  1213. " * n e\n"
  1214. " */\n"
  1215. "}",
  1216. format("{\n"
  1217. "/*\n"
  1218. " * This i s"
  1219. " another comment"
  1220. " t hat doesn' t"
  1221. " fit on one l i"
  1222. " n e\n"
  1223. " */\n"
  1224. "}", getLLVMStyleWithColumns(20)));
  1225. EXPECT_EQ("/*\n"
  1226. " * This is a long\n"
  1227. " * comment that\n"
  1228. " * doesn't fit on\n"
  1229. " * one line\n"
  1230. " */",
  1231. format(" /*\n"
  1232. " * This is a long comment that doesn't fit on one line\n"
  1233. " */", getLLVMStyleWithColumns(20)));
  1234. EXPECT_EQ("{\n"
  1235. " if (something) /* This is a\n"
  1236. " long\n"
  1237. " comment */\n"
  1238. " ;\n"
  1239. "}",
  1240. format("{\n"
  1241. " if (something) /* This is a long comment */\n"
  1242. " ;\n"
  1243. "}",
  1244. getLLVMStyleWithColumns(30)));
  1245. EXPECT_EQ("/* A comment before\n"
  1246. " * a macro\n"
  1247. " * definition */\n"
  1248. "#define a b",
  1249. format("/* A comment before a macro definition */\n"
  1250. "#define a b",
  1251. getLLVMStyleWithColumns(20)));
  1252. EXPECT_EQ("/* some comment\n"
  1253. " * a comment\n"
  1254. "* that we break\n"
  1255. " * another comment\n"
  1256. "* we have to break\n"
  1257. "* a left comment\n"
  1258. " */",
  1259. format(" /* some comment\n"
  1260. " * a comment that we break\n"
  1261. " * another comment we have to break\n"
  1262. "* a left comment\n"
  1263. " */",
  1264. getLLVMStyleWithColumns(20)));
  1265. EXPECT_EQ("/*\n"
  1266. "\n"
  1267. "\n"
  1268. " */\n",
  1269. format(" /* \n"
  1270. " \n"
  1271. " \n"
  1272. " */\n"));
  1273. EXPECT_EQ("/* a a */",
  1274. format("/* a a */", getLLVMStyleWithColumns(15)));
  1275. EXPECT_EQ("/* a a bc */",
  1276. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1277. EXPECT_EQ("/* aaa aaa\n"
  1278. " * aaaaa */",
  1279. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1280. EXPECT_EQ("/* aaa aaa\n"
  1281. " * aaaaa */",
  1282. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1283. }
  1284. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1285. EXPECT_EQ("#define X \\\n"
  1286. " /* \\\n"
  1287. " Test \\\n"
  1288. " Macro comment \\\n"
  1289. " with a long \\\n"
  1290. " line \\\n"
  1291. " */ \\\n"
  1292. " A + B",
  1293. format("#define X \\\n"
  1294. " /*\n"
  1295. " Test\n"
  1296. " Macro comment with a long line\n"
  1297. " */ \\\n"
  1298. " A + B",
  1299. getLLVMStyleWithColumns(20)));
  1300. EXPECT_EQ("#define X \\\n"
  1301. " /* Macro comment \\\n"
  1302. " with a long \\\n"
  1303. " line */ \\\n"
  1304. " A + B",
  1305. format("#define X \\\n"
  1306. " /* Macro comment with a long\n"
  1307. " line */ \\\n"
  1308. " A + B",
  1309. getLLVMStyleWithColumns(20)));
  1310. EXPECT_EQ("#define X \\\n"
  1311. " /* Macro comment \\\n"
  1312. " * with a long \\\n"
  1313. " * line */ \\\n"
  1314. " A + B",
  1315. format("#define X \\\n"
  1316. " /* Macro comment with a long line */ \\\n"
  1317. " A + B",
  1318. getLLVMStyleWithColumns(20)));
  1319. }
  1320. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1321. EXPECT_EQ(
  1322. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1323. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1324. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1325. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1326. " aaaaaaaaaaaaaaaaaaaa};",
  1327. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1328. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1329. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1330. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1331. " aaaaaaaaaaaaaaaaaaaa };"));
  1332. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1333. " bbbbbbbbbbb, ccccccccccc};");
  1334. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1335. " // comment for bb....\n"
  1336. " bbbbbbbbbbb, ccccccccccc};");
  1337. verifyGoogleFormat(
  1338. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1339. " bbbbbbbbbbb, ccccccccccc};");
  1340. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1341. " // comment for bb....\n"
  1342. " bbbbbbbbbbb, ccccccccccc};");
  1343. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1344. " {d, e, f}, // Group #2\n"
  1345. " {g, h, i}}; // Group #3");
  1346. verifyFormat("S s = {{// Group #1\n"
  1347. " a, b, c},\n"
  1348. " {// Group #2\n"
  1349. " d, e, f},\n"
  1350. " {// Group #3\n"
  1351. " g, h, i}};");
  1352. EXPECT_EQ("S s = {\n"
  1353. " // Some comment\n"
  1354. " a,\n"
  1355. "\n"
  1356. " // Comment after empty line\n"
  1357. " b}",
  1358. format("S s = {\n"
  1359. " // Some comment\n"
  1360. " a,\n"
  1361. " \n"
  1362. " // Comment after empty line\n"
  1363. " b\n"
  1364. "}"));
  1365. EXPECT_EQ("S s = {\n"
  1366. " /* Some comment */\n"
  1367. " a,\n"
  1368. "\n"
  1369. " /* Comment after empty line */\n"
  1370. " b}",
  1371. format("S s = {\n"
  1372. " /* Some comment */\n"
  1373. " a,\n"
  1374. " \n"
  1375. " /* Comment after empty line */\n"
  1376. " b\n"
  1377. "}"));
  1378. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1379. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1380. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1381. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1382. }
  1383. TEST_F(FormatTest, IgnoresIf0Contents) {
  1384. EXPECT_EQ("#if 0\n"
  1385. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1386. "#endif\n"
  1387. "void f() {}",
  1388. format("#if 0\n"
  1389. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1390. "#endif\n"
  1391. "void f( ) { }"));
  1392. EXPECT_EQ("#if false\n"
  1393. "void f( ) { }\n"
  1394. "#endif\n"
  1395. "void g() {}\n",
  1396. format("#if false\n"
  1397. "void f( ) { }\n"
  1398. "#endif\n"
  1399. "void g( ) { }\n"));
  1400. EXPECT_EQ("enum E {\n"
  1401. " One,\n"
  1402. " Two,\n"
  1403. "#if 0\n"
  1404. "Three,\n"
  1405. " Four,\n"
  1406. "#endif\n"
  1407. " Five\n"
  1408. "};",
  1409. format("enum E {\n"
  1410. " One,Two,\n"
  1411. "#if 0\n"
  1412. "Three,\n"
  1413. " Four,\n"
  1414. "#endif\n"
  1415. " Five};"));
  1416. EXPECT_EQ("enum F {\n"
  1417. " One,\n"
  1418. "#if 1\n"
  1419. " Two,\n"
  1420. "#if 0\n"
  1421. "Three,\n"
  1422. " Four,\n"
  1423. "#endif\n"
  1424. " Five\n"
  1425. "#endif\n"
  1426. "};",
  1427. format("enum F {\n"
  1428. "One,\n"
  1429. "#if 1\n"
  1430. "Two,\n"
  1431. "#if 0\n"
  1432. "Three,\n"
  1433. " Four,\n"
  1434. "#endif\n"
  1435. "Five\n"
  1436. "#endif\n"
  1437. "};"));
  1438. EXPECT_EQ("enum G {\n"
  1439. " One,\n"
  1440. "#if 0\n"
  1441. "Two,\n"
  1442. "#else\n"
  1443. " Three,\n"
  1444. "#endif\n"
  1445. " Four\n"
  1446. "};",
  1447. format("enum G {\n"
  1448. "One,\n"
  1449. "#if 0\n"
  1450. "Two,\n"
  1451. "#else\n"
  1452. "Three,\n"
  1453. "#endif\n"
  1454. "Four\n"
  1455. "};"));
  1456. EXPECT_EQ("enum H {\n"
  1457. " One,\n"
  1458. "#if 0\n"
  1459. "#ifdef Q\n"
  1460. "Two,\n"
  1461. "#else\n"
  1462. "Three,\n"
  1463. "#endif\n"
  1464. "#endif\n"
  1465. " Four\n"
  1466. "};",
  1467. format("enum H {\n"
  1468. "One,\n"
  1469. "#if 0\n"
  1470. "#ifdef Q\n"
  1471. "Two,\n"
  1472. "#else\n"
  1473. "Three,\n"
  1474. "#endif\n"
  1475. "#endif\n"
  1476. "Four\n"
  1477. "};"));
  1478. EXPECT_EQ("enum I {\n"
  1479. " One,\n"
  1480. "#if /* test */ 0 || 1\n"
  1481. "Two,\n"
  1482. "Three,\n"
  1483. "#endif\n"
  1484. " Four\n"
  1485. "};",
  1486. format("enum I {\n"
  1487. "One,\n"
  1488. "#if /* test */ 0 || 1\n"
  1489. "Two,\n"
  1490. "Three,\n"
  1491. "#endif\n"
  1492. "Four\n"
  1493. "};"));
  1494. EXPECT_EQ("enum J {\n"
  1495. " One,\n"
  1496. "#if 0\n"
  1497. "#if 0\n"
  1498. "Two,\n"
  1499. "#else\n"
  1500. "Three,\n"
  1501. "#endif\n"
  1502. "Four,\n"
  1503. "#endif\n"
  1504. " Five\n"
  1505. "};",
  1506. format("enum J {\n"
  1507. "One,\n"
  1508. "#if 0\n"
  1509. "#if 0\n"
  1510. "Two,\n"
  1511. "#else\n"
  1512. "Three,\n"
  1513. "#endif\n"
  1514. "Four,\n"
  1515. "#endif\n"
  1516. "Five\n"
  1517. "};"));
  1518. }
  1519. //===----------------------------------------------------------------------===//
  1520. // Tests for classes, namespaces, etc.
  1521. //===----------------------------------------------------------------------===//
  1522. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1523. verifyFormat("class A {};");
  1524. }
  1525. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1526. verifyFormat("class A {\n"
  1527. "public:\n"
  1528. "public: // comment\n"
  1529. "protected:\n"
  1530. "private:\n"
  1531. " void f() {}\n"
  1532. "};");
  1533. verifyGoogleFormat("class A {\n"
  1534. " public:\n"
  1535. " protected:\n"
  1536. " private:\n"
  1537. " void f() {}\n"
  1538. "};");
  1539. verifyFormat("class A {\n"
  1540. "public slots:\n"
  1541. " void f() {}\n"
  1542. "public Q_SLOTS:\n"
  1543. " void f() {}\n"
  1544. "};");
  1545. }
  1546. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1547. EXPECT_EQ("class A {\n"
  1548. "public:\n"
  1549. " void f();\n"
  1550. "\n"
  1551. "private:\n"
  1552. " void g() {}\n"
  1553. " // test\n"
  1554. "protected:\n"
  1555. " int h;\n"
  1556. "};",
  1557. format("class A {\n"
  1558. "public:\n"
  1559. "void f();\n"
  1560. "private:\n"
  1561. "void g() {}\n"
  1562. "// test\n"
  1563. "protected:\n"
  1564. "int h;\n"
  1565. "};"));
  1566. EXPECT_EQ("class A {\n"
  1567. "protected:\n"
  1568. "public:\n"
  1569. " void f();\n"
  1570. "};",
  1571. format("class A {\n"
  1572. "protected:\n"
  1573. "\n"
  1574. "public:\n"
  1575. "\n"
  1576. " void f();\n"
  1577. "};"));
  1578. }
  1579. TEST_F(FormatTest, FormatsClasses) {
  1580. verifyFormat("class A : public B {};");
  1581. verifyFormat("class A : public ::B {};");
  1582. verifyFormat(
  1583. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1584. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1585. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1586. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1587. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1588. verifyFormat(
  1589. "class A : public B, public C, public D, public E, public F {};");
  1590. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1591. " public C,\n"
  1592. " public D,\n"
  1593. " public E,\n"
  1594. " public F,\n"
  1595. " public G {};");
  1596. verifyFormat("class\n"
  1597. " ReallyReallyLongClassName {\n"
  1598. " int i;\n"
  1599. "};",
  1600. getLLVMStyleWithColumns(32));
  1601. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1602. " aaaaaaaaaaaaaaaa> {};");
  1603. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1604. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1605. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1606. verifyFormat("template <class R, class C>\n"
  1607. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1608. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1609. }
  1610. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1611. verifyFormat("class A {\n} a, b;");
  1612. verifyFormat("struct A {\n} a, b;");
  1613. verifyFormat("union A {\n} a;");
  1614. }
  1615. TEST_F(FormatTest, FormatsEnum) {
  1616. verifyFormat("enum {\n"
  1617. " Zero,\n"
  1618. " One = 1,\n"
  1619. " Two = One + 1,\n"
  1620. " Three = (One + Two),\n"
  1621. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1622. " Five = (One, Two, Three, Four, 5)\n"
  1623. "};");
  1624. verifyGoogleFormat("enum {\n"
  1625. " Zero,\n"
  1626. " One = 1,\n"
  1627. " Two = One + 1,\n"
  1628. " Three = (One + Two),\n"
  1629. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1630. " Five = (One, Two, Three, Four, 5)\n"
  1631. "};");
  1632. verifyFormat("enum Enum {};");
  1633. verifyFormat("enum {};");
  1634. verifyFormat("enum X E {} d;");
  1635. verifyFormat("enum __attribute__((...)) E {} d;");
  1636. verifyFormat("enum __declspec__((...)) E {} d;");
  1637. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1638. verifyFormat("enum {\n"
  1639. " Bar = Foo<int, int>::value\n"
  1640. "};",
  1641. getLLVMStyleWithColumns(30));
  1642. verifyFormat("enum ShortEnum { A, B, C };");
  1643. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1644. }
  1645. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1646. verifyFormat("enum Type {\n"
  1647. " One = 0; // These semicolons should be commas.\n"
  1648. " Two = 1;\n"
  1649. "};");
  1650. verifyFormat("namespace n {\n"
  1651. "enum Type {\n"
  1652. " One,\n"
  1653. " Two, // missing };\n"
  1654. " int i;\n"
  1655. "}\n"
  1656. "void g() {}");
  1657. }
  1658. TEST_F(FormatTest, FormatsEnumStruct) {
  1659. verifyFormat("enum struct {\n"
  1660. " Zero,\n"
  1661. " One = 1,\n"
  1662. " Two = One + 1,\n"
  1663. " Three = (One + Two),\n"
  1664. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1665. " Five = (One, Two, Three, Four, 5)\n"
  1666. "};");
  1667. verifyFormat("enum struct Enum {};");
  1668. verifyFormat("enum struct {};");
  1669. verifyFormat("enum struct X E {} d;");
  1670. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1671. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1672. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1673. }
  1674. TEST_F(FormatTest, FormatsEnumClass) {
  1675. verifyFormat("enum class {\n"
  1676. " Zero,\n"
  1677. " One = 1,\n"
  1678. " Two = One + 1,\n"
  1679. " Three = (One + Two),\n"
  1680. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1681. " Five = (One, Two, Three, Four, 5)\n"
  1682. "};");
  1683. verifyFormat("enum class Enum {};");
  1684. verifyFormat("enum class {};");
  1685. verifyFormat("enum class X E {} d;");
  1686. verifyFormat("enum class __attribute__((...)) E {} d;");
  1687. verifyFormat("enum class __declspec__((...)) E {} d;");
  1688. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1689. }
  1690. TEST_F(FormatTest, FormatsEnumTypes) {
  1691. verifyFormat("enum X : int {\n"
  1692. " A, // Force multiple lines.\n"
  1693. " B\n"
  1694. "};");
  1695. verifyFormat("enum X : int { A, B };");
  1696. verifyFormat("enum X : std::uint32_t { A, B };");
  1697. }
  1698. TEST_F(FormatTest, FormatsNSEnums) {
  1699. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1700. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1701. " // Information about someDecentlyLongValue.\n"
  1702. " someDecentlyLongValue,\n"
  1703. " // Information about anotherDecentlyLongValue.\n"
  1704. " anotherDecentlyLongValue,\n"
  1705. " // Information about aThirdDecentlyLongValue.\n"
  1706. " aThirdDecentlyLongValue\n"
  1707. "};");
  1708. }
  1709. TEST_F(FormatTest, FormatsBitfields) {
  1710. verifyFormat("struct Bitfields {\n"
  1711. " unsigned sClass : 8;\n"
  1712. " unsigned ValueKind : 2;\n"
  1713. "};");
  1714. verifyFormat("struct A {\n"
  1715. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1716. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1717. "};");
  1718. }
  1719. TEST_F(FormatTest, FormatsNamespaces) {
  1720. verifyFormat("namespace some_namespace {\n"
  1721. "class A {};\n"
  1722. "void f() { f(); }\n"
  1723. "}");
  1724. verifyFormat("namespace {\n"
  1725. "class A {};\n"
  1726. "void f() { f(); }\n"
  1727. "}");
  1728. verifyFormat("inline namespace X {\n"
  1729. "class A {};\n"
  1730. "void f() { f(); }\n"
  1731. "}");
  1732. verifyFormat("using namespace some_namespace;\n"
  1733. "class A {};\n"
  1734. "void f() { f(); }");
  1735. // This code is more common than we thought; if we
  1736. // layout this correctly the semicolon will go into
  1737. // its own line, which is undesirable.
  1738. verifyFormat("namespace {};");
  1739. verifyFormat("namespace {\n"
  1740. "class A {};\n"
  1741. "};");
  1742. verifyFormat("namespace {\n"
  1743. "int SomeVariable = 0; // comment\n"
  1744. "} // namespace");
  1745. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1746. "#define HEADER_GUARD\n"
  1747. "namespace my_namespace {\n"
  1748. "int i;\n"
  1749. "} // my_namespace\n"
  1750. "#endif // HEADER_GUARD",
  1751. format("#ifndef HEADER_GUARD\n"
  1752. " #define HEADER_GUARD\n"
  1753. " namespace my_namespace {\n"
  1754. "int i;\n"
  1755. "} // my_namespace\n"
  1756. "#endif // HEADER_GUARD"));
  1757. FormatStyle Style = getLLVMStyle();
  1758. Style.NamespaceIndentation = FormatStyle::NI_All;
  1759. EXPECT_EQ("namespace out {\n"
  1760. " int i;\n"
  1761. " namespace in {\n"
  1762. " int i;\n"
  1763. " } // namespace\n"
  1764. "} // namespace",
  1765. format("namespace out {\n"
  1766. "int i;\n"
  1767. "namespace in {\n"
  1768. "int i;\n"
  1769. "} // namespace\n"
  1770. "} // namespace",
  1771. Style));
  1772. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1773. EXPECT_EQ("namespace out {\n"
  1774. "int i;\n"
  1775. "namespace in {\n"
  1776. " int i;\n"
  1777. "} // namespace\n"
  1778. "} // namespace",
  1779. format("namespace out {\n"
  1780. "int i;\n"
  1781. "namespace in {\n"
  1782. "int i;\n"
  1783. "} // namespace\n"
  1784. "} // namespace",
  1785. Style));
  1786. }
  1787. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  1788. TEST_F(FormatTest, FormatsInlineASM) {
  1789. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  1790. verifyFormat(
  1791. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  1792. " \"cpuid\\n\\t\"\n"
  1793. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  1794. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  1795. " : \"a\"(value));");
  1796. }
  1797. TEST_F(FormatTest, FormatTryCatch) {
  1798. // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
  1799. // also not create single-line-blocks.
  1800. verifyFormat("try {\n"
  1801. " throw a * b;\n"
  1802. "}\n"
  1803. "catch (int a) {\n"
  1804. " // Do nothing.\n"
  1805. "}\n"
  1806. "catch (...) {\n"
  1807. " exit(42);\n"
  1808. "}");
  1809. // Function-level try statements.
  1810. verifyFormat("int f() try { return 4; }\n"
  1811. "catch (...) {\n"
  1812. " return 5;\n"
  1813. "}");
  1814. verifyFormat("class A {\n"
  1815. " int a;\n"
  1816. " A() try : a(0) {}\n"
  1817. " catch (...) {\n"
  1818. " throw;\n"
  1819. " }\n"
  1820. "};\n");
  1821. }
  1822. TEST_F(FormatTest, FormatObjCTryCatch) {
  1823. verifyFormat("@try {\n"
  1824. " f();\n"
  1825. "}\n"
  1826. "@catch (NSException e) {\n"
  1827. " @throw;\n"
  1828. "}\n"
  1829. "@finally {\n"
  1830. " exit(42);\n"
  1831. "}");
  1832. }
  1833. TEST_F(FormatTest, StaticInitializers) {
  1834. verifyFormat("static SomeClass SC = {1, 'a'};");
  1835. verifyFormat(
  1836. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  1837. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  1838. // Here, everything other than the "}" would fit on a line.
  1839. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  1840. " 10000000000000000000000000};");
  1841. EXPECT_EQ("S s = {a, b};", format("S s = {\n"
  1842. " a,\n"
  1843. "\n"
  1844. " b\n"
  1845. "};"));
  1846. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  1847. // line. However, the formatting looks a bit off and this probably doesn't
  1848. // happen often in practice.
  1849. verifyFormat("static int Variable[1] = {\n"
  1850. " {1000000000000000000000000000000000000}};",
  1851. getLLVMStyleWithColumns(40));
  1852. }
  1853. TEST_F(FormatTest, DesignatedInitializers) {
  1854. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  1855. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  1856. " .bbbbbbbbbb = 2,\n"
  1857. " .cccccccccc = 3,\n"
  1858. " .dddddddddd = 4,\n"
  1859. " .eeeeeeeeee = 5};");
  1860. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  1861. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  1862. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  1863. " .ccccccccccccccccccccccccccc = 3,\n"
  1864. " .ddddddddddddddddddddddddddd = 4,\n"
  1865. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  1866. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  1867. }
  1868. TEST_F(FormatTest, NestedStaticInitializers) {
  1869. verifyFormat("static A x = {{{}}};\n");
  1870. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  1871. " {init1, init2, init3, init4}}};",
  1872. getLLVMStyleWithColumns(50));
  1873. verifyFormat("somes Status::global_reps[3] = {\n"
  1874. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1875. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1876. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  1877. getLLVMStyleWithColumns(60));
  1878. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  1879. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1880. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1881. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  1882. verifyFormat(
  1883. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  1884. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  1885. verifyFormat(
  1886. "SomeArrayOfSomeType a = {\n"
  1887. " {{1, 2, 3},\n"
  1888. " {1, 2, 3},\n"
  1889. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  1890. " 333333333333333333333333333333},\n"
  1891. " {1, 2, 3},\n"
  1892. " {1, 2, 3}}};");
  1893. verifyFormat(
  1894. "SomeArrayOfSomeType a = {\n"
  1895. " {{1, 2, 3}},\n"
  1896. " {{1, 2, 3}},\n"
  1897. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  1898. " 333333333333333333333333333333}},\n"
  1899. " {{1, 2, 3}},\n"
  1900. " {{1, 2, 3}}};");
  1901. verifyFormat(
  1902. "struct {\n"
  1903. " unsigned bit;\n"
  1904. " const char *const name;\n"
  1905. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  1906. " {kOsWin, \"Windows\"},\n"
  1907. " {kOsLinux, \"Linux\"},\n"
  1908. " {kOsCrOS, \"Chrome OS\"}};");
  1909. }
  1910. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  1911. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1912. " \\\n"
  1913. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  1914. }
  1915. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  1916. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  1917. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  1918. }
  1919. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  1920. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  1921. getLLVMStyleWithColumns(40));
  1922. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  1923. getLLVMStyleWithColumns(40));
  1924. EXPECT_EQ("#define Q \\\n"
  1925. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  1926. " \"aaaaaaaa.cpp\"",
  1927. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  1928. getLLVMStyleWithColumns(40)));
  1929. }
  1930. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  1931. EXPECT_EQ("# 123 \"A string literal\"",
  1932. format(" # 123 \"A string literal\""));
  1933. }
  1934. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  1935. EXPECT_EQ("#;", format("#;"));
  1936. verifyFormat("#\n;\n;\n;");
  1937. }
  1938. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  1939. EXPECT_EQ("#line 42 \"test\"\n",
  1940. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  1941. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  1942. getLLVMStyleWithColumns(12)));
  1943. }
  1944. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  1945. EXPECT_EQ("#line 42 \"test\"",
  1946. format("# \\\n line \\\n 42 \\\n \"test\""));
  1947. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  1948. }
  1949. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  1950. verifyFormat("#define A \\x20");
  1951. verifyFormat("#define A \\ x20");
  1952. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  1953. verifyFormat("#define A ''");
  1954. verifyFormat("#define A ''qqq");
  1955. verifyFormat("#define A `qqq");
  1956. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  1957. EXPECT_EQ("const char *c = STRINGIFY(\n"
  1958. "\\na : b);",
  1959. format("const char * c = STRINGIFY(\n"
  1960. "\\na : b);"));
  1961. }
  1962. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  1963. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  1964. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  1965. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  1966. // FIXME: We never break before the macro name.
  1967. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  1968. verifyFormat("#define A A\n#define A A");
  1969. verifyFormat("#define A(X) A\n#define A A");
  1970. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  1971. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  1972. }
  1973. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  1974. EXPECT_EQ("// somecomment\n"
  1975. "#include \"a.h\"\n"
  1976. "#define A( \\\n"
  1977. " A, B)\n"
  1978. "#include \"b.h\"\n"
  1979. "// somecomment\n",
  1980. format(" // somecomment\n"
  1981. " #include \"a.h\"\n"
  1982. "#define A(A,\\\n"
  1983. " B)\n"
  1984. " #include \"b.h\"\n"
  1985. " // somecomment\n",
  1986. getLLVMStyleWithColumns(13)));
  1987. }
  1988. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  1989. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  1990. EXPECT_EQ("#define A \\\n"
  1991. " c; \\\n"
  1992. " e;\n"
  1993. "f;",
  1994. format("#define A c; e;\n"
  1995. "f;",
  1996. getLLVMStyleWithColumns(14)));
  1997. }
  1998. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  1999. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2000. EXPECT_EQ("int i;\n"
  2001. "#define A \\\n"
  2002. " int i; \\\n"
  2003. " int j\n"
  2004. "int k;",
  2005. format("int i;\n"
  2006. "#define A \\\n"
  2007. " int i ; \\\n"
  2008. " int j\n"
  2009. "int k;",
  2010. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2011. EXPECT_EQ("int i;\n"
  2012. "#define A \\\n"
  2013. " int i; \\\n"
  2014. " int j\n"
  2015. "int k;",
  2016. format("int i;\n"
  2017. "#define A \\\n"
  2018. " int i ; \\\n"
  2019. " int j\n"
  2020. "int k;",
  2021. 45, 0, getGoogleStyle())); // 45: position of "j".
  2022. }
  2023. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2024. EXPECT_EQ("int x,\n"
  2025. "#define A\n"
  2026. " y;",
  2027. format("int x,\n#define A\ny;"));
  2028. }
  2029. TEST_F(FormatTest, HashInMacroDefinition) {
  2030. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2031. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2032. verifyFormat("#define A \\\n"
  2033. " { \\\n"
  2034. " f(#c); \\\n"
  2035. " }",
  2036. getLLVMStyleWithColumns(11));
  2037. verifyFormat("#define A(X) \\\n"
  2038. " void function##X()",
  2039. getLLVMStyleWithColumns(22));
  2040. verifyFormat("#define A(a, b, c) \\\n"
  2041. " void a##b##c()",
  2042. getLLVMStyleWithColumns(22));
  2043. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2044. }
  2045. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2046. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2047. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2048. }
  2049. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2050. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2051. " \\\n"
  2052. " b;",
  2053. getLLVMStyleWithColumns(25)));
  2054. EXPECT_EQ("#define A \\\n"
  2055. " \\\n"
  2056. " a; \\\n"
  2057. " b;",
  2058. format("#define A \\\n"
  2059. " \\\n"
  2060. " a; \\\n"
  2061. " b;",
  2062. getLLVMStyleWithColumns(11)));
  2063. EXPECT_EQ("#define A \\\n"
  2064. " a; \\\n"
  2065. " \\\n"
  2066. " b;",
  2067. format("#define A \\\n"
  2068. " a; \\\n"
  2069. " \\\n"
  2070. " b;",
  2071. getLLVMStyleWithColumns(11)));
  2072. }
  2073. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2074. verifyFormat("#define A :");
  2075. verifyFormat("#define SOMECASES \\\n"
  2076. " case 1: \\\n"
  2077. " case 2\n",
  2078. getLLVMStyleWithColumns(20));
  2079. verifyFormat("#define A template <typename T>");
  2080. verifyFormat("#define STR(x) #x\n"
  2081. "f(STR(this_is_a_string_literal{));");
  2082. verifyFormat("#pragma omp threadprivate( \\\n"
  2083. " y)), // expected-warning",
  2084. getLLVMStyleWithColumns(28));
  2085. }
  2086. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2087. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2088. EXPECT_EQ("class A : public QObject {\n"
  2089. " Q_OBJECT\n"
  2090. "\n"
  2091. " A() {}\n"
  2092. "};",
  2093. format("class A : public QObject {\n"
  2094. " Q_OBJECT\n"
  2095. "\n"
  2096. " A() {\n}\n"
  2097. "} ;"));
  2098. EXPECT_EQ("SOME_MACRO\n"
  2099. "namespace {\n"
  2100. "void f();\n"
  2101. "}",
  2102. format("SOME_MACRO\n"
  2103. " namespace {\n"
  2104. "void f( );\n"
  2105. "}"));
  2106. // Only if the identifier contains at least 5 characters.
  2107. EXPECT_EQ("HTTP f();",
  2108. format("HTTP\nf();"));
  2109. EXPECT_EQ("MACRO\nf();",
  2110. format("MACRO\nf();"));
  2111. // Only if everything is upper case.
  2112. EXPECT_EQ("class A : public QObject {\n"
  2113. " Q_Object A() {}\n"
  2114. "};",
  2115. format("class A : public QObject {\n"
  2116. " Q_Object\n"
  2117. "\n"
  2118. " A() {\n}\n"
  2119. "} ;"));
  2120. }
  2121. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2122. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2123. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2124. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2125. "class X {};\n"
  2126. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2127. "int *createScopDetectionPass() { return 0; }",
  2128. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2129. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2130. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2131. " class X {};\n"
  2132. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2133. " int *createScopDetectionPass() { return 0; }"));
  2134. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2135. // braces, so that inner block is indented one level more.
  2136. EXPECT_EQ("int q() {\n"
  2137. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2138. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2139. " IPC_END_MESSAGE_MAP()\n"
  2140. "}",
  2141. format("int q() {\n"
  2142. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2143. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2144. " IPC_END_MESSAGE_MAP()\n"
  2145. "}"));
  2146. // Same inside macros.
  2147. EXPECT_EQ("#define LIST(L) \\\n"
  2148. " L(A) \\\n"
  2149. " L(B) \\\n"
  2150. " L(C)",
  2151. format("#define LIST(L) \\\n"
  2152. " L(A) \\\n"
  2153. " L(B) \\\n"
  2154. " L(C)",
  2155. getGoogleStyle()));
  2156. // These must not be recognized as macros.
  2157. EXPECT_EQ("int q() {\n"
  2158. " f(x);\n"
  2159. " f(x) {}\n"
  2160. " f(x)->g();\n"
  2161. " f(x)->*g();\n"
  2162. " f(x).g();\n"
  2163. " f(x) = x;\n"
  2164. " f(x) += x;\n"
  2165. " f(x) -= x;\n"
  2166. " f(x) *= x;\n"
  2167. " f(x) /= x;\n"
  2168. " f(x) %= x;\n"
  2169. " f(x) &= x;\n"
  2170. " f(x) |= x;\n"
  2171. " f(x) ^= x;\n"
  2172. " f(x) >>= x;\n"
  2173. " f(x) <<= x;\n"
  2174. " f(x)[y].z();\n"
  2175. " LOG(INFO) << x;\n"
  2176. " ifstream(x) >> x;\n"
  2177. "}\n",
  2178. format("int q() {\n"
  2179. " f(x)\n;\n"
  2180. " f(x)\n {}\n"
  2181. " f(x)\n->g();\n"
  2182. " f(x)\n->*g();\n"
  2183. " f(x)\n.g();\n"
  2184. " f(x)\n = x;\n"
  2185. " f(x)\n += x;\n"
  2186. " f(x)\n -= x;\n"
  2187. " f(x)\n *= x;\n"
  2188. " f(x)\n /= x;\n"
  2189. " f(x)\n %= x;\n"
  2190. " f(x)\n &= x;\n"
  2191. " f(x)\n |= x;\n"
  2192. " f(x)\n ^= x;\n"
  2193. " f(x)\n >>= x;\n"
  2194. " f(x)\n <<= x;\n"
  2195. " f(x)\n[y].z();\n"
  2196. " LOG(INFO)\n << x;\n"
  2197. " ifstream(x)\n >> x;\n"
  2198. "}\n"));
  2199. EXPECT_EQ("int q() {\n"
  2200. " F(x)\n"
  2201. " if (1) {\n"
  2202. " }\n"
  2203. " F(x)\n"
  2204. " while (1) {\n"
  2205. " }\n"
  2206. " F(x)\n"
  2207. " G(x);\n"
  2208. " F(x)\n"
  2209. " try {\n"
  2210. " Q();\n"
  2211. " }\n"
  2212. " catch (...) {\n"
  2213. " }\n"
  2214. "}\n",
  2215. format("int q() {\n"
  2216. "F(x)\n"
  2217. "if (1) {}\n"
  2218. "F(x)\n"
  2219. "while (1) {}\n"
  2220. "F(x)\n"
  2221. "G(x);\n"
  2222. "F(x)\n"
  2223. "try { Q(); } catch (...) {}\n"
  2224. "}\n"));
  2225. EXPECT_EQ("class A {\n"
  2226. " A() : t(0) {}\n"
  2227. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2228. " try : t(0) {\n"
  2229. " }\n"
  2230. " catch (...) {\n"
  2231. " }\n"
  2232. "};",
  2233. format("class A {\n"
  2234. " A()\n : t(0) {}\n"
  2235. " A(X x)\n"
  2236. " try : t(0) {} catch (...) {}\n"
  2237. "};"));
  2238. EXPECT_EQ(
  2239. "class SomeClass {\n"
  2240. "public:\n"
  2241. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2242. "};",
  2243. format("class SomeClass {\n"
  2244. "public:\n"
  2245. " SomeClass()\n"
  2246. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2247. "};"));
  2248. EXPECT_EQ(
  2249. "class SomeClass {\n"
  2250. "public:\n"
  2251. " SomeClass()\n"
  2252. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2253. "};",
  2254. format("class SomeClass {\n"
  2255. "public:\n"
  2256. " SomeClass()\n"
  2257. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2258. "};", getLLVMStyleWithColumns(40)));
  2259. }
  2260. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2261. verifyFormat("#define A \\\n"
  2262. " f({ \\\n"
  2263. " g(); \\\n"
  2264. " });", getLLVMStyleWithColumns(11));
  2265. }
  2266. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2267. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2268. }
  2269. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2270. verifyFormat("{\n { a #c; }\n}");
  2271. }
  2272. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2273. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2274. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2275. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2276. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2277. }
  2278. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2279. EXPECT_EQ(
  2280. "#define A \\\n int i; \\\n int j;",
  2281. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2282. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2283. }
  2284. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2285. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2286. }
  2287. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2288. verifyFormat("#define A \\\n"
  2289. " int v( \\\n"
  2290. " a); \\\n"
  2291. " int i;",
  2292. getLLVMStyleWithColumns(11));
  2293. }
  2294. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2295. EXPECT_EQ(
  2296. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2297. " \\\n"
  2298. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2299. "\n"
  2300. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2301. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2302. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2303. "\\\n"
  2304. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2305. " \n"
  2306. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2307. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2308. }
  2309. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2310. EXPECT_EQ("int\n"
  2311. "#define A\n"
  2312. " a;",
  2313. format("int\n#define A\na;", getGoogleStyle()));
  2314. verifyFormat("functionCallTo(\n"
  2315. " someOtherFunction(\n"
  2316. " withSomeParameters, whichInSequence,\n"
  2317. " areLongerThanALine(andAnotherCall,\n"
  2318. "#define A B\n"
  2319. " withMoreParamters,\n"
  2320. " whichStronglyInfluenceTheLayout),\n"
  2321. " andMoreParameters),\n"
  2322. " trailing);",
  2323. getLLVMStyleWithColumns(69));
  2324. verifyFormat("Foo::Foo()\n"
  2325. "#ifdef BAR\n"
  2326. " : baz(0)\n"
  2327. "#endif\n"
  2328. "{\n"
  2329. "}");
  2330. verifyFormat("void f() {\n"
  2331. " if (true)\n"
  2332. "#ifdef A\n"
  2333. " f(42);\n"
  2334. " x();\n"
  2335. "#else\n"
  2336. " g();\n"
  2337. " x();\n"
  2338. "#endif\n"
  2339. "}");
  2340. verifyFormat("void f(param1, param2,\n"
  2341. " param3,\n"
  2342. "#ifdef A\n"
  2343. " param4(param5,\n"
  2344. "#ifdef A1\n"
  2345. " param6,\n"
  2346. "#ifdef A2\n"
  2347. " param7),\n"
  2348. "#else\n"
  2349. " param8),\n"
  2350. " param9,\n"
  2351. "#endif\n"
  2352. " param10,\n"
  2353. "#endif\n"
  2354. " param11)\n"
  2355. "#else\n"
  2356. " param12)\n"
  2357. "#endif\n"
  2358. "{\n"
  2359. " x();\n"
  2360. "}",
  2361. getLLVMStyleWithColumns(28));
  2362. verifyFormat("#if 1\n"
  2363. "int i;");
  2364. verifyFormat(
  2365. "#if 1\n"
  2366. "#endif\n"
  2367. "#if 1\n"
  2368. "#else\n"
  2369. "#endif\n");
  2370. verifyFormat("DEBUG({\n"
  2371. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2372. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2373. "});\n"
  2374. "#if a\n"
  2375. "#else\n"
  2376. "#endif");
  2377. }
  2378. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2379. verifyFormat("#endif\n"
  2380. "#if B");
  2381. }
  2382. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2383. FormatStyle SingleLine = getLLVMStyle();
  2384. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2385. verifyFormat(
  2386. "#if 0\n"
  2387. "#elif 1\n"
  2388. "#endif\n"
  2389. "void foo() {\n"
  2390. " if (test) foo2();\n"
  2391. "}",
  2392. SingleLine);
  2393. }
  2394. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2395. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2396. EXPECT_EQ("functionCall({\n"
  2397. " int i;\n"
  2398. " int j;\n"
  2399. "});",
  2400. format(" functionCall ( {int i;int j;} );"));
  2401. EXPECT_EQ("functionCall({\n"
  2402. " int i;\n"
  2403. " int j;\n"
  2404. " },\n"
  2405. " aaaa, bbbb, cccc);",
  2406. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2407. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2408. format(" functionCall (aaaa, bbbb, {int i;});"));
  2409. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2410. " int i;\n"
  2411. " int j;\n"
  2412. "});",
  2413. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2414. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2415. format(" functionCall (aaaa, bbbb, {int i;});"));
  2416. verifyFormat(
  2417. "Aaa({\n"
  2418. " int i; // break\n"
  2419. " },\n"
  2420. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2421. " ccccccccccccccccc));");
  2422. verifyFormat("DEBUG({\n"
  2423. " if (a)\n"
  2424. " f();\n"
  2425. "});");
  2426. }
  2427. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2428. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2429. "int i;",
  2430. format(" SOME_MACRO {int i;} int i;"));
  2431. }
  2432. TEST_F(FormatTest, LayoutNestedBlocks) {
  2433. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2434. " struct s {\n"
  2435. " int i;\n"
  2436. " };\n"
  2437. " s kBitsToOs[] = {{10}};\n"
  2438. " for (int i = 0; i < 10; ++i)\n"
  2439. " return;\n"
  2440. "}");
  2441. verifyFormat("call(parameter, {\n"
  2442. " something();\n"
  2443. " // Comment using all columns.\n"
  2444. " somethingelse();\n"
  2445. "});",
  2446. getLLVMStyleWithColumns(40));
  2447. verifyFormat("DEBUG( //\n"
  2448. " { f(); }, a);");
  2449. verifyFormat("DEBUG( //\n"
  2450. " {\n"
  2451. " f(); //\n"
  2452. " },\n"
  2453. " a);");
  2454. EXPECT_EQ("call(parameter, {\n"
  2455. " something();\n"
  2456. " // Comment too\n"
  2457. " // looooooooooong.\n"
  2458. " somethingElse();\n"
  2459. "});",
  2460. format("call(parameter, {\n"
  2461. " something();\n"
  2462. " // Comment too looooooooooong.\n"
  2463. " somethingElse();\n"
  2464. "});",
  2465. getLLVMStyleWithColumns(29)));
  2466. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2467. EXPECT_EQ("DEBUG({ // comment\n"
  2468. " int i;\n"
  2469. "});",
  2470. format("DEBUG({ // comment\n"
  2471. "int i;\n"
  2472. "});"));
  2473. EXPECT_EQ("DEBUG({\n"
  2474. " int i;\n"
  2475. "\n"
  2476. " // comment\n"
  2477. " int j;\n"
  2478. "});",
  2479. format("DEBUG({\n"
  2480. " int i;\n"
  2481. "\n"
  2482. " // comment\n"
  2483. " int j;\n"
  2484. "});"));
  2485. verifyFormat("DEBUG({\n"
  2486. " if (a)\n"
  2487. " return;\n"
  2488. "});");
  2489. verifyGoogleFormat("DEBUG({\n"
  2490. " if (a) return;\n"
  2491. "});");
  2492. FormatStyle Style = getGoogleStyle();
  2493. Style.ColumnLimit = 45;
  2494. verifyFormat("Debug(aaaaa, {\n"
  2495. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2496. " return;\n"
  2497. " },\n"
  2498. " a);", Style);
  2499. }
  2500. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2501. EXPECT_EQ("DEBUG({\n"
  2502. " int i;\n"
  2503. " int j;\n"
  2504. "});",
  2505. format("DEBUG( {\n"
  2506. " int i;\n"
  2507. " int j;\n"
  2508. "} ) ;",
  2509. 20, 1, getLLVMStyle()));
  2510. EXPECT_EQ("DEBUG( {\n"
  2511. " int i;\n"
  2512. " int j;\n"
  2513. "} ) ;",
  2514. format("DEBUG( {\n"
  2515. " int i;\n"
  2516. " int j;\n"
  2517. "} ) ;",
  2518. 41, 1, getLLVMStyle()));
  2519. EXPECT_EQ("DEBUG( {\n"
  2520. " int i;\n"
  2521. " int j;\n"
  2522. "} ) ;",
  2523. format("DEBUG( {\n"
  2524. " int i;\n"
  2525. " int j;\n"
  2526. "} ) ;",
  2527. 41, 1, getLLVMStyle()));
  2528. EXPECT_EQ("DEBUG({\n"
  2529. " int i;\n"
  2530. " int j;\n"
  2531. "});",
  2532. format("DEBUG( {\n"
  2533. " int i;\n"
  2534. " int j;\n"
  2535. "} ) ;",
  2536. 20, 1, getLLVMStyle()));
  2537. EXPECT_EQ("Debug({\n"
  2538. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2539. " return;\n"
  2540. " },\n"
  2541. " a);",
  2542. format("Debug({\n"
  2543. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2544. " return;\n"
  2545. " },\n"
  2546. " a);",
  2547. 50, 1, getLLVMStyle()));
  2548. EXPECT_EQ("DEBUG({\n"
  2549. " DEBUG({\n"
  2550. " int a;\n"
  2551. " int b;\n"
  2552. " }) ;\n"
  2553. "});",
  2554. format("DEBUG({\n"
  2555. " DEBUG({\n"
  2556. " int a;\n"
  2557. " int b;\n" // Format this line only.
  2558. " }) ;\n" // Don't touch this line.
  2559. "});",
  2560. 35, 0, getLLVMStyle()));
  2561. EXPECT_EQ("DEBUG({\n"
  2562. " int a; //\n"
  2563. "});",
  2564. format("DEBUG({\n"
  2565. " int a; //\n"
  2566. "});",
  2567. 0, 0, getLLVMStyle()));
  2568. }
  2569. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2570. EXPECT_EQ("{}", format("{}"));
  2571. verifyFormat("enum E {};");
  2572. verifyFormat("enum E {}");
  2573. }
  2574. //===----------------------------------------------------------------------===//
  2575. // Line break tests.
  2576. //===----------------------------------------------------------------------===//
  2577. TEST_F(FormatTest, PreventConfusingIndents) {
  2578. verifyFormat(
  2579. "void f() {\n"
  2580. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2581. " parameter, parameter, parameter)),\n"
  2582. " SecondLongCall(parameter));\n"
  2583. "}");
  2584. verifyFormat(
  2585. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2586. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2587. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2588. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2589. verifyFormat(
  2590. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2591. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2592. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2593. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2594. verifyFormat(
  2595. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2596. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2597. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2598. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2599. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2600. "#define A Just forcing a new line\n"
  2601. " ddd);");
  2602. }
  2603. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2604. verifyFormat(
  2605. "bool aaaaaaa =\n"
  2606. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2607. " bbbbbbbb();");
  2608. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2609. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2610. " ccccccccc == ddddddddddd;");
  2611. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2612. " aaaaaa) &&\n"
  2613. " bbbbbb && cccccc;");
  2614. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2615. " aaaaaa) >>\n"
  2616. " bbbbbb;");
  2617. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2618. " SourceMgr.getSpellingColumnNumber(\n"
  2619. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2620. " 1);");
  2621. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2622. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2623. " cccccc) {\n}");
  2624. // If the LHS of a comparison is not a binary expression itself, the
  2625. // additional linebreak confuses many people.
  2626. verifyFormat(
  2627. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2628. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2629. "}");
  2630. verifyFormat(
  2631. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2632. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2633. "}");
  2634. verifyFormat(
  2635. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2636. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2637. "}");
  2638. // Even explicit parentheses stress the precedence enough to make the
  2639. // additional break unnecessary.
  2640. verifyFormat(
  2641. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2642. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2643. "}");
  2644. // This cases is borderline, but with the indentation it is still readable.
  2645. verifyFormat(
  2646. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2647. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2648. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2649. "}",
  2650. getLLVMStyleWithColumns(75));
  2651. // If the LHS is a binary expression, we should still use the additional break
  2652. // as otherwise the formatting hides the operator precedence.
  2653. verifyFormat(
  2654. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2655. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2656. " 5) {\n"
  2657. "}");
  2658. FormatStyle OnePerLine = getLLVMStyle();
  2659. OnePerLine.BinPackParameters = false;
  2660. verifyFormat(
  2661. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2662. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2663. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2664. OnePerLine);
  2665. }
  2666. TEST_F(FormatTest, ExpressionIndentation) {
  2667. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2668. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2669. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2670. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2671. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2672. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2673. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2674. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  2675. " ccccccccccccccccccccccccccccccccccccccccc;");
  2676. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2677. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2678. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2679. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2680. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2681. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2682. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2683. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2684. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2685. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2686. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2687. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2688. verifyFormat("if () {\n"
  2689. "} else if (aaaaa && bbbbb > // break\n"
  2690. " ccccc) {\n"
  2691. "}");
  2692. // Presence of a trailing comment used to change indentation of b.
  2693. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  2694. " b;\n"
  2695. "return aaaaaaaaaaaaaaaaaaa +\n"
  2696. " b; //",
  2697. getLLVMStyleWithColumns(30));
  2698. }
  2699. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  2700. // Not sure what the best system is here. Like this, the LHS can be found
  2701. // immediately above an operator (everything with the same or a higher
  2702. // indent). The RHS is aligned right of the operator and so compasses
  2703. // everything until something with the same indent as the operator is found.
  2704. // FIXME: Is this a good system?
  2705. FormatStyle Style = getLLVMStyle();
  2706. Style.BreakBeforeBinaryOperators = true;
  2707. verifyFormat(
  2708. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2709. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2710. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2711. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2712. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2713. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2714. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2715. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2716. " > ccccccccccccccccccccccccccccccccccccccccc;",
  2717. Style);
  2718. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2719. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2720. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2721. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2722. Style);
  2723. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2724. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2725. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2726. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2727. Style);
  2728. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2729. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2730. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2731. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2732. Style);
  2733. verifyFormat("if () {\n"
  2734. "} else if (aaaaa && bbbbb // break\n"
  2735. " > ccccc) {\n"
  2736. "}",
  2737. Style);
  2738. // Forced by comments.
  2739. verifyFormat(
  2740. "unsigned ContentSize =\n"
  2741. " sizeof(int16_t) // DWARF ARange version number\n"
  2742. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  2743. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  2744. " + sizeof(int8_t); // Segment Size (in bytes)");
  2745. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  2746. " == boost::fusion::at_c<1>(iiii).second;",
  2747. Style);
  2748. }
  2749. TEST_F(FormatTest, ConstructorInitializers) {
  2750. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  2751. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  2752. getLLVMStyleWithColumns(45));
  2753. verifyFormat("Constructor()\n"
  2754. " : Inttializer(FitsOnTheLine) {}",
  2755. getLLVMStyleWithColumns(44));
  2756. verifyFormat("Constructor()\n"
  2757. " : Inttializer(FitsOnTheLine) {}",
  2758. getLLVMStyleWithColumns(43));
  2759. verifyFormat(
  2760. "SomeClass::Constructor()\n"
  2761. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2762. verifyFormat(
  2763. "SomeClass::Constructor()\n"
  2764. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2765. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  2766. verifyFormat(
  2767. "SomeClass::Constructor()\n"
  2768. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2769. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2770. verifyFormat("Constructor()\n"
  2771. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2772. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2773. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2774. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  2775. verifyFormat("Constructor()\n"
  2776. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2777. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2778. verifyFormat("Constructor(int Parameter = 0)\n"
  2779. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  2780. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  2781. verifyFormat("Constructor()\n"
  2782. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  2783. "}",
  2784. getLLVMStyleWithColumns(60));
  2785. verifyFormat("Constructor()\n"
  2786. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2787. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  2788. // Here a line could be saved by splitting the second initializer onto two
  2789. // lines, but that is not desirable.
  2790. verifyFormat("Constructor()\n"
  2791. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2792. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  2793. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2794. FormatStyle OnePerLine = getLLVMStyle();
  2795. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2796. verifyFormat("SomeClass::Constructor()\n"
  2797. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2798. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2799. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2800. OnePerLine);
  2801. verifyFormat("SomeClass::Constructor()\n"
  2802. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  2803. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2804. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2805. OnePerLine);
  2806. verifyFormat("MyClass::MyClass(int var)\n"
  2807. " : some_var_(var), // 4 space indent\n"
  2808. " some_other_var_(var + 1) { // lined up\n"
  2809. "}",
  2810. OnePerLine);
  2811. verifyFormat("Constructor()\n"
  2812. " : aaaaa(aaaaaa),\n"
  2813. " aaaaa(aaaaaa),\n"
  2814. " aaaaa(aaaaaa),\n"
  2815. " aaaaa(aaaaaa),\n"
  2816. " aaaaa(aaaaaa) {}",
  2817. OnePerLine);
  2818. verifyFormat("Constructor()\n"
  2819. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  2820. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  2821. OnePerLine);
  2822. EXPECT_EQ("Constructor()\n"
  2823. " : // Comment forcing unwanted break.\n"
  2824. " aaaa(aaaa) {}",
  2825. format("Constructor() :\n"
  2826. " // Comment forcing unwanted break.\n"
  2827. " aaaa(aaaa) {}"));
  2828. }
  2829. TEST_F(FormatTest, MemoizationTests) {
  2830. // This breaks if the memoization lookup does not take \c Indent and
  2831. // \c LastSpace into account.
  2832. verifyFormat(
  2833. "extern CFRunLoopTimerRef\n"
  2834. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  2835. " CFTimeInterval interval, CFOptionFlags flags,\n"
  2836. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  2837. " CFRunLoopTimerContext *context) {}");
  2838. // Deep nesting somewhat works around our memoization.
  2839. verifyFormat(
  2840. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2841. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2842. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2843. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2844. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  2845. getLLVMStyleWithColumns(65));
  2846. verifyFormat(
  2847. "aaaaa(\n"
  2848. " aaaaa,\n"
  2849. " aaaaa(\n"
  2850. " aaaaa,\n"
  2851. " aaaaa(\n"
  2852. " aaaaa,\n"
  2853. " aaaaa(\n"
  2854. " aaaaa,\n"
  2855. " aaaaa(\n"
  2856. " aaaaa,\n"
  2857. " aaaaa(\n"
  2858. " aaaaa,\n"
  2859. " aaaaa(\n"
  2860. " aaaaa,\n"
  2861. " aaaaa(\n"
  2862. " aaaaa,\n"
  2863. " aaaaa(\n"
  2864. " aaaaa,\n"
  2865. " aaaaa(\n"
  2866. " aaaaa,\n"
  2867. " aaaaa(\n"
  2868. " aaaaa,\n"
  2869. " aaaaa(\n"
  2870. " aaaaa,\n"
  2871. " aaaaa))))))))))));",
  2872. getLLVMStyleWithColumns(65));
  2873. verifyFormat(
  2874. "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"
  2875. " a),\n"
  2876. " a),\n"
  2877. " a),\n"
  2878. " a),\n"
  2879. " a),\n"
  2880. " a),\n"
  2881. " a),\n"
  2882. " a),\n"
  2883. " a),\n"
  2884. " a),\n"
  2885. " a),\n"
  2886. " a),\n"
  2887. " a),\n"
  2888. " a),\n"
  2889. " a),\n"
  2890. " a),\n"
  2891. " a)",
  2892. getLLVMStyleWithColumns(65));
  2893. // This test takes VERY long when memoization is broken.
  2894. FormatStyle OnePerLine = getLLVMStyle();
  2895. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2896. OnePerLine.BinPackParameters = false;
  2897. std::string input = "Constructor()\n"
  2898. " : aaaa(a,\n";
  2899. for (unsigned i = 0, e = 80; i != e; ++i) {
  2900. input += " a,\n";
  2901. }
  2902. input += " a) {}";
  2903. verifyFormat(input, OnePerLine);
  2904. }
  2905. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  2906. verifyFormat(
  2907. "void f() {\n"
  2908. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  2909. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  2910. " f();\n"
  2911. "}");
  2912. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  2913. " Intervals[i - 1].getRange().getLast()) {\n}");
  2914. }
  2915. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  2916. // Principially, we break function declarations in a certain order:
  2917. // 1) break amongst arguments.
  2918. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  2919. " Cccccccccccccc cccccccccccccc);");
  2920. verifyFormat(
  2921. "template <class TemplateIt>\n"
  2922. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  2923. " TemplateIt *stop) {}");
  2924. // 2) break after return type.
  2925. verifyFormat(
  2926. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2927. " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  2928. getGoogleStyle());
  2929. // 3) break after (.
  2930. verifyFormat(
  2931. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  2932. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  2933. getGoogleStyle());
  2934. // 4) break before after nested name specifiers.
  2935. verifyFormat(
  2936. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2937. " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  2938. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  2939. getGoogleStyle());
  2940. // However, there are exceptions, if a sufficient amount of lines can be
  2941. // saved.
  2942. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  2943. // more adjusting.
  2944. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2945. " Cccccccccccccc cccccccccc,\n"
  2946. " Cccccccccccccc cccccccccc,\n"
  2947. " Cccccccccccccc cccccccccc,\n"
  2948. " Cccccccccccccc cccccccccc);");
  2949. verifyFormat(
  2950. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2951. " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2952. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2953. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  2954. getGoogleStyle());
  2955. verifyFormat(
  2956. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2957. " Cccccccccccccc cccccccccc,\n"
  2958. " Cccccccccccccc cccccccccc,\n"
  2959. " Cccccccccccccc cccccccccc,\n"
  2960. " Cccccccccccccc cccccccccc,\n"
  2961. " Cccccccccccccc cccccccccc,\n"
  2962. " Cccccccccccccc cccccccccc);");
  2963. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2964. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2965. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2966. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2967. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  2968. // Break after multi-line parameters.
  2969. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2970. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2971. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2972. " bbbb bbbb);");
  2973. // Treat overloaded operators like other functions.
  2974. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2975. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  2976. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2977. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  2978. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2979. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  2980. verifyGoogleFormat(
  2981. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  2982. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2983. verifyGoogleFormat(
  2984. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  2985. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2986. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2987. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  2988. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  2989. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  2990. }
  2991. TEST_F(FormatTest, TrailingReturnType) {
  2992. verifyFormat("auto foo() -> int;\n");
  2993. verifyFormat("struct S {\n"
  2994. " auto bar() const -> int;\n"
  2995. "};");
  2996. verifyFormat("template <size_t Order, typename T>\n"
  2997. "auto load_img(const std::string &filename)\n"
  2998. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  2999. // Not trailing return types.
  3000. verifyFormat("void f() { auto a = b->c(); }");
  3001. }
  3002. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3003. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3004. // they are not function-like.
  3005. FormatStyle Style = getGoogleStyle();
  3006. Style.ColumnLimit = 47;
  3007. verifyFormat("void\n"
  3008. "someLongFunction(int someLongParameter) const {\n}",
  3009. getLLVMStyleWithColumns(47));
  3010. verifyFormat("LoooooongReturnType\n"
  3011. "someLoooooooongFunction() const {}",
  3012. getLLVMStyleWithColumns(47));
  3013. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3014. " const {}",
  3015. Style);
  3016. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3017. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3018. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3019. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3020. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3021. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3022. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3023. " aaaaaaaaaaa aaaaa) const override;");
  3024. verifyGoogleFormat(
  3025. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3026. " const override;");
  3027. // Even if the first parameter has to be wrapped.
  3028. verifyFormat("void someLongFunction(\n"
  3029. " int someLongParameter) const {}",
  3030. getLLVMStyleWithColumns(46));
  3031. verifyFormat("void someLongFunction(\n"
  3032. " int someLongParameter) const {}",
  3033. Style);
  3034. verifyFormat("void someLongFunction(\n"
  3035. " int someLongParameter) override {}",
  3036. Style);
  3037. verifyFormat("void someLongFunction(\n"
  3038. " int someLongParameter) final {}",
  3039. Style);
  3040. verifyFormat("void someLongFunction(\n"
  3041. " int parameter) const override {}",
  3042. Style);
  3043. // Unless these are unknown annotations.
  3044. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3045. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3046. " LONG_AND_UGLY_ANNOTATION;");
  3047. // Breaking before function-like trailing annotations is fine to keep them
  3048. // close to their arguments.
  3049. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3050. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3051. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3052. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3053. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3054. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3055. verifyFormat(
  3056. "void aaaaaaaaaaaaaaaaaa()\n"
  3057. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3058. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3059. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3060. " __attribute__((unused));");
  3061. verifyFormat(
  3062. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3063. " GUARDED_BY(aaaaaaaaaaaa);",
  3064. getGoogleStyle());
  3065. verifyFormat(
  3066. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3067. " GUARDED_BY(aaaaaaaaaaaa);",
  3068. getGoogleStyle());
  3069. }
  3070. TEST_F(FormatTest, BreaksDesireably) {
  3071. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3072. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3073. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3074. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3075. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3076. "}");
  3077. verifyFormat(
  3078. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3079. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3080. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3081. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3082. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3083. verifyFormat(
  3084. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3085. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3086. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3087. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3088. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3089. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3090. verifyFormat(
  3091. "void f() {\n"
  3092. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3093. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3094. "}");
  3095. verifyFormat(
  3096. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3097. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3098. verifyFormat(
  3099. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3100. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3101. verifyFormat(
  3102. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3103. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3104. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3105. // Indent consistently independent of call expression.
  3106. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3107. " dddddddddddddddddddddddddddddd));\n"
  3108. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3109. " dddddddddddddddddddddddddddddd));");
  3110. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3111. // taking into account the StopAt value.
  3112. verifyFormat(
  3113. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3114. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3115. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3116. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3117. verifyFormat("{\n {\n {\n"
  3118. " Annotation.SpaceRequiredBefore =\n"
  3119. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3120. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3121. " }\n }\n}");
  3122. // Break on an outer level if there was a break on an inner level.
  3123. EXPECT_EQ("f(g(h(a, // comment\n"
  3124. " b, c),\n"
  3125. " d, e),\n"
  3126. " x, y);",
  3127. format("f(g(h(a, // comment\n"
  3128. " b, c), d, e), x, y);"));
  3129. // Prefer breaking similar line breaks.
  3130. verifyFormat(
  3131. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3132. " NSTrackingMouseEnteredAndExited |\n"
  3133. " NSTrackingActiveAlways;");
  3134. }
  3135. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3136. FormatStyle NoBinPacking = getGoogleStyle();
  3137. NoBinPacking.BinPackParameters = false;
  3138. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3139. " aaaaaaaaaaaaaaaaaaaa,\n"
  3140. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3141. NoBinPacking);
  3142. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3143. " aaaaaaaaaaaaa,\n"
  3144. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3145. NoBinPacking);
  3146. verifyFormat(
  3147. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3148. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3149. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3150. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3152. NoBinPacking);
  3153. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3154. " .aaaaaaaaaaaaaaaaaa();",
  3155. NoBinPacking);
  3156. verifyFormat("void f() {\n"
  3157. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3158. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3159. "}",
  3160. NoBinPacking);
  3161. verifyFormat(
  3162. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3163. " aaaaaaaaaaaa,\n"
  3164. " aaaaaaaaaaaa);",
  3165. NoBinPacking);
  3166. verifyFormat(
  3167. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3168. " ddddddddddddddddddddddddddddd),\n"
  3169. " test);",
  3170. NoBinPacking);
  3171. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3172. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3173. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3174. NoBinPacking);
  3175. verifyFormat("a(\"a\"\n"
  3176. " \"a\",\n"
  3177. " a);");
  3178. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3179. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3180. " aaaaaaaaa,\n"
  3181. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3182. NoBinPacking);
  3183. verifyFormat(
  3184. "void f() {\n"
  3185. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3186. " .aaaaaaa();\n"
  3187. "}",
  3188. NoBinPacking);
  3189. verifyFormat(
  3190. "template <class SomeType, class SomeOtherType>\n"
  3191. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3192. NoBinPacking);
  3193. }
  3194. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3195. FormatStyle Style = getLLVMStyleWithColumns(15);
  3196. Style.ExperimentalAutoDetectBinPacking = true;
  3197. EXPECT_EQ("aaa(aaaa,\n"
  3198. " aaaa,\n"
  3199. " aaaa);\n"
  3200. "aaa(aaaa,\n"
  3201. " aaaa,\n"
  3202. " aaaa);",
  3203. format("aaa(aaaa,\n" // one-per-line
  3204. " aaaa,\n"
  3205. " aaaa );\n"
  3206. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3207. Style));
  3208. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3209. " aaaa);\n"
  3210. "aaa(aaaa, aaaa,\n"
  3211. " aaaa);",
  3212. format("aaa(aaaa, aaaa,\n" // bin-packed
  3213. " aaaa );\n"
  3214. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3215. Style));
  3216. }
  3217. TEST_F(FormatTest, FormatsBuilderPattern) {
  3218. verifyFormat(
  3219. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3220. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3221. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3222. " .StartsWith(\".init\", ORDER_INIT)\n"
  3223. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3224. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3225. " .Default(ORDER_TEXT);\n");
  3226. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3227. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3228. verifyFormat(
  3229. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3230. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3231. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3232. verifyFormat(
  3233. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3234. " aaaaaaaaaaaaaa);");
  3235. verifyFormat(
  3236. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3237. " aaaaaa->aaaaaaaaaaaa()\n"
  3238. " ->aaaaaaaaaaaaaaaa(\n"
  3239. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3240. " ->aaaaaaaaaaaaaaaaa();");
  3241. verifyGoogleFormat(
  3242. "void f() {\n"
  3243. " someo->Add((new util::filetools::Handler(dir))\n"
  3244. " ->OnEvent1(NewPermanentCallback(\n"
  3245. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3246. " ->OnEvent2(NewPermanentCallback(\n"
  3247. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3248. " ->OnEvent3(NewPermanentCallback(\n"
  3249. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3250. " ->OnEvent5(NewPermanentCallback(\n"
  3251. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3252. " ->OnEvent6(NewPermanentCallback(\n"
  3253. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3254. "}");
  3255. verifyFormat(
  3256. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3257. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3258. " .aaaaaaaaaaaaaaa()\n"
  3259. " .aaaaaaaaaaaaaaa()\n"
  3260. " .aaaaaaaaaaaaaaa()\n"
  3261. " .aaaaaaaaaaaaaaa();");
  3262. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3263. " .aaaaaaaaaaaaaaa()\n"
  3264. " .aaaaaaaaaaaaaaa()\n"
  3265. " .aaaaaaaaaaaaaaa();");
  3266. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3267. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3268. " .aaaaaaaaaaaaaaa();");
  3269. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3270. " ->aaaaaaaaaaaaaae(0)\n"
  3271. " ->aaaaaaaaaaaaaaa();");
  3272. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3273. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3274. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3275. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3276. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3277. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3278. // Prefer not to break after empty parentheses.
  3279. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3280. " First->LastNewlineOffset);");
  3281. }
  3282. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3283. verifyFormat(
  3284. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3285. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3286. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3287. " ccccccccccccccccccccccccc) {\n}");
  3288. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3289. " ccccccccccccccccccccccccc) {\n}");
  3290. verifyFormat(
  3291. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3292. " ccccccccccccccccccccccccc) {\n}");
  3293. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3294. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3295. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3296. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3297. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3298. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3299. " aaaaaaaaaaaaaaa != aa) {\n}");
  3300. }
  3301. TEST_F(FormatTest, BreaksAfterAssignments) {
  3302. verifyFormat(
  3303. "unsigned Cost =\n"
  3304. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3305. " SI->getPointerAddressSpaceee());\n");
  3306. verifyFormat(
  3307. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3308. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3309. verifyFormat(
  3310. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3311. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3312. verifyFormat("unsigned OriginalStartColumn =\n"
  3313. " SourceMgr.getSpellingColumnNumber(\n"
  3314. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3315. " 1;");
  3316. }
  3317. TEST_F(FormatTest, AlignsAfterAssignments) {
  3318. verifyFormat(
  3319. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3320. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3321. verifyFormat(
  3322. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3323. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3324. verifyFormat(
  3325. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3326. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3327. verifyFormat(
  3328. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3329. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3330. verifyFormat(
  3331. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3332. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3333. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3334. }
  3335. TEST_F(FormatTest, AlignsAfterReturn) {
  3336. verifyFormat(
  3337. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3338. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3339. verifyFormat(
  3340. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3341. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3342. verifyFormat(
  3343. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3344. " aaaaaaaaaaaaaaaaaaaaaa();");
  3345. verifyFormat(
  3346. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3347. " aaaaaaaaaaaaaaaaaaaaaa());");
  3348. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3349. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3350. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3351. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3352. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3353. verifyFormat("return\n"
  3354. " // true if code is one of a or b.\n"
  3355. " code == a || code == b;");
  3356. }
  3357. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3358. verifyFormat(
  3359. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3360. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3361. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3362. verifyFormat(
  3363. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3364. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3365. verifyFormat(
  3366. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3367. " : aaaaaaaaaaaaa);");
  3368. verifyFormat(
  3369. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3370. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3371. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3372. " aaaaaaaaaaaaa);");
  3373. verifyFormat(
  3374. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3375. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3376. " aaaaaaaaaaaaa);");
  3377. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3378. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3379. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3380. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3381. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3382. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3383. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3384. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3385. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3386. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3388. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3389. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3390. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3391. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3392. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3393. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3394. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3395. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3396. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3397. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3398. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3399. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3400. " : aaaaaaaaaaaaaaaa;");
  3401. verifyFormat(
  3402. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3403. " ? aaaaaaaaaaaaaaa\n"
  3404. " : aaaaaaaaaaaaaaa;");
  3405. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3406. " aaaaaaaaa\n"
  3407. " ? b\n"
  3408. " : c);");
  3409. verifyFormat(
  3410. "unsigned Indent =\n"
  3411. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3412. " ? IndentForLevel[TheLine.Level]\n"
  3413. " : TheLine * 2,\n"
  3414. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3415. getLLVMStyleWithColumns(70));
  3416. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3417. " ? aaaaaaaaaaaaaaa\n"
  3418. " : bbbbbbbbbbbbbbb //\n"
  3419. " ? ccccccccccccccc\n"
  3420. " : ddddddddddddddd;");
  3421. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3422. " ? aaaaaaaaaaaaaaa\n"
  3423. " : (bbbbbbbbbbbbbbb //\n"
  3424. " ? ccccccccccccccc\n"
  3425. " : ddddddddddddddd);");
  3426. FormatStyle NoBinPacking = getLLVMStyle();
  3427. NoBinPacking.BinPackParameters = false;
  3428. verifyFormat(
  3429. "void f() {\n"
  3430. " g(aaa,\n"
  3431. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3432. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3433. " ? aaaaaaaaaaaaaaa\n"
  3434. " : aaaaaaaaaaaaaaa);\n"
  3435. "}",
  3436. NoBinPacking);
  3437. verifyFormat(
  3438. "void f() {\n"
  3439. " g(aaa,\n"
  3440. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3441. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3442. " ?: aaaaaaaaaaaaaaa);\n"
  3443. "}",
  3444. NoBinPacking);
  3445. }
  3446. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  3447. FormatStyle Style = getLLVMStyle();
  3448. Style.BreakBeforeTernaryOperators = false;
  3449. Style.ColumnLimit = 70;
  3450. verifyFormat(
  3451. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3452. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3453. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3454. Style);
  3455. verifyFormat(
  3456. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3458. Style);
  3459. verifyFormat(
  3460. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  3461. " aaaaaaaaaaaaa);",
  3462. Style);
  3463. verifyFormat(
  3464. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3465. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3466. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3467. " aaaaaaaaaaaaa);",
  3468. Style);
  3469. verifyFormat(
  3470. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3471. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3472. " aaaaaaaaaaaaa);",
  3473. Style);
  3474. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3475. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3476. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3477. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3478. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3479. Style);
  3480. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3481. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3482. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3483. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3484. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3485. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3486. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3487. Style);
  3488. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3489. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  3490. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3491. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3492. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3493. Style);
  3494. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3495. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3496. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3497. Style);
  3498. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3499. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3500. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3501. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3502. Style);
  3503. verifyFormat(
  3504. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3505. " aaaaaaaaaaaaaaa :\n"
  3506. " aaaaaaaaaaaaaaa;",
  3507. Style);
  3508. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3509. " aaaaaaaaa ?\n"
  3510. " b :\n"
  3511. " c);",
  3512. Style);
  3513. verifyFormat(
  3514. "unsigned Indent =\n"
  3515. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  3516. " IndentForLevel[TheLine.Level] :\n"
  3517. " TheLine * 2,\n"
  3518. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3519. Style);
  3520. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3521. " aaaaaaaaaaaaaaa :\n"
  3522. " bbbbbbbbbbbbbbb ? //\n"
  3523. " ccccccccccccccc :\n"
  3524. " ddddddddddddddd;",
  3525. Style);
  3526. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3527. " aaaaaaaaaaaaaaa :\n"
  3528. " (bbbbbbbbbbbbbbb ? //\n"
  3529. " ccccccccccccccc :\n"
  3530. " ddddddddddddddd);",
  3531. Style);
  3532. }
  3533. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  3534. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  3535. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  3536. verifyFormat("bool a = true, b = false;");
  3537. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3538. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  3539. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  3540. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  3541. verifyFormat(
  3542. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3543. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  3544. " d = e && f;");
  3545. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  3546. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  3547. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3548. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  3549. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  3550. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  3551. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  3552. // line. Also fix indent for breaking after the type, this looks bad.
  3553. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3554. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  3555. " *b = bbbbbbbbbbbbbbbbbbb;",
  3556. getGoogleStyle());
  3557. // Not ideal, but pointer-with-type does not allow much here.
  3558. verifyGoogleFormat(
  3559. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3560. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
  3561. }
  3562. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  3563. verifyFormat("arr[foo ? bar : baz];");
  3564. verifyFormat("f()[foo ? bar : baz];");
  3565. verifyFormat("(a + b)[foo ? bar : baz];");
  3566. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  3567. }
  3568. TEST_F(FormatTest, AlignsStringLiterals) {
  3569. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  3570. " \"short literal\");");
  3571. verifyFormat(
  3572. "looooooooooooooooooooooooongFunction(\n"
  3573. " \"short literal\"\n"
  3574. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  3575. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  3576. " \" string literals\",\n"
  3577. " and, other, parameters);");
  3578. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  3579. " \"5678\";",
  3580. format("fun + \"1243\" /* comment */\n"
  3581. " \"5678\";",
  3582. getLLVMStyleWithColumns(28)));
  3583. EXPECT_EQ(
  3584. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  3585. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  3586. " \"aaaaaaaaaaaaaaaa\";",
  3587. format("aaaaaa ="
  3588. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  3589. "aaaaaaaaaaaaaaaaaaaaa\" "
  3590. "\"aaaaaaaaaaaaaaaa\";"));
  3591. verifyFormat("a = a + \"a\"\n"
  3592. " \"a\"\n"
  3593. " \"a\";");
  3594. verifyFormat("f(\"a\", \"b\"\n"
  3595. " \"c\");");
  3596. verifyFormat(
  3597. "#define LL_FORMAT \"ll\"\n"
  3598. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  3599. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  3600. verifyFormat("#define A(X) \\\n"
  3601. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  3602. " \"ccccc\"",
  3603. getLLVMStyleWithColumns(23));
  3604. verifyFormat("#define A \"def\"\n"
  3605. "f(\"abc\" A \"ghi\"\n"
  3606. " \"jkl\");");
  3607. verifyFormat("f(L\"a\"\n"
  3608. " L\"b\")");
  3609. verifyFormat("#define A(X) \\\n"
  3610. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  3611. " L\"ccccc\"",
  3612. getLLVMStyleWithColumns(25));
  3613. }
  3614. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  3615. FormatStyle NoBreak = getLLVMStyle();
  3616. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  3617. FormatStyle Break = getLLVMStyle();
  3618. Break.AlwaysBreakBeforeMultilineStrings = true;
  3619. verifyFormat("aaaa = \"bbbb\"\n"
  3620. " \"cccc\";",
  3621. NoBreak);
  3622. verifyFormat("aaaa =\n"
  3623. " \"bbbb\"\n"
  3624. " \"cccc\";",
  3625. Break);
  3626. verifyFormat("aaaa(\"bbbb\"\n"
  3627. " \"cccc\");",
  3628. NoBreak);
  3629. verifyFormat("aaaa(\n"
  3630. " \"bbbb\"\n"
  3631. " \"cccc\");",
  3632. Break);
  3633. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  3634. " \"cccc\");",
  3635. NoBreak);
  3636. verifyFormat("aaaa(qqq,\n"
  3637. " \"bbbb\"\n"
  3638. " \"cccc\");",
  3639. Break);
  3640. verifyFormat("aaaa(qqq,\n"
  3641. " L\"bbbb\"\n"
  3642. " L\"cccc\");",
  3643. Break);
  3644. // Don't break if there is no column gain.
  3645. verifyFormat("f(\"aaaa\"\n"
  3646. " \"bbbb\");",
  3647. Break);
  3648. // Treat literals with escaped newlines like multi-line string literals.
  3649. EXPECT_EQ("x = \"a\\\n"
  3650. "b\\\n"
  3651. "c\";",
  3652. format("x = \"a\\\n"
  3653. "b\\\n"
  3654. "c\";",
  3655. NoBreak));
  3656. EXPECT_EQ("x =\n"
  3657. " \"a\\\n"
  3658. "b\\\n"
  3659. "c\";",
  3660. format("x = \"a\\\n"
  3661. "b\\\n"
  3662. "c\";",
  3663. Break));
  3664. // Exempt ObjC strings for now.
  3665. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  3666. " \"bbbb\";",
  3667. format("NSString *const kString = @\"aaaa\"\n"
  3668. "\"bbbb\";",
  3669. Break));
  3670. }
  3671. TEST_F(FormatTest, AlignsPipes) {
  3672. verifyFormat(
  3673. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3674. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3675. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3676. verifyFormat(
  3677. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  3678. " << aaaaaaaaaaaaaaaaaaaa;");
  3679. verifyFormat(
  3680. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3681. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3682. verifyFormat(
  3683. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  3684. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  3685. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  3686. verifyFormat(
  3687. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3688. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3689. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3690. verifyFormat("return out << \"somepacket = {\\n\"\n"
  3691. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  3692. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  3693. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  3694. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  3695. " << \"}\";");
  3696. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  3697. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  3698. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  3699. verifyFormat(
  3700. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  3701. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  3702. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  3703. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  3704. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  3705. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  3706. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3707. verifyFormat(
  3708. "void f() {\n"
  3709. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  3710. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3711. "}");
  3712. // Breaking before the first "<<" is generally not desirable.
  3713. verifyFormat(
  3714. "llvm::errs()\n"
  3715. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3716. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3717. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3718. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3719. getLLVMStyleWithColumns(70));
  3720. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3721. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3722. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3723. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3724. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3725. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3726. getLLVMStyleWithColumns(70));
  3727. // But sometimes, breaking before the first "<<" is desirable.
  3728. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  3729. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  3730. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  3731. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3732. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3733. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  3734. " << BEF << IsTemplate << Description << E->getType();");
  3735. verifyFormat(
  3736. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3737. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3738. // Incomplete string literal.
  3739. EXPECT_EQ("llvm::errs() << \"\n"
  3740. " << a;",
  3741. format("llvm::errs() << \"\n<<a;"));
  3742. verifyFormat("void f() {\n"
  3743. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  3744. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  3745. "}");
  3746. }
  3747. TEST_F(FormatTest, UnderstandsEquals) {
  3748. verifyFormat(
  3749. "aaaaaaaaaaaaaaaaa =\n"
  3750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3751. verifyFormat(
  3752. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3754. verifyFormat(
  3755. "if (a) {\n"
  3756. " f();\n"
  3757. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3758. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3759. "}");
  3760. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3761. " 100000000 + 10000000) {\n}");
  3762. }
  3763. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  3764. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3765. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  3766. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3767. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  3768. verifyFormat(
  3769. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  3770. " Parameter2);");
  3771. verifyFormat(
  3772. "ShortObject->shortFunction(\n"
  3773. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  3774. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  3775. verifyFormat("loooooooooooooongFunction(\n"
  3776. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  3777. verifyFormat(
  3778. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  3779. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  3780. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  3781. " .WillRepeatedly(Return(SomeValue));");
  3782. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  3783. " ccccccccccccccccccccccc);");
  3784. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3785. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  3786. " aaaaaaaaaaaaaaaaaaaaa);");
  3787. verifyFormat("void f() {\n"
  3788. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3789. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  3790. "}");
  3791. verifyFormat(
  3792. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3793. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3794. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3795. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3796. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3797. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3798. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3799. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3800. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  3801. "}");
  3802. // Here, it is not necessary to wrap at "." or "->".
  3803. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  3804. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3805. verifyFormat(
  3806. "aaaaaaaaaaa->aaaaaaaaa(\n"
  3807. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3808. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  3809. verifyFormat(
  3810. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3811. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  3812. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  3813. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3814. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  3815. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3816. // FIXME: Should we break before .a()?
  3817. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3818. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  3819. FormatStyle NoBinPacking = getLLVMStyle();
  3820. NoBinPacking.BinPackParameters = false;
  3821. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3822. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3823. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  3824. " aaaaaaaaaaaaaaaaaaa,\n"
  3825. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3826. NoBinPacking);
  3827. // If there is a subsequent call, change to hanging indentation.
  3828. verifyFormat(
  3829. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3830. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  3831. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3832. verifyFormat(
  3833. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3834. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  3835. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3836. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3837. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3838. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3839. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3840. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  3841. }
  3842. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  3843. verifyFormat("template <typename T>\n"
  3844. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  3845. verifyFormat("template <typename T>\n"
  3846. "// T should be one of {A, B}.\n"
  3847. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  3848. verifyFormat(
  3849. "template <typename T>\n"
  3850. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  3851. verifyFormat("template <typename T>\n"
  3852. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  3853. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  3854. verifyFormat(
  3855. "template <typename T>\n"
  3856. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  3857. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  3858. verifyFormat(
  3859. "template <typename T>\n"
  3860. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  3861. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  3862. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3863. verifyFormat("template <typename T>\n"
  3864. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3865. " int aaaaaaaaaaaaaaaaaaaaaa);");
  3866. verifyFormat(
  3867. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  3868. " typename T4 = char>\n"
  3869. "void f();");
  3870. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  3871. " template <typename> class cccccccccccccccccccccc,\n"
  3872. " typename ddddddddddddd>\n"
  3873. "class C {};");
  3874. verifyFormat(
  3875. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  3876. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3877. verifyFormat("void f() {\n"
  3878. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  3879. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  3880. "}");
  3881. verifyFormat("template <typename T> class C {};");
  3882. verifyFormat("template <typename T> void f();");
  3883. verifyFormat("template <typename T> void f() {}");
  3884. verifyFormat(
  3885. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3886. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3887. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  3888. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3889. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3890. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  3891. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  3892. getLLVMStyleWithColumns(72));
  3893. FormatStyle AlwaysBreak = getLLVMStyle();
  3894. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  3895. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  3896. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  3897. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  3898. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3899. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  3900. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  3901. verifyFormat("template <template <typename> class Fooooooo,\n"
  3902. " template <typename> class Baaaaaaar>\n"
  3903. "struct C {};",
  3904. AlwaysBreak);
  3905. verifyFormat("template <typename T> // T can be A, B or C.\n"
  3906. "struct C {};",
  3907. AlwaysBreak);
  3908. }
  3909. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  3910. verifyFormat(
  3911. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3912. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3913. verifyFormat(
  3914. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3915. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  3917. // FIXME: Should we have the extra indent after the second break?
  3918. verifyFormat(
  3919. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3920. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3921. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3922. verifyFormat(
  3923. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  3924. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  3925. // Breaking at nested name specifiers is generally not desirable.
  3926. verifyFormat(
  3927. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3928. " aaaaaaaaaaaaaaaaaaaaaaa);");
  3929. verifyFormat(
  3930. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3931. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3932. " aaaaaaaaaaaaaaaaaaaaa);",
  3933. getLLVMStyleWithColumns(74));
  3934. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3935. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3936. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3937. }
  3938. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  3939. verifyFormat("A<int> a;");
  3940. verifyFormat("A<A<A<int>>> a;");
  3941. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  3942. verifyFormat("bool x = a < 1 || 2 > a;");
  3943. verifyFormat("bool x = 5 < f<int>();");
  3944. verifyFormat("bool x = f<int>() > 5;");
  3945. verifyFormat("bool x = 5 < a<int>::x;");
  3946. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  3947. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  3948. verifyGoogleFormat("A<A<int>> a;");
  3949. verifyGoogleFormat("A<A<A<int>>> a;");
  3950. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  3951. verifyGoogleFormat("A<A<int> > a;");
  3952. verifyGoogleFormat("A<A<A<int> > > a;");
  3953. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  3954. verifyGoogleFormat("A<::A<int>> a;");
  3955. verifyGoogleFormat("A<::A> a;");
  3956. verifyGoogleFormat("A< ::A> a;");
  3957. verifyGoogleFormat("A< ::A<int> > a;");
  3958. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  3959. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  3960. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  3961. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  3962. verifyFormat("test >> a >> b;");
  3963. verifyFormat("test << a >> b;");
  3964. verifyFormat("f<int>();");
  3965. verifyFormat("template <typename T> void f() {}");
  3966. // Not template parameters.
  3967. verifyFormat("return a < b && c > d;");
  3968. verifyFormat("void f() {\n"
  3969. " while (a < b && c > d) {\n"
  3970. " }\n"
  3971. "}");
  3972. verifyFormat("template <typename... Types>\n"
  3973. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  3974. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3975. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  3976. getLLVMStyleWithColumns(60));
  3977. }
  3978. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  3979. verifyFormat("COMPARE(a, ==, b);");
  3980. }
  3981. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  3982. verifyFormat("int A::*x;");
  3983. verifyFormat("int (S::*func)(void *);");
  3984. verifyFormat("void f() { int (S::*func)(void *); }");
  3985. verifyFormat("typedef bool *(Class::*Member)() const;");
  3986. verifyFormat("void f() {\n"
  3987. " (a->*f)();\n"
  3988. " a->*x;\n"
  3989. " (a.*f)();\n"
  3990. " ((*a).*f)();\n"
  3991. " a.*x;\n"
  3992. "}");
  3993. verifyFormat("void f() {\n"
  3994. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  3995. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  3996. "}");
  3997. FormatStyle Style = getLLVMStyle();
  3998. Style.PointerBindsToType = true;
  3999. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4000. }
  4001. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4002. verifyFormat("int a = -2;");
  4003. verifyFormat("f(-1, -2, -3);");
  4004. verifyFormat("a[-1] = 5;");
  4005. verifyFormat("int a = 5 + -2;");
  4006. verifyFormat("if (i == -1) {\n}");
  4007. verifyFormat("if (i != -1) {\n}");
  4008. verifyFormat("if (i > -1) {\n}");
  4009. verifyFormat("if (i < -1) {\n}");
  4010. verifyFormat("++(a->f());");
  4011. verifyFormat("--(a->f());");
  4012. verifyFormat("(a->f())++;");
  4013. verifyFormat("a[42]++;");
  4014. verifyFormat("if (!(a->f())) {\n}");
  4015. verifyFormat("a-- > b;");
  4016. verifyFormat("b ? -a : c;");
  4017. verifyFormat("n * sizeof char16;");
  4018. verifyFormat("n * alignof char16;", getGoogleStyle());
  4019. verifyFormat("sizeof(char);");
  4020. verifyFormat("alignof(char);", getGoogleStyle());
  4021. verifyFormat("return -1;");
  4022. verifyFormat("switch (a) {\n"
  4023. "case -1:\n"
  4024. " break;\n"
  4025. "}");
  4026. verifyFormat("#define X -1");
  4027. verifyFormat("#define X -kConstant");
  4028. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4029. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4030. verifyFormat("int a = /* confusing comment */ -1;");
  4031. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4032. verifyFormat("int a = i /* confusing comment */++;");
  4033. }
  4034. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4035. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4036. " aaaaa)) {\n"
  4037. "}");
  4038. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4039. " aaaaa));");
  4040. verifyFormat("*aaa = aaaaaaa( // break\n"
  4041. " bbbbbb);");
  4042. }
  4043. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4044. verifyFormat("bool operator<();");
  4045. verifyFormat("bool operator>();");
  4046. verifyFormat("bool operator=();");
  4047. verifyFormat("bool operator==();");
  4048. verifyFormat("bool operator!=();");
  4049. verifyFormat("int operator+();");
  4050. verifyFormat("int operator++();");
  4051. verifyFormat("bool operator();");
  4052. verifyFormat("bool operator()();");
  4053. verifyFormat("bool operator[]();");
  4054. verifyFormat("operator bool();");
  4055. verifyFormat("operator int();");
  4056. verifyFormat("operator void *();");
  4057. verifyFormat("operator SomeType<int>();");
  4058. verifyFormat("operator SomeType<int, int>();");
  4059. verifyFormat("operator SomeType<SomeType<int>>();");
  4060. verifyFormat("void *operator new(std::size_t size);");
  4061. verifyFormat("void *operator new[](std::size_t size);");
  4062. verifyFormat("void operator delete(void *ptr);");
  4063. verifyFormat("void operator delete[](void *ptr);");
  4064. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4065. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4066. verifyFormat(
  4067. "ostream &operator<<(ostream &OutputStream,\n"
  4068. " SomeReallyLongType WithSomeReallyLongValue);");
  4069. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4070. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4071. " return left.group < right.group;\n"
  4072. "}");
  4073. verifyFormat("SomeType &operator=(const SomeType &S);");
  4074. verifyGoogleFormat("operator void*();");
  4075. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4076. verifyGoogleFormat("operator ::A();");
  4077. verifyFormat("using A::operator+;");
  4078. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4079. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4080. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4081. verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
  4082. }
  4083. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4084. verifyFormat("void f() {\n"
  4085. " A *a = new A;\n"
  4086. " A *a = new (placement) A;\n"
  4087. " delete a;\n"
  4088. " delete (A *)a;\n"
  4089. "}");
  4090. }
  4091. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4092. verifyFormat("int *f(int *a) {}");
  4093. verifyFormat("int main(int argc, char **argv) {}");
  4094. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4095. verifyIndependentOfContext("f(a, *a);");
  4096. verifyFormat("void g() { f(*a); }");
  4097. verifyIndependentOfContext("int a = b * 10;");
  4098. verifyIndependentOfContext("int a = 10 * b;");
  4099. verifyIndependentOfContext("int a = b * c;");
  4100. verifyIndependentOfContext("int a += b * c;");
  4101. verifyIndependentOfContext("int a -= b * c;");
  4102. verifyIndependentOfContext("int a *= b * c;");
  4103. verifyIndependentOfContext("int a /= b * c;");
  4104. verifyIndependentOfContext("int a = *b;");
  4105. verifyIndependentOfContext("int a = *b * c;");
  4106. verifyIndependentOfContext("int a = b * *c;");
  4107. verifyIndependentOfContext("return 10 * b;");
  4108. verifyIndependentOfContext("return *b * *c;");
  4109. verifyIndependentOfContext("return a & ~b;");
  4110. verifyIndependentOfContext("f(b ? *c : *d);");
  4111. verifyIndependentOfContext("int a = b ? *c : *d;");
  4112. verifyIndependentOfContext("*b = a;");
  4113. verifyIndependentOfContext("a * ~b;");
  4114. verifyIndependentOfContext("a * !b;");
  4115. verifyIndependentOfContext("a * +b;");
  4116. verifyIndependentOfContext("a * -b;");
  4117. verifyIndependentOfContext("a * ++b;");
  4118. verifyIndependentOfContext("a * --b;");
  4119. verifyIndependentOfContext("a[4] * b;");
  4120. verifyIndependentOfContext("a[a * a] = 1;");
  4121. verifyIndependentOfContext("f() * b;");
  4122. verifyIndependentOfContext("a * [self dostuff];");
  4123. verifyIndependentOfContext("int x = a * (a + b);");
  4124. verifyIndependentOfContext("(a *)(a + b);");
  4125. verifyIndependentOfContext("int *pa = (int *)&a;");
  4126. verifyIndependentOfContext("return sizeof(int **);");
  4127. verifyIndependentOfContext("return sizeof(int ******);");
  4128. verifyIndependentOfContext("return (int **&)a;");
  4129. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4130. verifyFormat("void f(Type (*parameter)[10]) {}");
  4131. verifyGoogleFormat("return sizeof(int**);");
  4132. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4133. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4134. verifyFormat("auto a = [](int **&, int ***) {};");
  4135. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4136. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4137. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4138. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4139. verifyIndependentOfContext("A<int *> a;");
  4140. verifyIndependentOfContext("A<int **> a;");
  4141. verifyIndependentOfContext("A<int *, int *> a;");
  4142. verifyIndependentOfContext(
  4143. "const char *const p = reinterpret_cast<const char *const>(q);");
  4144. verifyIndependentOfContext("A<int **, int **> a;");
  4145. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4146. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4147. verifyFormat("for (; a && b;) {\n}");
  4148. verifyFormat(
  4149. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4150. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4151. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4152. verifyGoogleFormat("A<int*> a;");
  4153. verifyGoogleFormat("A<int**> a;");
  4154. verifyGoogleFormat("A<int*, int*> a;");
  4155. verifyGoogleFormat("A<int**, int**> a;");
  4156. verifyGoogleFormat("f(b ? *c : *d);");
  4157. verifyGoogleFormat("int a = b ? *c : *d;");
  4158. verifyGoogleFormat("Type* t = **x;");
  4159. verifyGoogleFormat("Type* t = *++*x;");
  4160. verifyGoogleFormat("*++*x;");
  4161. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4162. verifyGoogleFormat("Type* t = x++ * y;");
  4163. verifyGoogleFormat(
  4164. "const char* const p = reinterpret_cast<const char* const>(q);");
  4165. verifyIndependentOfContext("a = *(x + y);");
  4166. verifyIndependentOfContext("a = &(x + y);");
  4167. verifyIndependentOfContext("*(x + y).call();");
  4168. verifyIndependentOfContext("&(x + y)->call();");
  4169. verifyFormat("void f() { &(*I).first; }");
  4170. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4171. verifyFormat(
  4172. "int *MyValues = {*A, // Operator detection might be confused by the '{'\n"
  4173. " *BB // Operator detection might be confused by previous comment\n"
  4174. "};");
  4175. verifyIndependentOfContext("if (int *a = &b)");
  4176. verifyIndependentOfContext("if (int &a = *b)");
  4177. verifyIndependentOfContext("if (a & b[i])");
  4178. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4179. verifyIndependentOfContext("if (*b[i])");
  4180. verifyIndependentOfContext("if (int *a = (&b))");
  4181. verifyIndependentOfContext("while (int *a = &b)");
  4182. verifyIndependentOfContext("size = sizeof *a;");
  4183. verifyFormat("void f() {\n"
  4184. " for (const int &v : Values) {\n"
  4185. " }\n"
  4186. "}");
  4187. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4188. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4189. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4190. verifyFormat("#define A (!a * b)");
  4191. verifyFormat("#define MACRO \\\n"
  4192. " int *i = a * b; \\\n"
  4193. " void f(a *b);",
  4194. getLLVMStyleWithColumns(19));
  4195. verifyIndependentOfContext("A = new SomeType *[Length];");
  4196. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4197. verifyIndependentOfContext("T **t = new T *;");
  4198. verifyIndependentOfContext("T **t = new T *();");
  4199. verifyGoogleFormat("A = new SomeType* [Length]();");
  4200. verifyGoogleFormat("A = new SomeType* [Length];");
  4201. verifyGoogleFormat("T** t = new T*;");
  4202. verifyGoogleFormat("T** t = new T*();");
  4203. FormatStyle PointerLeft = getLLVMStyle();
  4204. PointerLeft.PointerBindsToType = true;
  4205. verifyFormat("delete *x;", PointerLeft);
  4206. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4207. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4208. verifyFormat("template <bool a, bool b> "
  4209. "typename t::if<x && y>::type f() {};");
  4210. verifyFormat("template <int *y> f() {};");
  4211. verifyFormat("vector<int *> v;");
  4212. verifyFormat("vector<int *const> v;");
  4213. verifyFormat("vector<int *const **const *> v;");
  4214. verifyFormat("vector<int *volatile> v;");
  4215. verifyFormat("vector<a * b> v;");
  4216. verifyFormat("foo<b && false>();");
  4217. verifyFormat("foo<b & 1>();");
  4218. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4219. // foo<x> d > v; doesn't make sense.
  4220. verifyFormat("foo<a < b && c> d > v;");
  4221. }
  4222. TEST_F(FormatTest, UnderstandsAttributes) {
  4223. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4224. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4225. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4226. }
  4227. TEST_F(FormatTest, UnderstandsEllipsis) {
  4228. verifyFormat("int printf(const char *fmt, ...);");
  4229. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4230. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4231. FormatStyle PointersLeft = getLLVMStyle();
  4232. PointersLeft.PointerBindsToType = true;
  4233. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4234. }
  4235. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4236. EXPECT_EQ("int *a;\n"
  4237. "int *a;\n"
  4238. "int *a;",
  4239. format("int *a;\n"
  4240. "int* a;\n"
  4241. "int *a;",
  4242. getGoogleStyle()));
  4243. EXPECT_EQ("int* a;\n"
  4244. "int* a;\n"
  4245. "int* a;",
  4246. format("int* a;\n"
  4247. "int* a;\n"
  4248. "int *a;",
  4249. getGoogleStyle()));
  4250. EXPECT_EQ("int *a;\n"
  4251. "int *a;\n"
  4252. "int *a;",
  4253. format("int *a;\n"
  4254. "int * a;\n"
  4255. "int * a;",
  4256. getGoogleStyle()));
  4257. }
  4258. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  4259. verifyFormat("int f(int &&a) {}");
  4260. verifyFormat("int f(int a, char &&b) {}");
  4261. verifyFormat("void f() { int &&a = b; }");
  4262. verifyGoogleFormat("int f(int a, char&& b) {}");
  4263. verifyGoogleFormat("void f() { int&& a = b; }");
  4264. verifyIndependentOfContext("A<int &&> a;");
  4265. verifyIndependentOfContext("A<int &&, int &&> a;");
  4266. verifyGoogleFormat("A<int&&> a;");
  4267. verifyGoogleFormat("A<int&&, int&&> a;");
  4268. // Not rvalue references:
  4269. verifyFormat("template <bool B, bool C> class A {\n"
  4270. " static_assert(B && C, \"Something is wrong\");\n"
  4271. "};");
  4272. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  4273. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  4274. }
  4275. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  4276. verifyFormat("void f() {\n"
  4277. " x[aaaaaaaaa -\n"
  4278. " b] = 23;\n"
  4279. "}",
  4280. getLLVMStyleWithColumns(15));
  4281. }
  4282. TEST_F(FormatTest, FormatsCasts) {
  4283. verifyFormat("Type *A = static_cast<Type *>(P);");
  4284. verifyFormat("Type *A = (Type *)P;");
  4285. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  4286. verifyFormat("int a = (int)(2.0f);");
  4287. verifyFormat("int a = (int)2.0f;");
  4288. verifyFormat("x[(int32)y];");
  4289. verifyFormat("x = (int32)y;");
  4290. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  4291. verifyFormat("int a = (int)*b;");
  4292. verifyFormat("int a = (int)2.0f;");
  4293. verifyFormat("int a = (int)~0;");
  4294. verifyFormat("int a = (int)++a;");
  4295. verifyFormat("int a = (int)sizeof(int);");
  4296. verifyFormat("int a = (int)+2;");
  4297. verifyFormat("my_int a = (my_int)2.0f;");
  4298. verifyFormat("my_int a = (my_int)sizeof(int);");
  4299. verifyFormat("return (my_int)aaa;");
  4300. verifyFormat("#define x ((int)-1)");
  4301. verifyFormat("#define p(q) ((int *)&q)");
  4302. // FIXME: Without type knowledge, this can still fall apart miserably.
  4303. verifyFormat("void f() { my_int a = (my_int) * b; }");
  4304. verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
  4305. verifyFormat("my_int a = (my_int) ~0;");
  4306. verifyFormat("my_int a = (my_int)++ a;");
  4307. verifyFormat("my_int a = (my_int) + 2;");
  4308. // Don't break after a cast's
  4309. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4310. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  4311. " bbbbbbbbbbbbbbbbbbbbbb);");
  4312. // These are not casts.
  4313. verifyFormat("void f(int *) {}");
  4314. verifyFormat("f(foo)->b;");
  4315. verifyFormat("f(foo).b;");
  4316. verifyFormat("f(foo)(b);");
  4317. verifyFormat("f(foo)[b];");
  4318. verifyFormat("[](foo) { return 4; }(bar);");
  4319. verifyFormat("(*funptr)(foo)[4];");
  4320. verifyFormat("funptrs[4](foo)[4];");
  4321. verifyFormat("void f(int *);");
  4322. verifyFormat("void f(int *) = 0;");
  4323. verifyFormat("void f(SmallVector<int>) {}");
  4324. verifyFormat("void f(SmallVector<int>);");
  4325. verifyFormat("void f(SmallVector<int>) = 0;");
  4326. verifyFormat("void f(int i = (kValue) * kMask) {}");
  4327. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  4328. verifyFormat("int a = sizeof(int) * b;");
  4329. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  4330. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  4331. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  4332. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  4333. // These are not casts, but at some point were confused with casts.
  4334. verifyFormat("virtual void foo(int *) override;");
  4335. verifyFormat("virtual void foo(char &) const;");
  4336. verifyFormat("virtual void foo(int *a, char *) const;");
  4337. verifyFormat("int a = sizeof(int *) + b;");
  4338. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  4339. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  4340. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4341. // FIXME: The indentation here is not ideal.
  4342. verifyFormat(
  4343. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4344. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  4345. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  4346. }
  4347. TEST_F(FormatTest, FormatsFunctionTypes) {
  4348. verifyFormat("A<bool()> a;");
  4349. verifyFormat("A<SomeType()> a;");
  4350. verifyFormat("A<void (*)(int, std::string)> a;");
  4351. verifyFormat("A<void *(int)>;");
  4352. verifyFormat("void *(*a)(int *, SomeType *);");
  4353. verifyFormat("int (*func)(void *);");
  4354. verifyFormat("void f() { int (*func)(void *); }");
  4355. verifyFormat("template <class CallbackClass>\n"
  4356. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  4357. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  4358. verifyGoogleFormat("void* (*a)(int);");
  4359. verifyGoogleFormat(
  4360. "template <class CallbackClass>\n"
  4361. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  4362. // Other constructs can look somewhat like function types:
  4363. verifyFormat("A<sizeof(*x)> a;");
  4364. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  4365. verifyFormat("some_var = function(*some_pointer_var)[0];");
  4366. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  4367. }
  4368. TEST_F(FormatTest, BreaksLongDeclarations) {
  4369. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  4370. " AnotherNameForTheLongType;",
  4371. getGoogleStyle());
  4372. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  4373. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4374. getGoogleStyle());
  4375. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4376. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  4377. getGoogleStyle());
  4378. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  4379. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  4380. getGoogleStyle());
  4381. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4382. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  4383. getGoogleStyle());
  4384. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4385. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4386. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  4387. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4388. // FIXME: Without the comment, this breaks after "(".
  4389. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  4390. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  4391. getGoogleStyle());
  4392. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  4393. " int LoooooooooooooooooooongParam2) {}");
  4394. verifyFormat(
  4395. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  4396. " SourceLocation L, IdentifierIn *II,\n"
  4397. " Type *T) {}");
  4398. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  4399. "ReallyReallyLongFunctionName(\n"
  4400. " const std::string &SomeParameter,\n"
  4401. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  4402. " ReallyReallyLongParameterName,\n"
  4403. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  4404. " AnotherLongParameterName) {}");
  4405. verifyFormat("template <typename A>\n"
  4406. "SomeLoooooooooooooooooooooongType<\n"
  4407. " typename some_namespace::SomeOtherType<A>::Type>\n"
  4408. "Function() {}");
  4409. verifyGoogleFormat(
  4410. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  4411. " aaaaaaaaaaaaaaaaaaaaaaa;");
  4412. verifyGoogleFormat(
  4413. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  4414. " SourceLocation L) {}");
  4415. verifyGoogleFormat(
  4416. "some_namespace::LongReturnType\n"
  4417. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  4418. " int first_long_parameter, int second_parameter) {}");
  4419. verifyGoogleFormat("template <typename T>\n"
  4420. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  4421. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  4422. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4423. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  4424. }
  4425. TEST_F(FormatTest, FormatsArrays) {
  4426. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  4427. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  4428. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4429. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  4430. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4431. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  4432. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4433. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  4434. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  4435. verifyFormat(
  4436. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  4437. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  4438. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  4439. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  4440. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  4441. }
  4442. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  4443. verifyFormat("(a)->b();");
  4444. verifyFormat("--a;");
  4445. }
  4446. TEST_F(FormatTest, HandlesIncludeDirectives) {
  4447. verifyFormat("#include <string>\n"
  4448. "#include <a/b/c.h>\n"
  4449. "#include \"a/b/string\"\n"
  4450. "#include \"string.h\"\n"
  4451. "#include \"string.h\"\n"
  4452. "#include <a-a>\n"
  4453. "#include < path with space >\n"
  4454. "#include \"abc.h\" // this is included for ABC\n"
  4455. "#include \"some long include\" // with a comment\n"
  4456. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  4457. getLLVMStyleWithColumns(35));
  4458. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  4459. EXPECT_EQ("#include <a>", format("#include<a>"));
  4460. verifyFormat("#import <string>");
  4461. verifyFormat("#import <a/b/c.h>");
  4462. verifyFormat("#import \"a/b/string\"");
  4463. verifyFormat("#import \"string.h\"");
  4464. verifyFormat("#import \"string.h\"");
  4465. verifyFormat("#if __has_include(<strstream>)\n"
  4466. "#include <strstream>\n"
  4467. "#endif");
  4468. // Protocol buffer definition or missing "#".
  4469. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  4470. getLLVMStyleWithColumns(30));
  4471. }
  4472. //===----------------------------------------------------------------------===//
  4473. // Error recovery tests.
  4474. //===----------------------------------------------------------------------===//
  4475. TEST_F(FormatTest, IncompleteParameterLists) {
  4476. FormatStyle NoBinPacking = getLLVMStyle();
  4477. NoBinPacking.BinPackParameters = false;
  4478. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  4479. " double *min_x,\n"
  4480. " double *max_x,\n"
  4481. " double *min_y,\n"
  4482. " double *max_y,\n"
  4483. " double *min_z,\n"
  4484. " double *max_z, ) {}",
  4485. NoBinPacking);
  4486. }
  4487. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  4488. verifyFormat("void f() { return; }\n42");
  4489. verifyFormat("void f() {\n"
  4490. " if (0)\n"
  4491. " return;\n"
  4492. "}\n"
  4493. "42");
  4494. verifyFormat("void f() { return }\n42");
  4495. verifyFormat("void f() {\n"
  4496. " if (0)\n"
  4497. " return\n"
  4498. "}\n"
  4499. "42");
  4500. }
  4501. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  4502. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  4503. EXPECT_EQ("void f() {\n"
  4504. " if (a)\n"
  4505. " return\n"
  4506. "}",
  4507. format("void f ( ) { if ( a ) return }"));
  4508. EXPECT_EQ("namespace N {\n"
  4509. "void f()\n"
  4510. "}",
  4511. format("namespace N { void f() }"));
  4512. EXPECT_EQ("namespace N {\n"
  4513. "void f() {}\n"
  4514. "void g()\n"
  4515. "}",
  4516. format("namespace N { void f( ) { } void g( ) }"));
  4517. }
  4518. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  4519. verifyFormat("int aaaaaaaa =\n"
  4520. " // Overlylongcomment\n"
  4521. " b;",
  4522. getLLVMStyleWithColumns(20));
  4523. verifyFormat("function(\n"
  4524. " ShortArgument,\n"
  4525. " LoooooooooooongArgument);\n",
  4526. getLLVMStyleWithColumns(20));
  4527. }
  4528. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  4529. verifyFormat("public:");
  4530. verifyFormat("class A {\n"
  4531. "public\n"
  4532. " void f() {}\n"
  4533. "};");
  4534. verifyFormat("public\n"
  4535. "int qwerty;");
  4536. verifyFormat("public\n"
  4537. "B {}");
  4538. verifyFormat("public\n"
  4539. "{}");
  4540. verifyFormat("public\n"
  4541. "B { int x; }");
  4542. }
  4543. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  4544. verifyFormat("{");
  4545. verifyFormat("#})");
  4546. }
  4547. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  4548. verifyFormat("do {\n}");
  4549. verifyFormat("do {\n}\n"
  4550. "f();");
  4551. verifyFormat("do {\n}\n"
  4552. "wheeee(fun);");
  4553. verifyFormat("do {\n"
  4554. " f();\n"
  4555. "}");
  4556. }
  4557. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  4558. verifyFormat("if {\n foo;\n foo();\n}");
  4559. verifyFormat("switch {\n foo;\n foo();\n}");
  4560. verifyFormat("for {\n foo;\n foo();\n}");
  4561. verifyFormat("while {\n foo;\n foo();\n}");
  4562. verifyFormat("do {\n foo;\n foo();\n} while;");
  4563. }
  4564. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  4565. verifyFormat("namespace {\n"
  4566. "class Foo { Foo (\n"
  4567. "};\n"
  4568. "} // comment");
  4569. }
  4570. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  4571. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  4572. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  4573. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  4574. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  4575. EXPECT_EQ("{\n"
  4576. " {\n"
  4577. " breakme(\n"
  4578. " qwe);\n"
  4579. " }\n",
  4580. format("{\n"
  4581. " {\n"
  4582. " breakme(qwe);\n"
  4583. "}\n",
  4584. getLLVMStyleWithColumns(10)));
  4585. }
  4586. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  4587. verifyFormat("int x = {\n"
  4588. " avariable,\n"
  4589. " b(alongervariable)};",
  4590. getLLVMStyleWithColumns(25));
  4591. }
  4592. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  4593. verifyFormat("return (a)(b) {1, 2, 3};");
  4594. }
  4595. TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
  4596. verifyFormat("vector<int> x{1, 2, 3, 4};");
  4597. verifyFormat("vector<int> x{\n"
  4598. " 1, 2, 3, 4,\n"
  4599. "};");
  4600. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  4601. verifyFormat("f({1, 2});");
  4602. verifyFormat("auto v = Foo{-1};");
  4603. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  4604. verifyFormat("Class::Class : member{1, 2, 3} {}");
  4605. verifyFormat("new vector<int>{1, 2, 3};");
  4606. verifyFormat("new int[3]{1, 2, 3};");
  4607. verifyFormat("return {arg1, arg2};");
  4608. verifyFormat("return {arg1, SomeType{parameter}};");
  4609. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  4610. verifyFormat("new T{arg1, arg2};");
  4611. verifyFormat("f(MyMap[{composite, key}]);");
  4612. verifyFormat("class Class {\n"
  4613. " T member = {arg1, arg2};\n"
  4614. "};");
  4615. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  4616. // FIXME: The alignment of these trailing comments might be bad. Then again,
  4617. // this might be utterly useless in real code.
  4618. verifyFormat("Constructor::Constructor()\n"
  4619. " : some_value{ //\n"
  4620. " aaaaaaa //\n"
  4621. " } {}");
  4622. // In braced lists, the first comment is always assumed to belong to the
  4623. // first element. Thus, it can be moved to the next or previous line as
  4624. // appropriate.
  4625. EXPECT_EQ("function({// First element:\n"
  4626. " 1,\n"
  4627. " // Second element:\n"
  4628. " 2});",
  4629. format("function({\n"
  4630. " // First element:\n"
  4631. " 1,\n"
  4632. " // Second element:\n"
  4633. " 2});"));
  4634. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  4635. " // First element:\n"
  4636. " 1,\n"
  4637. " // Second element:\n"
  4638. " 2};",
  4639. format("std::vector<int> MyNumbers{// First element:\n"
  4640. " 1,\n"
  4641. " // Second element:\n"
  4642. " 2};",
  4643. getLLVMStyleWithColumns(30)));
  4644. FormatStyle ExtraSpaces = getLLVMStyle();
  4645. ExtraSpaces.Cpp11BracedListStyle = false;
  4646. ExtraSpaces.ColumnLimit = 75;
  4647. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  4648. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  4649. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  4650. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  4651. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  4652. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  4653. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  4654. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  4655. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  4656. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  4657. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  4658. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  4659. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  4660. verifyFormat("class Class {\n"
  4661. " T member = { arg1, arg2 };\n"
  4662. "};",
  4663. ExtraSpaces);
  4664. verifyFormat(
  4665. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4666. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  4667. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  4668. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  4669. ExtraSpaces);
  4670. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  4671. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  4672. ExtraSpaces);
  4673. verifyFormat(
  4674. "someFunction(OtherParam,\n"
  4675. " BracedList{ // comment 1 (Forcing interesting break)\n"
  4676. " param1, param2,\n"
  4677. " // comment 2\n"
  4678. " param3, param4 });",
  4679. ExtraSpaces);
  4680. verifyFormat(
  4681. "std::this_thread::sleep_for(\n"
  4682. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  4683. ExtraSpaces);
  4684. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  4685. " aaaaaaa, aaaaaaaaaa,\n"
  4686. " aaaaa, aaaaaaaaaaaaaaa,\n"
  4687. " aaa, aaaaaaaaaa,\n"
  4688. " a, aaaaaaaaaaaaaaaaaaaaa,\n"
  4689. " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  4690. " aaaaaaa, a\n"
  4691. "};",
  4692. ExtraSpaces);
  4693. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  4694. }
  4695. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  4696. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4697. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4698. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4699. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4700. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4701. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  4702. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4703. " // line comment\n"
  4704. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4705. " 1, 22, 333, 4444, 55555,\n"
  4706. " // line comment\n"
  4707. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4708. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  4709. verifyFormat(
  4710. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4711. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4712. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  4713. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  4714. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  4715. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  4716. " 7777777};");
  4717. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  4718. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  4719. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  4720. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  4721. " 1, 1, 1, 1};",
  4722. getLLVMStyleWithColumns(39));
  4723. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  4724. " 1, 1, 1, 1};",
  4725. getLLVMStyleWithColumns(38));
  4726. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  4727. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  4728. getLLVMStyleWithColumns(43));
  4729. // Trailing commas.
  4730. verifyFormat("vector<int> x = {\n"
  4731. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  4732. "};",
  4733. getLLVMStyleWithColumns(39));
  4734. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  4735. " 1, 1, 1, 1, //\n"
  4736. "};",
  4737. getLLVMStyleWithColumns(39));
  4738. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  4739. " 1, 1, 1, 1,\n"
  4740. " /**/ /**/};",
  4741. getLLVMStyleWithColumns(39));
  4742. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  4743. " {aaaaaaaaaaaaaaaaaaa},\n"
  4744. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  4745. " {aaaaaaaaaaaaaaaaa}};",
  4746. getLLVMStyleWithColumns(60));
  4747. // With nested lists, we should either format one item per line or all nested
  4748. // lists one one line.
  4749. // FIXME: For some nested lists, we can do better.
  4750. verifyFormat(
  4751. "SomeStruct my_struct_array = {\n"
  4752. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  4753. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  4754. " {aaa, aaa},\n"
  4755. " {aaa, aaa},\n"
  4756. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  4757. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4758. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  4759. // No column layout should be used here.
  4760. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  4761. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  4762. }
  4763. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  4764. FormatStyle DoNotMerge = getLLVMStyle();
  4765. DoNotMerge.AllowShortFunctionsOnASingleLine = false;
  4766. verifyFormat("void f() { return 42; }");
  4767. verifyFormat("void f() {\n"
  4768. " return 42;\n"
  4769. "}",
  4770. DoNotMerge);
  4771. verifyFormat("void f() {\n"
  4772. " // Comment\n"
  4773. "}");
  4774. verifyFormat("{\n"
  4775. "#error {\n"
  4776. " int a;\n"
  4777. "}");
  4778. verifyFormat("{\n"
  4779. " int a;\n"
  4780. "#error {\n"
  4781. "}");
  4782. verifyFormat("void f() {} // comment");
  4783. verifyFormat("void f() { int a; } // comment");
  4784. verifyFormat("void f() {\n"
  4785. "} // comment",
  4786. DoNotMerge);
  4787. verifyFormat("void f() {\n"
  4788. " int a;\n"
  4789. "} // comment",
  4790. DoNotMerge);
  4791. verifyFormat("void f() {\n"
  4792. "} // comment",
  4793. getLLVMStyleWithColumns(15));
  4794. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  4795. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  4796. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  4797. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  4798. verifyFormat("class C {\n"
  4799. " C()\n"
  4800. " : iiiiiiii(nullptr),\n"
  4801. " kkkkkkk(nullptr),\n"
  4802. " mmmmmmm(nullptr),\n"
  4803. " nnnnnnn(nullptr) {}\n"
  4804. "};",
  4805. getGoogleStyle());
  4806. FormatStyle NoColumnLimit = getLLVMStyle();
  4807. NoColumnLimit.ColumnLimit = 0;
  4808. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  4809. EXPECT_EQ("class C {\n"
  4810. " A() : b(0) {}\n"
  4811. "};", format("class C{A():b(0){}};", NoColumnLimit));
  4812. EXPECT_EQ("A()\n"
  4813. " : b(0) {\n"
  4814. "}",
  4815. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  4816. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  4817. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = false;
  4818. EXPECT_EQ("A()\n"
  4819. " : b(0) {\n"
  4820. "}",
  4821. format("A():b(0){}", DoNotMergeNoColumnLimit));
  4822. EXPECT_EQ("A()\n"
  4823. " : b(0) {\n"
  4824. "}",
  4825. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  4826. verifyFormat("#define A \\\n"
  4827. " void f() { \\\n"
  4828. " int i; \\\n"
  4829. " }",
  4830. getLLVMStyleWithColumns(20));
  4831. verifyFormat("#define A \\\n"
  4832. " void f() { int i; }",
  4833. getLLVMStyleWithColumns(21));
  4834. verifyFormat("#define A \\\n"
  4835. " void f() { \\\n"
  4836. " int i; \\\n"
  4837. " } \\\n"
  4838. " int j;",
  4839. getLLVMStyleWithColumns(22));
  4840. verifyFormat("#define A \\\n"
  4841. " void f() { int i; } \\\n"
  4842. " int j;",
  4843. getLLVMStyleWithColumns(23));
  4844. }
  4845. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  4846. // Elaborate type variable declarations.
  4847. verifyFormat("struct foo a = {bar};\nint n;");
  4848. verifyFormat("class foo a = {bar};\nint n;");
  4849. verifyFormat("union foo a = {bar};\nint n;");
  4850. // Elaborate types inside function definitions.
  4851. verifyFormat("struct foo f() {}\nint n;");
  4852. verifyFormat("class foo f() {}\nint n;");
  4853. verifyFormat("union foo f() {}\nint n;");
  4854. // Templates.
  4855. verifyFormat("template <class X> void f() {}\nint n;");
  4856. verifyFormat("template <struct X> void f() {}\nint n;");
  4857. verifyFormat("template <union X> void f() {}\nint n;");
  4858. // Actual definitions...
  4859. verifyFormat("struct {\n} n;");
  4860. verifyFormat(
  4861. "template <template <class T, class Y>, class Z> class X {\n} n;");
  4862. verifyFormat("union Z {\n int n;\n} x;");
  4863. verifyFormat("class MACRO Z {\n} n;");
  4864. verifyFormat("class MACRO(X) Z {\n} n;");
  4865. verifyFormat("class __attribute__(X) Z {\n} n;");
  4866. verifyFormat("class __declspec(X) Z {\n} n;");
  4867. verifyFormat("class A##B##C {\n} n;");
  4868. verifyFormat("class alignas(16) Z {\n} n;");
  4869. // Redefinition from nested context:
  4870. verifyFormat("class A::B::C {\n} n;");
  4871. // Template definitions.
  4872. verifyFormat(
  4873. "template <typename F>\n"
  4874. "Matcher(const Matcher<F> &Other,\n"
  4875. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  4876. " !is_same<F, T>::value>::type * = 0)\n"
  4877. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  4878. // FIXME: This is still incorrectly handled at the formatter side.
  4879. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  4880. // FIXME:
  4881. // This now gets parsed incorrectly as class definition.
  4882. // verifyFormat("class A<int> f() {\n}\nint n;");
  4883. // Elaborate types where incorrectly parsing the structural element would
  4884. // break the indent.
  4885. verifyFormat("if (true)\n"
  4886. " class X x;\n"
  4887. "else\n"
  4888. " f();\n");
  4889. // This is simply incomplete. Formatting is not important, but must not crash.
  4890. verifyFormat("class A:");
  4891. }
  4892. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  4893. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  4894. format("#error Leave all white!!!!! space* alone!\n"));
  4895. EXPECT_EQ(
  4896. "#warning Leave all white!!!!! space* alone!\n",
  4897. format("#warning Leave all white!!!!! space* alone!\n"));
  4898. EXPECT_EQ("#error 1", format(" # error 1"));
  4899. EXPECT_EQ("#warning 1", format(" # warning 1"));
  4900. }
  4901. TEST_F(FormatTest, FormatHashIfExpressions) {
  4902. verifyFormat("#if AAAA && BBBB");
  4903. // FIXME: Come up with a better indentation for #elif.
  4904. verifyFormat(
  4905. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  4906. " defined(BBBBBBBB)\n"
  4907. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  4908. " defined(BBBBBBBB)\n"
  4909. "#endif",
  4910. getLLVMStyleWithColumns(65));
  4911. }
  4912. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  4913. FormatStyle AllowsMergedIf = getGoogleStyle();
  4914. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  4915. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  4916. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  4917. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  4918. EXPECT_EQ("if (true) return 42;",
  4919. format("if (true)\nreturn 42;", AllowsMergedIf));
  4920. FormatStyle ShortMergedIf = AllowsMergedIf;
  4921. ShortMergedIf.ColumnLimit = 25;
  4922. verifyFormat("#define A \\\n"
  4923. " if (true) return 42;",
  4924. ShortMergedIf);
  4925. verifyFormat("#define A \\\n"
  4926. " f(); \\\n"
  4927. " if (true)\n"
  4928. "#define B",
  4929. ShortMergedIf);
  4930. verifyFormat("#define A \\\n"
  4931. " f(); \\\n"
  4932. " if (true)\n"
  4933. "g();",
  4934. ShortMergedIf);
  4935. verifyFormat("{\n"
  4936. "#ifdef A\n"
  4937. " // Comment\n"
  4938. " if (true) continue;\n"
  4939. "#endif\n"
  4940. " // Comment\n"
  4941. " if (true) continue;\n"
  4942. "}",
  4943. ShortMergedIf);
  4944. ShortMergedIf.ColumnLimit = 29;
  4945. verifyFormat("#define A \\\n"
  4946. " if (aaaaaaaaaa) return 1; \\\n"
  4947. " return 2;",
  4948. ShortMergedIf);
  4949. ShortMergedIf.ColumnLimit = 28;
  4950. verifyFormat("#define A \\\n"
  4951. " if (aaaaaaaaaa) \\\n"
  4952. " return 1; \\\n"
  4953. " return 2;",
  4954. ShortMergedIf);
  4955. }
  4956. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  4957. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  4958. " f();\n"
  4959. "}");
  4960. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  4961. " f();\n"
  4962. "} /* another comment */ else /* comment #3 */ {\n"
  4963. " g();\n"
  4964. "}");
  4965. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  4966. " f();\n"
  4967. "}");
  4968. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  4969. " f();\n"
  4970. "}");
  4971. verifyFormat("do /* a comment in a strange place */ {\n"
  4972. " f();\n"
  4973. "} /* another comment */ while (0);");
  4974. }
  4975. TEST_F(FormatTest, BlockComments) {
  4976. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  4977. format("/* *//* */ /* */\n/* *//* */ /* */"));
  4978. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  4979. EXPECT_EQ("#define A /*123*/ \\\n"
  4980. " b\n"
  4981. "/* */\n"
  4982. "someCall(\n"
  4983. " parameter);",
  4984. format("#define A /*123*/ b\n"
  4985. "/* */\n"
  4986. "someCall(parameter);",
  4987. getLLVMStyleWithColumns(15)));
  4988. EXPECT_EQ("#define A\n"
  4989. "/* */ someCall(\n"
  4990. " parameter);",
  4991. format("#define A\n"
  4992. "/* */someCall(parameter);",
  4993. getLLVMStyleWithColumns(15)));
  4994. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  4995. EXPECT_EQ("/*\n"
  4996. "*\n"
  4997. " * aaaaaa\n"
  4998. "*aaaaaa\n"
  4999. "*/",
  5000. format("/*\n"
  5001. "*\n"
  5002. " * aaaaaa aaaaaa\n"
  5003. "*/",
  5004. getLLVMStyleWithColumns(10)));
  5005. EXPECT_EQ("/*\n"
  5006. "**\n"
  5007. "* aaaaaa\n"
  5008. "*aaaaaa\n"
  5009. "*/",
  5010. format("/*\n"
  5011. "**\n"
  5012. "* aaaaaa aaaaaa\n"
  5013. "*/",
  5014. getLLVMStyleWithColumns(10)));
  5015. FormatStyle NoBinPacking = getLLVMStyle();
  5016. NoBinPacking.BinPackParameters = false;
  5017. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5018. " 2, /* comment 2 */\n"
  5019. " 3, /* comment 3 */\n"
  5020. " aaaa,\n"
  5021. " bbbb);",
  5022. format("someFunction (1, /* comment 1 */\n"
  5023. " 2, /* comment 2 */ \n"
  5024. " 3, /* comment 3 */\n"
  5025. "aaaa, bbbb );",
  5026. NoBinPacking));
  5027. verifyFormat(
  5028. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5029. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5030. EXPECT_EQ(
  5031. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5032. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5034. format(
  5035. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5036. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5038. EXPECT_EQ(
  5039. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5040. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5041. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5042. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5043. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5044. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5045. verifyFormat("void f(int * /* unused */) {}");
  5046. EXPECT_EQ("/*\n"
  5047. " **\n"
  5048. " */",
  5049. format("/*\n"
  5050. " **\n"
  5051. " */"));
  5052. EXPECT_EQ("/*\n"
  5053. " *q\n"
  5054. " */",
  5055. format("/*\n"
  5056. " *q\n"
  5057. " */"));
  5058. EXPECT_EQ("/*\n"
  5059. " * q\n"
  5060. " */",
  5061. format("/*\n"
  5062. " * q\n"
  5063. " */"));
  5064. EXPECT_EQ("/*\n"
  5065. " **/",
  5066. format("/*\n"
  5067. " **/"));
  5068. EXPECT_EQ("/*\n"
  5069. " ***/",
  5070. format("/*\n"
  5071. " ***/"));
  5072. }
  5073. TEST_F(FormatTest, BlockCommentsInMacros) {
  5074. EXPECT_EQ("#define A \\\n"
  5075. " { \\\n"
  5076. " /* one line */ \\\n"
  5077. " someCall();",
  5078. format("#define A { \\\n"
  5079. " /* one line */ \\\n"
  5080. " someCall();",
  5081. getLLVMStyleWithColumns(20)));
  5082. EXPECT_EQ("#define A \\\n"
  5083. " { \\\n"
  5084. " /* previous */ \\\n"
  5085. " /* one line */ \\\n"
  5086. " someCall();",
  5087. format("#define A { \\\n"
  5088. " /* previous */ \\\n"
  5089. " /* one line */ \\\n"
  5090. " someCall();",
  5091. getLLVMStyleWithColumns(20)));
  5092. }
  5093. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  5094. EXPECT_EQ("a = {\n"
  5095. " 1111 /* */\n"
  5096. "};",
  5097. format("a = {1111 /* */\n"
  5098. "};",
  5099. getLLVMStyleWithColumns(15)));
  5100. EXPECT_EQ("a = {\n"
  5101. " 1111 /* */\n"
  5102. "};",
  5103. format("a = {1111 /* */\n"
  5104. "};",
  5105. getLLVMStyleWithColumns(15)));
  5106. // FIXME: The formatting is still wrong here.
  5107. EXPECT_EQ("a = {\n"
  5108. " 1111 /* a\n"
  5109. " */\n"
  5110. "};",
  5111. format("a = {1111 /* a */\n"
  5112. "};",
  5113. getLLVMStyleWithColumns(15)));
  5114. }
  5115. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  5116. // FIXME: This is not what we want...
  5117. verifyFormat("{\n"
  5118. "// a"
  5119. "// b");
  5120. }
  5121. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5122. verifyFormat("void f(int *a);");
  5123. verifyFormat("void f() { f(fint * b); }");
  5124. verifyFormat("class A {\n void f(int *a);\n};");
  5125. verifyFormat("class A {\n int *a;\n};");
  5126. verifyFormat("namespace a {\n"
  5127. "namespace b {\n"
  5128. "class A {\n"
  5129. " void f() {}\n"
  5130. " int *a;\n"
  5131. "};\n"
  5132. "}\n"
  5133. "}");
  5134. }
  5135. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  5136. verifyFormat("while");
  5137. verifyFormat("operator");
  5138. }
  5139. //===----------------------------------------------------------------------===//
  5140. // Objective-C tests.
  5141. //===----------------------------------------------------------------------===//
  5142. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  5143. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  5144. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  5145. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  5146. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  5147. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  5148. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  5149. format("-(NSInteger)Method3:(id)anObject;"));
  5150. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  5151. format("-(NSInteger)Method4:(id)anObject;"));
  5152. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  5153. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  5154. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  5155. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  5156. EXPECT_EQ(
  5157. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  5158. format(
  5159. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  5160. // Very long objectiveC method declaration.
  5161. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  5162. " inRange:(NSRange)range\n"
  5163. " outRange:(NSRange)out_range\n"
  5164. " outRange1:(NSRange)out_range1\n"
  5165. " outRange2:(NSRange)out_range2\n"
  5166. " outRange3:(NSRange)out_range3\n"
  5167. " outRange4:(NSRange)out_range4\n"
  5168. " outRange5:(NSRange)out_range5\n"
  5169. " outRange6:(NSRange)out_range6\n"
  5170. " outRange7:(NSRange)out_range7\n"
  5171. " outRange8:(NSRange)out_range8\n"
  5172. " outRange9:(NSRange)out_range9;");
  5173. verifyFormat("- (int)sum:(vector<int>)numbers;");
  5174. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  5175. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  5176. // protocol lists (but not for template classes):
  5177. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  5178. verifyFormat("- (int (*)())foo:(int (*)())f;");
  5179. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  5180. // If there's no return type (very rare in practice!), LLVM and Google style
  5181. // agree.
  5182. verifyFormat("- foo;");
  5183. verifyFormat("- foo:(int)f;");
  5184. verifyGoogleFormat("- foo:(int)foo;");
  5185. }
  5186. TEST_F(FormatTest, FormatObjCInterface) {
  5187. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  5188. "@public\n"
  5189. " int field1;\n"
  5190. "@protected\n"
  5191. " int field2;\n"
  5192. "@private\n"
  5193. " int field3;\n"
  5194. "@package\n"
  5195. " int field4;\n"
  5196. "}\n"
  5197. "+ (id)init;\n"
  5198. "@end");
  5199. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  5200. " @public\n"
  5201. " int field1;\n"
  5202. " @protected\n"
  5203. " int field2;\n"
  5204. " @private\n"
  5205. " int field3;\n"
  5206. " @package\n"
  5207. " int field4;\n"
  5208. "}\n"
  5209. "+ (id)init;\n"
  5210. "@end");
  5211. verifyFormat("@interface /* wait for it */ Foo\n"
  5212. "+ (id)init;\n"
  5213. "// Look, a comment!\n"
  5214. "- (int)answerWith:(int)i;\n"
  5215. "@end");
  5216. verifyFormat("@interface Foo\n"
  5217. "@end\n"
  5218. "@interface Bar\n"
  5219. "@end");
  5220. verifyFormat("@interface Foo : Bar\n"
  5221. "+ (id)init;\n"
  5222. "@end");
  5223. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  5224. "+ (id)init;\n"
  5225. "@end");
  5226. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  5227. "+ (id)init;\n"
  5228. "@end");
  5229. verifyFormat("@interface Foo (HackStuff)\n"
  5230. "+ (id)init;\n"
  5231. "@end");
  5232. verifyFormat("@interface Foo ()\n"
  5233. "+ (id)init;\n"
  5234. "@end");
  5235. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  5236. "+ (id)init;\n"
  5237. "@end");
  5238. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  5239. "+ (id)init;\n"
  5240. "@end");
  5241. verifyFormat("@interface Foo {\n"
  5242. " int _i;\n"
  5243. "}\n"
  5244. "+ (id)init;\n"
  5245. "@end");
  5246. verifyFormat("@interface Foo : Bar {\n"
  5247. " int _i;\n"
  5248. "}\n"
  5249. "+ (id)init;\n"
  5250. "@end");
  5251. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  5252. " int _i;\n"
  5253. "}\n"
  5254. "+ (id)init;\n"
  5255. "@end");
  5256. verifyFormat("@interface Foo (HackStuff) {\n"
  5257. " int _i;\n"
  5258. "}\n"
  5259. "+ (id)init;\n"
  5260. "@end");
  5261. verifyFormat("@interface Foo () {\n"
  5262. " int _i;\n"
  5263. "}\n"
  5264. "+ (id)init;\n"
  5265. "@end");
  5266. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  5267. " int _i;\n"
  5268. "}\n"
  5269. "+ (id)init;\n"
  5270. "@end");
  5271. }
  5272. TEST_F(FormatTest, FormatObjCImplementation) {
  5273. verifyFormat("@implementation Foo : NSObject {\n"
  5274. "@public\n"
  5275. " int field1;\n"
  5276. "@protected\n"
  5277. " int field2;\n"
  5278. "@private\n"
  5279. " int field3;\n"
  5280. "@package\n"
  5281. " int field4;\n"
  5282. "}\n"
  5283. "+ (id)init {\n}\n"
  5284. "@end");
  5285. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  5286. " @public\n"
  5287. " int field1;\n"
  5288. " @protected\n"
  5289. " int field2;\n"
  5290. " @private\n"
  5291. " int field3;\n"
  5292. " @package\n"
  5293. " int field4;\n"
  5294. "}\n"
  5295. "+ (id)init {\n}\n"
  5296. "@end");
  5297. verifyFormat("@implementation Foo\n"
  5298. "+ (id)init {\n"
  5299. " if (true)\n"
  5300. " return nil;\n"
  5301. "}\n"
  5302. "// Look, a comment!\n"
  5303. "- (int)answerWith:(int)i {\n"
  5304. " return i;\n"
  5305. "}\n"
  5306. "+ (int)answerWith:(int)i {\n"
  5307. " return i;\n"
  5308. "}\n"
  5309. "@end");
  5310. verifyFormat("@implementation Foo\n"
  5311. "@end\n"
  5312. "@implementation Bar\n"
  5313. "@end");
  5314. verifyFormat("@implementation Foo : Bar\n"
  5315. "+ (id)init {\n}\n"
  5316. "- (void)foo {\n}\n"
  5317. "@end");
  5318. verifyFormat("@implementation Foo {\n"
  5319. " int _i;\n"
  5320. "}\n"
  5321. "+ (id)init {\n}\n"
  5322. "@end");
  5323. verifyFormat("@implementation Foo : Bar {\n"
  5324. " int _i;\n"
  5325. "}\n"
  5326. "+ (id)init {\n}\n"
  5327. "@end");
  5328. verifyFormat("@implementation Foo (HackStuff)\n"
  5329. "+ (id)init {\n}\n"
  5330. "@end");
  5331. verifyFormat("@implementation ObjcClass\n"
  5332. "- (void)method;\n"
  5333. "{}\n"
  5334. "@end");
  5335. }
  5336. TEST_F(FormatTest, FormatObjCProtocol) {
  5337. verifyFormat("@protocol Foo\n"
  5338. "@property(weak) id delegate;\n"
  5339. "- (NSUInteger)numberOfThings;\n"
  5340. "@end");
  5341. verifyFormat("@protocol MyProtocol <NSObject>\n"
  5342. "- (NSUInteger)numberOfThings;\n"
  5343. "@end");
  5344. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  5345. "- (NSUInteger)numberOfThings;\n"
  5346. "@end");
  5347. verifyFormat("@protocol Foo;\n"
  5348. "@protocol Bar;\n");
  5349. verifyFormat("@protocol Foo\n"
  5350. "@end\n"
  5351. "@protocol Bar\n"
  5352. "@end");
  5353. verifyFormat("@protocol myProtocol\n"
  5354. "- (void)mandatoryWithInt:(int)i;\n"
  5355. "@optional\n"
  5356. "- (void)optional;\n"
  5357. "@required\n"
  5358. "- (void)required;\n"
  5359. "@optional\n"
  5360. "@property(assign) int madProp;\n"
  5361. "@end\n");
  5362. verifyFormat("@property(nonatomic, assign, readonly)\n"
  5363. " int *looooooooooooooooooooooooooooongNumber;\n"
  5364. "@property(nonatomic, assign, readonly)\n"
  5365. " NSString *looooooooooooooooooooooooooooongName;");
  5366. verifyFormat("@implementation PR18406\n"
  5367. "}\n"
  5368. "@end");
  5369. }
  5370. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  5371. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  5372. " rect:(NSRect)theRect\n"
  5373. " interval:(float)theInterval {\n"
  5374. "}");
  5375. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  5376. " longKeyword:(NSRect)theRect\n"
  5377. " evenLongerKeyword:(float)theInterval\n"
  5378. " error:(NSError **)theError {\n"
  5379. "}");
  5380. }
  5381. TEST_F(FormatTest, FormatObjCMethodExpr) {
  5382. verifyFormat("[foo bar:baz];");
  5383. verifyFormat("return [foo bar:baz];");
  5384. verifyFormat("f([foo bar:baz]);");
  5385. verifyFormat("f(2, [foo bar:baz]);");
  5386. verifyFormat("f(2, a ? b : c);");
  5387. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  5388. // Unary operators.
  5389. verifyFormat("int a = +[foo bar:baz];");
  5390. verifyFormat("int a = -[foo bar:baz];");
  5391. verifyFormat("int a = ![foo bar:baz];");
  5392. verifyFormat("int a = ~[foo bar:baz];");
  5393. verifyFormat("int a = ++[foo bar:baz];");
  5394. verifyFormat("int a = --[foo bar:baz];");
  5395. verifyFormat("int a = sizeof [foo bar:baz];");
  5396. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  5397. verifyFormat("int a = &[foo bar:baz];");
  5398. verifyFormat("int a = *[foo bar:baz];");
  5399. // FIXME: Make casts work, without breaking f()[4].
  5400. //verifyFormat("int a = (int)[foo bar:baz];");
  5401. //verifyFormat("return (int)[foo bar:baz];");
  5402. //verifyFormat("(void)[foo bar:baz];");
  5403. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  5404. // Binary operators.
  5405. verifyFormat("[foo bar:baz], [foo bar:baz];");
  5406. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  5407. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  5408. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  5409. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  5410. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  5411. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  5412. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  5413. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  5414. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  5415. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  5416. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  5417. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  5418. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  5419. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  5420. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  5421. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  5422. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  5423. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  5424. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  5425. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  5426. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  5427. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  5428. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  5429. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  5430. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  5431. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  5432. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  5433. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  5434. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  5435. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  5436. // Whew!
  5437. verifyFormat("return in[42];");
  5438. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  5439. "}");
  5440. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  5441. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  5442. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  5443. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  5444. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  5445. verifyFormat("[button setAction:@selector(zoomOut:)];");
  5446. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  5447. verifyFormat("arr[[self indexForFoo:a]];");
  5448. verifyFormat("throw [self errorFor:a];");
  5449. verifyFormat("@throw [self errorFor:a];");
  5450. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  5451. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  5452. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  5453. // This tests that the formatter doesn't break after "backing" but before ":",
  5454. // which would be at 80 columns.
  5455. verifyFormat(
  5456. "void f() {\n"
  5457. " if ((self = [super initWithContentRect:contentRect\n"
  5458. " styleMask:styleMask ?: otherMask\n"
  5459. " backing:NSBackingStoreBuffered\n"
  5460. " defer:YES]))");
  5461. verifyFormat(
  5462. "[foo checkThatBreakingAfterColonWorksOk:\n"
  5463. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  5464. verifyFormat("[myObj short:arg1 // Force line break\n"
  5465. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  5466. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  5467. " error:arg4];");
  5468. verifyFormat(
  5469. "void f() {\n"
  5470. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  5471. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  5472. " pos.width(), pos.height())\n"
  5473. " styleMask:NSBorderlessWindowMask\n"
  5474. " backing:NSBackingStoreBuffered\n"
  5475. " defer:NO]);\n"
  5476. "}");
  5477. verifyFormat(
  5478. "void f() {\n"
  5479. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  5480. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  5481. " pos.width(), pos.height())\n"
  5482. " syeMask:NSBorderlessWindowMask\n"
  5483. " bking:NSBackingStoreBuffered\n"
  5484. " der:NO]);\n"
  5485. "}",
  5486. getLLVMStyleWithColumns(70));
  5487. verifyFormat("{\n"
  5488. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  5489. " initWithContentRect:NSMakeRect(origin_global.x,\n"
  5490. " origin_global.y,\n"
  5491. " pos.width(),\n"
  5492. " pos.height())\n"
  5493. " styleMask:NSBorderlessWindowMask\n"
  5494. " backing:NSBackingStoreBuffered\n"
  5495. " defer:NO]);\n"
  5496. "}",
  5497. getChromiumStyle(FormatStyle::LK_Cpp));
  5498. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  5499. " with:contentsNativeView];");
  5500. verifyFormat(
  5501. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  5502. " owner:nillllll];");
  5503. verifyFormat(
  5504. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  5505. " forType:kBookmarkButtonDragType];");
  5506. verifyFormat("[defaultCenter addObserver:self\n"
  5507. " selector:@selector(willEnterFullscreen)\n"
  5508. " name:kWillEnterFullscreenNotification\n"
  5509. " object:nil];");
  5510. verifyFormat("[image_rep drawInRect:drawRect\n"
  5511. " fromRect:NSZeroRect\n"
  5512. " operation:NSCompositeCopy\n"
  5513. " fraction:1.0\n"
  5514. " respectFlipped:NO\n"
  5515. " hints:nil];");
  5516. verifyFormat(
  5517. "scoped_nsobject<NSTextField> message(\n"
  5518. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  5519. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  5520. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  5521. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  5522. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  5523. " aaaa:bbb];");
  5524. verifyFormat("[self param:function( //\n"
  5525. " parameter)]");
  5526. verifyFormat(
  5527. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  5528. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  5529. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  5530. // Variadic parameters.
  5531. verifyFormat(
  5532. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  5533. verifyFormat(
  5534. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  5535. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  5536. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  5537. verifyFormat("[self // break\n"
  5538. " a:a\n"
  5539. " aaa:aaa];");
  5540. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  5541. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  5542. }
  5543. TEST_F(FormatTest, ObjCAt) {
  5544. verifyFormat("@autoreleasepool");
  5545. verifyFormat("@catch");
  5546. verifyFormat("@class");
  5547. verifyFormat("@compatibility_alias");
  5548. verifyFormat("@defs");
  5549. verifyFormat("@dynamic");
  5550. verifyFormat("@encode");
  5551. verifyFormat("@end");
  5552. verifyFormat("@finally");
  5553. verifyFormat("@implementation");
  5554. verifyFormat("@import");
  5555. verifyFormat("@interface");
  5556. verifyFormat("@optional");
  5557. verifyFormat("@package");
  5558. verifyFormat("@private");
  5559. verifyFormat("@property");
  5560. verifyFormat("@protected");
  5561. verifyFormat("@protocol");
  5562. verifyFormat("@public");
  5563. verifyFormat("@required");
  5564. verifyFormat("@selector");
  5565. verifyFormat("@synchronized");
  5566. verifyFormat("@synthesize");
  5567. verifyFormat("@throw");
  5568. verifyFormat("@try");
  5569. EXPECT_EQ("@interface", format("@ interface"));
  5570. // The precise formatting of this doesn't matter, nobody writes code like
  5571. // this.
  5572. verifyFormat("@ /*foo*/ interface");
  5573. }
  5574. TEST_F(FormatTest, ObjCSnippets) {
  5575. verifyFormat("@autoreleasepool {\n"
  5576. " foo();\n"
  5577. "}");
  5578. verifyFormat("@class Foo, Bar;");
  5579. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  5580. verifyFormat("@dynamic textColor;");
  5581. verifyFormat("char *buf1 = @encode(int *);");
  5582. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  5583. verifyFormat("char *buf1 = @encode(int **);");
  5584. verifyFormat("Protocol *proto = @protocol(p1);");
  5585. verifyFormat("SEL s = @selector(foo:);");
  5586. verifyFormat("@synchronized(self) {\n"
  5587. " f();\n"
  5588. "}");
  5589. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  5590. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  5591. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  5592. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  5593. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  5594. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  5595. getMozillaStyle());
  5596. verifyFormat("@property BOOL editable;", getMozillaStyle());
  5597. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  5598. getWebKitStyle());
  5599. verifyFormat("@property BOOL editable;", getWebKitStyle());
  5600. verifyFormat("@import foo.bar;\n"
  5601. "@import baz;");
  5602. }
  5603. TEST_F(FormatTest, ObjCLiterals) {
  5604. verifyFormat("@\"String\"");
  5605. verifyFormat("@1");
  5606. verifyFormat("@+4.8");
  5607. verifyFormat("@-4");
  5608. verifyFormat("@1LL");
  5609. verifyFormat("@.5");
  5610. verifyFormat("@'c'");
  5611. verifyFormat("@true");
  5612. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  5613. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  5614. verifyFormat("NSNumber *favoriteColor = @(Green);");
  5615. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  5616. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  5617. }
  5618. TEST_F(FormatTest, ObjCDictLiterals) {
  5619. verifyFormat("@{");
  5620. verifyFormat("@{}");
  5621. verifyFormat("@{@\"one\" : @1}");
  5622. verifyFormat("return @{@\"one\" : @1;");
  5623. verifyFormat("@{@\"one\" : @1}");
  5624. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  5625. verifyFormat("@{\n"
  5626. " @\"one\" : @{@2 : @1},\n"
  5627. "}");
  5628. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  5629. verifyFormat("[self setDict:@{}");
  5630. verifyFormat("[self setDict:@{@1 : @2}");
  5631. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  5632. verifyFormat(
  5633. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  5634. verifyFormat(
  5635. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  5636. verifyFormat(
  5637. "NSDictionary *d = @{\n"
  5638. " @\"nam\" : NSUserNam(),\n"
  5639. " @\"dte\" : [NSDate date],\n"
  5640. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  5641. "};");
  5642. verifyFormat(
  5643. "@{\n"
  5644. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  5645. "regularFont,\n"
  5646. "};");
  5647. verifyGoogleFormat(
  5648. "@{\n"
  5649. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  5650. "regularFont,\n"
  5651. "};");
  5652. verifyFormat(
  5653. "@{\n"
  5654. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  5655. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  5656. "};");
  5657. // We should try to be robust in case someone forgets the "@".
  5658. verifyFormat(
  5659. "NSDictionary *d = {\n"
  5660. " @\"nam\" : NSUserNam(),\n"
  5661. " @\"dte\" : [NSDate date],\n"
  5662. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  5663. "};");
  5664. }
  5665. TEST_F(FormatTest, ObjCArrayLiterals) {
  5666. verifyFormat("@[");
  5667. verifyFormat("@[]");
  5668. verifyFormat(
  5669. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  5670. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  5671. verifyFormat("NSArray *array = @[ [foo description] ];");
  5672. verifyFormat(
  5673. "NSArray *some_variable = @[\n"
  5674. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  5675. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5676. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5677. " @\"aaaaaaaaaaaaaaaaa\"\n"
  5678. "];");
  5679. verifyFormat("NSArray *some_variable = @[\n"
  5680. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5681. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5682. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5683. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5684. "];");
  5685. verifyGoogleFormat("NSArray *some_variable = @[\n"
  5686. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5687. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5688. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5689. " @\"aaaaaaaaaaaaaaaaa\"\n"
  5690. "];");
  5691. // We should try to be robust in case someone forgets the "@".
  5692. verifyFormat("NSArray *some_variable = [\n"
  5693. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5694. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5695. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5696. " @\"aaaaaaaaaaaaaaaaa\",\n"
  5697. "];");
  5698. verifyFormat(
  5699. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  5700. " index:(NSUInteger)index\n"
  5701. " nonDigitAttributes:\n"
  5702. " (NSDictionary *)noDigitAttributes;");
  5703. verifyFormat(
  5704. "[someFunction someLooooooooooooongParameter:\n"
  5705. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  5706. }
  5707. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  5708. EXPECT_EQ("{\n"
  5709. "{\n"
  5710. "a;\n"
  5711. "b;\n"
  5712. "}\n"
  5713. "}",
  5714. format("{\n"
  5715. "{\n"
  5716. "a;\n"
  5717. " b;\n"
  5718. "}\n"
  5719. "}",
  5720. 13, 2, getLLVMStyle()));
  5721. EXPECT_EQ("{\n"
  5722. "{\n"
  5723. " a;\n"
  5724. "b;\n"
  5725. "}\n"
  5726. "}",
  5727. format("{\n"
  5728. "{\n"
  5729. " a;\n"
  5730. "b;\n"
  5731. "}\n"
  5732. "}",
  5733. 9, 2, getLLVMStyle()));
  5734. EXPECT_EQ("{\n"
  5735. "{\n"
  5736. "public:\n"
  5737. " b;\n"
  5738. "}\n"
  5739. "}",
  5740. format("{\n"
  5741. "{\n"
  5742. "public:\n"
  5743. " b;\n"
  5744. "}\n"
  5745. "}",
  5746. 17, 2, getLLVMStyle()));
  5747. EXPECT_EQ("{\n"
  5748. "{\n"
  5749. "a;\n"
  5750. "}\n"
  5751. "{\n"
  5752. " b; //\n"
  5753. "}\n"
  5754. "}",
  5755. format("{\n"
  5756. "{\n"
  5757. "a;\n"
  5758. "}\n"
  5759. "{\n"
  5760. " b; //\n"
  5761. "}\n"
  5762. "}",
  5763. 22, 2, getLLVMStyle()));
  5764. EXPECT_EQ(" {\n"
  5765. " a; //\n"
  5766. " }",
  5767. format(" {\n"
  5768. "a; //\n"
  5769. " }",
  5770. 4, 2, getLLVMStyle()));
  5771. EXPECT_EQ("void f() {}\n"
  5772. "void g() {}",
  5773. format("void f() {}\n"
  5774. "void g() {}",
  5775. 13, 0, getLLVMStyle()));
  5776. EXPECT_EQ("int a; // comment\n"
  5777. " // line 2\n"
  5778. "int b;",
  5779. format("int a; // comment\n"
  5780. " // line 2\n"
  5781. " int b;",
  5782. 35, 0, getLLVMStyle()));
  5783. EXPECT_EQ(" int a;\n"
  5784. " void\n"
  5785. " ffffff() {\n"
  5786. " }",
  5787. format(" int a;\n"
  5788. "void ffffff() {}",
  5789. 11, 0, getLLVMStyleWithColumns(11)));
  5790. EXPECT_EQ(" void f() {\n"
  5791. "#define A 1\n"
  5792. " }",
  5793. format(" void f() {\n"
  5794. " #define A 1\n" // Format this line.
  5795. " }",
  5796. 20, 0, getLLVMStyle()));
  5797. EXPECT_EQ(" void f() {\n"
  5798. " int i;\n"
  5799. "#define A \\\n"
  5800. " int i; \\\n"
  5801. " int j;\n"
  5802. " int k;\n"
  5803. " }",
  5804. format(" void f() {\n"
  5805. " int i;\n"
  5806. "#define A \\\n"
  5807. " int i; \\\n"
  5808. " int j;\n"
  5809. " int k;\n" // Format this line.
  5810. " }",
  5811. 67, 0, getLLVMStyle()));
  5812. }
  5813. TEST_F(FormatTest, BreakStringLiterals) {
  5814. EXPECT_EQ("\"some text \"\n"
  5815. "\"other\";",
  5816. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  5817. EXPECT_EQ("\"some text \"\n"
  5818. "\"other\";",
  5819. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  5820. EXPECT_EQ(
  5821. "#define A \\\n"
  5822. " \"some \" \\\n"
  5823. " \"text \" \\\n"
  5824. " \"other\";",
  5825. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  5826. EXPECT_EQ(
  5827. "#define A \\\n"
  5828. " \"so \" \\\n"
  5829. " \"text \" \\\n"
  5830. " \"other\";",
  5831. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  5832. EXPECT_EQ("\"some text\"",
  5833. format("\"some text\"", getLLVMStyleWithColumns(1)));
  5834. EXPECT_EQ("\"some text\"",
  5835. format("\"some text\"", getLLVMStyleWithColumns(11)));
  5836. EXPECT_EQ("\"some \"\n"
  5837. "\"text\"",
  5838. format("\"some text\"", getLLVMStyleWithColumns(10)));
  5839. EXPECT_EQ("\"some \"\n"
  5840. "\"text\"",
  5841. format("\"some text\"", getLLVMStyleWithColumns(7)));
  5842. EXPECT_EQ("\"some\"\n"
  5843. "\" tex\"\n"
  5844. "\"t\"",
  5845. format("\"some text\"", getLLVMStyleWithColumns(6)));
  5846. EXPECT_EQ("\"some\"\n"
  5847. "\" tex\"\n"
  5848. "\" and\"",
  5849. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  5850. EXPECT_EQ("\"some\"\n"
  5851. "\"/tex\"\n"
  5852. "\"/and\"",
  5853. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  5854. EXPECT_EQ("variable =\n"
  5855. " \"long string \"\n"
  5856. " \"literal\";",
  5857. format("variable = \"long string literal\";",
  5858. getLLVMStyleWithColumns(20)));
  5859. EXPECT_EQ("variable = f(\n"
  5860. " \"long string \"\n"
  5861. " \"literal\",\n"
  5862. " short,\n"
  5863. " loooooooooooooooooooong);",
  5864. format("variable = f(\"long string literal\", short, "
  5865. "loooooooooooooooooooong);",
  5866. getLLVMStyleWithColumns(20)));
  5867. EXPECT_EQ("f(g(\"long string \"\n"
  5868. " \"literal\"),\n"
  5869. " b);",
  5870. format("f(g(\"long string literal\"), b);",
  5871. getLLVMStyleWithColumns(20)));
  5872. EXPECT_EQ("f(g(\"long string \"\n"
  5873. " \"literal\",\n"
  5874. " a),\n"
  5875. " b);",
  5876. format("f(g(\"long string literal\", a), b);",
  5877. getLLVMStyleWithColumns(20)));
  5878. EXPECT_EQ(
  5879. "f(\"one two\".split(\n"
  5880. " variable));",
  5881. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  5882. EXPECT_EQ("f(\"one two three four five six \"\n"
  5883. " \"seven\".split(\n"
  5884. " really_looooong_variable));",
  5885. format("f(\"one two three four five six seven\"."
  5886. "split(really_looooong_variable));",
  5887. getLLVMStyleWithColumns(33)));
  5888. EXPECT_EQ("f(\"some \"\n"
  5889. " \"text\",\n"
  5890. " other);",
  5891. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  5892. // Only break as a last resort.
  5893. verifyFormat(
  5894. "aaaaaaaaaaaaaaaaaaaa(\n"
  5895. " aaaaaaaaaaaaaaaaaaaa,\n"
  5896. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  5897. EXPECT_EQ(
  5898. "\"splitmea\"\n"
  5899. "\"trandomp\"\n"
  5900. "\"oint\"",
  5901. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  5902. EXPECT_EQ(
  5903. "\"split/\"\n"
  5904. "\"pathat/\"\n"
  5905. "\"slashes\"",
  5906. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  5907. EXPECT_EQ(
  5908. "\"split/\"\n"
  5909. "\"pathat/\"\n"
  5910. "\"slashes\"",
  5911. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  5912. EXPECT_EQ("\"split at \"\n"
  5913. "\"spaces/at/\"\n"
  5914. "\"slashes.at.any$\"\n"
  5915. "\"non-alphanumeric%\"\n"
  5916. "\"1111111111characte\"\n"
  5917. "\"rs\"",
  5918. format("\"split at "
  5919. "spaces/at/"
  5920. "slashes.at."
  5921. "any$non-"
  5922. "alphanumeric%"
  5923. "1111111111characte"
  5924. "rs\"",
  5925. getLLVMStyleWithColumns(20)));
  5926. // Verify that splitting the strings understands
  5927. // Style::AlwaysBreakBeforeMultilineStrings.
  5928. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  5929. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  5930. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  5931. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  5932. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  5933. "aaaaaaaaaaaaaaaaaaaaaa\");",
  5934. getGoogleStyle()));
  5935. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5936. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  5937. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  5938. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  5939. "aaaaaaaaaaaaaaaaaaaaaa\";",
  5940. getGoogleStyle()));
  5941. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5942. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  5943. format("llvm::outs() << "
  5944. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  5945. "aaaaaaaaaaaaaaaaaaa\";"));
  5946. EXPECT_EQ("ffff(\n"
  5947. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5948. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  5949. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  5950. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  5951. getGoogleStyle()));
  5952. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  5953. AlignLeft.AlignEscapedNewlinesLeft = true;
  5954. EXPECT_EQ(
  5955. "#define A \\\n"
  5956. " \"some \" \\\n"
  5957. " \"text \" \\\n"
  5958. " \"other\";",
  5959. format("#define A \"some text other\";", AlignLeft));
  5960. }
  5961. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  5962. EXPECT_EQ(
  5963. "u8\"utf8 string \"\n"
  5964. "u8\"literal\";",
  5965. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  5966. EXPECT_EQ(
  5967. "u\"utf16 string \"\n"
  5968. "u\"literal\";",
  5969. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  5970. EXPECT_EQ(
  5971. "U\"utf32 string \"\n"
  5972. "U\"literal\";",
  5973. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  5974. EXPECT_EQ("L\"wide string \"\n"
  5975. "L\"literal\";",
  5976. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  5977. EXPECT_EQ("@\"NSString \"\n"
  5978. "@\"literal\";",
  5979. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  5980. }
  5981. TEST_F(FormatTest, BreaksRawStringLiterals) {
  5982. EXPECT_EQ("R\"x(raw )x\"\n"
  5983. "R\"x(literal)x\";",
  5984. format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
  5985. EXPECT_EQ("uR\"x(raw )x\"\n"
  5986. "uR\"x(literal)x\";",
  5987. format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  5988. EXPECT_EQ("u8R\"x(raw )x\"\n"
  5989. "u8R\"x(literal)x\";",
  5990. format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
  5991. EXPECT_EQ("LR\"x(raw )x\"\n"
  5992. "LR\"x(literal)x\";",
  5993. format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  5994. EXPECT_EQ("UR\"x(raw )x\"\n"
  5995. "UR\"x(literal)x\";",
  5996. format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  5997. }
  5998. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  5999. FormatStyle Style = getLLVMStyleWithColumns(20);
  6000. EXPECT_EQ(
  6001. "_T(\"aaaaaaaaaaaaaa\")\n"
  6002. "_T(\"aaaaaaaaaaaaaa\")\n"
  6003. "_T(\"aaaaaaaaaaaa\")",
  6004. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6005. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6006. " _T(\"aaaaaa\"),\n"
  6007. " z);",
  6008. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6009. // FIXME: Handle embedded spaces in one iteration.
  6010. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6011. // "_T(\"aaaaaaaaaaaaa\")\n"
  6012. // "_T(\"aaaaaaaaaaaaa\")\n"
  6013. // "_T(\"a\")",
  6014. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6015. // getLLVMStyleWithColumns(20)));
  6016. EXPECT_EQ(
  6017. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6018. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  6019. }
  6020. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  6021. EXPECT_EQ(
  6022. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6023. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6024. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6025. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6027. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  6028. }
  6029. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  6030. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  6031. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  6032. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  6033. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6034. ")x\",\n"
  6035. " a),\n"
  6036. " b);",
  6037. format("fffffffffff(g(R\"x(\n"
  6038. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6039. ")x\", a), b);",
  6040. getGoogleStyleWithColumns(20)));
  6041. EXPECT_EQ("fffffffffff(\n"
  6042. " g(R\"x(qqq\n"
  6043. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6044. ")x\",\n"
  6045. " a),\n"
  6046. " b);",
  6047. format("fffffffffff(g(R\"x(qqq\n"
  6048. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6049. ")x\", a), b);",
  6050. getGoogleStyleWithColumns(20)));
  6051. EXPECT_EQ("fffffffffff(R\"x(\n"
  6052. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6053. ")x\");",
  6054. format("fffffffffff(R\"x(\n"
  6055. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6056. ")x\");",
  6057. getGoogleStyleWithColumns(20)));
  6058. EXPECT_EQ("fffffffffff(R\"x(\n"
  6059. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6060. ")x\" + bbbbbb);",
  6061. format("fffffffffff(R\"x(\n"
  6062. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6063. ")x\" + bbbbbb);",
  6064. getGoogleStyleWithColumns(20)));
  6065. EXPECT_EQ("fffffffffff(\n"
  6066. " R\"x(\n"
  6067. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6068. ")x\" +\n"
  6069. " bbbbbb);",
  6070. format("fffffffffff(\n"
  6071. " R\"x(\n"
  6072. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6073. ")x\" + bbbbbb);",
  6074. getGoogleStyleWithColumns(20)));
  6075. }
  6076. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6077. verifyFormat("string a = \"unterminated;");
  6078. EXPECT_EQ("function(\"unterminated,\n"
  6079. " OtherParameter);",
  6080. format("function( \"unterminated,\n"
  6081. " OtherParameter);"));
  6082. }
  6083. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6084. FormatStyle Style = getLLVMStyle();
  6085. Style.Standard = FormatStyle::LS_Cpp03;
  6086. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6087. format("#define x(_a) printf(\"foo\"_a);", Style));
  6088. }
  6089. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6090. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6091. " \"ddeeefff\");",
  6092. format("someFunction(\"aaabbbcccdddeeefff\");",
  6093. getLLVMStyleWithColumns(25)));
  6094. EXPECT_EQ("someFunction1234567890(\n"
  6095. " \"aaabbbcccdddeeefff\");",
  6096. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6097. getLLVMStyleWithColumns(26)));
  6098. EXPECT_EQ("someFunction1234567890(\n"
  6099. " \"aaabbbcccdddeeeff\"\n"
  6100. " \"f\");",
  6101. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6102. getLLVMStyleWithColumns(25)));
  6103. EXPECT_EQ("someFunction1234567890(\n"
  6104. " \"aaabbbcccdddeeeff\"\n"
  6105. " \"f\");",
  6106. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6107. getLLVMStyleWithColumns(24)));
  6108. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6109. " \"ddde \"\n"
  6110. " \"efff\");",
  6111. format("someFunction(\"aaabbbcc ddde efff\");",
  6112. getLLVMStyleWithColumns(25)));
  6113. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  6114. " \"ddeeefff\");",
  6115. format("someFunction(\"aaabbbccc ddeeefff\");",
  6116. getLLVMStyleWithColumns(25)));
  6117. EXPECT_EQ("someFunction1234567890(\n"
  6118. " \"aaabb \"\n"
  6119. " \"cccdddeeefff\");",
  6120. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  6121. getLLVMStyleWithColumns(25)));
  6122. EXPECT_EQ("#define A \\\n"
  6123. " string s = \\\n"
  6124. " \"123456789\" \\\n"
  6125. " \"0\"; \\\n"
  6126. " int i;",
  6127. format("#define A string s = \"1234567890\"; int i;",
  6128. getLLVMStyleWithColumns(20)));
  6129. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  6130. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6131. " \"dddeeeff\"\n"
  6132. " \"f\");",
  6133. format("someFunction(\"aaabbbcc dddeeefff\");",
  6134. getLLVMStyleWithColumns(25)));
  6135. }
  6136. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  6137. EXPECT_EQ("\"\\a\"",
  6138. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  6139. EXPECT_EQ("\"\\\"",
  6140. format("\"\\\"", getLLVMStyleWithColumns(2)));
  6141. EXPECT_EQ("\"test\"\n"
  6142. "\"\\n\"",
  6143. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  6144. EXPECT_EQ("\"tes\\\\\"\n"
  6145. "\"n\"",
  6146. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  6147. EXPECT_EQ("\"\\\\\\\\\"\n"
  6148. "\"\\n\"",
  6149. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  6150. EXPECT_EQ("\"\\uff01\"",
  6151. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  6152. EXPECT_EQ("\"\\uff01\"\n"
  6153. "\"test\"",
  6154. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  6155. EXPECT_EQ("\"\\Uff01ff02\"",
  6156. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  6157. EXPECT_EQ("\"\\x000000000001\"\n"
  6158. "\"next\"",
  6159. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  6160. EXPECT_EQ("\"\\x000000000001next\"",
  6161. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  6162. EXPECT_EQ("\"\\x000000000001\"",
  6163. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  6164. EXPECT_EQ("\"test\"\n"
  6165. "\"\\000000\"\n"
  6166. "\"000001\"",
  6167. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  6168. EXPECT_EQ("\"test\\000\"\n"
  6169. "\"00000000\"\n"
  6170. "\"1\"",
  6171. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  6172. // FIXME: We probably don't need to care about escape sequences in raw
  6173. // literals.
  6174. EXPECT_EQ("R\"(\\x)\"\n"
  6175. "R\"(\\x00)\"\n",
  6176. format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
  6177. }
  6178. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  6179. verifyFormat("void f() {\n"
  6180. " return g() {}\n"
  6181. " void h() {}");
  6182. verifyFormat("int a[] = {void forgot_closing_brace() {f();\n"
  6183. "g();\n"
  6184. "}");
  6185. }
  6186. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  6187. verifyFormat(
  6188. "void f() {\n"
  6189. " return C{param1, param2}.SomeCall(param1, param2);\n"
  6190. "}\n");
  6191. }
  6192. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  6193. verifyFormat("class X {\n"
  6194. " void f() {\n"
  6195. " }\n"
  6196. "};",
  6197. getLLVMStyleWithColumns(12));
  6198. }
  6199. TEST_F(FormatTest, ConfigurableIndentWidth) {
  6200. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  6201. EightIndent.IndentWidth = 8;
  6202. EightIndent.ContinuationIndentWidth = 8;
  6203. verifyFormat("void f() {\n"
  6204. " someFunction();\n"
  6205. " if (true) {\n"
  6206. " f();\n"
  6207. " }\n"
  6208. "}",
  6209. EightIndent);
  6210. verifyFormat("class X {\n"
  6211. " void f() {\n"
  6212. " }\n"
  6213. "};",
  6214. EightIndent);
  6215. verifyFormat("int x[] = {\n"
  6216. " call(),\n"
  6217. " call()};",
  6218. EightIndent);
  6219. }
  6220. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  6221. verifyFormat("void\n"
  6222. "f();",
  6223. getLLVMStyleWithColumns(8));
  6224. }
  6225. TEST_F(FormatTest, ConfigurableUseOfTab) {
  6226. FormatStyle Tab = getLLVMStyleWithColumns(42);
  6227. Tab.IndentWidth = 8;
  6228. Tab.UseTab = FormatStyle::UT_Always;
  6229. Tab.AlignEscapedNewlinesLeft = true;
  6230. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6231. " bb)\t\t// w\n"
  6232. "\t;",
  6233. format("if (aaaaaaaa &&// q\n"
  6234. "bb)// w\n"
  6235. ";",
  6236. Tab));
  6237. EXPECT_EQ("if (aaa && bbb) // w\n"
  6238. "\t;",
  6239. format("if(aaa&&bbb)// w\n"
  6240. ";",
  6241. Tab));
  6242. verifyFormat("class X {\n"
  6243. "\tvoid f() {\n"
  6244. "\t\tsomeFunction(parameter1,\n"
  6245. "\t\t\t parameter2);\n"
  6246. "\t}\n"
  6247. "};",
  6248. Tab);
  6249. verifyFormat("#define A \\\n"
  6250. "\tvoid f() { \\\n"
  6251. "\t\tsomeFunction( \\\n"
  6252. "\t\t parameter1, \\\n"
  6253. "\t\t parameter2); \\\n"
  6254. "\t}",
  6255. Tab);
  6256. EXPECT_EQ("void f() {\n"
  6257. "\tf();\n"
  6258. "\tg();\n"
  6259. "}",
  6260. format("void f() {\n"
  6261. "\tf();\n"
  6262. "\tg();\n"
  6263. "}",
  6264. 0, 0, Tab));
  6265. EXPECT_EQ("void f() {\n"
  6266. "\tf();\n"
  6267. "\tg();\n"
  6268. "}",
  6269. format("void f() {\n"
  6270. "\tf();\n"
  6271. "\tg();\n"
  6272. "}",
  6273. 16, 0, Tab));
  6274. EXPECT_EQ("void f() {\n"
  6275. " \tf();\n"
  6276. "\tg();\n"
  6277. "}",
  6278. format("void f() {\n"
  6279. " \tf();\n"
  6280. " \tg();\n"
  6281. "}",
  6282. 21, 0, Tab));
  6283. Tab.TabWidth = 4;
  6284. Tab.IndentWidth = 8;
  6285. verifyFormat("class TabWidth4Indent8 {\n"
  6286. "\t\tvoid f() {\n"
  6287. "\t\t\t\tsomeFunction(parameter1,\n"
  6288. "\t\t\t\t\t\t\t parameter2);\n"
  6289. "\t\t}\n"
  6290. "};",
  6291. Tab);
  6292. Tab.TabWidth = 4;
  6293. Tab.IndentWidth = 4;
  6294. verifyFormat("class TabWidth4Indent4 {\n"
  6295. "\tvoid f() {\n"
  6296. "\t\tsomeFunction(parameter1,\n"
  6297. "\t\t\t\t\t parameter2);\n"
  6298. "\t}\n"
  6299. "};",
  6300. Tab);
  6301. Tab.TabWidth = 8;
  6302. Tab.IndentWidth = 4;
  6303. verifyFormat("class TabWidth8Indent4 {\n"
  6304. " void f() {\n"
  6305. "\tsomeFunction(parameter1,\n"
  6306. "\t\t parameter2);\n"
  6307. " }\n"
  6308. "};",
  6309. Tab);
  6310. Tab.TabWidth = 8;
  6311. Tab.IndentWidth = 8;
  6312. EXPECT_EQ("/*\n"
  6313. "\t a\t\tcomment\n"
  6314. "\t in multiple lines\n"
  6315. " */",
  6316. format(" /*\t \t \n"
  6317. " \t \t a\t\tcomment\t \t\n"
  6318. " \t \t in multiple lines\t\n"
  6319. " \t */",
  6320. Tab));
  6321. Tab.UseTab = FormatStyle::UT_ForIndentation;
  6322. verifyFormat("{\n"
  6323. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6324. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6325. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6326. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6327. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6328. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6329. "};",
  6330. Tab);
  6331. verifyFormat("enum A {\n"
  6332. "\ta1, // Force multiple lines\n"
  6333. "\ta2,\n"
  6334. "\ta3\n"
  6335. "};",
  6336. Tab);
  6337. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6338. " bb) // w\n"
  6339. "\t;",
  6340. format("if (aaaaaaaa &&// q\n"
  6341. "bb)// w\n"
  6342. ";",
  6343. Tab));
  6344. verifyFormat("class X {\n"
  6345. "\tvoid f() {\n"
  6346. "\t\tsomeFunction(parameter1,\n"
  6347. "\t\t parameter2);\n"
  6348. "\t}\n"
  6349. "};",
  6350. Tab);
  6351. verifyFormat("{\n"
  6352. "\tQ({\n"
  6353. "\t\t int a;\n"
  6354. "\t\t someFunction(aaaaaaaaaa,\n"
  6355. "\t\t bbbbbbbbb);\n"
  6356. "\t },\n"
  6357. "\t p);\n"
  6358. "}",
  6359. Tab);
  6360. EXPECT_EQ("{\n"
  6361. "\t/* aaaa\n"
  6362. "\t bbbb */\n"
  6363. "}",
  6364. format("{\n"
  6365. "/* aaaa\n"
  6366. " bbbb */\n"
  6367. "}",
  6368. Tab));
  6369. EXPECT_EQ("{\n"
  6370. "\t/*\n"
  6371. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6372. "\t bbbbbbbbbbbbb\n"
  6373. "\t*/\n"
  6374. "}",
  6375. format("{\n"
  6376. "/*\n"
  6377. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6378. "*/\n"
  6379. "}",
  6380. Tab));
  6381. EXPECT_EQ("{\n"
  6382. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6383. "\t// bbbbbbbbbbbbb\n"
  6384. "}",
  6385. format("{\n"
  6386. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6387. "}",
  6388. Tab));
  6389. EXPECT_EQ("{\n"
  6390. "\t/*\n"
  6391. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6392. "\t bbbbbbbbbbbbb\n"
  6393. "\t*/\n"
  6394. "}",
  6395. format("{\n"
  6396. "\t/*\n"
  6397. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6398. "\t*/\n"
  6399. "}",
  6400. Tab));
  6401. EXPECT_EQ("{\n"
  6402. "\t/*\n"
  6403. "\n"
  6404. "\t*/\n"
  6405. "}",
  6406. format("{\n"
  6407. "\t/*\n"
  6408. "\n"
  6409. "\t*/\n"
  6410. "}",
  6411. Tab));
  6412. EXPECT_EQ("{\n"
  6413. "\t/*\n"
  6414. " asdf\n"
  6415. "\t*/\n"
  6416. "}",
  6417. format("{\n"
  6418. "\t/*\n"
  6419. " asdf\n"
  6420. "\t*/\n"
  6421. "}",
  6422. Tab));
  6423. Tab.UseTab = FormatStyle::UT_Never;
  6424. EXPECT_EQ("/*\n"
  6425. " a\t\tcomment\n"
  6426. " in multiple lines\n"
  6427. " */",
  6428. format(" /*\t \t \n"
  6429. " \t \t a\t\tcomment\t \t\n"
  6430. " \t \t in multiple lines\t\n"
  6431. " \t */",
  6432. Tab));
  6433. EXPECT_EQ("/* some\n"
  6434. " comment */",
  6435. format(" \t \t /* some\n"
  6436. " \t \t comment */",
  6437. Tab));
  6438. EXPECT_EQ("int a; /* some\n"
  6439. " comment */",
  6440. format(" \t \t int a; /* some\n"
  6441. " \t \t comment */",
  6442. Tab));
  6443. EXPECT_EQ("int a; /* some\n"
  6444. "comment */",
  6445. format(" \t \t int\ta; /* some\n"
  6446. " \t \t comment */",
  6447. Tab));
  6448. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  6449. " comment */",
  6450. format(" \t \t f(\"\t\t\"); /* some\n"
  6451. " \t \t comment */",
  6452. Tab));
  6453. EXPECT_EQ("{\n"
  6454. " /*\n"
  6455. " * Comment\n"
  6456. " */\n"
  6457. " int i;\n"
  6458. "}",
  6459. format("{\n"
  6460. "\t/*\n"
  6461. "\t * Comment\n"
  6462. "\t */\n"
  6463. "\t int i;\n"
  6464. "}"));
  6465. }
  6466. TEST_F(FormatTest, CalculatesOriginalColumn) {
  6467. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6468. "q\"; /* some\n"
  6469. " comment */",
  6470. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6471. "q\"; /* some\n"
  6472. " comment */",
  6473. getLLVMStyle()));
  6474. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  6475. "/* some\n"
  6476. " comment */",
  6477. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  6478. " /* some\n"
  6479. " comment */",
  6480. getLLVMStyle()));
  6481. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6482. "qqq\n"
  6483. "/* some\n"
  6484. " comment */",
  6485. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6486. "qqq\n"
  6487. " /* some\n"
  6488. " comment */",
  6489. getLLVMStyle()));
  6490. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6491. "wwww; /* some\n"
  6492. " comment */",
  6493. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6494. "wwww; /* some\n"
  6495. " comment */",
  6496. getLLVMStyle()));
  6497. }
  6498. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  6499. FormatStyle NoSpace = getLLVMStyle();
  6500. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  6501. verifyFormat("while(true)\n"
  6502. " continue;", NoSpace);
  6503. verifyFormat("for(;;)\n"
  6504. " continue;", NoSpace);
  6505. verifyFormat("if(true)\n"
  6506. " f();\n"
  6507. "else if(true)\n"
  6508. " f();", NoSpace);
  6509. verifyFormat("do {\n"
  6510. " do_something();\n"
  6511. "} while(something());", NoSpace);
  6512. verifyFormat("switch(x) {\n"
  6513. "default:\n"
  6514. " break;\n"
  6515. "}", NoSpace);
  6516. FormatStyle Space = getLLVMStyle();
  6517. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  6518. verifyFormat("int f ();", Space);
  6519. verifyFormat("void f (int a, T b) {\n"
  6520. " while (true)\n"
  6521. " continue;\n"
  6522. "}",
  6523. Space);
  6524. verifyFormat("if (true)\n"
  6525. " f ();\n"
  6526. "else if (true)\n"
  6527. " f ();",
  6528. Space);
  6529. verifyFormat("do {\n"
  6530. " do_something ();\n"
  6531. "} while (something ());",
  6532. Space);
  6533. verifyFormat("switch (x) {\n"
  6534. "default:\n"
  6535. " break;\n"
  6536. "}",
  6537. Space);
  6538. verifyFormat("A::A () : a (1) {}", Space);
  6539. verifyFormat("void f () __attribute__ ((asdf));", Space);
  6540. verifyFormat("*(&a + 1);\n"
  6541. "&((&a)[1]);\n"
  6542. "a[(b + c) * d];\n"
  6543. "(((a + 1) * 2) + 3) * 4;",
  6544. Space);
  6545. verifyFormat("#define A(x) x", Space);
  6546. verifyFormat("#define A (x) x", Space);
  6547. verifyFormat("#if defined(x)\n"
  6548. "#endif",
  6549. Space);
  6550. }
  6551. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  6552. FormatStyle Spaces = getLLVMStyle();
  6553. Spaces.SpacesInParentheses = true;
  6554. verifyFormat("call( x, y, z );", Spaces);
  6555. verifyFormat("while ( (bool)1 )\n"
  6556. " continue;", Spaces);
  6557. verifyFormat("for ( ;; )\n"
  6558. " continue;", Spaces);
  6559. verifyFormat("if ( true )\n"
  6560. " f();\n"
  6561. "else if ( true )\n"
  6562. " f();", Spaces);
  6563. verifyFormat("do {\n"
  6564. " do_something( (int)i );\n"
  6565. "} while ( something() );", Spaces);
  6566. verifyFormat("switch ( x ) {\n"
  6567. "default:\n"
  6568. " break;\n"
  6569. "}", Spaces);
  6570. Spaces.SpacesInParentheses = false;
  6571. Spaces.SpacesInCStyleCastParentheses = true;
  6572. verifyFormat("Type *A = ( Type * )P;", Spaces);
  6573. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  6574. verifyFormat("x = ( int32 )y;", Spaces);
  6575. verifyFormat("int a = ( int )(2.0f);", Spaces);
  6576. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  6577. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  6578. verifyFormat("#define x (( int )-1)", Spaces);
  6579. Spaces.SpacesInParentheses = false;
  6580. Spaces.SpaceInEmptyParentheses = true;
  6581. verifyFormat("call(x, y, z);", Spaces);
  6582. verifyFormat("call( )", Spaces);
  6583. // Run the first set of tests again with
  6584. // Spaces.SpacesInParentheses = false,
  6585. // Spaces.SpaceInEmptyParentheses = true and
  6586. // Spaces.SpacesInCStyleCastParentheses = true
  6587. Spaces.SpacesInParentheses = false,
  6588. Spaces.SpaceInEmptyParentheses = true;
  6589. Spaces.SpacesInCStyleCastParentheses = true;
  6590. verifyFormat("call(x, y, z);", Spaces);
  6591. verifyFormat("while (( bool )1)\n"
  6592. " continue;", Spaces);
  6593. verifyFormat("for (;;)\n"
  6594. " continue;", Spaces);
  6595. verifyFormat("if (true)\n"
  6596. " f( );\n"
  6597. "else if (true)\n"
  6598. " f( );", Spaces);
  6599. verifyFormat("do {\n"
  6600. " do_something(( int )i);\n"
  6601. "} while (something( ));", Spaces);
  6602. verifyFormat("switch (x) {\n"
  6603. "default:\n"
  6604. " break;\n"
  6605. "}", Spaces);
  6606. }
  6607. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  6608. verifyFormat("int a = 5;");
  6609. verifyFormat("a += 42;");
  6610. verifyFormat("a or_eq 8;");
  6611. FormatStyle Spaces = getLLVMStyle();
  6612. Spaces.SpaceBeforeAssignmentOperators = false;
  6613. verifyFormat("int a= 5;", Spaces);
  6614. verifyFormat("a+= 42;", Spaces);
  6615. verifyFormat("a or_eq 8;", Spaces);
  6616. }
  6617. TEST_F(FormatTest, LinuxBraceBreaking) {
  6618. FormatStyle BreakBeforeBrace = getLLVMStyle();
  6619. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
  6620. verifyFormat("namespace a\n"
  6621. "{\n"
  6622. "class A\n"
  6623. "{\n"
  6624. " void f()\n"
  6625. " {\n"
  6626. " if (true) {\n"
  6627. " a();\n"
  6628. " b();\n"
  6629. " }\n"
  6630. " }\n"
  6631. " void g() { return; }\n"
  6632. "}\n"
  6633. "}",
  6634. BreakBeforeBrace);
  6635. }
  6636. TEST_F(FormatTest, StroustrupBraceBreaking) {
  6637. FormatStyle BreakBeforeBrace = getLLVMStyle();
  6638. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  6639. verifyFormat("namespace a {\n"
  6640. "class A {\n"
  6641. " void f()\n"
  6642. " {\n"
  6643. " if (true) {\n"
  6644. " a();\n"
  6645. " b();\n"
  6646. " }\n"
  6647. " }\n"
  6648. " void g() { return; }\n"
  6649. "}\n"
  6650. "}",
  6651. BreakBeforeBrace);
  6652. }
  6653. TEST_F(FormatTest, AllmanBraceBreaking) {
  6654. FormatStyle BreakBeforeBrace = getLLVMStyle();
  6655. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
  6656. verifyFormat("namespace a\n"
  6657. "{\n"
  6658. "class A\n"
  6659. "{\n"
  6660. " void f()\n"
  6661. " {\n"
  6662. " if (true)\n"
  6663. " {\n"
  6664. " a();\n"
  6665. " b();\n"
  6666. " }\n"
  6667. " }\n"
  6668. " void g() { return; }\n"
  6669. "}\n"
  6670. "}",
  6671. BreakBeforeBrace);
  6672. verifyFormat("void f()\n"
  6673. "{\n"
  6674. " if (true)\n"
  6675. " {\n"
  6676. " a();\n"
  6677. " }\n"
  6678. " else if (false)\n"
  6679. " {\n"
  6680. " b();\n"
  6681. " }\n"
  6682. " else\n"
  6683. " {\n"
  6684. " c();\n"
  6685. " }\n"
  6686. "}\n",
  6687. BreakBeforeBrace);
  6688. verifyFormat("void f()\n"
  6689. "{\n"
  6690. " for (int i = 0; i < 10; ++i)\n"
  6691. " {\n"
  6692. " a();\n"
  6693. " }\n"
  6694. " while (false)\n"
  6695. " {\n"
  6696. " b();\n"
  6697. " }\n"
  6698. " do\n"
  6699. " {\n"
  6700. " c();\n"
  6701. " } while (false)\n"
  6702. "}\n",
  6703. BreakBeforeBrace);
  6704. verifyFormat("void f(int a)\n"
  6705. "{\n"
  6706. " switch (a)\n"
  6707. " {\n"
  6708. " case 0:\n"
  6709. " break;\n"
  6710. " case 1:\n"
  6711. " {\n"
  6712. " break;\n"
  6713. " }\n"
  6714. " case 2:\n"
  6715. " {\n"
  6716. " }\n"
  6717. " break;\n"
  6718. " default:\n"
  6719. " break;\n"
  6720. " }\n"
  6721. "}\n",
  6722. BreakBeforeBrace);
  6723. verifyFormat("enum X\n"
  6724. "{\n"
  6725. " Y = 0,\n"
  6726. "}\n",
  6727. BreakBeforeBrace);
  6728. BreakBeforeBrace.ColumnLimit = 19;
  6729. verifyFormat("void f() { int i; }", BreakBeforeBrace);
  6730. BreakBeforeBrace.ColumnLimit = 18;
  6731. verifyFormat("void f()\n"
  6732. "{\n"
  6733. " int i;\n"
  6734. "}",
  6735. BreakBeforeBrace);
  6736. BreakBeforeBrace.ColumnLimit = 80;
  6737. FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
  6738. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  6739. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  6740. verifyFormat("void f(bool b)\n"
  6741. "{\n"
  6742. " if (b)\n"
  6743. " {\n"
  6744. " return;\n"
  6745. " }\n"
  6746. "}\n",
  6747. BreakBeforeBraceShortIfs);
  6748. verifyFormat("void f(bool b)\n"
  6749. "{\n"
  6750. " if (b) return;\n"
  6751. "}\n",
  6752. BreakBeforeBraceShortIfs);
  6753. verifyFormat("void f(bool b)\n"
  6754. "{\n"
  6755. " while (b)\n"
  6756. " {\n"
  6757. " return;\n"
  6758. " }\n"
  6759. "}\n",
  6760. BreakBeforeBraceShortIfs);
  6761. }
  6762. TEST_F(FormatTest, GNUBraceBreaking) {
  6763. FormatStyle GNUBraceStyle = getLLVMStyle();
  6764. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  6765. verifyFormat("namespace a\n"
  6766. "{\n"
  6767. "class A\n"
  6768. "{\n"
  6769. " void f()\n"
  6770. " {\n"
  6771. " int a;\n"
  6772. " {\n"
  6773. " int b;\n"
  6774. " }\n"
  6775. " if (true)\n"
  6776. " {\n"
  6777. " a();\n"
  6778. " b();\n"
  6779. " }\n"
  6780. " }\n"
  6781. " void g() { return; }\n"
  6782. "}\n"
  6783. "}",
  6784. GNUBraceStyle);
  6785. verifyFormat("void f()\n"
  6786. "{\n"
  6787. " if (true)\n"
  6788. " {\n"
  6789. " a();\n"
  6790. " }\n"
  6791. " else if (false)\n"
  6792. " {\n"
  6793. " b();\n"
  6794. " }\n"
  6795. " else\n"
  6796. " {\n"
  6797. " c();\n"
  6798. " }\n"
  6799. "}\n",
  6800. GNUBraceStyle);
  6801. verifyFormat("void f()\n"
  6802. "{\n"
  6803. " for (int i = 0; i < 10; ++i)\n"
  6804. " {\n"
  6805. " a();\n"
  6806. " }\n"
  6807. " while (false)\n"
  6808. " {\n"
  6809. " b();\n"
  6810. " }\n"
  6811. " do\n"
  6812. " {\n"
  6813. " c();\n"
  6814. " }\n"
  6815. " while (false);\n"
  6816. "}\n",
  6817. GNUBraceStyle);
  6818. verifyFormat("void f(int a)\n"
  6819. "{\n"
  6820. " switch (a)\n"
  6821. " {\n"
  6822. " case 0:\n"
  6823. " break;\n"
  6824. " case 1:\n"
  6825. " {\n"
  6826. " break;\n"
  6827. " }\n"
  6828. " case 2:\n"
  6829. " {\n"
  6830. " }\n"
  6831. " break;\n"
  6832. " default:\n"
  6833. " break;\n"
  6834. " }\n"
  6835. "}\n",
  6836. GNUBraceStyle);
  6837. verifyFormat("enum X\n"
  6838. "{\n"
  6839. " Y = 0,\n"
  6840. "}\n",
  6841. GNUBraceStyle);
  6842. }
  6843. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  6844. verifyFormat("void f() {\n"
  6845. " try {\n"
  6846. " }\n"
  6847. " catch (const Exception &e) {\n"
  6848. " }\n"
  6849. "}\n",
  6850. getLLVMStyle());
  6851. }
  6852. TEST_F(FormatTest, UnderstandsPragmas) {
  6853. verifyFormat("#pragma omp reduction(| : var)");
  6854. verifyFormat("#pragma omp reduction(+ : var)");
  6855. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  6856. "(including parentheses).",
  6857. format("#pragma mark Any non-hyphenated or hyphenated string "
  6858. "(including parentheses)."));
  6859. }
  6860. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  6861. for (size_t i = 1; i < Styles.size(); ++i) \
  6862. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  6863. << Styles.size() \
  6864. << " differs from Style #0"
  6865. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  6866. SmallVector<FormatStyle, 3> Styles;
  6867. Styles.resize(3);
  6868. Styles[0] = getLLVMStyle();
  6869. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  6870. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  6871. EXPECT_ALL_STYLES_EQUAL(Styles);
  6872. Styles[0] = getGoogleStyle();
  6873. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  6874. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  6875. EXPECT_ALL_STYLES_EQUAL(Styles);
  6876. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  6877. EXPECT_TRUE(
  6878. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  6879. EXPECT_TRUE(
  6880. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  6881. EXPECT_ALL_STYLES_EQUAL(Styles);
  6882. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  6883. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  6884. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  6885. EXPECT_ALL_STYLES_EQUAL(Styles);
  6886. Styles[0] = getMozillaStyle();
  6887. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  6888. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  6889. EXPECT_ALL_STYLES_EQUAL(Styles);
  6890. Styles[0] = getWebKitStyle();
  6891. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  6892. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  6893. EXPECT_ALL_STYLES_EQUAL(Styles);
  6894. Styles[0] = getGNUStyle();
  6895. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  6896. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  6897. EXPECT_ALL_STYLES_EQUAL(Styles);
  6898. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  6899. }
  6900. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  6901. SmallVector<FormatStyle, 8> Styles;
  6902. Styles.resize(2);
  6903. Styles[0] = getGoogleStyle();
  6904. Styles[1] = getLLVMStyle();
  6905. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  6906. EXPECT_ALL_STYLES_EQUAL(Styles);
  6907. Styles.resize(5);
  6908. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  6909. Styles[1] = getLLVMStyle();
  6910. Styles[1].Language = FormatStyle::LK_JavaScript;
  6911. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  6912. Styles[2] = getLLVMStyle();
  6913. Styles[2].Language = FormatStyle::LK_JavaScript;
  6914. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  6915. "BasedOnStyle: Google",
  6916. &Styles[2]).value());
  6917. Styles[3] = getLLVMStyle();
  6918. Styles[3].Language = FormatStyle::LK_JavaScript;
  6919. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  6920. "Language: JavaScript",
  6921. &Styles[3]).value());
  6922. Styles[4] = getLLVMStyle();
  6923. Styles[4].Language = FormatStyle::LK_JavaScript;
  6924. EXPECT_EQ(0, parseConfiguration("---\n"
  6925. "BasedOnStyle: LLVM\n"
  6926. "IndentWidth: 123\n"
  6927. "---\n"
  6928. "BasedOnStyle: Google\n"
  6929. "Language: JavaScript",
  6930. &Styles[4]).value());
  6931. EXPECT_ALL_STYLES_EQUAL(Styles);
  6932. }
  6933. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  6934. EXPECT_NE(VALUE, Style.FIELD); \
  6935. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  6936. EXPECT_EQ(VALUE, Style.FIELD)
  6937. #define CHECK_PARSE_BOOL(FIELD) \
  6938. Style.FIELD = false; \
  6939. EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
  6940. EXPECT_TRUE(Style.FIELD); \
  6941. EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
  6942. EXPECT_FALSE(Style.FIELD);
  6943. TEST_F(FormatTest, ParsesConfiguration) {
  6944. FormatStyle Style = {};
  6945. Style.Language = FormatStyle::LK_Cpp;
  6946. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  6947. CHECK_PARSE_BOOL(AlignTrailingComments);
  6948. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  6949. CHECK_PARSE_BOOL(AllowShortFunctionsOnASingleLine);
  6950. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  6951. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  6952. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  6953. CHECK_PARSE_BOOL(BinPackParameters);
  6954. CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
  6955. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  6956. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  6957. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  6958. CHECK_PARSE_BOOL(DerivePointerBinding);
  6959. CHECK_PARSE_BOOL(IndentCaseLabels);
  6960. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  6961. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  6962. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  6963. CHECK_PARSE_BOOL(PointerBindsToType);
  6964. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  6965. CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
  6966. CHECK_PARSE_BOOL(SpacesInParentheses);
  6967. CHECK_PARSE_BOOL(SpacesInAngles);
  6968. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  6969. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  6970. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  6971. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  6972. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  6973. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  6974. ConstructorInitializerIndentWidth, 1234u);
  6975. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  6976. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  6977. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  6978. PenaltyBreakBeforeFirstCallParameter, 1234u);
  6979. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  6980. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  6981. PenaltyReturnTypeOnItsOwnLine, 1234u);
  6982. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  6983. SpacesBeforeTrailingComments, 1234u);
  6984. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  6985. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  6986. Style.Standard = FormatStyle::LS_Auto;
  6987. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  6988. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  6989. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  6990. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  6991. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  6992. Style.UseTab = FormatStyle::UT_ForIndentation;
  6993. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  6994. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  6995. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  6996. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  6997. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  6998. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  6999. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  7000. FormatStyle::SBPO_Never);
  7001. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  7002. FormatStyle::SBPO_Always);
  7003. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  7004. FormatStyle::SBPO_ControlStatements);
  7005. // For backward compatibility:
  7006. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  7007. FormatStyle::SBPO_Never);
  7008. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  7009. FormatStyle::SBPO_ControlStatements);
  7010. Style.ColumnLimit = 123;
  7011. FormatStyle BaseStyle = getLLVMStyle();
  7012. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  7013. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  7014. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7015. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  7016. FormatStyle::BS_Attach);
  7017. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  7018. FormatStyle::BS_Linux);
  7019. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  7020. FormatStyle::BS_Stroustrup);
  7021. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  7022. FormatStyle::BS_Allman);
  7023. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  7024. Style.NamespaceIndentation = FormatStyle::NI_All;
  7025. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  7026. FormatStyle::NI_None);
  7027. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  7028. FormatStyle::NI_Inner);
  7029. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  7030. FormatStyle::NI_All);
  7031. }
  7032. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  7033. FormatStyle Style = {};
  7034. Style.Language = FormatStyle::LK_Cpp;
  7035. CHECK_PARSE("Language: Cpp\n"
  7036. "IndentWidth: 12",
  7037. IndentWidth, 12u);
  7038. EXPECT_EQ(llvm::errc::not_supported,
  7039. parseConfiguration("Language: JavaScript\n"
  7040. "IndentWidth: 34",
  7041. &Style));
  7042. EXPECT_EQ(12u, Style.IndentWidth);
  7043. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  7044. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  7045. Style.Language = FormatStyle::LK_JavaScript;
  7046. CHECK_PARSE("Language: JavaScript\n"
  7047. "IndentWidth: 12",
  7048. IndentWidth, 12u);
  7049. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  7050. EXPECT_EQ(llvm::errc::not_supported, parseConfiguration("Language: Cpp\n"
  7051. "IndentWidth: 34",
  7052. &Style));
  7053. EXPECT_EQ(23u, Style.IndentWidth);
  7054. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  7055. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  7056. CHECK_PARSE("BasedOnStyle: LLVM\n"
  7057. "IndentWidth: 67",
  7058. IndentWidth, 67u);
  7059. CHECK_PARSE("---\n"
  7060. "Language: JavaScript\n"
  7061. "IndentWidth: 12\n"
  7062. "---\n"
  7063. "Language: Cpp\n"
  7064. "IndentWidth: 34\n"
  7065. "...\n",
  7066. IndentWidth, 12u);
  7067. Style.Language = FormatStyle::LK_Cpp;
  7068. CHECK_PARSE("---\n"
  7069. "Language: JavaScript\n"
  7070. "IndentWidth: 12\n"
  7071. "---\n"
  7072. "Language: Cpp\n"
  7073. "IndentWidth: 34\n"
  7074. "...\n",
  7075. IndentWidth, 34u);
  7076. CHECK_PARSE("---\n"
  7077. "IndentWidth: 78\n"
  7078. "---\n"
  7079. "Language: JavaScript\n"
  7080. "IndentWidth: 56\n"
  7081. "...\n",
  7082. IndentWidth, 78u);
  7083. Style.ColumnLimit = 123;
  7084. Style.IndentWidth = 234;
  7085. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  7086. Style.TabWidth = 345;
  7087. EXPECT_EQ(llvm::errc::success,
  7088. parseConfiguration("---\n"
  7089. "IndentWidth: 456\n"
  7090. "BreakBeforeBraces: Allman\n"
  7091. "---\n"
  7092. "Language: JavaScript\n"
  7093. "IndentWidth: 111\n"
  7094. "TabWidth: 111\n"
  7095. "---\n"
  7096. "Language: Cpp\n"
  7097. "BreakBeforeBraces: Stroustrup\n"
  7098. "TabWidth: 789\n"
  7099. "...\n",
  7100. &Style));
  7101. EXPECT_EQ(123u, Style.ColumnLimit);
  7102. EXPECT_EQ(456u, Style.IndentWidth);
  7103. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  7104. EXPECT_EQ(789u, Style.TabWidth);
  7105. EXPECT_EQ(llvm::errc::invalid_argument,
  7106. parseConfiguration("---\n"
  7107. "Language: JavaScript\n"
  7108. "IndentWidth: 56\n"
  7109. "---\n"
  7110. "IndentWidth: 78\n"
  7111. "...\n",
  7112. &Style));
  7113. EXPECT_EQ(llvm::errc::invalid_argument,
  7114. parseConfiguration("---\n"
  7115. "Language: JavaScript\n"
  7116. "IndentWidth: 56\n"
  7117. "---\n"
  7118. "Language: JavaScript\n"
  7119. "IndentWidth: 78\n"
  7120. "...\n",
  7121. &Style));
  7122. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  7123. }
  7124. #undef CHECK_PARSE
  7125. #undef CHECK_PARSE_BOOL
  7126. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  7127. FormatStyle Style = {};
  7128. Style.Language = FormatStyle::LK_JavaScript;
  7129. Style.BreakBeforeTernaryOperators = true;
  7130. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  7131. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  7132. Style.BreakBeforeTernaryOperators = true;
  7133. EXPECT_EQ(0, parseConfiguration("---\n"
  7134. "BasedOnStyle: Google\n"
  7135. "---\n"
  7136. "Language: JavaScript\n"
  7137. "IndentWidth: 76\n"
  7138. "...\n", &Style).value());
  7139. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  7140. EXPECT_EQ(76u, Style.IndentWidth);
  7141. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  7142. }
  7143. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  7144. FormatStyle Style = getLLVMStyle();
  7145. std::string YAML = configurationAsText(Style);
  7146. FormatStyle ParsedStyle = {};
  7147. ParsedStyle.Language = FormatStyle::LK_Cpp;
  7148. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  7149. EXPECT_EQ(Style, ParsedStyle);
  7150. }
  7151. TEST_F(FormatTest, WorksFor8bitEncodings) {
  7152. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  7153. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  7154. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  7155. "\"\xef\xee\xf0\xf3...\"",
  7156. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  7157. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  7158. "\xef\xee\xf0\xf3...\"",
  7159. getLLVMStyleWithColumns(12)));
  7160. }
  7161. TEST_F(FormatTest, HandlesUTF8BOM) {
  7162. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  7163. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  7164. format("\xef\xbb\xbf#include <iostream>"));
  7165. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  7166. format("\xef\xbb\xbf\n#include <iostream>"));
  7167. }
  7168. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  7169. #if !defined(_MSC_VER)
  7170. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  7171. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  7172. getLLVMStyleWithColumns(35));
  7173. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  7174. getLLVMStyleWithColumns(31));
  7175. verifyFormat("// Однажды в студёную зимнюю пору...",
  7176. getLLVMStyleWithColumns(36));
  7177. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  7178. getLLVMStyleWithColumns(32));
  7179. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  7180. getLLVMStyleWithColumns(39));
  7181. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  7182. getLLVMStyleWithColumns(35));
  7183. }
  7184. TEST_F(FormatTest, SplitsUTF8Strings) {
  7185. // Non-printable characters' width is currently considered to be the length in
  7186. // bytes in UTF8. The characters can be displayed in very different manner
  7187. // (zero-width, single width with a substitution glyph, expanded to their code
  7188. // (e.g. "<8d>"), so there's no single correct way to handle them.
  7189. EXPECT_EQ("\"aaaaÄ\"\n"
  7190. "\"\xc2\x8d\";",
  7191. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  7192. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  7193. "\"\xc2\x8d\";",
  7194. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  7195. EXPECT_EQ(
  7196. "\"Однажды, в \"\n"
  7197. "\"студёную \"\n"
  7198. "\"зимнюю \"\n"
  7199. "\"пору,\"",
  7200. format("\"Однажды, в студёную зимнюю пору,\"",
  7201. getLLVMStyleWithColumns(13)));
  7202. EXPECT_EQ("\"一 二 三 \"\n"
  7203. "\"四 五六 \"\n"
  7204. "\"七 八 九 \"\n"
  7205. "\"十\"",
  7206. format("\"一 二 三 四 五六 七 八 九 十\"",
  7207. getLLVMStyleWithColumns(11)));
  7208. EXPECT_EQ("\"一\t二 \"\n"
  7209. "\"\t三 \"\n"
  7210. "\"四 五\t六 \"\n"
  7211. "\"\t七 \"\n"
  7212. "\"八九十\tqq\"",
  7213. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  7214. getLLVMStyleWithColumns(11)));
  7215. }
  7216. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  7217. EXPECT_EQ("const char *sssss =\n"
  7218. " \"一二三四五六七八\\\n"
  7219. " 九 十\";",
  7220. format("const char *sssss = \"一二三四五六七八\\\n"
  7221. " 九 十\";",
  7222. getLLVMStyleWithColumns(30)));
  7223. }
  7224. TEST_F(FormatTest, SplitsUTF8LineComments) {
  7225. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  7226. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  7227. EXPECT_EQ("// Я из лесу\n"
  7228. "// вышел; был\n"
  7229. "// сильный\n"
  7230. "// мороз.",
  7231. format("// Я из лесу вышел; был сильный мороз.",
  7232. getLLVMStyleWithColumns(13)));
  7233. EXPECT_EQ("// 一二三\n"
  7234. "// 四五六七\n"
  7235. "// 八 九\n"
  7236. "// 十",
  7237. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  7238. }
  7239. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  7240. EXPECT_EQ("/* Гляжу,\n"
  7241. " * поднимается\n"
  7242. " * медленно в\n"
  7243. " * гору\n"
  7244. " * Лошадка,\n"
  7245. " * везущая\n"
  7246. " * хворосту\n"
  7247. " * воз. */",
  7248. format("/* Гляжу, поднимается медленно в гору\n"
  7249. " * Лошадка, везущая хворосту воз. */",
  7250. getLLVMStyleWithColumns(13)));
  7251. EXPECT_EQ(
  7252. "/* 一二三\n"
  7253. " * 四五六七\n"
  7254. " * 八 九\n"
  7255. " * 十 */",
  7256. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  7257. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  7258. " * 𝕓𝕪𝕥𝕖\n"
  7259. " * 𝖀𝕿𝕱-𝟠 */",
  7260. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  7261. }
  7262. #endif // _MSC_VER
  7263. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  7264. FormatStyle Style = getLLVMStyle();
  7265. Style.ConstructorInitializerIndentWidth = 4;
  7266. verifyFormat(
  7267. "SomeClass::Constructor()\n"
  7268. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  7269. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  7270. Style);
  7271. Style.ConstructorInitializerIndentWidth = 2;
  7272. verifyFormat(
  7273. "SomeClass::Constructor()\n"
  7274. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  7275. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  7276. Style);
  7277. Style.ConstructorInitializerIndentWidth = 0;
  7278. verifyFormat(
  7279. "SomeClass::Constructor()\n"
  7280. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  7281. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  7282. Style);
  7283. Style.BreakConstructorInitializersBeforeComma = true;
  7284. Style.ConstructorInitializerIndentWidth = 4;
  7285. verifyFormat("SomeClass::Constructor()\n"
  7286. " : a(a)\n"
  7287. " , b(b)\n"
  7288. " , c(c) {}",
  7289. Style);
  7290. verifyFormat("SomeClass::Constructor()\n"
  7291. " : a(a) {}",
  7292. Style);
  7293. Style.ColumnLimit = 0;
  7294. verifyFormat("SomeClass::Constructor()\n"
  7295. " : a(a) {}",
  7296. Style);
  7297. verifyFormat("SomeClass::Constructor()\n"
  7298. " : a(a)\n"
  7299. " , b(b)\n"
  7300. " , c(c) {}",
  7301. Style);
  7302. verifyFormat("SomeClass::Constructor()\n"
  7303. " : a(a) {\n"
  7304. " foo();\n"
  7305. " bar();\n"
  7306. "}",
  7307. Style);
  7308. Style.AllowShortFunctionsOnASingleLine = false;
  7309. verifyFormat("SomeClass::Constructor()\n"
  7310. " : a(a)\n"
  7311. " , b(b)\n"
  7312. " , c(c) {\n}",
  7313. Style);
  7314. verifyFormat("SomeClass::Constructor()\n"
  7315. " : a(a) {\n}",
  7316. Style);
  7317. Style.ColumnLimit = 80;
  7318. Style.AllowShortFunctionsOnASingleLine = true;
  7319. Style.ConstructorInitializerIndentWidth = 2;
  7320. verifyFormat("SomeClass::Constructor()\n"
  7321. " : a(a)\n"
  7322. " , b(b)\n"
  7323. " , c(c) {}",
  7324. Style);
  7325. Style.ConstructorInitializerIndentWidth = 0;
  7326. verifyFormat("SomeClass::Constructor()\n"
  7327. ": a(a)\n"
  7328. ", b(b)\n"
  7329. ", c(c) {}",
  7330. Style);
  7331. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  7332. Style.ConstructorInitializerIndentWidth = 4;
  7333. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  7334. verifyFormat(
  7335. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  7336. Style);
  7337. verifyFormat(
  7338. "SomeClass::Constructor()\n"
  7339. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  7340. Style);
  7341. Style.ConstructorInitializerIndentWidth = 4;
  7342. Style.ColumnLimit = 60;
  7343. verifyFormat("SomeClass::Constructor()\n"
  7344. " : aaaaaaaa(aaaaaaaa)\n"
  7345. " , aaaaaaaa(aaaaaaaa)\n"
  7346. " , aaaaaaaa(aaaaaaaa) {}",
  7347. Style);
  7348. }
  7349. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  7350. FormatStyle Style = getWebKitStyle();
  7351. // Don't indent in outer namespaces.
  7352. verifyFormat("namespace outer {\n"
  7353. "int i;\n"
  7354. "namespace inner {\n"
  7355. " int i;\n"
  7356. "} // namespace inner\n"
  7357. "} // namespace outer\n"
  7358. "namespace other_outer {\n"
  7359. "int i;\n"
  7360. "}",
  7361. Style);
  7362. // Don't indent case labels.
  7363. verifyFormat("switch (variable) {\n"
  7364. "case 1:\n"
  7365. "case 2:\n"
  7366. " doSomething();\n"
  7367. " break;\n"
  7368. "default:\n"
  7369. " ++variable;\n"
  7370. "}",
  7371. Style);
  7372. // Wrap before binary operators.
  7373. EXPECT_EQ(
  7374. "void f()\n"
  7375. "{\n"
  7376. " if (aaaaaaaaaaaaaaaa\n"
  7377. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  7378. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  7379. " return;\n"
  7380. "}",
  7381. format(
  7382. "void f() {\n"
  7383. "if (aaaaaaaaaaaaaaaa\n"
  7384. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  7385. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  7386. "return;\n"
  7387. "}",
  7388. Style));
  7389. // Constructor initializers are formatted one per line with the "," on the
  7390. // new line.
  7391. verifyFormat("Constructor()\n"
  7392. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  7393. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  7394. " aaaaaaaaaaaaaa)\n"
  7395. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  7396. "{\n"
  7397. "}",
  7398. Style);
  7399. verifyFormat("SomeClass::Constructor()\n"
  7400. " : a(a)\n"
  7401. "{\n"
  7402. "}",
  7403. Style);
  7404. EXPECT_EQ("SomeClass::Constructor()\n"
  7405. " : a(a)\n"
  7406. "{\n"
  7407. "}",
  7408. format("SomeClass::Constructor():a(a){}", Style));
  7409. verifyFormat("SomeClass::Constructor()\n"
  7410. " : a(a)\n"
  7411. " , b(b)\n"
  7412. " , c(c)\n"
  7413. "{\n"
  7414. "}", Style);
  7415. verifyFormat("SomeClass::Constructor()\n"
  7416. " : a(a)\n"
  7417. "{\n"
  7418. " foo();\n"
  7419. " bar();\n"
  7420. "}",
  7421. Style);
  7422. // Access specifiers should be aligned left.
  7423. verifyFormat("class C {\n"
  7424. "public:\n"
  7425. " int i;\n"
  7426. "};",
  7427. Style);
  7428. // Do not align comments.
  7429. verifyFormat("int a; // Do not\n"
  7430. "double b; // align comments.",
  7431. Style);
  7432. // Accept input's line breaks.
  7433. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  7434. " || bbbbbbbbbbbbbbb) {\n"
  7435. " i++;\n"
  7436. "}",
  7437. format("if (aaaaaaaaaaaaaaa\n"
  7438. "|| bbbbbbbbbbbbbbb) { i++; }",
  7439. Style));
  7440. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  7441. " i++;\n"
  7442. "}",
  7443. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  7444. // Don't automatically break all macro definitions (llvm.org/PR17842).
  7445. verifyFormat("#define aNumber 10", Style);
  7446. // However, generally keep the line breaks that the user authored.
  7447. EXPECT_EQ("#define aNumber \\\n"
  7448. " 10",
  7449. format("#define aNumber \\\n"
  7450. " 10",
  7451. Style));
  7452. }
  7453. TEST_F(FormatTest, FormatsLambdas) {
  7454. verifyFormat("int c = [b]() mutable {\n"
  7455. " return [&b] { return b++; }();\n"
  7456. "}();\n");
  7457. verifyFormat("int c = [&] {\n"
  7458. " [=] { return b++; }();\n"
  7459. "}();\n");
  7460. verifyFormat("int c = [&, &a, a] {\n"
  7461. " [=, c, &d] { return b++; }();\n"
  7462. "}();\n");
  7463. verifyFormat("int c = [&a, &a, a] {\n"
  7464. " [=, a, b, &c] { return b++; }();\n"
  7465. "}();\n");
  7466. verifyFormat("auto c = {[&a, &a, a] {\n"
  7467. " [=, a, b, &c] { return b++; }();\n"
  7468. "}}\n");
  7469. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  7470. verifyFormat("void f() {\n"
  7471. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  7472. "}\n");
  7473. verifyFormat("void f() {\n"
  7474. " other(x.begin(), //\n"
  7475. " x.end(), //\n"
  7476. " [&](int, int) { return 1; });\n"
  7477. "}\n");
  7478. // Lambdas with return types.
  7479. verifyFormat("int c = []() -> int { return 2; }();\n");
  7480. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  7481. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  7482. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  7483. " int j) -> int {\n"
  7484. " return fffffffffffffffffffffffffffffffffffffff(i * j);\n"
  7485. "};");
  7486. // Not lambdas.
  7487. verifyFormat("constexpr char hello[]{\"hello\"};");
  7488. verifyFormat("double &operator[](int i) { return 0; }\n"
  7489. "int i;");
  7490. verifyFormat("std::unique_ptr<int[]> foo() {}");
  7491. verifyFormat("int i = a[a][a]->f();");
  7492. verifyFormat("int i = (*b)[a]->f();");
  7493. // Other corner cases.
  7494. verifyFormat("void f() {\n"
  7495. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  7496. " );\n"
  7497. "}");
  7498. // Lambdas created through weird macros.
  7499. verifyFormat("void f() {\n"
  7500. " MACRO((const AA &a) { return 1; });\n"
  7501. "}");
  7502. }
  7503. TEST_F(FormatTest, FormatsBlocks) {
  7504. verifyFormat("int (^Block)(int, int);");
  7505. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
  7506. verifyFormat("void (^block)(int) = ^(id test) { int i; };");
  7507. verifyFormat("void (^block)(int) = ^(int test) { int i; };");
  7508. verifyFormat("void (^block)(int) = ^id(int test) { int i; };");
  7509. verifyFormat("void (^block)(int) = ^int(int test) { int i; };");
  7510. verifyFormat("foo(^{ bar(); });");
  7511. verifyFormat("foo(a, ^{ bar(); });");
  7512. // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
  7513. // it would ideally look.
  7514. verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
  7515. verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
  7516. "onOperationDone]; }]};");
  7517. verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];");
  7518. verifyFormat("int a = [operation block:^int(int *i) { return 1; }];");
  7519. verifyFormat("[myObject doSomethingWith:arg1\n"
  7520. " aaa:^int(int *a) { return 1; }\n"
  7521. " bbb:f(a * b)];");
  7522. verifyFormat("[operation setCompletionBlock:^{\n"
  7523. " [self.delegate newDataAvailable];\n"
  7524. "}];",
  7525. getLLVMStyleWithColumns(60));
  7526. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  7527. " NSString *path = [self sessionFilePath];\n"
  7528. " if (path) {\n"
  7529. " // ...\n"
  7530. " }\n"
  7531. "});");
  7532. verifyFormat("[[SessionService sharedService]\n"
  7533. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  7534. " if (window) {\n"
  7535. " [self windowDidLoad:window];\n"
  7536. " } else {\n"
  7537. " [self errorLoadingWindow];\n"
  7538. " }\n"
  7539. " }];");
  7540. verifyFormat("void (^largeBlock)(void) = ^{\n"
  7541. " // ...\n"
  7542. "};\n",
  7543. getLLVMStyleWithColumns(40));
  7544. verifyFormat("[[SessionService sharedService]\n"
  7545. " loadWindowWithCompletionBlock: //\n"
  7546. " ^(SessionWindow *window) {\n"
  7547. " if (window) {\n"
  7548. " [self windowDidLoad:window];\n"
  7549. " } else {\n"
  7550. " [self errorLoadingWindow];\n"
  7551. " }\n"
  7552. " }];",
  7553. getLLVMStyleWithColumns(60));
  7554. verifyFormat("[myObject doSomethingWith:arg1\n"
  7555. " firstBlock:^(Foo *a) {\n"
  7556. " // ...\n"
  7557. " int i;\n"
  7558. " }\n"
  7559. " secondBlock:^(Bar *b) {\n"
  7560. " // ...\n"
  7561. " int i;\n"
  7562. " }\n"
  7563. " thirdBlock:^Foo(Bar *b) {\n"
  7564. " // ...\n"
  7565. " int i;\n"
  7566. " }];");
  7567. verifyFormat("f(^{\n"
  7568. " @autoreleasepool {\n"
  7569. " if (a) {\n"
  7570. " g();\n"
  7571. " }\n"
  7572. " }\n"
  7573. "});");
  7574. }
  7575. TEST_F(FormatTest, SupportsCRLF) {
  7576. EXPECT_EQ("int a;\r\n"
  7577. "int b;\r\n"
  7578. "int c;\r\n",
  7579. format("int a;\r\n"
  7580. " int b;\r\n"
  7581. " int c;\r\n",
  7582. getLLVMStyle()));
  7583. EXPECT_EQ("int a;\r\n"
  7584. "int b;\r\n"
  7585. "int c;\r\n",
  7586. format("int a;\r\n"
  7587. " int b;\n"
  7588. " int c;\r\n",
  7589. getLLVMStyle()));
  7590. EXPECT_EQ("int a;\n"
  7591. "int b;\n"
  7592. "int c;\n",
  7593. format("int a;\r\n"
  7594. " int b;\n"
  7595. " int c;\n",
  7596. getLLVMStyle()));
  7597. EXPECT_EQ("\"aaaaaaa \"\r\n"
  7598. "\"bbbbbbb\";\r\n",
  7599. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  7600. EXPECT_EQ("#define A \\\r\n"
  7601. " b; \\\r\n"
  7602. " c; \\\r\n"
  7603. " d;\r\n",
  7604. format("#define A \\\r\n"
  7605. " b; \\\r\n"
  7606. " c; d; \r\n",
  7607. getGoogleStyle()));
  7608. EXPECT_EQ("/*\r\n"
  7609. "multi line block comments\r\n"
  7610. "should not introduce\r\n"
  7611. "an extra carriage return\r\n"
  7612. "*/\r\n",
  7613. format("/*\r\n"
  7614. "multi line block comments\r\n"
  7615. "should not introduce\r\n"
  7616. "an extra carriage return\r\n"
  7617. "*/\r\n"));
  7618. }
  7619. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  7620. verifyFormat("MY_CLASS(C) {\n"
  7621. " int i;\n"
  7622. " int j;\n"
  7623. "};");
  7624. }
  7625. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  7626. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  7627. TwoIndent.ContinuationIndentWidth = 2;
  7628. EXPECT_EQ("int i =\n"
  7629. " longFunction(\n"
  7630. " arg);",
  7631. format("int i = longFunction(arg);", TwoIndent));
  7632. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  7633. SixIndent.ContinuationIndentWidth = 6;
  7634. EXPECT_EQ("int i =\n"
  7635. " longFunction(\n"
  7636. " arg);",
  7637. format("int i = longFunction(arg);", SixIndent));
  7638. }
  7639. TEST_F(FormatTest, SpacesInAngles) {
  7640. FormatStyle Spaces = getLLVMStyle();
  7641. Spaces.SpacesInAngles = true;
  7642. verifyFormat("static_cast< int >(arg);", Spaces);
  7643. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  7644. verifyFormat("f< int, float >();", Spaces);
  7645. verifyFormat("template <> g() {}", Spaces);
  7646. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  7647. Spaces.Standard = FormatStyle::LS_Cpp03;
  7648. Spaces.SpacesInAngles = true;
  7649. verifyFormat("A< A< int > >();", Spaces);
  7650. Spaces.SpacesInAngles = false;
  7651. verifyFormat("A<A<int> >();", Spaces);
  7652. Spaces.Standard = FormatStyle::LS_Cpp11;
  7653. Spaces.SpacesInAngles = true;
  7654. verifyFormat("A< A< int > >();", Spaces);
  7655. Spaces.SpacesInAngles = false;
  7656. verifyFormat("A<A<int>>();", Spaces);
  7657. }
  7658. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  7659. std::string code = "#if A\n"
  7660. "#if B\n"
  7661. "a.\n"
  7662. "#endif\n"
  7663. " a = 1;\n"
  7664. "#else\n"
  7665. "#endif\n"
  7666. "#if C\n"
  7667. "#else\n"
  7668. "#endif\n";
  7669. EXPECT_EQ(code, format(code));
  7670. }
  7671. } // end namespace tooling
  7672. } // end namespace clang