FormatTest.cpp 301 KB

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