FormatTest.cpp 377 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Format/Format.h"
  10. #include "../Tooling/ReplacementTest.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. using clang::tooling::ReplacementTest;
  18. using clang::tooling::toReplacements;
  19. namespace clang {
  20. namespace format {
  21. namespace {
  22. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  23. class FormatTest : public ::testing::Test {
  24. protected:
  25. enum IncompleteCheck {
  26. IC_ExpectComplete,
  27. IC_ExpectIncomplete,
  28. IC_DoNotCheck
  29. };
  30. std::string format(llvm::StringRef Code,
  31. const FormatStyle &Style = getLLVMStyle(),
  32. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  33. DEBUG(llvm::errs() << "---\n");
  34. DEBUG(llvm::errs() << Code << "\n\n");
  35. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  36. bool IncompleteFormat = false;
  37. tooling::Replacements Replaces =
  38. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  39. if (CheckIncomplete != IC_DoNotCheck) {
  40. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  41. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  42. }
  43. ReplacementCount = Replaces.size();
  44. auto Result = applyAllReplacements(Code, Replaces);
  45. EXPECT_TRUE(static_cast<bool>(Result));
  46. DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  47. return *Result;
  48. }
  49. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getLLVMStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  55. FormatStyle Style = getGoogleStyle();
  56. Style.ColumnLimit = ColumnLimit;
  57. return Style;
  58. }
  59. void verifyFormat(llvm::StringRef Code,
  60. const FormatStyle &Style = getLLVMStyle()) {
  61. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  62. }
  63. void verifyIncompleteFormat(llvm::StringRef Code,
  64. const FormatStyle &Style = getLLVMStyle()) {
  65. EXPECT_EQ(Code.str(),
  66. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  67. }
  68. void verifyGoogleFormat(llvm::StringRef Code) {
  69. verifyFormat(Code, getGoogleStyle());
  70. }
  71. void verifyIndependentOfContext(llvm::StringRef text) {
  72. verifyFormat(text);
  73. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  74. }
  75. /// \brief Verify that clang-format does not crash on the given input.
  76. void verifyNoCrash(llvm::StringRef Code,
  77. const FormatStyle &Style = getLLVMStyle()) {
  78. format(Code, Style, IC_DoNotCheck);
  79. }
  80. int ReplacementCount;
  81. };
  82. TEST_F(FormatTest, MessUp) {
  83. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  84. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  85. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  86. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  87. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  88. }
  89. //===----------------------------------------------------------------------===//
  90. // Basic function tests.
  91. //===----------------------------------------------------------------------===//
  92. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  93. EXPECT_EQ(";", format(";"));
  94. }
  95. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  96. EXPECT_EQ("int i;", format(" int i;"));
  97. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  98. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  99. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  100. }
  101. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  102. EXPECT_EQ("int i;", format("int\ni;"));
  103. }
  104. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  105. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  106. }
  107. TEST_F(FormatTest, FormatsNestedCall) {
  108. verifyFormat("Method(f1, f2(f3));");
  109. verifyFormat("Method(f1(f2, f3()));");
  110. verifyFormat("Method(f1(f2, (f3())));");
  111. }
  112. TEST_F(FormatTest, NestedNameSpecifiers) {
  113. verifyFormat("vector<::Type> v;");
  114. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  115. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  116. verifyFormat("bool a = 2 < ::SomeFunction();");
  117. }
  118. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  119. EXPECT_EQ("if (a) {\n"
  120. " f();\n"
  121. "}",
  122. format("if(a){f();}"));
  123. EXPECT_EQ(4, ReplacementCount);
  124. EXPECT_EQ("if (a) {\n"
  125. " f();\n"
  126. "}",
  127. format("if (a) {\n"
  128. " f();\n"
  129. "}"));
  130. EXPECT_EQ(0, ReplacementCount);
  131. EXPECT_EQ("/*\r\n"
  132. "\r\n"
  133. "*/\r\n",
  134. format("/*\r\n"
  135. "\r\n"
  136. "*/\r\n"));
  137. EXPECT_EQ(0, ReplacementCount);
  138. }
  139. TEST_F(FormatTest, RemovesEmptyLines) {
  140. EXPECT_EQ("class C {\n"
  141. " int i;\n"
  142. "};",
  143. format("class C {\n"
  144. " int i;\n"
  145. "\n"
  146. "};"));
  147. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  148. EXPECT_EQ("namespace N {\n"
  149. "\n"
  150. "int i;\n"
  151. "}",
  152. format("namespace N {\n"
  153. "\n"
  154. "int i;\n"
  155. "}",
  156. getGoogleStyle()));
  157. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  158. "\n"
  159. "int i;\n"
  160. "}",
  161. format("extern /**/ \"C\" /**/ {\n"
  162. "\n"
  163. "int i;\n"
  164. "}",
  165. getGoogleStyle()));
  166. // ...but do keep inlining and removing empty lines for non-block extern "C"
  167. // functions.
  168. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  169. EXPECT_EQ("extern \"C\" int f() {\n"
  170. " int i = 42;\n"
  171. " return i;\n"
  172. "}",
  173. format("extern \"C\" int f() {\n"
  174. "\n"
  175. " int i = 42;\n"
  176. " return i;\n"
  177. "}",
  178. getGoogleStyle()));
  179. // Remove empty lines at the beginning and end of blocks.
  180. EXPECT_EQ("void f() {\n"
  181. "\n"
  182. " if (a) {\n"
  183. "\n"
  184. " f();\n"
  185. " }\n"
  186. "}",
  187. format("void f() {\n"
  188. "\n"
  189. " if (a) {\n"
  190. "\n"
  191. " f();\n"
  192. "\n"
  193. " }\n"
  194. "\n"
  195. "}",
  196. getLLVMStyle()));
  197. EXPECT_EQ("void f() {\n"
  198. " if (a) {\n"
  199. " f();\n"
  200. " }\n"
  201. "}",
  202. format("void f() {\n"
  203. "\n"
  204. " if (a) {\n"
  205. "\n"
  206. " f();\n"
  207. "\n"
  208. " }\n"
  209. "\n"
  210. "}",
  211. getGoogleStyle()));
  212. // Don't remove empty lines in more complex control statements.
  213. EXPECT_EQ("void f() {\n"
  214. " if (a) {\n"
  215. " f();\n"
  216. "\n"
  217. " } else if (b) {\n"
  218. " f();\n"
  219. " }\n"
  220. "}",
  221. format("void f() {\n"
  222. " if (a) {\n"
  223. " f();\n"
  224. "\n"
  225. " } else if (b) {\n"
  226. " f();\n"
  227. "\n"
  228. " }\n"
  229. "\n"
  230. "}"));
  231. // FIXME: This is slightly inconsistent.
  232. EXPECT_EQ("namespace {\n"
  233. "int i;\n"
  234. "}",
  235. format("namespace {\n"
  236. "int i;\n"
  237. "\n"
  238. "}"));
  239. EXPECT_EQ("namespace {\n"
  240. "int i;\n"
  241. "\n"
  242. "} // namespace",
  243. format("namespace {\n"
  244. "int i;\n"
  245. "\n"
  246. "} // namespace"));
  247. FormatStyle Style = getLLVMStyle();
  248. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  249. Style.MaxEmptyLinesToKeep = 2;
  250. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  251. Style.BraceWrapping.AfterClass = true;
  252. Style.BraceWrapping.AfterFunction = true;
  253. Style.KeepEmptyLinesAtTheStartOfBlocks = false;
  254. EXPECT_EQ("class Foo\n"
  255. "{\n"
  256. " Foo() {}\n"
  257. "\n"
  258. " void funk() {}\n"
  259. "};",
  260. format("class Foo\n"
  261. "{\n"
  262. " Foo()\n"
  263. " {\n"
  264. " }\n"
  265. "\n"
  266. " void funk() {}\n"
  267. "};",
  268. Style));
  269. }
  270. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  271. verifyFormat("x = (a) and (b);");
  272. verifyFormat("x = (a) or (b);");
  273. verifyFormat("x = (a) bitand (b);");
  274. verifyFormat("x = (a) bitor (b);");
  275. verifyFormat("x = (a) not_eq (b);");
  276. verifyFormat("x = (a) and_eq (b);");
  277. verifyFormat("x = (a) or_eq (b);");
  278. verifyFormat("x = (a) xor (b);");
  279. }
  280. //===----------------------------------------------------------------------===//
  281. // Tests for control statements.
  282. //===----------------------------------------------------------------------===//
  283. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  284. verifyFormat("if (true)\n f();\ng();");
  285. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  286. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  287. FormatStyle AllowsMergedIf = getLLVMStyle();
  288. AllowsMergedIf.AlignEscapedNewlinesLeft = true;
  289. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  290. verifyFormat("if (a)\n"
  291. " // comment\n"
  292. " f();",
  293. AllowsMergedIf);
  294. verifyFormat("{\n"
  295. " if (a)\n"
  296. " label:\n"
  297. " f();\n"
  298. "}",
  299. AllowsMergedIf);
  300. verifyFormat("#define A \\\n"
  301. " if (a) \\\n"
  302. " label: \\\n"
  303. " f()",
  304. AllowsMergedIf);
  305. verifyFormat("if (a)\n"
  306. " ;",
  307. AllowsMergedIf);
  308. verifyFormat("if (a)\n"
  309. " if (b) return;",
  310. AllowsMergedIf);
  311. verifyFormat("if (a) // Can't merge this\n"
  312. " f();\n",
  313. AllowsMergedIf);
  314. verifyFormat("if (a) /* still don't merge */\n"
  315. " f();",
  316. AllowsMergedIf);
  317. verifyFormat("if (a) { // Never merge this\n"
  318. " f();\n"
  319. "}",
  320. AllowsMergedIf);
  321. verifyFormat("if (a) { /* Never merge this */\n"
  322. " f();\n"
  323. "}",
  324. AllowsMergedIf);
  325. AllowsMergedIf.ColumnLimit = 14;
  326. verifyFormat("if (a) return;", AllowsMergedIf);
  327. verifyFormat("if (aaaaaaaaa)\n"
  328. " return;",
  329. AllowsMergedIf);
  330. AllowsMergedIf.ColumnLimit = 13;
  331. verifyFormat("if (a)\n return;", AllowsMergedIf);
  332. }
  333. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  334. FormatStyle AllowsMergedLoops = getLLVMStyle();
  335. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  336. verifyFormat("while (true) continue;", AllowsMergedLoops);
  337. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  338. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  339. verifyFormat("while (true)\n"
  340. " ;",
  341. AllowsMergedLoops);
  342. verifyFormat("for (;;)\n"
  343. " ;",
  344. AllowsMergedLoops);
  345. verifyFormat("for (;;)\n"
  346. " for (;;) continue;",
  347. AllowsMergedLoops);
  348. verifyFormat("for (;;) // Can't merge this\n"
  349. " continue;",
  350. AllowsMergedLoops);
  351. verifyFormat("for (;;) /* still don't merge */\n"
  352. " continue;",
  353. AllowsMergedLoops);
  354. }
  355. TEST_F(FormatTest, FormatShortBracedStatements) {
  356. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  357. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  358. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  359. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  360. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  361. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  363. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  364. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  365. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  366. verifyFormat("if (true) { //\n"
  367. " f();\n"
  368. "}",
  369. AllowSimpleBracedStatements);
  370. verifyFormat("if (true) {\n"
  371. " f();\n"
  372. " f();\n"
  373. "}",
  374. AllowSimpleBracedStatements);
  375. verifyFormat("if (true) {\n"
  376. " f();\n"
  377. "} else {\n"
  378. " f();\n"
  379. "}",
  380. AllowSimpleBracedStatements);
  381. verifyFormat("template <int> struct A2 {\n"
  382. " struct B {};\n"
  383. "};",
  384. AllowSimpleBracedStatements);
  385. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  386. verifyFormat("if (true) {\n"
  387. " f();\n"
  388. "}",
  389. AllowSimpleBracedStatements);
  390. verifyFormat("if (true) {\n"
  391. " f();\n"
  392. "} else {\n"
  393. " f();\n"
  394. "}",
  395. AllowSimpleBracedStatements);
  396. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  397. verifyFormat("while (true) {\n"
  398. " f();\n"
  399. "}",
  400. AllowSimpleBracedStatements);
  401. verifyFormat("for (;;) {\n"
  402. " f();\n"
  403. "}",
  404. AllowSimpleBracedStatements);
  405. }
  406. TEST_F(FormatTest, ParseIfElse) {
  407. verifyFormat("if (true)\n"
  408. " if (true)\n"
  409. " if (true)\n"
  410. " f();\n"
  411. " else\n"
  412. " g();\n"
  413. " else\n"
  414. " h();\n"
  415. "else\n"
  416. " i();");
  417. verifyFormat("if (true)\n"
  418. " if (true)\n"
  419. " if (true) {\n"
  420. " if (true)\n"
  421. " f();\n"
  422. " } else {\n"
  423. " g();\n"
  424. " }\n"
  425. " else\n"
  426. " h();\n"
  427. "else {\n"
  428. " i();\n"
  429. "}");
  430. verifyFormat("void f() {\n"
  431. " if (a) {\n"
  432. " } else {\n"
  433. " }\n"
  434. "}");
  435. }
  436. TEST_F(FormatTest, ElseIf) {
  437. verifyFormat("if (a) {\n} else if (b) {\n}");
  438. verifyFormat("if (a)\n"
  439. " f();\n"
  440. "else if (b)\n"
  441. " g();\n"
  442. "else\n"
  443. " h();");
  444. verifyFormat("if (a) {\n"
  445. " f();\n"
  446. "}\n"
  447. "// or else ..\n"
  448. "else {\n"
  449. " g()\n"
  450. "}");
  451. verifyFormat("if (a) {\n"
  452. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  453. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  454. "}");
  455. verifyFormat("if (a) {\n"
  456. "} else if (\n"
  457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  458. "}",
  459. getLLVMStyleWithColumns(62));
  460. }
  461. TEST_F(FormatTest, FormatsForLoop) {
  462. verifyFormat(
  463. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  464. " ++VeryVeryLongLoopVariable)\n"
  465. " ;");
  466. verifyFormat("for (;;)\n"
  467. " f();");
  468. verifyFormat("for (;;) {\n}");
  469. verifyFormat("for (;;) {\n"
  470. " f();\n"
  471. "}");
  472. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  473. verifyFormat(
  474. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  475. " E = UnwrappedLines.end();\n"
  476. " I != E; ++I) {\n}");
  477. verifyFormat(
  478. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  479. " ++IIIII) {\n}");
  480. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  481. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  482. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  483. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  484. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  485. " E = FD->getDeclsInPrototypeScope().end();\n"
  486. " I != E; ++I) {\n}");
  487. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  488. " I = Container.begin(),\n"
  489. " E = Container.end();\n"
  490. " I != E; ++I) {\n}",
  491. getLLVMStyleWithColumns(76));
  492. verifyFormat(
  493. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  494. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  495. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  496. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  497. " ++aaaaaaaaaaa) {\n}");
  498. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  499. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  500. " ++i) {\n}");
  501. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  502. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  503. "}");
  504. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  505. " aaaaaaaaaa);\n"
  506. " iter; ++iter) {\n"
  507. "}");
  508. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  509. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  510. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  511. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  512. FormatStyle NoBinPacking = getLLVMStyle();
  513. NoBinPacking.BinPackParameters = false;
  514. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  515. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  516. " aaaaaaaaaaaaaaaa,\n"
  517. " aaaaaaaaaaaaaaaa,\n"
  518. " aaaaaaaaaaaaaaaa);\n"
  519. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  520. "}",
  521. NoBinPacking);
  522. verifyFormat(
  523. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  524. " E = UnwrappedLines.end();\n"
  525. " I != E;\n"
  526. " ++I) {\n}",
  527. NoBinPacking);
  528. }
  529. TEST_F(FormatTest, RangeBasedForLoops) {
  530. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  531. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  532. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  533. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  534. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  535. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  536. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  537. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  538. }
  539. TEST_F(FormatTest, ForEachLoops) {
  540. verifyFormat("void f() {\n"
  541. " foreach (Item *item, itemlist) {}\n"
  542. " Q_FOREACH (Item *item, itemlist) {}\n"
  543. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  544. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  545. "}");
  546. // As function-like macros.
  547. verifyFormat("#define foreach(x, y)\n"
  548. "#define Q_FOREACH(x, y)\n"
  549. "#define BOOST_FOREACH(x, y)\n"
  550. "#define UNKNOWN_FOREACH(x, y)\n");
  551. // Not as function-like macros.
  552. verifyFormat("#define foreach (x, y)\n"
  553. "#define Q_FOREACH (x, y)\n"
  554. "#define BOOST_FOREACH (x, y)\n"
  555. "#define UNKNOWN_FOREACH (x, y)\n");
  556. }
  557. TEST_F(FormatTest, FormatsWhileLoop) {
  558. verifyFormat("while (true) {\n}");
  559. verifyFormat("while (true)\n"
  560. " f();");
  561. verifyFormat("while () {\n}");
  562. verifyFormat("while () {\n"
  563. " f();\n"
  564. "}");
  565. }
  566. TEST_F(FormatTest, FormatsDoWhile) {
  567. verifyFormat("do {\n"
  568. " do_something();\n"
  569. "} while (something());");
  570. verifyFormat("do\n"
  571. " do_something();\n"
  572. "while (something());");
  573. }
  574. TEST_F(FormatTest, FormatsSwitchStatement) {
  575. verifyFormat("switch (x) {\n"
  576. "case 1:\n"
  577. " f();\n"
  578. " break;\n"
  579. "case kFoo:\n"
  580. "case ns::kBar:\n"
  581. "case kBaz:\n"
  582. " break;\n"
  583. "default:\n"
  584. " g();\n"
  585. " break;\n"
  586. "}");
  587. verifyFormat("switch (x) {\n"
  588. "case 1: {\n"
  589. " f();\n"
  590. " break;\n"
  591. "}\n"
  592. "case 2: {\n"
  593. " break;\n"
  594. "}\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "case 1: {\n"
  598. " f();\n"
  599. " {\n"
  600. " g();\n"
  601. " h();\n"
  602. " }\n"
  603. " break;\n"
  604. "}\n"
  605. "}");
  606. verifyFormat("switch (x) {\n"
  607. "case 1: {\n"
  608. " f();\n"
  609. " if (foo) {\n"
  610. " g();\n"
  611. " h();\n"
  612. " }\n"
  613. " break;\n"
  614. "}\n"
  615. "}");
  616. verifyFormat("switch (x) {\n"
  617. "case 1: {\n"
  618. " f();\n"
  619. " g();\n"
  620. "} break;\n"
  621. "}");
  622. verifyFormat("switch (test)\n"
  623. " ;");
  624. verifyFormat("switch (x) {\n"
  625. "default: {\n"
  626. " // Do nothing.\n"
  627. "}\n"
  628. "}");
  629. verifyFormat("switch (x) {\n"
  630. "// comment\n"
  631. "// if 1, do f()\n"
  632. "case 1:\n"
  633. " f();\n"
  634. "}");
  635. verifyFormat("switch (x) {\n"
  636. "case 1:\n"
  637. " // Do amazing stuff\n"
  638. " {\n"
  639. " f();\n"
  640. " g();\n"
  641. " }\n"
  642. " break;\n"
  643. "}");
  644. verifyFormat("#define A \\\n"
  645. " switch (x) { \\\n"
  646. " case a: \\\n"
  647. " foo = b; \\\n"
  648. " }",
  649. getLLVMStyleWithColumns(20));
  650. verifyFormat("#define OPERATION_CASE(name) \\\n"
  651. " case OP_name: \\\n"
  652. " return operations::Operation##name\n",
  653. getLLVMStyleWithColumns(40));
  654. verifyFormat("switch (x) {\n"
  655. "case 1:;\n"
  656. "default:;\n"
  657. " int i;\n"
  658. "}");
  659. verifyGoogleFormat("switch (x) {\n"
  660. " case 1:\n"
  661. " f();\n"
  662. " break;\n"
  663. " case kFoo:\n"
  664. " case ns::kBar:\n"
  665. " case kBaz:\n"
  666. " break;\n"
  667. " default:\n"
  668. " g();\n"
  669. " break;\n"
  670. "}");
  671. verifyGoogleFormat("switch (x) {\n"
  672. " case 1: {\n"
  673. " f();\n"
  674. " break;\n"
  675. " }\n"
  676. "}");
  677. verifyGoogleFormat("switch (test)\n"
  678. " ;");
  679. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  680. " case OP_name: \\\n"
  681. " return operations::Operation##name\n");
  682. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  683. " // Get the correction operation class.\n"
  684. " switch (OpCode) {\n"
  685. " CASE(Add);\n"
  686. " CASE(Subtract);\n"
  687. " default:\n"
  688. " return operations::Unknown;\n"
  689. " }\n"
  690. "#undef OPERATION_CASE\n"
  691. "}");
  692. verifyFormat("DEBUG({\n"
  693. " switch (x) {\n"
  694. " case A:\n"
  695. " f();\n"
  696. " break;\n"
  697. " // On B:\n"
  698. " case B:\n"
  699. " g();\n"
  700. " break;\n"
  701. " }\n"
  702. "});");
  703. verifyFormat("switch (a) {\n"
  704. "case (b):\n"
  705. " return;\n"
  706. "}");
  707. verifyFormat("switch (a) {\n"
  708. "case some_namespace::\n"
  709. " some_constant:\n"
  710. " return;\n"
  711. "}",
  712. getLLVMStyleWithColumns(34));
  713. }
  714. TEST_F(FormatTest, CaseRanges) {
  715. verifyFormat("switch (x) {\n"
  716. "case 'A' ... 'Z':\n"
  717. "case 1 ... 5:\n"
  718. "case a ... b:\n"
  719. " break;\n"
  720. "}");
  721. }
  722. TEST_F(FormatTest, ShortCaseLabels) {
  723. FormatStyle Style = getLLVMStyle();
  724. Style.AllowShortCaseLabelsOnASingleLine = true;
  725. verifyFormat("switch (a) {\n"
  726. "case 1: x = 1; break;\n"
  727. "case 2: return;\n"
  728. "case 3:\n"
  729. "case 4:\n"
  730. "case 5: return;\n"
  731. "case 6: // comment\n"
  732. " return;\n"
  733. "case 7:\n"
  734. " // comment\n"
  735. " return;\n"
  736. "case 8:\n"
  737. " x = 8; // comment\n"
  738. " break;\n"
  739. "default: y = 1; break;\n"
  740. "}",
  741. Style);
  742. verifyFormat("switch (a) {\n"
  743. "#if FOO\n"
  744. "case 0: return 0;\n"
  745. "#endif\n"
  746. "}",
  747. Style);
  748. verifyFormat("switch (a) {\n"
  749. "case 1: {\n"
  750. "}\n"
  751. "case 2: {\n"
  752. " return;\n"
  753. "}\n"
  754. "case 3: {\n"
  755. " x = 1;\n"
  756. " return;\n"
  757. "}\n"
  758. "case 4:\n"
  759. " if (x)\n"
  760. " return;\n"
  761. "}",
  762. Style);
  763. Style.ColumnLimit = 21;
  764. verifyFormat("switch (a) {\n"
  765. "case 1: x = 1; break;\n"
  766. "case 2: return;\n"
  767. "case 3:\n"
  768. "case 4:\n"
  769. "case 5: return;\n"
  770. "default:\n"
  771. " y = 1;\n"
  772. " break;\n"
  773. "}",
  774. Style);
  775. }
  776. TEST_F(FormatTest, FormatsLabels) {
  777. verifyFormat("void f() {\n"
  778. " some_code();\n"
  779. "test_label:\n"
  780. " some_other_code();\n"
  781. " {\n"
  782. " some_more_code();\n"
  783. " another_label:\n"
  784. " some_more_code();\n"
  785. " }\n"
  786. "}");
  787. verifyFormat("{\n"
  788. " some_code();\n"
  789. "test_label:\n"
  790. " some_other_code();\n"
  791. "}");
  792. verifyFormat("{\n"
  793. " some_code();\n"
  794. "test_label:;\n"
  795. " int i = 0;\n"
  796. "}");
  797. }
  798. //===----------------------------------------------------------------------===//
  799. // Tests for classes, namespaces, etc.
  800. //===----------------------------------------------------------------------===//
  801. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  802. verifyFormat("class A {};");
  803. }
  804. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  805. verifyFormat("class A {\n"
  806. "public:\n"
  807. "public: // comment\n"
  808. "protected:\n"
  809. "private:\n"
  810. " void f() {}\n"
  811. "};");
  812. verifyGoogleFormat("class A {\n"
  813. " public:\n"
  814. " protected:\n"
  815. " private:\n"
  816. " void f() {}\n"
  817. "};");
  818. verifyFormat("class A {\n"
  819. "public slots:\n"
  820. " void f1() {}\n"
  821. "public Q_SLOTS:\n"
  822. " void f2() {}\n"
  823. "protected slots:\n"
  824. " void f3() {}\n"
  825. "protected Q_SLOTS:\n"
  826. " void f4() {}\n"
  827. "private slots:\n"
  828. " void f5() {}\n"
  829. "private Q_SLOTS:\n"
  830. " void f6() {}\n"
  831. "signals:\n"
  832. " void g1();\n"
  833. "Q_SIGNALS:\n"
  834. " void g2();\n"
  835. "};");
  836. // Don't interpret 'signals' the wrong way.
  837. verifyFormat("signals.set();");
  838. verifyFormat("for (Signals signals : f()) {\n}");
  839. verifyFormat("{\n"
  840. " signals.set(); // This needs indentation.\n"
  841. "}");
  842. verifyFormat("void f() {\n"
  843. "label:\n"
  844. " signals.baz();\n"
  845. "}");
  846. }
  847. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  848. EXPECT_EQ("class A {\n"
  849. "public:\n"
  850. " void f();\n"
  851. "\n"
  852. "private:\n"
  853. " void g() {}\n"
  854. " // test\n"
  855. "protected:\n"
  856. " int h;\n"
  857. "};",
  858. format("class A {\n"
  859. "public:\n"
  860. "void f();\n"
  861. "private:\n"
  862. "void g() {}\n"
  863. "// test\n"
  864. "protected:\n"
  865. "int h;\n"
  866. "};"));
  867. EXPECT_EQ("class A {\n"
  868. "protected:\n"
  869. "public:\n"
  870. " void f();\n"
  871. "};",
  872. format("class A {\n"
  873. "protected:\n"
  874. "\n"
  875. "public:\n"
  876. "\n"
  877. " void f();\n"
  878. "};"));
  879. // Even ensure proper spacing inside macros.
  880. EXPECT_EQ("#define B \\\n"
  881. " class A { \\\n"
  882. " protected: \\\n"
  883. " public: \\\n"
  884. " void f(); \\\n"
  885. " };",
  886. format("#define B \\\n"
  887. " class A { \\\n"
  888. " protected: \\\n"
  889. " \\\n"
  890. " public: \\\n"
  891. " \\\n"
  892. " void f(); \\\n"
  893. " };",
  894. getGoogleStyle()));
  895. // But don't remove empty lines after macros ending in access specifiers.
  896. EXPECT_EQ("#define A private:\n"
  897. "\n"
  898. "int i;",
  899. format("#define A private:\n"
  900. "\n"
  901. "int i;"));
  902. }
  903. TEST_F(FormatTest, FormatsClasses) {
  904. verifyFormat("class A : public B {};");
  905. verifyFormat("class A : public ::B {};");
  906. verifyFormat(
  907. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  908. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  909. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  910. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  911. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  912. verifyFormat(
  913. "class A : public B, public C, public D, public E, public F {};");
  914. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  915. " public C,\n"
  916. " public D,\n"
  917. " public E,\n"
  918. " public F,\n"
  919. " public G {};");
  920. verifyFormat("class\n"
  921. " ReallyReallyLongClassName {\n"
  922. " int i;\n"
  923. "};",
  924. getLLVMStyleWithColumns(32));
  925. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  926. " aaaaaaaaaaaaaaaa> {};");
  927. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  928. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  929. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  930. verifyFormat("template <class R, class C>\n"
  931. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  932. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  933. verifyFormat("class ::A::B {};");
  934. }
  935. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  936. verifyFormat("class A {\n} a, b;");
  937. verifyFormat("struct A {\n} a, b;");
  938. verifyFormat("union A {\n} a;");
  939. }
  940. TEST_F(FormatTest, FormatsEnum) {
  941. verifyFormat("enum {\n"
  942. " Zero,\n"
  943. " One = 1,\n"
  944. " Two = One + 1,\n"
  945. " Three = (One + Two),\n"
  946. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  947. " Five = (One, Two, Three, Four, 5)\n"
  948. "};");
  949. verifyGoogleFormat("enum {\n"
  950. " Zero,\n"
  951. " One = 1,\n"
  952. " Two = One + 1,\n"
  953. " Three = (One + Two),\n"
  954. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  955. " Five = (One, Two, Three, Four, 5)\n"
  956. "};");
  957. verifyFormat("enum Enum {};");
  958. verifyFormat("enum {};");
  959. verifyFormat("enum X E {} d;");
  960. verifyFormat("enum __attribute__((...)) E {} d;");
  961. verifyFormat("enum __declspec__((...)) E {} d;");
  962. verifyFormat("enum {\n"
  963. " Bar = Foo<int, int>::value\n"
  964. "};",
  965. getLLVMStyleWithColumns(30));
  966. verifyFormat("enum ShortEnum { A, B, C };");
  967. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  968. EXPECT_EQ("enum KeepEmptyLines {\n"
  969. " ONE,\n"
  970. "\n"
  971. " TWO,\n"
  972. "\n"
  973. " THREE\n"
  974. "}",
  975. format("enum KeepEmptyLines {\n"
  976. " ONE,\n"
  977. "\n"
  978. " TWO,\n"
  979. "\n"
  980. "\n"
  981. " THREE\n"
  982. "}"));
  983. verifyFormat("enum E { // comment\n"
  984. " ONE,\n"
  985. " TWO\n"
  986. "};\n"
  987. "int i;");
  988. // Not enums.
  989. verifyFormat("enum X f() {\n"
  990. " a();\n"
  991. " return 42;\n"
  992. "}");
  993. verifyFormat("enum X Type::f() {\n"
  994. " a();\n"
  995. " return 42;\n"
  996. "}");
  997. verifyFormat("enum ::X f() {\n"
  998. " a();\n"
  999. " return 42;\n"
  1000. "}");
  1001. verifyFormat("enum ns::X f() {\n"
  1002. " a();\n"
  1003. " return 42;\n"
  1004. "}");
  1005. }
  1006. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1007. verifyFormat("enum Type {\n"
  1008. " One = 0; // These semicolons should be commas.\n"
  1009. " Two = 1;\n"
  1010. "};");
  1011. verifyFormat("namespace n {\n"
  1012. "enum Type {\n"
  1013. " One,\n"
  1014. " Two, // missing };\n"
  1015. " int i;\n"
  1016. "}\n"
  1017. "void g() {}");
  1018. }
  1019. TEST_F(FormatTest, FormatsEnumStruct) {
  1020. verifyFormat("enum struct {\n"
  1021. " Zero,\n"
  1022. " One = 1,\n"
  1023. " Two = One + 1,\n"
  1024. " Three = (One + Two),\n"
  1025. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1026. " Five = (One, Two, Three, Four, 5)\n"
  1027. "};");
  1028. verifyFormat("enum struct Enum {};");
  1029. verifyFormat("enum struct {};");
  1030. verifyFormat("enum struct X E {} d;");
  1031. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1032. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1033. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1034. }
  1035. TEST_F(FormatTest, FormatsEnumClass) {
  1036. verifyFormat("enum class {\n"
  1037. " Zero,\n"
  1038. " One = 1,\n"
  1039. " Two = One + 1,\n"
  1040. " Three = (One + Two),\n"
  1041. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1042. " Five = (One, Two, Three, Four, 5)\n"
  1043. "};");
  1044. verifyFormat("enum class Enum {};");
  1045. verifyFormat("enum class {};");
  1046. verifyFormat("enum class X E {} d;");
  1047. verifyFormat("enum class __attribute__((...)) E {} d;");
  1048. verifyFormat("enum class __declspec__((...)) E {} d;");
  1049. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1050. }
  1051. TEST_F(FormatTest, FormatsEnumTypes) {
  1052. verifyFormat("enum X : int {\n"
  1053. " A, // Force multiple lines.\n"
  1054. " B\n"
  1055. "};");
  1056. verifyFormat("enum X : int { A, B };");
  1057. verifyFormat("enum X : std::uint32_t { A, B };");
  1058. }
  1059. TEST_F(FormatTest, FormatsNSEnums) {
  1060. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1061. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1062. " // Information about someDecentlyLongValue.\n"
  1063. " someDecentlyLongValue,\n"
  1064. " // Information about anotherDecentlyLongValue.\n"
  1065. " anotherDecentlyLongValue,\n"
  1066. " // Information about aThirdDecentlyLongValue.\n"
  1067. " aThirdDecentlyLongValue\n"
  1068. "};");
  1069. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1070. " a = 1,\n"
  1071. " b = 2,\n"
  1072. " c = 3,\n"
  1073. "};");
  1074. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1075. " a = 1,\n"
  1076. " b = 2,\n"
  1077. " c = 3,\n"
  1078. "};");
  1079. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  1080. " a = 1,\n"
  1081. " b = 2,\n"
  1082. " c = 3,\n"
  1083. "};");
  1084. }
  1085. TEST_F(FormatTest, FormatsBitfields) {
  1086. verifyFormat("struct Bitfields {\n"
  1087. " unsigned sClass : 8;\n"
  1088. " unsigned ValueKind : 2;\n"
  1089. "};");
  1090. verifyFormat("struct A {\n"
  1091. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1092. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1093. "};");
  1094. verifyFormat("struct MyStruct {\n"
  1095. " uchar data;\n"
  1096. " uchar : 8;\n"
  1097. " uchar : 8;\n"
  1098. " uchar other;\n"
  1099. "};");
  1100. }
  1101. TEST_F(FormatTest, FormatsNamespaces) {
  1102. verifyFormat("namespace some_namespace {\n"
  1103. "class A {};\n"
  1104. "void f() { f(); }\n"
  1105. "}");
  1106. verifyFormat("namespace {\n"
  1107. "class A {};\n"
  1108. "void f() { f(); }\n"
  1109. "}");
  1110. verifyFormat("inline namespace X {\n"
  1111. "class A {};\n"
  1112. "void f() { f(); }\n"
  1113. "}");
  1114. verifyFormat("using namespace some_namespace;\n"
  1115. "class A {};\n"
  1116. "void f() { f(); }");
  1117. // This code is more common than we thought; if we
  1118. // layout this correctly the semicolon will go into
  1119. // its own line, which is undesirable.
  1120. verifyFormat("namespace {};");
  1121. verifyFormat("namespace {\n"
  1122. "class A {};\n"
  1123. "};");
  1124. verifyFormat("namespace {\n"
  1125. "int SomeVariable = 0; // comment\n"
  1126. "} // namespace");
  1127. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1128. "#define HEADER_GUARD\n"
  1129. "namespace my_namespace {\n"
  1130. "int i;\n"
  1131. "} // my_namespace\n"
  1132. "#endif // HEADER_GUARD",
  1133. format("#ifndef HEADER_GUARD\n"
  1134. " #define HEADER_GUARD\n"
  1135. " namespace my_namespace {\n"
  1136. "int i;\n"
  1137. "} // my_namespace\n"
  1138. "#endif // HEADER_GUARD"));
  1139. EXPECT_EQ("namespace A::B {\n"
  1140. "class C {};\n"
  1141. "}",
  1142. format("namespace A::B {\n"
  1143. "class C {};\n"
  1144. "}"));
  1145. FormatStyle Style = getLLVMStyle();
  1146. Style.NamespaceIndentation = FormatStyle::NI_All;
  1147. EXPECT_EQ("namespace out {\n"
  1148. " int i;\n"
  1149. " namespace in {\n"
  1150. " int i;\n"
  1151. " } // namespace\n"
  1152. "} // namespace",
  1153. format("namespace out {\n"
  1154. "int i;\n"
  1155. "namespace in {\n"
  1156. "int i;\n"
  1157. "} // namespace\n"
  1158. "} // namespace",
  1159. Style));
  1160. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1161. EXPECT_EQ("namespace out {\n"
  1162. "int i;\n"
  1163. "namespace in {\n"
  1164. " int i;\n"
  1165. "} // namespace\n"
  1166. "} // namespace",
  1167. format("namespace out {\n"
  1168. "int i;\n"
  1169. "namespace in {\n"
  1170. "int i;\n"
  1171. "} // namespace\n"
  1172. "} // namespace",
  1173. Style));
  1174. }
  1175. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  1176. TEST_F(FormatTest, FormatsInlineASM) {
  1177. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  1178. verifyFormat("asm(\"nop\" ::: \"memory\");");
  1179. verifyFormat(
  1180. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  1181. " \"cpuid\\n\\t\"\n"
  1182. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  1183. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  1184. " : \"a\"(value));");
  1185. EXPECT_EQ(
  1186. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  1187. " __asm {\n"
  1188. " mov edx,[that] // vtable in edx\n"
  1189. " mov eax,methodIndex\n"
  1190. " call [edx][eax*4] // stdcall\n"
  1191. " }\n"
  1192. "}",
  1193. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  1194. " __asm {\n"
  1195. " mov edx,[that] // vtable in edx\n"
  1196. " mov eax,methodIndex\n"
  1197. " call [edx][eax*4] // stdcall\n"
  1198. " }\n"
  1199. "}"));
  1200. EXPECT_EQ("_asm {\n"
  1201. " xor eax, eax;\n"
  1202. " cpuid;\n"
  1203. "}",
  1204. format("_asm {\n"
  1205. " xor eax, eax;\n"
  1206. " cpuid;\n"
  1207. "}"));
  1208. verifyFormat("void function() {\n"
  1209. " // comment\n"
  1210. " asm(\"\");\n"
  1211. "}");
  1212. EXPECT_EQ("__asm {\n"
  1213. "}\n"
  1214. "int i;",
  1215. format("__asm {\n"
  1216. "}\n"
  1217. "int i;"));
  1218. }
  1219. TEST_F(FormatTest, FormatTryCatch) {
  1220. verifyFormat("try {\n"
  1221. " throw a * b;\n"
  1222. "} catch (int a) {\n"
  1223. " // Do nothing.\n"
  1224. "} catch (...) {\n"
  1225. " exit(42);\n"
  1226. "}");
  1227. // Function-level try statements.
  1228. verifyFormat("int f() try { return 4; } catch (...) {\n"
  1229. " return 5;\n"
  1230. "}");
  1231. verifyFormat("class A {\n"
  1232. " int a;\n"
  1233. " A() try : a(0) {\n"
  1234. " } catch (...) {\n"
  1235. " throw;\n"
  1236. " }\n"
  1237. "};\n");
  1238. // Incomplete try-catch blocks.
  1239. verifyIncompleteFormat("try {} catch (");
  1240. }
  1241. TEST_F(FormatTest, FormatSEHTryCatch) {
  1242. verifyFormat("__try {\n"
  1243. " int a = b * c;\n"
  1244. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  1245. " // Do nothing.\n"
  1246. "}");
  1247. verifyFormat("__try {\n"
  1248. " int a = b * c;\n"
  1249. "} __finally {\n"
  1250. " // Do nothing.\n"
  1251. "}");
  1252. verifyFormat("DEBUG({\n"
  1253. " __try {\n"
  1254. " } __finally {\n"
  1255. " }\n"
  1256. "});\n");
  1257. }
  1258. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  1259. verifyFormat("try {\n"
  1260. " f();\n"
  1261. "} catch {\n"
  1262. " g();\n"
  1263. "}");
  1264. verifyFormat("try {\n"
  1265. " f();\n"
  1266. "} catch (A a) MACRO(x) {\n"
  1267. " g();\n"
  1268. "} catch (B b) MACRO(x) {\n"
  1269. " g();\n"
  1270. "}");
  1271. }
  1272. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  1273. FormatStyle Style = getLLVMStyle();
  1274. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  1275. FormatStyle::BS_WebKit}) {
  1276. Style.BreakBeforeBraces = BraceStyle;
  1277. verifyFormat("try {\n"
  1278. " // something\n"
  1279. "} catch (...) {\n"
  1280. " // something\n"
  1281. "}",
  1282. Style);
  1283. }
  1284. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  1285. verifyFormat("try {\n"
  1286. " // something\n"
  1287. "}\n"
  1288. "catch (...) {\n"
  1289. " // something\n"
  1290. "}",
  1291. Style);
  1292. verifyFormat("__try {\n"
  1293. " // something\n"
  1294. "}\n"
  1295. "__finally {\n"
  1296. " // something\n"
  1297. "}",
  1298. Style);
  1299. verifyFormat("@try {\n"
  1300. " // something\n"
  1301. "}\n"
  1302. "@finally {\n"
  1303. " // something\n"
  1304. "}",
  1305. Style);
  1306. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  1307. verifyFormat("try\n"
  1308. "{\n"
  1309. " // something\n"
  1310. "}\n"
  1311. "catch (...)\n"
  1312. "{\n"
  1313. " // something\n"
  1314. "}",
  1315. Style);
  1316. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  1317. verifyFormat("try\n"
  1318. " {\n"
  1319. " // something\n"
  1320. " }\n"
  1321. "catch (...)\n"
  1322. " {\n"
  1323. " // something\n"
  1324. " }",
  1325. Style);
  1326. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  1327. Style.BraceWrapping.BeforeCatch = true;
  1328. verifyFormat("try {\n"
  1329. " // something\n"
  1330. "}\n"
  1331. "catch (...) {\n"
  1332. " // something\n"
  1333. "}",
  1334. Style);
  1335. }
  1336. TEST_F(FormatTest, StaticInitializers) {
  1337. verifyFormat("static SomeClass SC = {1, 'a'};");
  1338. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  1339. " 100000000, "
  1340. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  1341. // Here, everything other than the "}" would fit on a line.
  1342. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  1343. " 10000000000000000000000000};");
  1344. EXPECT_EQ("S s = {a,\n"
  1345. "\n"
  1346. " b};",
  1347. format("S s = {\n"
  1348. " a,\n"
  1349. "\n"
  1350. " b\n"
  1351. "};"));
  1352. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  1353. // line. However, the formatting looks a bit off and this probably doesn't
  1354. // happen often in practice.
  1355. verifyFormat("static int Variable[1] = {\n"
  1356. " {1000000000000000000000000000000000000}};",
  1357. getLLVMStyleWithColumns(40));
  1358. }
  1359. TEST_F(FormatTest, DesignatedInitializers) {
  1360. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  1361. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  1362. " .bbbbbbbbbb = 2,\n"
  1363. " .cccccccccc = 3,\n"
  1364. " .dddddddddd = 4,\n"
  1365. " .eeeeeeeeee = 5};");
  1366. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  1367. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  1368. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  1369. " .ccccccccccccccccccccccccccc = 3,\n"
  1370. " .ddddddddddddddddddddddddddd = 4,\n"
  1371. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  1372. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  1373. }
  1374. TEST_F(FormatTest, NestedStaticInitializers) {
  1375. verifyFormat("static A x = {{{}}};\n");
  1376. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  1377. " {init1, init2, init3, init4}}};",
  1378. getLLVMStyleWithColumns(50));
  1379. verifyFormat("somes Status::global_reps[3] = {\n"
  1380. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1381. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1382. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  1383. getLLVMStyleWithColumns(60));
  1384. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  1385. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1386. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1387. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  1388. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  1389. " {rect.fRight - rect.fLeft, rect.fBottom - "
  1390. "rect.fTop}};");
  1391. verifyFormat(
  1392. "SomeArrayOfSomeType a = {\n"
  1393. " {{1, 2, 3},\n"
  1394. " {1, 2, 3},\n"
  1395. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  1396. " 333333333333333333333333333333},\n"
  1397. " {1, 2, 3},\n"
  1398. " {1, 2, 3}}};");
  1399. verifyFormat(
  1400. "SomeArrayOfSomeType a = {\n"
  1401. " {{1, 2, 3}},\n"
  1402. " {{1, 2, 3}},\n"
  1403. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  1404. " 333333333333333333333333333333}},\n"
  1405. " {{1, 2, 3}},\n"
  1406. " {{1, 2, 3}}};");
  1407. verifyFormat("struct {\n"
  1408. " unsigned bit;\n"
  1409. " const char *const name;\n"
  1410. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  1411. " {kOsWin, \"Windows\"},\n"
  1412. " {kOsLinux, \"Linux\"},\n"
  1413. " {kOsCrOS, \"Chrome OS\"}};");
  1414. verifyFormat("struct {\n"
  1415. " unsigned bit;\n"
  1416. " const char *const name;\n"
  1417. "} kBitsToOs[] = {\n"
  1418. " {kOsMac, \"Mac\"},\n"
  1419. " {kOsWin, \"Windows\"},\n"
  1420. " {kOsLinux, \"Linux\"},\n"
  1421. " {kOsCrOS, \"Chrome OS\"},\n"
  1422. "};");
  1423. }
  1424. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  1425. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1426. " \\\n"
  1427. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  1428. }
  1429. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  1430. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  1431. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  1432. // Do break defaulted and deleted functions.
  1433. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  1434. " default;",
  1435. getLLVMStyleWithColumns(40));
  1436. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  1437. " delete;",
  1438. getLLVMStyleWithColumns(40));
  1439. }
  1440. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  1441. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  1442. getLLVMStyleWithColumns(40));
  1443. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  1444. getLLVMStyleWithColumns(40));
  1445. EXPECT_EQ("#define Q \\\n"
  1446. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  1447. " \"aaaaaaaa.cpp\"",
  1448. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  1449. getLLVMStyleWithColumns(40)));
  1450. }
  1451. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  1452. EXPECT_EQ("# 123 \"A string literal\"",
  1453. format(" # 123 \"A string literal\""));
  1454. }
  1455. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  1456. EXPECT_EQ("#;", format("#;"));
  1457. verifyFormat("#\n;\n;\n;");
  1458. }
  1459. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  1460. EXPECT_EQ("#line 42 \"test\"\n",
  1461. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  1462. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  1463. getLLVMStyleWithColumns(12)));
  1464. }
  1465. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  1466. EXPECT_EQ("#line 42 \"test\"",
  1467. format("# \\\n line \\\n 42 \\\n \"test\""));
  1468. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  1469. }
  1470. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  1471. verifyFormat("#define A \\x20");
  1472. verifyFormat("#define A \\ x20");
  1473. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  1474. verifyFormat("#define A ''");
  1475. verifyFormat("#define A ''qqq");
  1476. verifyFormat("#define A `qqq");
  1477. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  1478. EXPECT_EQ("const char *c = STRINGIFY(\n"
  1479. "\\na : b);",
  1480. format("const char * c = STRINGIFY(\n"
  1481. "\\na : b);"));
  1482. verifyFormat("a\r\\");
  1483. verifyFormat("a\v\\");
  1484. verifyFormat("a\f\\");
  1485. }
  1486. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  1487. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  1488. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  1489. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  1490. // FIXME: We never break before the macro name.
  1491. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  1492. verifyFormat("#define A A\n#define A A");
  1493. verifyFormat("#define A(X) A\n#define A A");
  1494. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  1495. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  1496. }
  1497. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  1498. EXPECT_EQ("// somecomment\n"
  1499. "#include \"a.h\"\n"
  1500. "#define A( \\\n"
  1501. " A, B)\n"
  1502. "#include \"b.h\"\n"
  1503. "// somecomment\n",
  1504. format(" // somecomment\n"
  1505. " #include \"a.h\"\n"
  1506. "#define A(A,\\\n"
  1507. " B)\n"
  1508. " #include \"b.h\"\n"
  1509. " // somecomment\n",
  1510. getLLVMStyleWithColumns(13)));
  1511. }
  1512. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  1513. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  1514. EXPECT_EQ("#define A \\\n"
  1515. " c; \\\n"
  1516. " e;\n"
  1517. "f;",
  1518. format("#define A c; e;\n"
  1519. "f;",
  1520. getLLVMStyleWithColumns(14)));
  1521. }
  1522. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  1523. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  1524. EXPECT_EQ("int x,\n"
  1525. "#define A\n"
  1526. " y;",
  1527. format("int x,\n#define A\ny;"));
  1528. }
  1529. TEST_F(FormatTest, HashInMacroDefinition) {
  1530. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  1531. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  1532. verifyFormat("#define A \\\n"
  1533. " { \\\n"
  1534. " f(#c); \\\n"
  1535. " }",
  1536. getLLVMStyleWithColumns(11));
  1537. verifyFormat("#define A(X) \\\n"
  1538. " void function##X()",
  1539. getLLVMStyleWithColumns(22));
  1540. verifyFormat("#define A(a, b, c) \\\n"
  1541. " void a##b##c()",
  1542. getLLVMStyleWithColumns(22));
  1543. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  1544. }
  1545. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  1546. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  1547. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  1548. }
  1549. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  1550. EXPECT_EQ("#define A b;", format("#define A \\\n"
  1551. " \\\n"
  1552. " b;",
  1553. getLLVMStyleWithColumns(25)));
  1554. EXPECT_EQ("#define A \\\n"
  1555. " \\\n"
  1556. " a; \\\n"
  1557. " b;",
  1558. format("#define A \\\n"
  1559. " \\\n"
  1560. " a; \\\n"
  1561. " b;",
  1562. getLLVMStyleWithColumns(11)));
  1563. EXPECT_EQ("#define A \\\n"
  1564. " a; \\\n"
  1565. " \\\n"
  1566. " b;",
  1567. format("#define A \\\n"
  1568. " a; \\\n"
  1569. " \\\n"
  1570. " b;",
  1571. getLLVMStyleWithColumns(11)));
  1572. }
  1573. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  1574. verifyIncompleteFormat("#define A :");
  1575. verifyFormat("#define SOMECASES \\\n"
  1576. " case 1: \\\n"
  1577. " case 2\n",
  1578. getLLVMStyleWithColumns(20));
  1579. verifyFormat("#define MACRO(a) \\\n"
  1580. " if (a) \\\n"
  1581. " f(); \\\n"
  1582. " else \\\n"
  1583. " g()",
  1584. getLLVMStyleWithColumns(18));
  1585. verifyFormat("#define A template <typename T>");
  1586. verifyIncompleteFormat("#define STR(x) #x\n"
  1587. "f(STR(this_is_a_string_literal{));");
  1588. verifyFormat("#pragma omp threadprivate( \\\n"
  1589. " y)), // expected-warning",
  1590. getLLVMStyleWithColumns(28));
  1591. verifyFormat("#d, = };");
  1592. verifyFormat("#if \"a");
  1593. verifyIncompleteFormat("({\n"
  1594. "#define b \\\n"
  1595. " } \\\n"
  1596. " a\n"
  1597. "a",
  1598. getLLVMStyleWithColumns(15));
  1599. verifyFormat("#define A \\\n"
  1600. " { \\\n"
  1601. " {\n"
  1602. "#define B \\\n"
  1603. " } \\\n"
  1604. " }",
  1605. getLLVMStyleWithColumns(15));
  1606. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  1607. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  1608. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  1609. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  1610. }
  1611. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  1612. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  1613. EXPECT_EQ("class A : public QObject {\n"
  1614. " Q_OBJECT\n"
  1615. "\n"
  1616. " A() {}\n"
  1617. "};",
  1618. format("class A : public QObject {\n"
  1619. " Q_OBJECT\n"
  1620. "\n"
  1621. " A() {\n}\n"
  1622. "} ;"));
  1623. EXPECT_EQ("MACRO\n"
  1624. "/*static*/ int i;",
  1625. format("MACRO\n"
  1626. " /*static*/ int i;"));
  1627. EXPECT_EQ("SOME_MACRO\n"
  1628. "namespace {\n"
  1629. "void f();\n"
  1630. "}",
  1631. format("SOME_MACRO\n"
  1632. " namespace {\n"
  1633. "void f( );\n"
  1634. "}"));
  1635. // Only if the identifier contains at least 5 characters.
  1636. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  1637. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  1638. // Only if everything is upper case.
  1639. EXPECT_EQ("class A : public QObject {\n"
  1640. " Q_Object A() {}\n"
  1641. "};",
  1642. format("class A : public QObject {\n"
  1643. " Q_Object\n"
  1644. " A() {\n}\n"
  1645. "} ;"));
  1646. // Only if the next line can actually start an unwrapped line.
  1647. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  1648. format("SOME_WEIRD_LOG_MACRO\n"
  1649. "<< SomeThing;"));
  1650. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  1651. "(n, buffers))\n",
  1652. getChromiumStyle(FormatStyle::LK_Cpp));
  1653. }
  1654. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  1655. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1656. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1657. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1658. "class X {};\n"
  1659. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1660. "int *createScopDetectionPass() { return 0; }",
  1661. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1662. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1663. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1664. " class X {};\n"
  1665. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1666. " int *createScopDetectionPass() { return 0; }"));
  1667. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  1668. // braces, so that inner block is indented one level more.
  1669. EXPECT_EQ("int q() {\n"
  1670. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1671. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1672. " IPC_END_MESSAGE_MAP()\n"
  1673. "}",
  1674. format("int q() {\n"
  1675. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1676. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1677. " IPC_END_MESSAGE_MAP()\n"
  1678. "}"));
  1679. // Same inside macros.
  1680. EXPECT_EQ("#define LIST(L) \\\n"
  1681. " L(A) \\\n"
  1682. " L(B) \\\n"
  1683. " L(C)",
  1684. format("#define LIST(L) \\\n"
  1685. " L(A) \\\n"
  1686. " L(B) \\\n"
  1687. " L(C)",
  1688. getGoogleStyle()));
  1689. // These must not be recognized as macros.
  1690. EXPECT_EQ("int q() {\n"
  1691. " f(x);\n"
  1692. " f(x) {}\n"
  1693. " f(x)->g();\n"
  1694. " f(x)->*g();\n"
  1695. " f(x).g();\n"
  1696. " f(x) = x;\n"
  1697. " f(x) += x;\n"
  1698. " f(x) -= x;\n"
  1699. " f(x) *= x;\n"
  1700. " f(x) /= x;\n"
  1701. " f(x) %= x;\n"
  1702. " f(x) &= x;\n"
  1703. " f(x) |= x;\n"
  1704. " f(x) ^= x;\n"
  1705. " f(x) >>= x;\n"
  1706. " f(x) <<= x;\n"
  1707. " f(x)[y].z();\n"
  1708. " LOG(INFO) << x;\n"
  1709. " ifstream(x) >> x;\n"
  1710. "}\n",
  1711. format("int q() {\n"
  1712. " f(x)\n;\n"
  1713. " f(x)\n {}\n"
  1714. " f(x)\n->g();\n"
  1715. " f(x)\n->*g();\n"
  1716. " f(x)\n.g();\n"
  1717. " f(x)\n = x;\n"
  1718. " f(x)\n += x;\n"
  1719. " f(x)\n -= x;\n"
  1720. " f(x)\n *= x;\n"
  1721. " f(x)\n /= x;\n"
  1722. " f(x)\n %= x;\n"
  1723. " f(x)\n &= x;\n"
  1724. " f(x)\n |= x;\n"
  1725. " f(x)\n ^= x;\n"
  1726. " f(x)\n >>= x;\n"
  1727. " f(x)\n <<= x;\n"
  1728. " f(x)\n[y].z();\n"
  1729. " LOG(INFO)\n << x;\n"
  1730. " ifstream(x)\n >> x;\n"
  1731. "}\n"));
  1732. EXPECT_EQ("int q() {\n"
  1733. " F(x)\n"
  1734. " if (1) {\n"
  1735. " }\n"
  1736. " F(x)\n"
  1737. " while (1) {\n"
  1738. " }\n"
  1739. " F(x)\n"
  1740. " G(x);\n"
  1741. " F(x)\n"
  1742. " try {\n"
  1743. " Q();\n"
  1744. " } catch (...) {\n"
  1745. " }\n"
  1746. "}\n",
  1747. format("int q() {\n"
  1748. "F(x)\n"
  1749. "if (1) {}\n"
  1750. "F(x)\n"
  1751. "while (1) {}\n"
  1752. "F(x)\n"
  1753. "G(x);\n"
  1754. "F(x)\n"
  1755. "try { Q(); } catch (...) {}\n"
  1756. "}\n"));
  1757. EXPECT_EQ("class A {\n"
  1758. " A() : t(0) {}\n"
  1759. " A(int i) noexcept() : {}\n"
  1760. " A(X x)\n" // FIXME: function-level try blocks are broken.
  1761. " try : t(0) {\n"
  1762. " } catch (...) {\n"
  1763. " }\n"
  1764. "};",
  1765. format("class A {\n"
  1766. " A()\n : t(0) {}\n"
  1767. " A(int i)\n noexcept() : {}\n"
  1768. " A(X x)\n"
  1769. " try : t(0) {} catch (...) {}\n"
  1770. "};"));
  1771. EXPECT_EQ("class SomeClass {\n"
  1772. "public:\n"
  1773. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  1774. "};",
  1775. format("class SomeClass {\n"
  1776. "public:\n"
  1777. " SomeClass()\n"
  1778. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  1779. "};"));
  1780. EXPECT_EQ("class SomeClass {\n"
  1781. "public:\n"
  1782. " SomeClass()\n"
  1783. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  1784. "};",
  1785. format("class SomeClass {\n"
  1786. "public:\n"
  1787. " SomeClass()\n"
  1788. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  1789. "};",
  1790. getLLVMStyleWithColumns(40)));
  1791. verifyFormat("MACRO(>)");
  1792. }
  1793. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  1794. verifyFormat("#define A \\\n"
  1795. " f({ \\\n"
  1796. " g(); \\\n"
  1797. " });",
  1798. getLLVMStyleWithColumns(11));
  1799. }
  1800. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  1801. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  1802. }
  1803. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  1804. verifyFormat("{\n { a #c; }\n}");
  1805. }
  1806. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  1807. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  1808. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  1809. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  1810. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  1811. }
  1812. TEST_F(FormatTest, EscapedNewlines) {
  1813. EXPECT_EQ(
  1814. "#define A \\\n int i; \\\n int j;",
  1815. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  1816. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  1817. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  1818. EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */"));
  1819. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  1820. }
  1821. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  1822. verifyFormat("#define A \\\n"
  1823. " int v( \\\n"
  1824. " a); \\\n"
  1825. " int i;",
  1826. getLLVMStyleWithColumns(11));
  1827. }
  1828. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  1829. EXPECT_EQ(
  1830. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1831. " \\\n"
  1832. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1833. "\n"
  1834. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1835. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  1836. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1837. "\\\n"
  1838. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1839. " \n"
  1840. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1841. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  1842. }
  1843. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  1844. EXPECT_EQ("int\n"
  1845. "#define A\n"
  1846. " a;",
  1847. format("int\n#define A\na;"));
  1848. verifyFormat("functionCallTo(\n"
  1849. " someOtherFunction(\n"
  1850. " withSomeParameters, whichInSequence,\n"
  1851. " areLongerThanALine(andAnotherCall,\n"
  1852. "#define A B\n"
  1853. " withMoreParamters,\n"
  1854. " whichStronglyInfluenceTheLayout),\n"
  1855. " andMoreParameters),\n"
  1856. " trailing);",
  1857. getLLVMStyleWithColumns(69));
  1858. verifyFormat("Foo::Foo()\n"
  1859. "#ifdef BAR\n"
  1860. " : baz(0)\n"
  1861. "#endif\n"
  1862. "{\n"
  1863. "}");
  1864. verifyFormat("void f() {\n"
  1865. " if (true)\n"
  1866. "#ifdef A\n"
  1867. " f(42);\n"
  1868. " x();\n"
  1869. "#else\n"
  1870. " g();\n"
  1871. " x();\n"
  1872. "#endif\n"
  1873. "}");
  1874. verifyFormat("void f(param1, param2,\n"
  1875. " param3,\n"
  1876. "#ifdef A\n"
  1877. " param4(param5,\n"
  1878. "#ifdef A1\n"
  1879. " param6,\n"
  1880. "#ifdef A2\n"
  1881. " param7),\n"
  1882. "#else\n"
  1883. " param8),\n"
  1884. " param9,\n"
  1885. "#endif\n"
  1886. " param10,\n"
  1887. "#endif\n"
  1888. " param11)\n"
  1889. "#else\n"
  1890. " param12)\n"
  1891. "#endif\n"
  1892. "{\n"
  1893. " x();\n"
  1894. "}",
  1895. getLLVMStyleWithColumns(28));
  1896. verifyFormat("#if 1\n"
  1897. "int i;");
  1898. verifyFormat("#if 1\n"
  1899. "#endif\n"
  1900. "#if 1\n"
  1901. "#else\n"
  1902. "#endif\n");
  1903. verifyFormat("DEBUG({\n"
  1904. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1905. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  1906. "});\n"
  1907. "#if a\n"
  1908. "#else\n"
  1909. "#endif");
  1910. verifyIncompleteFormat("void f(\n"
  1911. "#if A\n"
  1912. " );\n"
  1913. "#else\n"
  1914. "#endif");
  1915. }
  1916. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  1917. verifyFormat("#endif\n"
  1918. "#if B");
  1919. }
  1920. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  1921. FormatStyle SingleLine = getLLVMStyle();
  1922. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  1923. verifyFormat("#if 0\n"
  1924. "#elif 1\n"
  1925. "#endif\n"
  1926. "void foo() {\n"
  1927. " if (test) foo2();\n"
  1928. "}",
  1929. SingleLine);
  1930. }
  1931. TEST_F(FormatTest, LayoutBlockInsideParens) {
  1932. verifyFormat("functionCall({ int i; });");
  1933. verifyFormat("functionCall({\n"
  1934. " int i;\n"
  1935. " int j;\n"
  1936. "});");
  1937. verifyFormat("functionCall(\n"
  1938. " {\n"
  1939. " int i;\n"
  1940. " int j;\n"
  1941. " },\n"
  1942. " aaaa, bbbb, cccc);");
  1943. verifyFormat("functionA(functionB({\n"
  1944. " int i;\n"
  1945. " int j;\n"
  1946. " }),\n"
  1947. " aaaa, bbbb, cccc);");
  1948. verifyFormat("functionCall(\n"
  1949. " {\n"
  1950. " int i;\n"
  1951. " int j;\n"
  1952. " },\n"
  1953. " aaaa, bbbb, // comment\n"
  1954. " cccc);");
  1955. verifyFormat("functionA(functionB({\n"
  1956. " int i;\n"
  1957. " int j;\n"
  1958. " }),\n"
  1959. " aaaa, bbbb, // comment\n"
  1960. " cccc);");
  1961. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  1962. verifyFormat("functionCall(aaaa, bbbb, {\n"
  1963. " int i;\n"
  1964. " int j;\n"
  1965. "});");
  1966. verifyFormat(
  1967. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  1968. " {\n"
  1969. " int i; // break\n"
  1970. " },\n"
  1971. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  1972. " ccccccccccccccccc));");
  1973. verifyFormat("DEBUG({\n"
  1974. " if (a)\n"
  1975. " f();\n"
  1976. "});");
  1977. }
  1978. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  1979. EXPECT_EQ("SOME_MACRO { int i; }\n"
  1980. "int i;",
  1981. format(" SOME_MACRO {int i;} int i;"));
  1982. }
  1983. TEST_F(FormatTest, LayoutNestedBlocks) {
  1984. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  1985. " struct s {\n"
  1986. " int i;\n"
  1987. " };\n"
  1988. " s kBitsToOs[] = {{10}};\n"
  1989. " for (int i = 0; i < 10; ++i)\n"
  1990. " return;\n"
  1991. "}");
  1992. verifyFormat("call(parameter, {\n"
  1993. " something();\n"
  1994. " // Comment using all columns.\n"
  1995. " somethingelse();\n"
  1996. "});",
  1997. getLLVMStyleWithColumns(40));
  1998. verifyFormat("DEBUG( //\n"
  1999. " { f(); }, a);");
  2000. verifyFormat("DEBUG( //\n"
  2001. " {\n"
  2002. " f(); //\n"
  2003. " },\n"
  2004. " a);");
  2005. EXPECT_EQ("call(parameter, {\n"
  2006. " something();\n"
  2007. " // Comment too\n"
  2008. " // looooooooooong.\n"
  2009. " somethingElse();\n"
  2010. "});",
  2011. format("call(parameter, {\n"
  2012. " something();\n"
  2013. " // Comment too looooooooooong.\n"
  2014. " somethingElse();\n"
  2015. "});",
  2016. getLLVMStyleWithColumns(29)));
  2017. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2018. EXPECT_EQ("DEBUG({ // comment\n"
  2019. " int i;\n"
  2020. "});",
  2021. format("DEBUG({ // comment\n"
  2022. "int i;\n"
  2023. "});"));
  2024. EXPECT_EQ("DEBUG({\n"
  2025. " int i;\n"
  2026. "\n"
  2027. " // comment\n"
  2028. " int j;\n"
  2029. "});",
  2030. format("DEBUG({\n"
  2031. " int i;\n"
  2032. "\n"
  2033. " // comment\n"
  2034. " int j;\n"
  2035. "});"));
  2036. verifyFormat("DEBUG({\n"
  2037. " if (a)\n"
  2038. " return;\n"
  2039. "});");
  2040. verifyGoogleFormat("DEBUG({\n"
  2041. " if (a) return;\n"
  2042. "});");
  2043. FormatStyle Style = getGoogleStyle();
  2044. Style.ColumnLimit = 45;
  2045. verifyFormat("Debug(aaaaa,\n"
  2046. " {\n"
  2047. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2048. " },\n"
  2049. " a);",
  2050. Style);
  2051. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  2052. verifyNoCrash("^{v^{a}}");
  2053. }
  2054. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  2055. EXPECT_EQ("#define MACRO() \\\n"
  2056. " Debug(aaa, /* force line break */ \\\n"
  2057. " { \\\n"
  2058. " int i; \\\n"
  2059. " int j; \\\n"
  2060. " })",
  2061. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  2062. " { int i; int j; })",
  2063. getGoogleStyle()));
  2064. EXPECT_EQ("#define A \\\n"
  2065. " [] { \\\n"
  2066. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  2067. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  2068. " }",
  2069. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  2070. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  2071. getGoogleStyle()));
  2072. }
  2073. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2074. EXPECT_EQ("{}", format("{}"));
  2075. verifyFormat("enum E {};");
  2076. verifyFormat("enum E {}");
  2077. }
  2078. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  2079. FormatStyle Style = getLLVMStyle();
  2080. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  2081. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  2082. verifyFormat("FOO_BEGIN\n"
  2083. " FOO_ENTRY\n"
  2084. "FOO_END", Style);
  2085. verifyFormat("FOO_BEGIN\n"
  2086. " NESTED_FOO_BEGIN\n"
  2087. " NESTED_FOO_ENTRY\n"
  2088. " NESTED_FOO_END\n"
  2089. "FOO_END", Style);
  2090. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  2091. " int x;\n"
  2092. " x = 1;\n"
  2093. "FOO_END(Baz)", Style);
  2094. }
  2095. //===----------------------------------------------------------------------===//
  2096. // Line break tests.
  2097. //===----------------------------------------------------------------------===//
  2098. TEST_F(FormatTest, PreventConfusingIndents) {
  2099. verifyFormat(
  2100. "void f() {\n"
  2101. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2102. " parameter, parameter, parameter)),\n"
  2103. " SecondLongCall(parameter));\n"
  2104. "}");
  2105. verifyFormat(
  2106. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2107. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2108. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2109. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2110. verifyFormat(
  2111. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2112. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2113. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2114. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2115. verifyFormat(
  2116. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2117. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2118. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2119. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2120. verifyFormat("int a = bbbb && ccc &&\n"
  2121. " fffff(\n"
  2122. "#define A Just forcing a new line\n"
  2123. " ddd);");
  2124. }
  2125. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2126. verifyFormat(
  2127. "bool aaaaaaa =\n"
  2128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2129. " bbbbbbbb();");
  2130. verifyFormat(
  2131. "bool aaaaaaa =\n"
  2132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  2133. " bbbbbbbb();");
  2134. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2136. " ccccccccc == ddddddddddd;");
  2137. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  2139. " ccccccccc == ddddddddddd;");
  2140. verifyFormat(
  2141. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2142. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  2143. " ccccccccc == ddddddddddd;");
  2144. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2145. " aaaaaa) &&\n"
  2146. " bbbbbb && cccccc;");
  2147. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2148. " aaaaaa) >>\n"
  2149. " bbbbbb;");
  2150. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  2151. " SourceMgr.getSpellingColumnNumber(\n"
  2152. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2153. " 1);");
  2154. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2155. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2156. " cccccc) {\n}");
  2157. verifyFormat("b = a &&\n"
  2158. " // Comment\n"
  2159. " b.c && d;");
  2160. // If the LHS of a comparison is not a binary expression itself, the
  2161. // additional linebreak confuses many people.
  2162. verifyFormat(
  2163. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2164. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2165. "}");
  2166. verifyFormat(
  2167. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2168. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2169. "}");
  2170. verifyFormat(
  2171. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2172. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2173. "}");
  2174. // Even explicit parentheses stress the precedence enough to make the
  2175. // additional break unnecessary.
  2176. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2177. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2178. "}");
  2179. // This cases is borderline, but with the indentation it is still readable.
  2180. verifyFormat(
  2181. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2182. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2183. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2184. "}",
  2185. getLLVMStyleWithColumns(75));
  2186. // If the LHS is a binary expression, we should still use the additional break
  2187. // as otherwise the formatting hides the operator precedence.
  2188. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2189. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2190. " 5) {\n"
  2191. "}");
  2192. FormatStyle OnePerLine = getLLVMStyle();
  2193. OnePerLine.BinPackParameters = false;
  2194. verifyFormat(
  2195. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2196. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2198. OnePerLine);
  2199. verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
  2200. " .aaa(aaaaaaaaaaaaa) *\n"
  2201. " aaaaaaa +\n"
  2202. " aaaaaaa;",
  2203. getLLVMStyleWithColumns(40));
  2204. }
  2205. TEST_F(FormatTest, ExpressionIndentation) {
  2206. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2207. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2210. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2211. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2212. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  2214. " ccccccccccccccccccccccccccccccccccccccccc;");
  2215. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2216. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2217. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2218. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2219. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2220. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2221. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2222. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2223. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2224. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2225. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2226. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2227. verifyFormat("if () {\n"
  2228. "} else if (aaaaa && bbbbb > // break\n"
  2229. " ccccc) {\n"
  2230. "}");
  2231. verifyFormat("if () {\n"
  2232. "} else if (aaaaa &&\n"
  2233. " bbbbb > // break\n"
  2234. " ccccc &&\n"
  2235. " ddddd) {\n"
  2236. "}");
  2237. // Presence of a trailing comment used to change indentation of b.
  2238. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  2239. " b;\n"
  2240. "return aaaaaaaaaaaaaaaaaaa +\n"
  2241. " b; //",
  2242. getLLVMStyleWithColumns(30));
  2243. }
  2244. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  2245. // Not sure what the best system is here. Like this, the LHS can be found
  2246. // immediately above an operator (everything with the same or a higher
  2247. // indent). The RHS is aligned right of the operator and so compasses
  2248. // everything until something with the same indent as the operator is found.
  2249. // FIXME: Is this a good system?
  2250. FormatStyle Style = getLLVMStyle();
  2251. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  2252. verifyFormat(
  2253. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2254. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2255. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2256. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2257. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2258. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2259. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2260. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2261. " > ccccccccccccccccccccccccccccccccccccccccc;",
  2262. Style);
  2263. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2264. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2265. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2266. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2267. Style);
  2268. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2269. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2270. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2271. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2272. Style);
  2273. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2274. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2275. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2276. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2277. Style);
  2278. verifyFormat("if () {\n"
  2279. "} else if (aaaaa\n"
  2280. " && bbbbb // break\n"
  2281. " > ccccc) {\n"
  2282. "}",
  2283. Style);
  2284. verifyFormat("return (a)\n"
  2285. " // comment\n"
  2286. " + b;",
  2287. Style);
  2288. verifyFormat(
  2289. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2290. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2291. " + cc;",
  2292. Style);
  2293. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2294. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  2295. Style);
  2296. // Forced by comments.
  2297. verifyFormat(
  2298. "unsigned ContentSize =\n"
  2299. " sizeof(int16_t) // DWARF ARange version number\n"
  2300. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  2301. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  2302. " + sizeof(int8_t); // Segment Size (in bytes)");
  2303. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  2304. " == boost::fusion::at_c<1>(iiii).second;",
  2305. Style);
  2306. Style.ColumnLimit = 60;
  2307. verifyFormat("zzzzzzzzzz\n"
  2308. " = bbbbbbbbbbbbbbbbb\n"
  2309. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2310. Style);
  2311. }
  2312. TEST_F(FormatTest, EnforcedOperatorWraps) {
  2313. // Here we'd like to wrap after the || operators, but a comment is forcing an
  2314. // earlier wrap.
  2315. verifyFormat("bool x = aaaaa //\n"
  2316. " || bbbbb\n"
  2317. " //\n"
  2318. " || cccc;");
  2319. }
  2320. TEST_F(FormatTest, NoOperandAlignment) {
  2321. FormatStyle Style = getLLVMStyle();
  2322. Style.AlignOperands = false;
  2323. verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
  2324. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2325. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2326. Style);
  2327. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  2328. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2329. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2330. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2331. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2332. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2333. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2334. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2335. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2336. " > ccccccccccccccccccccccccccccccccccccccccc;",
  2337. Style);
  2338. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2339. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2340. " + cc;",
  2341. Style);
  2342. verifyFormat("int a = aa\n"
  2343. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2344. " * cccccccccccccccccccccccccccccccccccc;\n",
  2345. Style);
  2346. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  2347. verifyFormat("return (a > b\n"
  2348. " // comment1\n"
  2349. " // comment2\n"
  2350. " || c);",
  2351. Style);
  2352. }
  2353. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  2354. FormatStyle Style = getLLVMStyle();
  2355. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  2356. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2357. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2358. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  2359. Style);
  2360. }
  2361. TEST_F(FormatTest, ConstructorInitializers) {
  2362. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  2363. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  2364. getLLVMStyleWithColumns(45));
  2365. verifyFormat("Constructor()\n"
  2366. " : Inttializer(FitsOnTheLine) {}",
  2367. getLLVMStyleWithColumns(44));
  2368. verifyFormat("Constructor()\n"
  2369. " : Inttializer(FitsOnTheLine) {}",
  2370. getLLVMStyleWithColumns(43));
  2371. verifyFormat("template <typename T>\n"
  2372. "Constructor() : Initializer(FitsOnTheLine) {}",
  2373. getLLVMStyleWithColumns(45));
  2374. verifyFormat(
  2375. "SomeClass::Constructor()\n"
  2376. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2377. verifyFormat(
  2378. "SomeClass::Constructor()\n"
  2379. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2380. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  2381. verifyFormat(
  2382. "SomeClass::Constructor()\n"
  2383. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2384. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2385. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2386. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2387. " : aaaaaaaaaa(aaaaaa) {}");
  2388. verifyFormat("Constructor()\n"
  2389. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2390. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2391. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2392. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  2393. verifyFormat("Constructor()\n"
  2394. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2395. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2396. verifyFormat("Constructor(int Parameter = 0)\n"
  2397. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  2398. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  2399. verifyFormat("Constructor()\n"
  2400. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  2401. "}",
  2402. getLLVMStyleWithColumns(60));
  2403. verifyFormat("Constructor()\n"
  2404. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2405. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  2406. // Here a line could be saved by splitting the second initializer onto two
  2407. // lines, but that is not desirable.
  2408. verifyFormat("Constructor()\n"
  2409. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2410. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  2411. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2412. FormatStyle OnePerLine = getLLVMStyle();
  2413. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2414. OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
  2415. verifyFormat("SomeClass::Constructor()\n"
  2416. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2417. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2418. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2419. OnePerLine);
  2420. verifyFormat("SomeClass::Constructor()\n"
  2421. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  2422. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2423. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2424. OnePerLine);
  2425. verifyFormat("MyClass::MyClass(int var)\n"
  2426. " : some_var_(var), // 4 space indent\n"
  2427. " some_other_var_(var + 1) { // lined up\n"
  2428. "}",
  2429. OnePerLine);
  2430. verifyFormat("Constructor()\n"
  2431. " : aaaaa(aaaaaa),\n"
  2432. " aaaaa(aaaaaa),\n"
  2433. " aaaaa(aaaaaa),\n"
  2434. " aaaaa(aaaaaa),\n"
  2435. " aaaaa(aaaaaa) {}",
  2436. OnePerLine);
  2437. verifyFormat("Constructor()\n"
  2438. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  2439. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  2440. OnePerLine);
  2441. OnePerLine.BinPackParameters = false;
  2442. verifyFormat(
  2443. "Constructor()\n"
  2444. " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2445. " aaaaaaaaaaa().aaa(),\n"
  2446. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  2447. OnePerLine);
  2448. OnePerLine.ColumnLimit = 60;
  2449. verifyFormat("Constructor()\n"
  2450. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  2451. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  2452. OnePerLine);
  2453. EXPECT_EQ("Constructor()\n"
  2454. " : // Comment forcing unwanted break.\n"
  2455. " aaaa(aaaa) {}",
  2456. format("Constructor() :\n"
  2457. " // Comment forcing unwanted break.\n"
  2458. " aaaa(aaaa) {}"));
  2459. }
  2460. TEST_F(FormatTest, MemoizationTests) {
  2461. // This breaks if the memoization lookup does not take \c Indent and
  2462. // \c LastSpace into account.
  2463. verifyFormat(
  2464. "extern CFRunLoopTimerRef\n"
  2465. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  2466. " CFTimeInterval interval, CFOptionFlags flags,\n"
  2467. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  2468. " CFRunLoopTimerContext *context) {}");
  2469. // Deep nesting somewhat works around our memoization.
  2470. verifyFormat(
  2471. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2472. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2473. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2474. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2475. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  2476. getLLVMStyleWithColumns(65));
  2477. verifyFormat(
  2478. "aaaaa(\n"
  2479. " aaaaa,\n"
  2480. " aaaaa(\n"
  2481. " aaaaa,\n"
  2482. " aaaaa(\n"
  2483. " aaaaa,\n"
  2484. " aaaaa(\n"
  2485. " aaaaa,\n"
  2486. " aaaaa(\n"
  2487. " aaaaa,\n"
  2488. " aaaaa(\n"
  2489. " aaaaa,\n"
  2490. " aaaaa(\n"
  2491. " aaaaa,\n"
  2492. " aaaaa(\n"
  2493. " aaaaa,\n"
  2494. " aaaaa(\n"
  2495. " aaaaa,\n"
  2496. " aaaaa(\n"
  2497. " aaaaa,\n"
  2498. " aaaaa(\n"
  2499. " aaaaa,\n"
  2500. " aaaaa(\n"
  2501. " aaaaa,\n"
  2502. " aaaaa))))))))))));",
  2503. getLLVMStyleWithColumns(65));
  2504. verifyFormat(
  2505. "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"
  2506. " a),\n"
  2507. " a),\n"
  2508. " a),\n"
  2509. " a),\n"
  2510. " a),\n"
  2511. " a),\n"
  2512. " a),\n"
  2513. " a),\n"
  2514. " a),\n"
  2515. " a),\n"
  2516. " a),\n"
  2517. " a),\n"
  2518. " a),\n"
  2519. " a),\n"
  2520. " a),\n"
  2521. " a),\n"
  2522. " a)",
  2523. getLLVMStyleWithColumns(65));
  2524. // This test takes VERY long when memoization is broken.
  2525. FormatStyle OnePerLine = getLLVMStyle();
  2526. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2527. OnePerLine.BinPackParameters = false;
  2528. std::string input = "Constructor()\n"
  2529. " : aaaa(a,\n";
  2530. for (unsigned i = 0, e = 80; i != e; ++i) {
  2531. input += " a,\n";
  2532. }
  2533. input += " a) {}";
  2534. verifyFormat(input, OnePerLine);
  2535. }
  2536. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  2537. verifyFormat(
  2538. "void f() {\n"
  2539. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  2540. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  2541. " f();\n"
  2542. "}");
  2543. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  2544. " Intervals[i - 1].getRange().getLast()) {\n}");
  2545. }
  2546. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  2547. // Principially, we break function declarations in a certain order:
  2548. // 1) break amongst arguments.
  2549. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  2550. " Cccccccccccccc cccccccccccccc);");
  2551. verifyFormat("template <class TemplateIt>\n"
  2552. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  2553. " TemplateIt *stop) {}");
  2554. // 2) break after return type.
  2555. verifyFormat(
  2556. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2557. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  2558. getGoogleStyle());
  2559. // 3) break after (.
  2560. verifyFormat(
  2561. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  2562. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  2563. getGoogleStyle());
  2564. // 4) break before after nested name specifiers.
  2565. verifyFormat(
  2566. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2567. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  2568. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  2569. getGoogleStyle());
  2570. // However, there are exceptions, if a sufficient amount of lines can be
  2571. // saved.
  2572. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  2573. // more adjusting.
  2574. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2575. " Cccccccccccccc cccccccccc,\n"
  2576. " Cccccccccccccc cccccccccc,\n"
  2577. " Cccccccccccccc cccccccccc,\n"
  2578. " Cccccccccccccc cccccccccc);");
  2579. verifyFormat(
  2580. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2581. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2582. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2583. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  2584. getGoogleStyle());
  2585. verifyFormat(
  2586. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2587. " Cccccccccccccc cccccccccc,\n"
  2588. " Cccccccccccccc cccccccccc,\n"
  2589. " Cccccccccccccc cccccccccc,\n"
  2590. " Cccccccccccccc cccccccccc,\n"
  2591. " Cccccccccccccc cccccccccc,\n"
  2592. " Cccccccccccccc cccccccccc);");
  2593. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2594. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2595. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2596. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2597. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  2598. // Break after multi-line parameters.
  2599. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2600. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2601. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2602. " bbbb bbbb);");
  2603. verifyFormat("void SomeLoooooooooooongFunction(\n"
  2604. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  2605. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2606. " int bbbbbbbbbbbbb);");
  2607. // Treat overloaded operators like other functions.
  2608. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2609. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  2610. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2611. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  2612. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2613. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  2614. verifyGoogleFormat(
  2615. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  2616. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2617. verifyGoogleFormat(
  2618. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  2619. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2620. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2621. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  2622. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  2623. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  2624. verifyGoogleFormat(
  2625. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  2626. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2627. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  2628. verifyGoogleFormat(
  2629. "template <typename T>\n"
  2630. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2631. "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
  2632. " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
  2633. FormatStyle Style = getLLVMStyle();
  2634. Style.PointerAlignment = FormatStyle::PAS_Left;
  2635. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2636. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  2637. Style);
  2638. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  2639. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  2640. Style);
  2641. }
  2642. TEST_F(FormatTest, TrailingReturnType) {
  2643. verifyFormat("auto foo() -> int;\n");
  2644. verifyFormat("struct S {\n"
  2645. " auto bar() const -> int;\n"
  2646. "};");
  2647. verifyFormat("template <size_t Order, typename T>\n"
  2648. "auto load_img(const std::string &filename)\n"
  2649. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  2650. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  2651. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  2652. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  2653. verifyFormat("template <typename T>\n"
  2654. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  2655. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  2656. // Not trailing return types.
  2657. verifyFormat("void f() { auto a = b->c(); }");
  2658. }
  2659. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  2660. // Avoid breaking before trailing 'const' or other trailing annotations, if
  2661. // they are not function-like.
  2662. FormatStyle Style = getGoogleStyle();
  2663. Style.ColumnLimit = 47;
  2664. verifyFormat("void someLongFunction(\n"
  2665. " int someLoooooooooooooongParameter) const {\n}",
  2666. getLLVMStyleWithColumns(47));
  2667. verifyFormat("LoooooongReturnType\n"
  2668. "someLoooooooongFunction() const {}",
  2669. getLLVMStyleWithColumns(47));
  2670. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  2671. " const {}",
  2672. Style);
  2673. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  2674. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  2675. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  2676. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  2677. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  2678. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  2679. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  2680. " aaaaaaaaaaa aaaaa) const override;");
  2681. verifyGoogleFormat(
  2682. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  2683. " const override;");
  2684. // Even if the first parameter has to be wrapped.
  2685. verifyFormat("void someLongFunction(\n"
  2686. " int someLongParameter) const {}",
  2687. getLLVMStyleWithColumns(46));
  2688. verifyFormat("void someLongFunction(\n"
  2689. " int someLongParameter) const {}",
  2690. Style);
  2691. verifyFormat("void someLongFunction(\n"
  2692. " int someLongParameter) override {}",
  2693. Style);
  2694. verifyFormat("void someLongFunction(\n"
  2695. " int someLongParameter) OVERRIDE {}",
  2696. Style);
  2697. verifyFormat("void someLongFunction(\n"
  2698. " int someLongParameter) final {}",
  2699. Style);
  2700. verifyFormat("void someLongFunction(\n"
  2701. " int someLongParameter) FINAL {}",
  2702. Style);
  2703. verifyFormat("void someLongFunction(\n"
  2704. " int parameter) const override {}",
  2705. Style);
  2706. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2707. verifyFormat("void someLongFunction(\n"
  2708. " int someLongParameter) const\n"
  2709. "{\n"
  2710. "}",
  2711. Style);
  2712. // Unless these are unknown annotations.
  2713. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  2714. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2715. " LONG_AND_UGLY_ANNOTATION;");
  2716. // Breaking before function-like trailing annotations is fine to keep them
  2717. // close to their arguments.
  2718. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2719. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2720. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2721. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2722. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2723. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  2724. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  2725. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  2726. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  2727. verifyFormat(
  2728. "void aaaaaaaaaaaaaaaaaa()\n"
  2729. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2730. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  2731. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2732. " __attribute__((unused));");
  2733. verifyGoogleFormat(
  2734. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2735. " GUARDED_BY(aaaaaaaaaaaa);");
  2736. verifyGoogleFormat(
  2737. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2738. " GUARDED_BY(aaaaaaaaaaaa);");
  2739. verifyGoogleFormat(
  2740. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  2741. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2742. verifyGoogleFormat(
  2743. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  2744. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2745. }
  2746. TEST_F(FormatTest, FunctionAnnotations) {
  2747. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  2748. "int OldFunction(const string &parameter) {}");
  2749. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  2750. "string OldFunction(const string &parameter) {}");
  2751. verifyFormat("template <typename T>\n"
  2752. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  2753. "string OldFunction(const string &parameter) {}");
  2754. // Not function annotations.
  2755. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2756. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  2757. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  2758. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  2759. verifyFormat("MACRO(abc).function() // wrap\n"
  2760. " << abc;");
  2761. verifyFormat("MACRO(abc)->function() // wrap\n"
  2762. " << abc;");
  2763. verifyFormat("MACRO(abc)::function() // wrap\n"
  2764. " << abc;");
  2765. }
  2766. TEST_F(FormatTest, BreaksDesireably) {
  2767. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2768. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2769. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  2770. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2771. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  2772. "}");
  2773. verifyFormat(
  2774. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2775. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2776. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2777. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2778. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2779. verifyFormat(
  2780. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  2781. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2782. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2783. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2784. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  2785. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2786. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2787. verifyFormat(
  2788. "void f() {\n"
  2789. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  2790. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  2791. "}");
  2792. verifyFormat(
  2793. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2794. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2795. verifyFormat(
  2796. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2797. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2798. verifyFormat(
  2799. "aaaaaa(aaa,\n"
  2800. " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2801. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2802. " aaaa);");
  2803. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2804. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2805. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2806. // Indent consistently independent of call expression and unary operator.
  2807. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2808. " dddddddddddddddddddddddddddddd));");
  2809. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2810. " dddddddddddddddddddddddddddddd));");
  2811. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  2812. " dddddddddddddddddddddddddddddd));");
  2813. // This test case breaks on an incorrect memoization, i.e. an optimization not
  2814. // taking into account the StopAt value.
  2815. verifyFormat(
  2816. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2817. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2818. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2819. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2820. verifyFormat("{\n {\n {\n"
  2821. " Annotation.SpaceRequiredBefore =\n"
  2822. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  2823. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  2824. " }\n }\n}");
  2825. // Break on an outer level if there was a break on an inner level.
  2826. EXPECT_EQ("f(g(h(a, // comment\n"
  2827. " b, c),\n"
  2828. " d, e),\n"
  2829. " x, y);",
  2830. format("f(g(h(a, // comment\n"
  2831. " b, c), d, e), x, y);"));
  2832. // Prefer breaking similar line breaks.
  2833. verifyFormat(
  2834. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  2835. " NSTrackingMouseEnteredAndExited |\n"
  2836. " NSTrackingActiveAlways;");
  2837. }
  2838. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  2839. FormatStyle NoBinPacking = getGoogleStyle();
  2840. NoBinPacking.BinPackParameters = false;
  2841. NoBinPacking.BinPackArguments = true;
  2842. verifyFormat("void f() {\n"
  2843. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  2844. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  2845. "}",
  2846. NoBinPacking);
  2847. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  2848. " int aaaaaaaaaaaaaaaaaaaa,\n"
  2849. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  2850. NoBinPacking);
  2851. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  2852. verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2853. " vector<int> bbbbbbbbbbbbbbb);",
  2854. NoBinPacking);
  2855. // FIXME: This behavior difference is probably not wanted. However, currently
  2856. // we cannot distinguish BreakBeforeParameter being set because of the wrapped
  2857. // template arguments from BreakBeforeParameter being set because of the
  2858. // one-per-line formatting.
  2859. verifyFormat(
  2860. "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2861. " aaaaaaaaaa> aaaaaaaaaa);",
  2862. NoBinPacking);
  2863. verifyFormat(
  2864. "void fffffffffff(\n"
  2865. " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
  2866. " aaaaaaaaaa);");
  2867. }
  2868. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  2869. FormatStyle NoBinPacking = getGoogleStyle();
  2870. NoBinPacking.BinPackParameters = false;
  2871. NoBinPacking.BinPackArguments = false;
  2872. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  2873. " aaaaaaaaaaaaaaaaaaaa,\n"
  2874. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  2875. NoBinPacking);
  2876. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  2877. " aaaaaaaaaaaaa,\n"
  2878. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  2879. NoBinPacking);
  2880. verifyFormat(
  2881. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  2882. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2883. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2884. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2885. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  2886. NoBinPacking);
  2887. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2888. " .aaaaaaaaaaaaaaaaaa();",
  2889. NoBinPacking);
  2890. verifyFormat("void f() {\n"
  2891. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2892. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  2893. "}",
  2894. NoBinPacking);
  2895. verifyFormat(
  2896. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2897. " aaaaaaaaaaaa,\n"
  2898. " aaaaaaaaaaaa);",
  2899. NoBinPacking);
  2900. verifyFormat(
  2901. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  2902. " ddddddddddddddddddddddddddddd),\n"
  2903. " test);",
  2904. NoBinPacking);
  2905. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2906. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2907. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  2908. " aaaaaaaaaaaaaaaaaa;",
  2909. NoBinPacking);
  2910. verifyFormat("a(\"a\"\n"
  2911. " \"a\",\n"
  2912. " a);");
  2913. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  2914. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  2915. " aaaaaaaaa,\n"
  2916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2917. NoBinPacking);
  2918. verifyFormat(
  2919. "void f() {\n"
  2920. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2921. " .aaaaaaa();\n"
  2922. "}",
  2923. NoBinPacking);
  2924. verifyFormat(
  2925. "template <class SomeType, class SomeOtherType>\n"
  2926. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  2927. NoBinPacking);
  2928. }
  2929. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  2930. FormatStyle Style = getLLVMStyleWithColumns(15);
  2931. Style.ExperimentalAutoDetectBinPacking = true;
  2932. EXPECT_EQ("aaa(aaaa,\n"
  2933. " aaaa,\n"
  2934. " aaaa);\n"
  2935. "aaa(aaaa,\n"
  2936. " aaaa,\n"
  2937. " aaaa);",
  2938. format("aaa(aaaa,\n" // one-per-line
  2939. " aaaa,\n"
  2940. " aaaa );\n"
  2941. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2942. Style));
  2943. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  2944. " aaaa);\n"
  2945. "aaa(aaaa, aaaa,\n"
  2946. " aaaa);",
  2947. format("aaa(aaaa, aaaa,\n" // bin-packed
  2948. " aaaa );\n"
  2949. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2950. Style));
  2951. }
  2952. TEST_F(FormatTest, FormatsBuilderPattern) {
  2953. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  2954. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  2955. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  2956. " .StartsWith(\".init\", ORDER_INIT)\n"
  2957. " .StartsWith(\".fini\", ORDER_FINI)\n"
  2958. " .StartsWith(\".hash\", ORDER_HASH)\n"
  2959. " .Default(ORDER_TEXT);\n");
  2960. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  2961. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  2962. verifyFormat(
  2963. "aaaaaaa->aaaaaaa\n"
  2964. " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2965. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2966. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  2967. verifyFormat(
  2968. "aaaaaaa->aaaaaaa\n"
  2969. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2970. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  2971. verifyFormat(
  2972. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  2973. " aaaaaaaaaaaaaa);");
  2974. verifyFormat(
  2975. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  2976. " aaaaaa->aaaaaaaaaaaa()\n"
  2977. " ->aaaaaaaaaaaaaaaa(\n"
  2978. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2979. " ->aaaaaaaaaaaaaaaaa();");
  2980. verifyGoogleFormat(
  2981. "void f() {\n"
  2982. " someo->Add((new util::filetools::Handler(dir))\n"
  2983. " ->OnEvent1(NewPermanentCallback(\n"
  2984. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  2985. " ->OnEvent2(NewPermanentCallback(\n"
  2986. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  2987. " ->OnEvent3(NewPermanentCallback(\n"
  2988. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  2989. " ->OnEvent5(NewPermanentCallback(\n"
  2990. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  2991. " ->OnEvent6(NewPermanentCallback(\n"
  2992. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  2993. "}");
  2994. verifyFormat(
  2995. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  2996. verifyFormat("aaaaaaaaaaaaaaa()\n"
  2997. " .aaaaaaaaaaaaaaa()\n"
  2998. " .aaaaaaaaaaaaaaa()\n"
  2999. " .aaaaaaaaaaaaaaa()\n"
  3000. " .aaaaaaaaaaaaaaa();");
  3001. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3002. " .aaaaaaaaaaaaaaa()\n"
  3003. " .aaaaaaaaaaaaaaa()\n"
  3004. " .aaaaaaaaaaaaaaa();");
  3005. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3006. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3007. " .aaaaaaaaaaaaaaa();");
  3008. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3009. " ->aaaaaaaaaaaaaae(0)\n"
  3010. " ->aaaaaaaaaaaaaaa();");
  3011. // Don't linewrap after very short segments.
  3012. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3013. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3014. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3015. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3016. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3017. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3018. verifyFormat("aaa()\n"
  3019. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3020. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3021. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3022. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3023. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3024. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3025. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3026. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3027. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3028. // Prefer not to break after empty parentheses.
  3029. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3030. " First->LastNewlineOffset);");
  3031. // Prefer not to create "hanging" indents.
  3032. verifyFormat(
  3033. "return !soooooooooooooome_map\n"
  3034. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3035. " .second;");
  3036. verifyFormat(
  3037. "return aaaaaaaaaaaaaaaa\n"
  3038. " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
  3039. " .aaaa(aaaaaaaaaaaaaa);");
  3040. // No hanging indent here.
  3041. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
  3042. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3043. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
  3044. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3045. verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  3046. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3047. getLLVMStyleWithColumns(60));
  3048. verifyFormat("aaaaaaaaaaaaaaaaaa\n"
  3049. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  3050. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3051. getLLVMStyleWithColumns(59));
  3052. verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3053. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3054. " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3055. }
  3056. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3057. verifyFormat(
  3058. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3059. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3060. verifyFormat(
  3061. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3062. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3063. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3064. " ccccccccccccccccccccccccc) {\n}");
  3065. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3066. " ccccccccccccccccccccccccc) {\n}");
  3067. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3068. " ccccccccccccccccccccccccc) {\n}");
  3069. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3070. " ccccccccccccccccccccccccc) {\n}");
  3071. verifyFormat(
  3072. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3073. " ccccccccccccccccccccccccc) {\n}");
  3074. verifyFormat(
  3075. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3076. " ccccccccccccccccccccccccc) {\n}");
  3077. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3078. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3079. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3080. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3081. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3082. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3083. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3084. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3085. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3086. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3087. " aaaaaaaaaaaaaaa != aa) {\n}");
  3088. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3089. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3090. " aaaaaaaaaaaaaaa != aa) {\n}");
  3091. }
  3092. TEST_F(FormatTest, BreaksAfterAssignments) {
  3093. verifyFormat(
  3094. "unsigned Cost =\n"
  3095. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3096. " SI->getPointerAddressSpaceee());\n");
  3097. verifyFormat(
  3098. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3099. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3100. verifyFormat(
  3101. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3102. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3103. verifyFormat("unsigned OriginalStartColumn =\n"
  3104. " SourceMgr.getSpellingColumnNumber(\n"
  3105. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3106. " 1;");
  3107. }
  3108. TEST_F(FormatTest, AlignsAfterAssignments) {
  3109. verifyFormat(
  3110. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3111. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3112. verifyFormat(
  3113. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3114. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3115. verifyFormat(
  3116. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3117. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3118. verifyFormat(
  3119. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3120. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3121. verifyFormat(
  3122. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3123. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3124. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3125. }
  3126. TEST_F(FormatTest, AlignsAfterReturn) {
  3127. verifyFormat(
  3128. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3129. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3130. verifyFormat(
  3131. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3132. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3133. verifyFormat(
  3134. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3135. " aaaaaaaaaaaaaaaaaaaaaa();");
  3136. verifyFormat(
  3137. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3138. " aaaaaaaaaaaaaaaaaaaaaa());");
  3139. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3141. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3142. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3143. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3144. verifyFormat("return\n"
  3145. " // true if code is one of a or b.\n"
  3146. " code == a || code == b;");
  3147. }
  3148. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3149. verifyFormat(
  3150. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3151. " aaaaaaaaa aaaaaaa) {}");
  3152. verifyFormat(
  3153. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3154. " aaaaaaaaaaa aaaaaaaaa);");
  3155. verifyFormat(
  3156. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3157. " aaaaaaaaaaaaaaaaaaaaa));");
  3158. FormatStyle Style = getLLVMStyle();
  3159. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3160. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3161. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3162. Style);
  3163. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  3164. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3165. Style);
  3166. verifyFormat("SomeLongVariableName->someFunction(\n"
  3167. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3168. Style);
  3169. verifyFormat(
  3170. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3171. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3172. Style);
  3173. verifyFormat(
  3174. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3175. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3176. Style);
  3177. verifyFormat(
  3178. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3179. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3180. Style);
  3181. verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
  3182. " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
  3183. " b));",
  3184. Style);
  3185. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  3186. Style.BinPackArguments = false;
  3187. Style.BinPackParameters = false;
  3188. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3189. " aaaaaaaaaaa aaaaaaaa,\n"
  3190. " aaaaaaaaa aaaaaaa,\n"
  3191. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3192. Style);
  3193. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  3194. " aaaaaaaaaaa aaaaaaaaa,\n"
  3195. " aaaaaaaaaaa aaaaaaaaa,\n"
  3196. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3197. Style);
  3198. verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
  3199. " aaaaaaaaaaaaaaa,\n"
  3200. " aaaaaaaaaaaaaaaaaaaaa,\n"
  3201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3202. Style);
  3203. verifyFormat(
  3204. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
  3205. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  3206. Style);
  3207. verifyFormat(
  3208. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
  3209. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  3210. Style);
  3211. verifyFormat(
  3212. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3213. " aaaaaaaaaaaaaaaaaaaaa(\n"
  3214. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
  3215. " aaaaaaaaaaaaaaaa);",
  3216. Style);
  3217. verifyFormat(
  3218. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3219. " aaaaaaaaaaaaaaaaaaaaa(\n"
  3220. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
  3221. " aaaaaaaaaaaaaaaa);",
  3222. Style);
  3223. }
  3224. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  3225. FormatStyle Style = getLLVMStyleWithColumns(40);
  3226. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3227. " bbbbbbbbbbbbbbbbbbbbbb);",
  3228. Style);
  3229. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  3230. Style.AlignOperands = false;
  3231. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3232. " bbbbbbbbbbbbbbbbbbbbbb);",
  3233. Style);
  3234. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3235. Style.AlignOperands = true;
  3236. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3237. " bbbbbbbbbbbbbbbbbbbbbb);",
  3238. Style);
  3239. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3240. Style.AlignOperands = false;
  3241. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3242. " bbbbbbbbbbbbbbbbbbbbbb);",
  3243. Style);
  3244. }
  3245. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3246. verifyFormat(
  3247. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3248. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3249. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3250. verifyFormat(
  3251. "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
  3252. " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3253. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3254. verifyFormat(
  3255. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3256. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3257. verifyFormat(
  3258. "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
  3259. " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3260. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3261. verifyFormat(
  3262. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3263. " : aaaaaaaaaaaaa);");
  3264. verifyFormat(
  3265. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3266. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3267. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3268. " aaaaaaaaaaaaa);");
  3269. verifyFormat(
  3270. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3271. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3272. " aaaaaaaaaaaaa);");
  3273. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3274. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3275. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3276. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3277. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3278. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3279. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3280. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3281. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3282. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3283. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3284. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3285. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3286. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3287. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3288. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3289. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3290. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3291. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3292. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3293. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3294. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3295. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3296. " : aaaaaaaaaaaaaaaa;");
  3297. verifyFormat(
  3298. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3299. " ? aaaaaaaaaaaaaaa\n"
  3300. " : aaaaaaaaaaaaaaa;");
  3301. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3302. " aaaaaaaaa\n"
  3303. " ? b\n"
  3304. " : c);");
  3305. verifyFormat("return aaaa == bbbb\n"
  3306. " // comment\n"
  3307. " ? aaaa\n"
  3308. " : bbbb;");
  3309. verifyFormat("unsigned Indent =\n"
  3310. " format(TheLine.First,\n"
  3311. " IndentForLevel[TheLine.Level] >= 0\n"
  3312. " ? IndentForLevel[TheLine.Level]\n"
  3313. " : TheLine * 2,\n"
  3314. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3315. getLLVMStyleWithColumns(60));
  3316. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3317. " ? aaaaaaaaaaaaaaa\n"
  3318. " : bbbbbbbbbbbbbbb //\n"
  3319. " ? ccccccccccccccc\n"
  3320. " : ddddddddddddddd;");
  3321. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3322. " ? aaaaaaaaaaaaaaa\n"
  3323. " : (bbbbbbbbbbbbbbb //\n"
  3324. " ? ccccccccccccccc\n"
  3325. " : ddddddddddddddd);");
  3326. verifyFormat(
  3327. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3328. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3329. " aaaaaaaaaaaaaaaaaaaaa +\n"
  3330. " aaaaaaaaaaaaaaaaaaaaa\n"
  3331. " : aaaaaaaaaa;");
  3332. verifyFormat(
  3333. "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3334. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  3335. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3336. FormatStyle NoBinPacking = getLLVMStyle();
  3337. NoBinPacking.BinPackArguments = false;
  3338. verifyFormat(
  3339. "void f() {\n"
  3340. " g(aaa,\n"
  3341. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3342. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3343. " ? aaaaaaaaaaaaaaa\n"
  3344. " : aaaaaaaaaaaaaaa);\n"
  3345. "}",
  3346. NoBinPacking);
  3347. verifyFormat(
  3348. "void f() {\n"
  3349. " g(aaa,\n"
  3350. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3351. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3352. " ?: aaaaaaaaaaaaaaa);\n"
  3353. "}",
  3354. NoBinPacking);
  3355. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  3356. " // comment.\n"
  3357. " ccccccccccccccccccccccccccccccccccccccc\n"
  3358. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3359. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  3360. // Assignments in conditional expressions. Apparently not uncommon :-(.
  3361. verifyFormat("return a != b\n"
  3362. " // comment\n"
  3363. " ? a = b\n"
  3364. " : a = b;");
  3365. verifyFormat("return a != b\n"
  3366. " // comment\n"
  3367. " ? a = a != b\n"
  3368. " // comment\n"
  3369. " ? a = b\n"
  3370. " : a\n"
  3371. " : a;\n");
  3372. verifyFormat("return a != b\n"
  3373. " // comment\n"
  3374. " ? a\n"
  3375. " : a = a != b\n"
  3376. " // comment\n"
  3377. " ? a = b\n"
  3378. " : a;");
  3379. }
  3380. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  3381. FormatStyle Style = getLLVMStyle();
  3382. Style.BreakBeforeTernaryOperators = false;
  3383. Style.ColumnLimit = 70;
  3384. verifyFormat(
  3385. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3386. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3388. Style);
  3389. verifyFormat(
  3390. "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
  3391. " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3392. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3393. Style);
  3394. verifyFormat(
  3395. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3396. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3397. Style);
  3398. verifyFormat(
  3399. "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
  3400. " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3401. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3402. Style);
  3403. verifyFormat(
  3404. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  3405. " aaaaaaaaaaaaa);",
  3406. Style);
  3407. verifyFormat(
  3408. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3409. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3410. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3411. " aaaaaaaaaaaaa);",
  3412. Style);
  3413. verifyFormat(
  3414. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3415. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3416. " aaaaaaaaaaaaa);",
  3417. Style);
  3418. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3419. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3420. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3421. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3422. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3423. Style);
  3424. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3425. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3426. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3427. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3428. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3429. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3430. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3431. Style);
  3432. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3433. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  3434. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3435. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3436. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3437. Style);
  3438. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3439. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3440. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3441. Style);
  3442. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3443. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3444. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3445. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3446. Style);
  3447. verifyFormat(
  3448. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3449. " aaaaaaaaaaaaaaa :\n"
  3450. " aaaaaaaaaaaaaaa;",
  3451. Style);
  3452. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3453. " aaaaaaaaa ?\n"
  3454. " b :\n"
  3455. " c);",
  3456. Style);
  3457. verifyFormat("unsigned Indent =\n"
  3458. " format(TheLine.First,\n"
  3459. " IndentForLevel[TheLine.Level] >= 0 ?\n"
  3460. " IndentForLevel[TheLine.Level] :\n"
  3461. " TheLine * 2,\n"
  3462. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3463. Style);
  3464. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3465. " aaaaaaaaaaaaaaa :\n"
  3466. " bbbbbbbbbbbbbbb ? //\n"
  3467. " ccccccccccccccc :\n"
  3468. " ddddddddddddddd;",
  3469. Style);
  3470. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3471. " aaaaaaaaaaaaaaa :\n"
  3472. " (bbbbbbbbbbbbbbb ? //\n"
  3473. " ccccccccccccccc :\n"
  3474. " ddddddddddddddd);",
  3475. Style);
  3476. verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3477. " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
  3478. " ccccccccccccccccccccccccccc;",
  3479. Style);
  3480. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3481. " aaaaa :\n"
  3482. " bbbbbbbbbbbbbbb + cccccccccccccccc;",
  3483. Style);
  3484. }
  3485. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  3486. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  3487. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  3488. verifyFormat("bool a = true, b = false;");
  3489. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3490. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  3491. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  3492. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  3493. verifyFormat(
  3494. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3495. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  3496. " d = e && f;");
  3497. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  3498. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  3499. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3500. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  3501. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  3502. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  3503. FormatStyle Style = getGoogleStyle();
  3504. Style.PointerAlignment = FormatStyle::PAS_Left;
  3505. Style.DerivePointerAlignment = false;
  3506. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3507. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  3508. " *b = bbbbbbbbbbbbbbbbbbb;",
  3509. Style);
  3510. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3511. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  3512. Style);
  3513. verifyFormat("vector<int*> a, b;", Style);
  3514. verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
  3515. }
  3516. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  3517. verifyFormat("arr[foo ? bar : baz];");
  3518. verifyFormat("f()[foo ? bar : baz];");
  3519. verifyFormat("(a + b)[foo ? bar : baz];");
  3520. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  3521. }
  3522. TEST_F(FormatTest, AlignsStringLiterals) {
  3523. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  3524. " \"short literal\");");
  3525. verifyFormat(
  3526. "looooooooooooooooooooooooongFunction(\n"
  3527. " \"short literal\"\n"
  3528. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  3529. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  3530. " \" string literals\",\n"
  3531. " and, other, parameters);");
  3532. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  3533. " \"5678\";",
  3534. format("fun + \"1243\" /* comment */\n"
  3535. " \"5678\";",
  3536. getLLVMStyleWithColumns(28)));
  3537. EXPECT_EQ(
  3538. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  3539. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  3540. " \"aaaaaaaaaaaaaaaa\";",
  3541. format("aaaaaa ="
  3542. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  3543. "aaaaaaaaaaaaaaaaaaaaa\" "
  3544. "\"aaaaaaaaaaaaaaaa\";"));
  3545. verifyFormat("a = a + \"a\"\n"
  3546. " \"a\"\n"
  3547. " \"a\";");
  3548. verifyFormat("f(\"a\", \"b\"\n"
  3549. " \"c\");");
  3550. verifyFormat(
  3551. "#define LL_FORMAT \"ll\"\n"
  3552. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  3553. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  3554. verifyFormat("#define A(X) \\\n"
  3555. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  3556. " \"ccccc\"",
  3557. getLLVMStyleWithColumns(23));
  3558. verifyFormat("#define A \"def\"\n"
  3559. "f(\"abc\" A \"ghi\"\n"
  3560. " \"jkl\");");
  3561. verifyFormat("f(L\"a\"\n"
  3562. " L\"b\");");
  3563. verifyFormat("#define A(X) \\\n"
  3564. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  3565. " L\"ccccc\"",
  3566. getLLVMStyleWithColumns(25));
  3567. verifyFormat("f(@\"a\"\n"
  3568. " @\"b\");");
  3569. verifyFormat("NSString s = @\"a\"\n"
  3570. " @\"b\"\n"
  3571. " @\"c\";");
  3572. verifyFormat("NSString s = @\"a\"\n"
  3573. " \"b\"\n"
  3574. " \"c\";");
  3575. }
  3576. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  3577. FormatStyle Style = getLLVMStyle();
  3578. // No declarations or definitions should be moved to own line.
  3579. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  3580. verifyFormat("class A {\n"
  3581. " int f() { return 1; }\n"
  3582. " int g();\n"
  3583. "};\n"
  3584. "int f() { return 1; }\n"
  3585. "int g();\n",
  3586. Style);
  3587. // All declarations and definitions should have the return type moved to its
  3588. // own
  3589. // line.
  3590. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  3591. verifyFormat("class E {\n"
  3592. " int\n"
  3593. " f() {\n"
  3594. " return 1;\n"
  3595. " }\n"
  3596. " int\n"
  3597. " g();\n"
  3598. "};\n"
  3599. "int\n"
  3600. "f() {\n"
  3601. " return 1;\n"
  3602. "}\n"
  3603. "int\n"
  3604. "g();\n",
  3605. Style);
  3606. // Top-level definitions, and no kinds of declarations should have the
  3607. // return type moved to its own line.
  3608. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  3609. verifyFormat("class B {\n"
  3610. " int f() { return 1; }\n"
  3611. " int g();\n"
  3612. "};\n"
  3613. "int\n"
  3614. "f() {\n"
  3615. " return 1;\n"
  3616. "}\n"
  3617. "int g();\n",
  3618. Style);
  3619. // Top-level definitions and declarations should have the return type moved
  3620. // to its own line.
  3621. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  3622. verifyFormat("class C {\n"
  3623. " int f() { return 1; }\n"
  3624. " int g();\n"
  3625. "};\n"
  3626. "int\n"
  3627. "f() {\n"
  3628. " return 1;\n"
  3629. "}\n"
  3630. "int\n"
  3631. "g();\n",
  3632. Style);
  3633. // All definitions should have the return type moved to its own line, but no
  3634. // kinds of declarations.
  3635. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  3636. verifyFormat("class D {\n"
  3637. " int\n"
  3638. " f() {\n"
  3639. " return 1;\n"
  3640. " }\n"
  3641. " int g();\n"
  3642. "};\n"
  3643. "int\n"
  3644. "f() {\n"
  3645. " return 1;\n"
  3646. "}\n"
  3647. "int g();\n",
  3648. Style);
  3649. verifyFormat("const char *\n"
  3650. "f(void) {\n" // Break here.
  3651. " return \"\";\n"
  3652. "}\n"
  3653. "const char *bar(void);\n", // No break here.
  3654. Style);
  3655. verifyFormat("template <class T>\n"
  3656. "T *\n"
  3657. "f(T &c) {\n" // Break here.
  3658. " return NULL;\n"
  3659. "}\n"
  3660. "template <class T> T *f(T &c);\n", // No break here.
  3661. Style);
  3662. verifyFormat("class C {\n"
  3663. " int\n"
  3664. " operator+() {\n"
  3665. " return 1;\n"
  3666. " }\n"
  3667. " int\n"
  3668. " operator()() {\n"
  3669. " return 1;\n"
  3670. " }\n"
  3671. "};\n",
  3672. Style);
  3673. verifyFormat("void\n"
  3674. "A::operator()() {}\n"
  3675. "void\n"
  3676. "A::operator>>() {}\n"
  3677. "void\n"
  3678. "A::operator+() {}\n",
  3679. Style);
  3680. verifyFormat("void *operator new(std::size_t s);", // No break here.
  3681. Style);
  3682. verifyFormat("void *\n"
  3683. "operator new(std::size_t s) {}",
  3684. Style);
  3685. verifyFormat("void *\n"
  3686. "operator delete[](void *ptr) {}",
  3687. Style);
  3688. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  3689. verifyFormat("const char *\n"
  3690. "f(void)\n" // Break here.
  3691. "{\n"
  3692. " return \"\";\n"
  3693. "}\n"
  3694. "const char *bar(void);\n", // No break here.
  3695. Style);
  3696. verifyFormat("template <class T>\n"
  3697. "T *\n" // Problem here: no line break
  3698. "f(T &c)\n" // Break here.
  3699. "{\n"
  3700. " return NULL;\n"
  3701. "}\n"
  3702. "template <class T> T *f(T &c);\n", // No break here.
  3703. Style);
  3704. }
  3705. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  3706. FormatStyle NoBreak = getLLVMStyle();
  3707. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  3708. FormatStyle Break = getLLVMStyle();
  3709. Break.AlwaysBreakBeforeMultilineStrings = true;
  3710. verifyFormat("aaaa = \"bbbb\"\n"
  3711. " \"cccc\";",
  3712. NoBreak);
  3713. verifyFormat("aaaa =\n"
  3714. " \"bbbb\"\n"
  3715. " \"cccc\";",
  3716. Break);
  3717. verifyFormat("aaaa(\"bbbb\"\n"
  3718. " \"cccc\");",
  3719. NoBreak);
  3720. verifyFormat("aaaa(\n"
  3721. " \"bbbb\"\n"
  3722. " \"cccc\");",
  3723. Break);
  3724. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  3725. " \"cccc\");",
  3726. NoBreak);
  3727. verifyFormat("aaaa(qqq,\n"
  3728. " \"bbbb\"\n"
  3729. " \"cccc\");",
  3730. Break);
  3731. verifyFormat("aaaa(qqq,\n"
  3732. " L\"bbbb\"\n"
  3733. " L\"cccc\");",
  3734. Break);
  3735. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  3736. " \"bbbb\"));",
  3737. Break);
  3738. verifyFormat("string s = someFunction(\n"
  3739. " \"abc\"\n"
  3740. " \"abc\");",
  3741. Break);
  3742. // As we break before unary operators, breaking right after them is bad.
  3743. verifyFormat("string foo = abc ? \"x\"\n"
  3744. " \"blah blah blah blah blah blah\"\n"
  3745. " : \"y\";",
  3746. Break);
  3747. // Don't break if there is no column gain.
  3748. verifyFormat("f(\"aaaa\"\n"
  3749. " \"bbbb\");",
  3750. Break);
  3751. // Treat literals with escaped newlines like multi-line string literals.
  3752. EXPECT_EQ("x = \"a\\\n"
  3753. "b\\\n"
  3754. "c\";",
  3755. format("x = \"a\\\n"
  3756. "b\\\n"
  3757. "c\";",
  3758. NoBreak));
  3759. EXPECT_EQ("xxxx =\n"
  3760. " \"a\\\n"
  3761. "b\\\n"
  3762. "c\";",
  3763. format("xxxx = \"a\\\n"
  3764. "b\\\n"
  3765. "c\";",
  3766. Break));
  3767. // Exempt ObjC strings for now.
  3768. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  3769. " @\"bbbb\";",
  3770. format("NSString *const kString = @\"aaaa\"\n"
  3771. "@\"bbbb\";",
  3772. Break));
  3773. Break.ColumnLimit = 0;
  3774. verifyFormat("const char *hello = \"hello llvm\";", Break);
  3775. }
  3776. TEST_F(FormatTest, AlignsPipes) {
  3777. verifyFormat(
  3778. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3779. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3780. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3781. verifyFormat(
  3782. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  3783. " << aaaaaaaaaaaaaaaaaaaa;");
  3784. verifyFormat(
  3785. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3786. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3787. verifyFormat(
  3788. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3789. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3790. verifyFormat(
  3791. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  3792. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  3793. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  3794. verifyFormat(
  3795. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3796. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3797. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3798. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3799. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3800. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3801. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3802. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  3803. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  3804. verifyFormat(
  3805. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3806. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3807. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  3808. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  3809. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3810. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3811. " aaaaaaaaaaaaaaaaaaaaa)\n"
  3812. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3813. verifyFormat("LOG_IF(aaa == //\n"
  3814. " bbb)\n"
  3815. " << a << b;");
  3816. // But sometimes, breaking before the first "<<" is desirable.
  3817. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  3818. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  3819. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  3820. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3821. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3822. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  3823. " << BEF << IsTemplate << Description << E->getType();");
  3824. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  3825. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3826. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3827. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  3828. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3829. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3830. " << aaa;");
  3831. verifyFormat(
  3832. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3833. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3834. // Incomplete string literal.
  3835. EXPECT_EQ("llvm::errs() << \"\n"
  3836. " << a;",
  3837. format("llvm::errs() << \"\n<<a;"));
  3838. verifyFormat("void f() {\n"
  3839. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  3840. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  3841. "}");
  3842. // Handle 'endl'.
  3843. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  3844. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  3845. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  3846. // Handle '\n'.
  3847. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
  3848. " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  3849. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
  3850. " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
  3851. verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
  3852. " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
  3853. verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  3854. }
  3855. TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
  3856. verifyFormat("return out << \"somepacket = {\\n\"\n"
  3857. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  3858. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  3859. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  3860. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  3861. " << \"}\";");
  3862. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  3863. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  3864. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  3865. verifyFormat(
  3866. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  3867. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  3868. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  3869. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  3870. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  3871. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  3872. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3873. verifyFormat(
  3874. "void f() {\n"
  3875. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  3876. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3877. "}");
  3878. // Breaking before the first "<<" is generally not desirable.
  3879. verifyFormat(
  3880. "llvm::errs()\n"
  3881. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3882. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3883. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3884. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3885. getLLVMStyleWithColumns(70));
  3886. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3887. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3888. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3889. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3890. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3891. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3892. getLLVMStyleWithColumns(70));
  3893. verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
  3894. " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
  3895. " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
  3896. verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
  3897. " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
  3898. " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
  3899. verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
  3900. " (aaaa + aaaa);",
  3901. getLLVMStyleWithColumns(40));
  3902. verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
  3903. " (aaaaaaa + aaaaa));",
  3904. getLLVMStyleWithColumns(40));
  3905. verifyFormat(
  3906. "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
  3907. " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
  3908. " bbbbbbbbbbbbbbbbbbbbbbb);");
  3909. }
  3910. TEST_F(FormatTest, UnderstandsEquals) {
  3911. verifyFormat(
  3912. "aaaaaaaaaaaaaaaaa =\n"
  3913. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3914. verifyFormat(
  3915. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3917. verifyFormat(
  3918. "if (a) {\n"
  3919. " f();\n"
  3920. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3921. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3922. "}");
  3923. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3924. " 100000000 + 10000000) {\n}");
  3925. }
  3926. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  3927. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3928. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  3929. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3930. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  3931. verifyFormat(
  3932. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  3933. " Parameter2);");
  3934. verifyFormat(
  3935. "ShortObject->shortFunction(\n"
  3936. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  3937. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  3938. verifyFormat("loooooooooooooongFunction(\n"
  3939. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  3940. verifyFormat(
  3941. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  3942. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  3943. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  3944. " .WillRepeatedly(Return(SomeValue));");
  3945. verifyFormat("void f() {\n"
  3946. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  3947. " .Times(2)\n"
  3948. " .WillRepeatedly(Return(SomeValue));\n"
  3949. "}");
  3950. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  3951. " ccccccccccccccccccccccc);");
  3952. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3953. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3954. " .aaaaa(aaaaa),\n"
  3955. " aaaaaaaaaaaaaaaaaaaaa);");
  3956. verifyFormat("void f() {\n"
  3957. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3958. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  3959. "}");
  3960. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3961. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3962. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3963. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3964. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3965. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3966. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3967. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3968. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  3969. "}");
  3970. // Here, it is not necessary to wrap at "." or "->".
  3971. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  3972. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3973. verifyFormat(
  3974. "aaaaaaaaaaa->aaaaaaaaa(\n"
  3975. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3976. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  3977. verifyFormat(
  3978. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3979. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  3980. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  3981. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3982. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  3983. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3984. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3985. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3986. " .a();");
  3987. FormatStyle NoBinPacking = getLLVMStyle();
  3988. NoBinPacking.BinPackParameters = false;
  3989. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3990. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3991. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  3992. " aaaaaaaaaaaaaaaaaaa,\n"
  3993. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3994. NoBinPacking);
  3995. // If there is a subsequent call, change to hanging indentation.
  3996. verifyFormat(
  3997. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3998. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  3999. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4000. verifyFormat(
  4001. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4002. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4003. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4004. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4005. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4006. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4008. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4009. }
  4010. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4011. verifyFormat("template <typename T>\n"
  4012. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4013. verifyFormat("template <typename T>\n"
  4014. "// T should be one of {A, B}.\n"
  4015. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4016. verifyFormat(
  4017. "template <typename T>\n"
  4018. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4019. verifyFormat("template <typename T>\n"
  4020. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4021. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4022. verifyFormat(
  4023. "template <typename T>\n"
  4024. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4025. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4026. verifyFormat(
  4027. "template <typename T>\n"
  4028. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4029. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4031. verifyFormat("template <typename T>\n"
  4032. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4033. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4034. verifyFormat(
  4035. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4036. " typename T4 = char>\n"
  4037. "void f();");
  4038. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4039. " template <typename> class cccccccccccccccccccccc,\n"
  4040. " typename ddddddddddddd>\n"
  4041. "class C {};");
  4042. verifyFormat(
  4043. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4044. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4045. verifyFormat("void f() {\n"
  4046. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4047. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4048. "}");
  4049. verifyFormat("template <typename T> class C {};");
  4050. verifyFormat("template <typename T> void f();");
  4051. verifyFormat("template <typename T> void f() {}");
  4052. verifyFormat(
  4053. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4054. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4055. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4056. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4057. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4059. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4060. getLLVMStyleWithColumns(72));
  4061. EXPECT_EQ("static_cast<A< //\n"
  4062. " B> *>(\n"
  4063. "\n"
  4064. " );",
  4065. format("static_cast<A<//\n"
  4066. " B>*>(\n"
  4067. "\n"
  4068. " );"));
  4069. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4070. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4071. FormatStyle AlwaysBreak = getLLVMStyle();
  4072. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4073. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4074. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4075. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4076. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4077. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4078. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4079. verifyFormat("template <template <typename> class Fooooooo,\n"
  4080. " template <typename> class Baaaaaaar>\n"
  4081. "struct C {};",
  4082. AlwaysBreak);
  4083. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4084. "struct C {};",
  4085. AlwaysBreak);
  4086. verifyFormat("template <enum E> class A {\n"
  4087. "public:\n"
  4088. " E *f();\n"
  4089. "};");
  4090. }
  4091. TEST_F(FormatTest, WrapsTemplateParameters) {
  4092. FormatStyle Style = getLLVMStyle();
  4093. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4094. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  4095. verifyFormat(
  4096. "template <typename... a> struct q {};\n"
  4097. "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
  4098. " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
  4099. " y;",
  4100. Style);
  4101. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4102. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  4103. verifyFormat(
  4104. "template <typename... a> struct r {};\n"
  4105. "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
  4106. " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
  4107. " y;",
  4108. Style);
  4109. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4110. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  4111. verifyFormat(
  4112. "template <typename... a> struct s {};\n"
  4113. "extern s<\n"
  4114. " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  4115. " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
  4116. " y;",
  4117. Style);
  4118. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4119. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  4120. verifyFormat(
  4121. "template <typename... a> struct t {};\n"
  4122. "extern t<\n"
  4123. " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  4124. " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
  4125. " y;",
  4126. Style);
  4127. }
  4128. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4129. verifyFormat(
  4130. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4131. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4132. verifyFormat(
  4133. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4134. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4136. // FIXME: Should we have the extra indent after the second break?
  4137. verifyFormat(
  4138. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4139. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4141. verifyFormat(
  4142. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4143. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4144. // Breaking at nested name specifiers is generally not desirable.
  4145. verifyFormat(
  4146. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4147. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4148. verifyFormat(
  4149. "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
  4150. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4152. " aaaaaaaaaaaaaaaaaaaaa);",
  4153. getLLVMStyleWithColumns(74));
  4154. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4155. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4156. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4157. }
  4158. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4159. verifyFormat("A<int> a;");
  4160. verifyFormat("A<A<A<int>>> a;");
  4161. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4162. verifyFormat("bool x = a < 1 || 2 > a;");
  4163. verifyFormat("bool x = 5 < f<int>();");
  4164. verifyFormat("bool x = f<int>() > 5;");
  4165. verifyFormat("bool x = 5 < a<int>::x;");
  4166. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4167. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4168. verifyGoogleFormat("A<A<int>> a;");
  4169. verifyGoogleFormat("A<A<A<int>>> a;");
  4170. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4171. verifyGoogleFormat("A<A<int> > a;");
  4172. verifyGoogleFormat("A<A<A<int> > > a;");
  4173. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4174. verifyGoogleFormat("A<::A<int>> a;");
  4175. verifyGoogleFormat("A<::A> a;");
  4176. verifyGoogleFormat("A< ::A> a;");
  4177. verifyGoogleFormat("A< ::A<int> > a;");
  4178. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4179. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4180. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4181. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4182. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  4183. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  4184. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4185. verifyFormat("test >> a >> b;");
  4186. verifyFormat("test << a >> b;");
  4187. verifyFormat("f<int>();");
  4188. verifyFormat("template <typename T> void f() {}");
  4189. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4190. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  4191. "sizeof(char)>::type>;");
  4192. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  4193. verifyFormat("f(a.operator()<A>());");
  4194. verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4195. " .template operator()<A>());",
  4196. getLLVMStyleWithColumns(35));
  4197. // Not template parameters.
  4198. verifyFormat("return a < b && c > d;");
  4199. verifyFormat("void f() {\n"
  4200. " while (a < b && c > d) {\n"
  4201. " }\n"
  4202. "}");
  4203. verifyFormat("template <typename... Types>\n"
  4204. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4205. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4206. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4207. getLLVMStyleWithColumns(60));
  4208. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4209. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4210. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  4211. }
  4212. TEST_F(FormatTest, BitshiftOperatorWidth) {
  4213. EXPECT_EQ("int a = 1 << 2; /* foo\n"
  4214. " bar */",
  4215. format("int a=1<<2; /* foo\n"
  4216. " bar */"));
  4217. EXPECT_EQ("int b = 256 >> 1; /* foo\n"
  4218. " bar */",
  4219. format("int b =256>>1 ; /* foo\n"
  4220. " bar */"));
  4221. }
  4222. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4223. verifyFormat("COMPARE(a, ==, b);");
  4224. verifyFormat("auto s = sizeof...(Ts) - 1;");
  4225. }
  4226. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4227. verifyFormat("int A::*x;");
  4228. verifyFormat("int (S::*func)(void *);");
  4229. verifyFormat("void f() { int (S::*func)(void *); }");
  4230. verifyFormat("typedef bool *(Class::*Member)() const;");
  4231. verifyFormat("void f() {\n"
  4232. " (a->*f)();\n"
  4233. " a->*x;\n"
  4234. " (a.*f)();\n"
  4235. " ((*a).*f)();\n"
  4236. " a.*x;\n"
  4237. "}");
  4238. verifyFormat("void f() {\n"
  4239. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4240. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4241. "}");
  4242. verifyFormat(
  4243. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4244. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4245. FormatStyle Style = getLLVMStyle();
  4246. Style.PointerAlignment = FormatStyle::PAS_Left;
  4247. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4248. }
  4249. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4250. verifyFormat("int a = -2;");
  4251. verifyFormat("f(-1, -2, -3);");
  4252. verifyFormat("a[-1] = 5;");
  4253. verifyFormat("int a = 5 + -2;");
  4254. verifyFormat("if (i == -1) {\n}");
  4255. verifyFormat("if (i != -1) {\n}");
  4256. verifyFormat("if (i > -1) {\n}");
  4257. verifyFormat("if (i < -1) {\n}");
  4258. verifyFormat("++(a->f());");
  4259. verifyFormat("--(a->f());");
  4260. verifyFormat("(a->f())++;");
  4261. verifyFormat("a[42]++;");
  4262. verifyFormat("if (!(a->f())) {\n}");
  4263. verifyFormat("a-- > b;");
  4264. verifyFormat("b ? -a : c;");
  4265. verifyFormat("n * sizeof char16;");
  4266. verifyFormat("n * alignof char16;", getGoogleStyle());
  4267. verifyFormat("sizeof(char);");
  4268. verifyFormat("alignof(char);", getGoogleStyle());
  4269. verifyFormat("return -1;");
  4270. verifyFormat("switch (a) {\n"
  4271. "case -1:\n"
  4272. " break;\n"
  4273. "}");
  4274. verifyFormat("#define X -1");
  4275. verifyFormat("#define X -kConstant");
  4276. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4277. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4278. verifyFormat("int a = /* confusing comment */ -1;");
  4279. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4280. verifyFormat("int a = i /* confusing comment */++;");
  4281. }
  4282. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4283. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4284. " aaaaa)) {\n"
  4285. "}");
  4286. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4287. " aaaaa));");
  4288. verifyFormat("*aaa = aaaaaaa( // break\n"
  4289. " bbbbbb);");
  4290. }
  4291. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4292. verifyFormat("bool operator<();");
  4293. verifyFormat("bool operator>();");
  4294. verifyFormat("bool operator=();");
  4295. verifyFormat("bool operator==();");
  4296. verifyFormat("bool operator!=();");
  4297. verifyFormat("int operator+();");
  4298. verifyFormat("int operator++();");
  4299. verifyFormat("bool operator,();");
  4300. verifyFormat("bool operator();");
  4301. verifyFormat("bool operator()();");
  4302. verifyFormat("bool operator[]();");
  4303. verifyFormat("operator bool();");
  4304. verifyFormat("operator int();");
  4305. verifyFormat("operator void *();");
  4306. verifyFormat("operator SomeType<int>();");
  4307. verifyFormat("operator SomeType<int, int>();");
  4308. verifyFormat("operator SomeType<SomeType<int>>();");
  4309. verifyFormat("void *operator new(std::size_t size);");
  4310. verifyFormat("void *operator new[](std::size_t size);");
  4311. verifyFormat("void operator delete(void *ptr);");
  4312. verifyFormat("void operator delete[](void *ptr);");
  4313. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4314. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4315. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
  4316. " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
  4317. verifyFormat(
  4318. "ostream &operator<<(ostream &OutputStream,\n"
  4319. " SomeReallyLongType WithSomeReallyLongValue);");
  4320. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4321. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4322. " return left.group < right.group;\n"
  4323. "}");
  4324. verifyFormat("SomeType &operator=(const SomeType &S);");
  4325. verifyFormat("f.template operator()<int>();");
  4326. verifyGoogleFormat("operator void*();");
  4327. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4328. verifyGoogleFormat("operator ::A();");
  4329. verifyFormat("using A::operator+;");
  4330. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  4331. "int i;");
  4332. }
  4333. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  4334. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  4335. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  4336. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  4337. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  4338. verifyFormat("Deleted &operator=(const Deleted &) &;");
  4339. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  4340. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  4341. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  4342. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  4343. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  4344. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  4345. verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
  4346. verifyFormat("template <typename T>\n"
  4347. "void F(T) && = delete;",
  4348. getGoogleStyle());
  4349. FormatStyle AlignLeft = getLLVMStyle();
  4350. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  4351. verifyFormat("void A::b() && {}", AlignLeft);
  4352. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  4353. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  4354. AlignLeft);
  4355. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  4356. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  4357. verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
  4358. verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
  4359. verifyFormat("auto Function(T) & -> void {}", AlignLeft);
  4360. verifyFormat("auto Function(T) & -> void;", AlignLeft);
  4361. verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
  4362. FormatStyle Spaces = getLLVMStyle();
  4363. Spaces.SpacesInCStyleCastParentheses = true;
  4364. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  4365. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  4366. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  4367. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  4368. Spaces.SpacesInCStyleCastParentheses = false;
  4369. Spaces.SpacesInParentheses = true;
  4370. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  4371. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  4372. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  4373. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  4374. }
  4375. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4376. verifyFormat("void f() {\n"
  4377. " A *a = new A;\n"
  4378. " A *a = new (placement) A;\n"
  4379. " delete a;\n"
  4380. " delete (A *)a;\n"
  4381. "}");
  4382. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4383. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4384. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4385. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4386. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4387. verifyFormat("delete[] h->p;");
  4388. }
  4389. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4390. verifyFormat("int *f(int *a) {}");
  4391. verifyFormat("int main(int argc, char **argv) {}");
  4392. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4393. verifyIndependentOfContext("f(a, *a);");
  4394. verifyFormat("void g() { f(*a); }");
  4395. verifyIndependentOfContext("int a = b * 10;");
  4396. verifyIndependentOfContext("int a = 10 * b;");
  4397. verifyIndependentOfContext("int a = b * c;");
  4398. verifyIndependentOfContext("int a += b * c;");
  4399. verifyIndependentOfContext("int a -= b * c;");
  4400. verifyIndependentOfContext("int a *= b * c;");
  4401. verifyIndependentOfContext("int a /= b * c;");
  4402. verifyIndependentOfContext("int a = *b;");
  4403. verifyIndependentOfContext("int a = *b * c;");
  4404. verifyIndependentOfContext("int a = b * *c;");
  4405. verifyIndependentOfContext("int a = b * (10);");
  4406. verifyIndependentOfContext("S << b * (10);");
  4407. verifyIndependentOfContext("return 10 * b;");
  4408. verifyIndependentOfContext("return *b * *c;");
  4409. verifyIndependentOfContext("return a & ~b;");
  4410. verifyIndependentOfContext("f(b ? *c : *d);");
  4411. verifyIndependentOfContext("int a = b ? *c : *d;");
  4412. verifyIndependentOfContext("*b = a;");
  4413. verifyIndependentOfContext("a * ~b;");
  4414. verifyIndependentOfContext("a * !b;");
  4415. verifyIndependentOfContext("a * +b;");
  4416. verifyIndependentOfContext("a * -b;");
  4417. verifyIndependentOfContext("a * ++b;");
  4418. verifyIndependentOfContext("a * --b;");
  4419. verifyIndependentOfContext("a[4] * b;");
  4420. verifyIndependentOfContext("a[a * a] = 1;");
  4421. verifyIndependentOfContext("f() * b;");
  4422. verifyIndependentOfContext("a * [self dostuff];");
  4423. verifyIndependentOfContext("int x = a * (a + b);");
  4424. verifyIndependentOfContext("(a *)(a + b);");
  4425. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4426. verifyIndependentOfContext("int *pa = (int *)&a;");
  4427. verifyIndependentOfContext("return sizeof(int **);");
  4428. verifyIndependentOfContext("return sizeof(int ******);");
  4429. verifyIndependentOfContext("return (int **&)a;");
  4430. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4431. verifyFormat("void f(Type (*parameter)[10]) {}");
  4432. verifyFormat("void f(Type (&parameter)[10]) {}");
  4433. verifyGoogleFormat("return sizeof(int**);");
  4434. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4435. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4436. verifyFormat("auto a = [](int **&, int ***) {};");
  4437. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4438. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4439. verifyFormat("[](const decltype(*a) &value) {}");
  4440. verifyFormat("decltype(a * b) F();");
  4441. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  4442. verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
  4443. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4444. verifyIndependentOfContext("int i{a * b};");
  4445. verifyIndependentOfContext("aaa && aaa->f();");
  4446. verifyIndependentOfContext("int x = ~*p;");
  4447. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4448. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  4449. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  4450. verifyFormat("void f() { f(a, c * d); }");
  4451. verifyFormat("void f() { f(new a(), c * d); }");
  4452. verifyFormat("void f(const MyOverride &override);");
  4453. verifyFormat("void f(const MyFinal &final);");
  4454. verifyIndependentOfContext("bool a = f() && override.f();");
  4455. verifyIndependentOfContext("bool a = f() && final.f();");
  4456. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4457. verifyIndependentOfContext("A<int *> a;");
  4458. verifyIndependentOfContext("A<int **> a;");
  4459. verifyIndependentOfContext("A<int *, int *> a;");
  4460. verifyIndependentOfContext("A<int *[]> a;");
  4461. verifyIndependentOfContext(
  4462. "const char *const p = reinterpret_cast<const char *const>(q);");
  4463. verifyIndependentOfContext("A<int **, int **> a;");
  4464. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4465. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4466. verifyFormat("for (; a && b;) {\n}");
  4467. verifyFormat("bool foo = true && [] { return false; }();");
  4468. verifyFormat(
  4469. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4471. verifyGoogleFormat("int const* a = &b;");
  4472. verifyGoogleFormat("**outparam = 1;");
  4473. verifyGoogleFormat("*outparam = a * b;");
  4474. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4475. verifyGoogleFormat("A<int*> a;");
  4476. verifyGoogleFormat("A<int**> a;");
  4477. verifyGoogleFormat("A<int*, int*> a;");
  4478. verifyGoogleFormat("A<int**, int**> a;");
  4479. verifyGoogleFormat("f(b ? *c : *d);");
  4480. verifyGoogleFormat("int a = b ? *c : *d;");
  4481. verifyGoogleFormat("Type* t = **x;");
  4482. verifyGoogleFormat("Type* t = *++*x;");
  4483. verifyGoogleFormat("*++*x;");
  4484. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4485. verifyGoogleFormat("Type* t = x++ * y;");
  4486. verifyGoogleFormat(
  4487. "const char* const p = reinterpret_cast<const char* const>(q);");
  4488. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4489. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4490. verifyGoogleFormat("template <typename T>\n"
  4491. "void f(int i = 0, SomeType** temps = NULL);");
  4492. FormatStyle Left = getLLVMStyle();
  4493. Left.PointerAlignment = FormatStyle::PAS_Left;
  4494. verifyFormat("x = *a(x) = *a(y);", Left);
  4495. verifyFormat("for (;; *a = b) {\n}", Left);
  4496. verifyFormat("return *this += 1;", Left);
  4497. verifyIndependentOfContext("a = *(x + y);");
  4498. verifyIndependentOfContext("a = &(x + y);");
  4499. verifyIndependentOfContext("*(x + y).call();");
  4500. verifyIndependentOfContext("&(x + y)->call();");
  4501. verifyFormat("void f() { &(*I).first; }");
  4502. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4503. verifyFormat(
  4504. "int *MyValues = {\n"
  4505. " *A, // Operator detection might be confused by the '{'\n"
  4506. " *BB // Operator detection might be confused by previous comment\n"
  4507. "};");
  4508. verifyIndependentOfContext("if (int *a = &b)");
  4509. verifyIndependentOfContext("if (int &a = *b)");
  4510. verifyIndependentOfContext("if (a & b[i])");
  4511. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4512. verifyIndependentOfContext("if (*b[i])");
  4513. verifyIndependentOfContext("if (int *a = (&b))");
  4514. verifyIndependentOfContext("while (int *a = &b)");
  4515. verifyIndependentOfContext("size = sizeof *a;");
  4516. verifyIndependentOfContext("if (a && (b = c))");
  4517. verifyFormat("void f() {\n"
  4518. " for (const int &v : Values) {\n"
  4519. " }\n"
  4520. "}");
  4521. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4522. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4523. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4524. verifyFormat("#define A (!a * b)");
  4525. verifyFormat("#define MACRO \\\n"
  4526. " int *i = a * b; \\\n"
  4527. " void f(a *b);",
  4528. getLLVMStyleWithColumns(19));
  4529. verifyIndependentOfContext("A = new SomeType *[Length];");
  4530. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4531. verifyIndependentOfContext("T **t = new T *;");
  4532. verifyIndependentOfContext("T **t = new T *();");
  4533. verifyGoogleFormat("A = new SomeType*[Length]();");
  4534. verifyGoogleFormat("A = new SomeType*[Length];");
  4535. verifyGoogleFormat("T** t = new T*;");
  4536. verifyGoogleFormat("T** t = new T*();");
  4537. FormatStyle PointerLeft = getLLVMStyle();
  4538. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4539. verifyFormat("delete *x;", PointerLeft);
  4540. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4541. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4542. verifyFormat("template <bool a, bool b> "
  4543. "typename t::if<x && y>::type f() {}");
  4544. verifyFormat("template <int *y> f() {}");
  4545. verifyFormat("vector<int *> v;");
  4546. verifyFormat("vector<int *const> v;");
  4547. verifyFormat("vector<int *const **const *> v;");
  4548. verifyFormat("vector<int *volatile> v;");
  4549. verifyFormat("vector<a * b> v;");
  4550. verifyFormat("foo<b && false>();");
  4551. verifyFormat("foo<b & 1>();");
  4552. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4553. verifyFormat(
  4554. "template <class T, class = typename std::enable_if<\n"
  4555. " std::is_integral<T>::value &&\n"
  4556. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  4557. "void F();",
  4558. getLLVMStyleWithColumns(70));
  4559. verifyFormat(
  4560. "template <class T,\n"
  4561. " class = typename std::enable_if<\n"
  4562. " std::is_integral<T>::value &&\n"
  4563. " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
  4564. " class U>\n"
  4565. "void F();",
  4566. getLLVMStyleWithColumns(70));
  4567. verifyFormat(
  4568. "template <class T,\n"
  4569. " class = typename ::std::enable_if<\n"
  4570. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  4571. "void F();",
  4572. getGoogleStyleWithColumns(68));
  4573. verifyIndependentOfContext("MACRO(int *i);");
  4574. verifyIndependentOfContext("MACRO(auto *a);");
  4575. verifyIndependentOfContext("MACRO(const A *a);");
  4576. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  4577. verifyFormat("void f() { f(float{1}, a * a); }");
  4578. // FIXME: Is there a way to make this work?
  4579. // verifyIndependentOfContext("MACRO(A *a);");
  4580. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  4581. verifyFormat("return options != nullptr && operator==(*options);");
  4582. EXPECT_EQ("#define OP(x) \\\n"
  4583. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4584. " return s << a.DebugString(); \\\n"
  4585. " }",
  4586. format("#define OP(x) \\\n"
  4587. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4588. " return s << a.DebugString(); \\\n"
  4589. " }",
  4590. getLLVMStyleWithColumns(50)));
  4591. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4592. // foo<x> d > v; doesn't make sense.
  4593. verifyFormat("foo<a<b && c> d> v;");
  4594. FormatStyle PointerMiddle = getLLVMStyle();
  4595. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  4596. verifyFormat("delete *x;", PointerMiddle);
  4597. verifyFormat("int * x;", PointerMiddle);
  4598. verifyFormat("template <int * y> f() {}", PointerMiddle);
  4599. verifyFormat("int * f(int * a) {}", PointerMiddle);
  4600. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  4601. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  4602. verifyFormat("A<int *> a;", PointerMiddle);
  4603. verifyFormat("A<int **> a;", PointerMiddle);
  4604. verifyFormat("A<int *, int *> a;", PointerMiddle);
  4605. verifyFormat("A<int * []> a;", PointerMiddle);
  4606. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  4607. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  4608. verifyFormat("T ** t = new T *;", PointerMiddle);
  4609. // Member function reference qualifiers aren't binary operators.
  4610. verifyFormat("string // break\n"
  4611. "operator()() & {}");
  4612. verifyFormat("string // break\n"
  4613. "operator()() && {}");
  4614. verifyGoogleFormat("template <typename T>\n"
  4615. "auto x() & -> int {}");
  4616. }
  4617. TEST_F(FormatTest, UnderstandsAttributes) {
  4618. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4619. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4620. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4621. FormatStyle AfterType = getLLVMStyle();
  4622. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4623. verifyFormat("__attribute__((nodebug)) void\n"
  4624. "foo() {}\n",
  4625. AfterType);
  4626. }
  4627. TEST_F(FormatTest, UnderstandsEllipsis) {
  4628. verifyFormat("int printf(const char *fmt, ...);");
  4629. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4630. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4631. FormatStyle PointersLeft = getLLVMStyle();
  4632. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  4633. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4634. }
  4635. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4636. EXPECT_EQ("int *a;\n"
  4637. "int *a;\n"
  4638. "int *a;",
  4639. format("int *a;\n"
  4640. "int* a;\n"
  4641. "int *a;",
  4642. getGoogleStyle()));
  4643. EXPECT_EQ("int* a;\n"
  4644. "int* a;\n"
  4645. "int* a;",
  4646. format("int* a;\n"
  4647. "int* a;\n"
  4648. "int *a;",
  4649. getGoogleStyle()));
  4650. EXPECT_EQ("int *a;\n"
  4651. "int *a;\n"
  4652. "int *a;",
  4653. format("int *a;\n"
  4654. "int * a;\n"
  4655. "int * a;",
  4656. getGoogleStyle()));
  4657. EXPECT_EQ("auto x = [] {\n"
  4658. " int *a;\n"
  4659. " int *a;\n"
  4660. " int *a;\n"
  4661. "};",
  4662. format("auto x=[]{int *a;\n"
  4663. "int * a;\n"
  4664. "int * a;};",
  4665. getGoogleStyle()));
  4666. }
  4667. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  4668. verifyFormat("int f(int &&a) {}");
  4669. verifyFormat("int f(int a, char &&b) {}");
  4670. verifyFormat("void f() { int &&a = b; }");
  4671. verifyGoogleFormat("int f(int a, char&& b) {}");
  4672. verifyGoogleFormat("void f() { int&& a = b; }");
  4673. verifyIndependentOfContext("A<int &&> a;");
  4674. verifyIndependentOfContext("A<int &&, int &&> a;");
  4675. verifyGoogleFormat("A<int&&> a;");
  4676. verifyGoogleFormat("A<int&&, int&&> a;");
  4677. // Not rvalue references:
  4678. verifyFormat("template <bool B, bool C> class A {\n"
  4679. " static_assert(B && C, \"Something is wrong\");\n"
  4680. "};");
  4681. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  4682. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  4683. verifyFormat("#define A(a, b) (a && b)");
  4684. }
  4685. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  4686. verifyFormat("void f() {\n"
  4687. " x[aaaaaaaaa -\n"
  4688. " b] = 23;\n"
  4689. "}",
  4690. getLLVMStyleWithColumns(15));
  4691. }
  4692. TEST_F(FormatTest, FormatsCasts) {
  4693. verifyFormat("Type *A = static_cast<Type *>(P);");
  4694. verifyFormat("Type *A = (Type *)P;");
  4695. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  4696. verifyFormat("int a = (int)(2.0f);");
  4697. verifyFormat("int a = (int)2.0f;");
  4698. verifyFormat("x[(int32)y];");
  4699. verifyFormat("x = (int32)y;");
  4700. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  4701. verifyFormat("int a = (int)*b;");
  4702. verifyFormat("int a = (int)2.0f;");
  4703. verifyFormat("int a = (int)~0;");
  4704. verifyFormat("int a = (int)++a;");
  4705. verifyFormat("int a = (int)sizeof(int);");
  4706. verifyFormat("int a = (int)+2;");
  4707. verifyFormat("my_int a = (my_int)2.0f;");
  4708. verifyFormat("my_int a = (my_int)sizeof(int);");
  4709. verifyFormat("return (my_int)aaa;");
  4710. verifyFormat("#define x ((int)-1)");
  4711. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  4712. verifyFormat("#define p(q) ((int *)&q)");
  4713. verifyFormat("fn(a)(b) + 1;");
  4714. verifyFormat("void f() { my_int a = (my_int)*b; }");
  4715. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  4716. verifyFormat("my_int a = (my_int)~0;");
  4717. verifyFormat("my_int a = (my_int)++a;");
  4718. verifyFormat("my_int a = (my_int)-2;");
  4719. verifyFormat("my_int a = (my_int)1;");
  4720. verifyFormat("my_int a = (my_int *)1;");
  4721. verifyFormat("my_int a = (const my_int)-1;");
  4722. verifyFormat("my_int a = (const my_int *)-1;");
  4723. verifyFormat("my_int a = (my_int)(my_int)-1;");
  4724. verifyFormat("my_int a = (ns::my_int)-2;");
  4725. verifyFormat("case (my_int)ONE:");
  4726. verifyFormat("auto x = (X)this;");
  4727. // FIXME: single value wrapped with paren will be treated as cast.
  4728. verifyFormat("void f(int i = (kValue)*kMask) {}");
  4729. verifyFormat("{ (void)F; }");
  4730. // Don't break after a cast's
  4731. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4732. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  4733. " bbbbbbbbbbbbbbbbbbbbbb);");
  4734. // These are not casts.
  4735. verifyFormat("void f(int *) {}");
  4736. verifyFormat("f(foo)->b;");
  4737. verifyFormat("f(foo).b;");
  4738. verifyFormat("f(foo)(b);");
  4739. verifyFormat("f(foo)[b];");
  4740. verifyFormat("[](foo) { return 4; }(bar);");
  4741. verifyFormat("(*funptr)(foo)[4];");
  4742. verifyFormat("funptrs[4](foo)[4];");
  4743. verifyFormat("void f(int *);");
  4744. verifyFormat("void f(int *) = 0;");
  4745. verifyFormat("void f(SmallVector<int>) {}");
  4746. verifyFormat("void f(SmallVector<int>);");
  4747. verifyFormat("void f(SmallVector<int>) = 0;");
  4748. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  4749. verifyFormat("int a = sizeof(int) * b;");
  4750. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  4751. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  4752. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  4753. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  4754. // These are not casts, but at some point were confused with casts.
  4755. verifyFormat("virtual void foo(int *) override;");
  4756. verifyFormat("virtual void foo(char &) const;");
  4757. verifyFormat("virtual void foo(int *a, char *) const;");
  4758. verifyFormat("int a = sizeof(int *) + b;");
  4759. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  4760. verifyFormat("bool b = f(g<int>) && c;");
  4761. verifyFormat("typedef void (*f)(int i) func;");
  4762. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  4763. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4764. // FIXME: The indentation here is not ideal.
  4765. verifyFormat(
  4766. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4767. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  4768. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  4769. }
  4770. TEST_F(FormatTest, FormatsFunctionTypes) {
  4771. verifyFormat("A<bool()> a;");
  4772. verifyFormat("A<SomeType()> a;");
  4773. verifyFormat("A<void (*)(int, std::string)> a;");
  4774. verifyFormat("A<void *(int)>;");
  4775. verifyFormat("void *(*a)(int *, SomeType *);");
  4776. verifyFormat("int (*func)(void *);");
  4777. verifyFormat("void f() { int (*func)(void *); }");
  4778. verifyFormat("template <class CallbackClass>\n"
  4779. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  4780. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  4781. verifyGoogleFormat("void* (*a)(int);");
  4782. verifyGoogleFormat(
  4783. "template <class CallbackClass>\n"
  4784. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  4785. // Other constructs can look somewhat like function types:
  4786. verifyFormat("A<sizeof(*x)> a;");
  4787. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  4788. verifyFormat("some_var = function(*some_pointer_var)[0];");
  4789. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  4790. verifyFormat("int x = f(&h)();");
  4791. verifyFormat("returnsFunction(&param1, &param2)(param);");
  4792. }
  4793. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  4794. verifyFormat("A (*foo_)[6];");
  4795. verifyFormat("vector<int> (*foo_)[6];");
  4796. }
  4797. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  4798. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4799. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  4800. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  4801. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  4802. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4803. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  4804. // Different ways of ()-initializiation.
  4805. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4806. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  4807. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4808. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  4809. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4810. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  4811. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4812. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  4813. // Lambdas should not confuse the variable declaration heuristic.
  4814. verifyFormat("LooooooooooooooooongType\n"
  4815. " variable(nullptr, [](A *a) {});",
  4816. getLLVMStyleWithColumns(40));
  4817. }
  4818. TEST_F(FormatTest, BreaksLongDeclarations) {
  4819. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  4820. " AnotherNameForTheLongType;");
  4821. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  4822. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4823. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4824. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  4825. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  4826. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  4827. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4828. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4829. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  4830. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4831. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  4832. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4833. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  4834. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4835. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4836. "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
  4837. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4838. "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
  4839. FormatStyle Indented = getLLVMStyle();
  4840. Indented.IndentWrappedFunctionNames = true;
  4841. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4842. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  4843. Indented);
  4844. verifyFormat(
  4845. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4846. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  4847. Indented);
  4848. verifyFormat(
  4849. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  4850. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  4851. Indented);
  4852. verifyFormat(
  4853. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  4854. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  4855. Indented);
  4856. // FIXME: Without the comment, this breaks after "(".
  4857. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  4858. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  4859. getGoogleStyle());
  4860. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  4861. " int LoooooooooooooooooooongParam2) {}");
  4862. verifyFormat(
  4863. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  4864. " SourceLocation L, IdentifierIn *II,\n"
  4865. " Type *T) {}");
  4866. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  4867. "ReallyReaaallyLongFunctionName(\n"
  4868. " const std::string &SomeParameter,\n"
  4869. " const SomeType<string, SomeOtherTemplateParameter>\n"
  4870. " &ReallyReallyLongParameterName,\n"
  4871. " const SomeType<string, SomeOtherTemplateParameter>\n"
  4872. " &AnotherLongParameterName) {}");
  4873. verifyFormat("template <typename A>\n"
  4874. "SomeLoooooooooooooooooooooongType<\n"
  4875. " typename some_namespace::SomeOtherType<A>::Type>\n"
  4876. "Function() {}");
  4877. verifyGoogleFormat(
  4878. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  4879. " aaaaaaaaaaaaaaaaaaaaaaa;");
  4880. verifyGoogleFormat(
  4881. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  4882. " SourceLocation L) {}");
  4883. verifyGoogleFormat(
  4884. "some_namespace::LongReturnType\n"
  4885. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  4886. " int first_long_parameter, int second_parameter) {}");
  4887. verifyGoogleFormat("template <typename T>\n"
  4888. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  4889. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  4890. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4891. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  4892. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4893. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4894. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4895. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4896. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  4897. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  4898. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4899. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  4900. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  4901. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4902. }
  4903. TEST_F(FormatTest, FormatsArrays) {
  4904. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  4905. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  4906. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  4907. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  4908. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  4909. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  4910. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4911. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  4912. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4913. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  4914. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4915. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  4916. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  4917. verifyFormat(
  4918. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  4919. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  4920. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  4921. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
  4922. " .aaaaaaaaaaaaaaaaaaaaaa();");
  4923. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  4924. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  4925. verifyFormat(
  4926. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  4927. " .aaaaaaa[0]\n"
  4928. " .aaaaaaaaaaaaaaaaaaaaaa();");
  4929. verifyFormat("a[::b::c];");
  4930. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  4931. FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
  4932. verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
  4933. }
  4934. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  4935. verifyFormat("(a)->b();");
  4936. verifyFormat("--a;");
  4937. }
  4938. TEST_F(FormatTest, HandlesIncludeDirectives) {
  4939. verifyFormat("#include <string>\n"
  4940. "#include <a/b/c.h>\n"
  4941. "#include \"a/b/string\"\n"
  4942. "#include \"string.h\"\n"
  4943. "#include \"string.h\"\n"
  4944. "#include <a-a>\n"
  4945. "#include < path with space >\n"
  4946. "#include_next <test.h>"
  4947. "#include \"abc.h\" // this is included for ABC\n"
  4948. "#include \"some long include\" // with a comment\n"
  4949. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  4950. getLLVMStyleWithColumns(35));
  4951. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  4952. EXPECT_EQ("#include <a>", format("#include<a>"));
  4953. verifyFormat("#import <string>");
  4954. verifyFormat("#import <a/b/c.h>");
  4955. verifyFormat("#import \"a/b/string\"");
  4956. verifyFormat("#import \"string.h\"");
  4957. verifyFormat("#import \"string.h\"");
  4958. verifyFormat("#if __has_include(<strstream>)\n"
  4959. "#include <strstream>\n"
  4960. "#endif");
  4961. verifyFormat("#define MY_IMPORT <a/b>");
  4962. verifyFormat("#if __has_include(<a/b>)");
  4963. verifyFormat("#if __has_include_next(<a/b>)");
  4964. verifyFormat("#define F __has_include(<a/b>)");
  4965. verifyFormat("#define F __has_include_next(<a/b>)");
  4966. // Protocol buffer definition or missing "#".
  4967. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  4968. getLLVMStyleWithColumns(30));
  4969. FormatStyle Style = getLLVMStyle();
  4970. Style.AlwaysBreakBeforeMultilineStrings = true;
  4971. Style.ColumnLimit = 0;
  4972. verifyFormat("#import \"abc.h\"", Style);
  4973. // But 'import' might also be a regular C++ namespace.
  4974. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4975. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4976. }
  4977. //===----------------------------------------------------------------------===//
  4978. // Error recovery tests.
  4979. //===----------------------------------------------------------------------===//
  4980. TEST_F(FormatTest, IncompleteParameterLists) {
  4981. FormatStyle NoBinPacking = getLLVMStyle();
  4982. NoBinPacking.BinPackParameters = false;
  4983. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  4984. " double *min_x,\n"
  4985. " double *max_x,\n"
  4986. " double *min_y,\n"
  4987. " double *max_y,\n"
  4988. " double *min_z,\n"
  4989. " double *max_z, ) {}",
  4990. NoBinPacking);
  4991. }
  4992. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  4993. verifyFormat("void f() { return; }\n42");
  4994. verifyFormat("void f() {\n"
  4995. " if (0)\n"
  4996. " return;\n"
  4997. "}\n"
  4998. "42");
  4999. verifyFormat("void f() { return }\n42");
  5000. verifyFormat("void f() {\n"
  5001. " if (0)\n"
  5002. " return\n"
  5003. "}\n"
  5004. "42");
  5005. }
  5006. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5007. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5008. EXPECT_EQ("void f() {\n"
  5009. " if (a)\n"
  5010. " return\n"
  5011. "}",
  5012. format("void f ( ) { if ( a ) return }"));
  5013. EXPECT_EQ("namespace N {\n"
  5014. "void f()\n"
  5015. "}",
  5016. format("namespace N { void f() }"));
  5017. EXPECT_EQ("namespace N {\n"
  5018. "void f() {}\n"
  5019. "void g()\n"
  5020. "}",
  5021. format("namespace N { void f( ) { } void g( ) }"));
  5022. }
  5023. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5024. verifyFormat("int aaaaaaaa =\n"
  5025. " // Overlylongcomment\n"
  5026. " b;",
  5027. getLLVMStyleWithColumns(20));
  5028. verifyFormat("function(\n"
  5029. " ShortArgument,\n"
  5030. " LoooooooooooongArgument);\n",
  5031. getLLVMStyleWithColumns(20));
  5032. }
  5033. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5034. verifyFormat("public:");
  5035. verifyFormat("class A {\n"
  5036. "public\n"
  5037. " void f() {}\n"
  5038. "};");
  5039. verifyFormat("public\n"
  5040. "int qwerty;");
  5041. verifyFormat("public\n"
  5042. "B {}");
  5043. verifyFormat("public\n"
  5044. "{}");
  5045. verifyFormat("public\n"
  5046. "B { int x; }");
  5047. }
  5048. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5049. verifyFormat("{");
  5050. verifyFormat("#})");
  5051. verifyNoCrash("(/**/[:!] ?[).");
  5052. }
  5053. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5054. verifyFormat("do {\n}");
  5055. verifyFormat("do {\n}\n"
  5056. "f();");
  5057. verifyFormat("do {\n}\n"
  5058. "wheeee(fun);");
  5059. verifyFormat("do {\n"
  5060. " f();\n"
  5061. "}");
  5062. }
  5063. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5064. verifyFormat("if {\n foo;\n foo();\n}");
  5065. verifyFormat("switch {\n foo;\n foo();\n}");
  5066. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5067. verifyFormat("while {\n foo;\n foo();\n}");
  5068. verifyFormat("do {\n foo;\n foo();\n} while;");
  5069. }
  5070. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5071. verifyIncompleteFormat("namespace {\n"
  5072. "class Foo { Foo (\n"
  5073. "};\n"
  5074. "} // comment");
  5075. }
  5076. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5077. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5078. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5079. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5080. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5081. EXPECT_EQ("{\n"
  5082. " {\n"
  5083. " breakme(\n"
  5084. " qwe);\n"
  5085. " }\n",
  5086. format("{\n"
  5087. " {\n"
  5088. " breakme(qwe);\n"
  5089. "}\n",
  5090. getLLVMStyleWithColumns(10)));
  5091. }
  5092. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5093. verifyFormat("int x = {\n"
  5094. " avariable,\n"
  5095. " b(alongervariable)};",
  5096. getLLVMStyleWithColumns(25));
  5097. }
  5098. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5099. verifyFormat("return (a)(b){1, 2, 3};");
  5100. }
  5101. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5102. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5103. verifyFormat("vector<int> x{\n"
  5104. " 1, 2, 3, 4,\n"
  5105. "};");
  5106. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5107. verifyFormat("f({1, 2});");
  5108. verifyFormat("auto v = Foo{-1};");
  5109. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5110. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5111. verifyFormat("new vector<int>{1, 2, 3};");
  5112. verifyFormat("new int[3]{1, 2, 3};");
  5113. verifyFormat("new int{1};");
  5114. verifyFormat("return {arg1, arg2};");
  5115. verifyFormat("return {arg1, SomeType{parameter}};");
  5116. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5117. verifyFormat("new T{arg1, arg2};");
  5118. verifyFormat("f(MyMap[{composite, key}]);");
  5119. verifyFormat("class Class {\n"
  5120. " T member = {arg1, arg2};\n"
  5121. "};");
  5122. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5123. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5124. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5125. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5126. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5127. verifyFormat("auto i = decltype(x){};");
  5128. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5129. verifyFormat("Node n{1, Node{1000}, //\n"
  5130. " 2};");
  5131. verifyFormat("Aaaa aaaaaaa{\n"
  5132. " {\n"
  5133. " aaaa,\n"
  5134. " },\n"
  5135. "};");
  5136. verifyFormat("class C : public D {\n"
  5137. " SomeClass SC{2};\n"
  5138. "};");
  5139. verifyFormat("class C : public A {\n"
  5140. " class D : public B {\n"
  5141. " void f() { int i{2}; }\n"
  5142. " };\n"
  5143. "};");
  5144. verifyFormat("#define A {a, a},");
  5145. // Cases where distinguising braced lists and blocks is hard.
  5146. verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
  5147. verifyFormat("void f() {\n"
  5148. " return; // comment\n"
  5149. "}\n"
  5150. "SomeType t;");
  5151. verifyFormat("void f() {\n"
  5152. " if (a) {\n"
  5153. " f();\n"
  5154. " }\n"
  5155. "}\n"
  5156. "SomeType t;");
  5157. // In combination with BinPackArguments = false.
  5158. FormatStyle NoBinPacking = getLLVMStyle();
  5159. NoBinPacking.BinPackArguments = false;
  5160. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5161. " bbbbb,\n"
  5162. " ccccc,\n"
  5163. " ddddd,\n"
  5164. " eeeee,\n"
  5165. " ffffff,\n"
  5166. " ggggg,\n"
  5167. " hhhhhh,\n"
  5168. " iiiiii,\n"
  5169. " jjjjjj,\n"
  5170. " kkkkkk};",
  5171. NoBinPacking);
  5172. verifyFormat("const Aaaaaa aaaaa = {\n"
  5173. " aaaaa,\n"
  5174. " bbbbb,\n"
  5175. " ccccc,\n"
  5176. " ddddd,\n"
  5177. " eeeee,\n"
  5178. " ffffff,\n"
  5179. " ggggg,\n"
  5180. " hhhhhh,\n"
  5181. " iiiiii,\n"
  5182. " jjjjjj,\n"
  5183. " kkkkkk,\n"
  5184. "};",
  5185. NoBinPacking);
  5186. verifyFormat(
  5187. "const Aaaaaa aaaaa = {\n"
  5188. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5189. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5190. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5191. "};",
  5192. NoBinPacking);
  5193. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5194. // this might be utterly useless in real code.
  5195. verifyFormat("Constructor::Constructor()\n"
  5196. " : some_value{ //\n"
  5197. " aaaaaaa, //\n"
  5198. " bbbbbbb} {}");
  5199. // In braced lists, the first comment is always assumed to belong to the
  5200. // first element. Thus, it can be moved to the next or previous line as
  5201. // appropriate.
  5202. EXPECT_EQ("function({// First element:\n"
  5203. " 1,\n"
  5204. " // Second element:\n"
  5205. " 2});",
  5206. format("function({\n"
  5207. " // First element:\n"
  5208. " 1,\n"
  5209. " // Second element:\n"
  5210. " 2});"));
  5211. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5212. " // First element:\n"
  5213. " 1,\n"
  5214. " // Second element:\n"
  5215. " 2};",
  5216. format("std::vector<int> MyNumbers{// First element:\n"
  5217. " 1,\n"
  5218. " // Second element:\n"
  5219. " 2};",
  5220. getLLVMStyleWithColumns(30)));
  5221. // A trailing comma should still lead to an enforced line break.
  5222. EXPECT_EQ("vector<int> SomeVector = {\n"
  5223. " // aaa\n"
  5224. " 1, 2,\n"
  5225. "};",
  5226. format("vector<int> SomeVector = { // aaa\n"
  5227. " 1, 2, };"));
  5228. FormatStyle ExtraSpaces = getLLVMStyle();
  5229. ExtraSpaces.Cpp11BracedListStyle = false;
  5230. ExtraSpaces.ColumnLimit = 75;
  5231. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5232. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5233. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5234. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5235. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5236. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5237. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5238. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5239. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5240. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5241. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5242. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5243. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5244. verifyFormat("class Class {\n"
  5245. " T member = { arg1, arg2 };\n"
  5246. "};",
  5247. ExtraSpaces);
  5248. verifyFormat(
  5249. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5250. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5251. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5252. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5253. ExtraSpaces);
  5254. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5255. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5256. ExtraSpaces);
  5257. verifyFormat(
  5258. "someFunction(OtherParam,\n"
  5259. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5260. " param1, param2,\n"
  5261. " // comment 2\n"
  5262. " param3, param4 });",
  5263. ExtraSpaces);
  5264. verifyFormat(
  5265. "std::this_thread::sleep_for(\n"
  5266. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5267. ExtraSpaces);
  5268. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
  5269. " aaaaaaa,\n"
  5270. " aaaaaaaaaa,\n"
  5271. " aaaaa,\n"
  5272. " aaaaaaaaaaaaaaa,\n"
  5273. " aaa,\n"
  5274. " aaaaaaaaaa,\n"
  5275. " a,\n"
  5276. " aaaaaaaaaaaaaaaaaaaaa,\n"
  5277. " aaaaaaaaaaaa,\n"
  5278. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  5279. " aaaaaaa,\n"
  5280. " a};");
  5281. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5282. }
  5283. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5284. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5285. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5286. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5287. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5288. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5289. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5290. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  5291. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5292. " 1, 22, 333, 4444, 55555, //\n"
  5293. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5294. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5295. verifyFormat(
  5296. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5297. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5298. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5299. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5300. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5301. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5302. " 7777777};");
  5303. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5304. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5305. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5306. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5307. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5308. " // Separating comment.\n"
  5309. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5310. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5311. " // Leading comment\n"
  5312. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5313. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5314. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5315. " 1, 1, 1, 1};",
  5316. getLLVMStyleWithColumns(39));
  5317. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5318. " 1, 1, 1, 1};",
  5319. getLLVMStyleWithColumns(38));
  5320. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5321. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5322. getLLVMStyleWithColumns(43));
  5323. verifyFormat(
  5324. "static unsigned SomeValues[10][3] = {\n"
  5325. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  5326. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  5327. verifyFormat("static auto fields = new vector<string>{\n"
  5328. " \"aaaaaaaaaaaaa\",\n"
  5329. " \"aaaaaaaaaaaaa\",\n"
  5330. " \"aaaaaaaaaaaa\",\n"
  5331. " \"aaaaaaaaaaaaaa\",\n"
  5332. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  5333. " \"aaaaaaaaaaaa\",\n"
  5334. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  5335. "};");
  5336. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  5337. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  5338. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  5339. " 3, cccccccccccccccccccccc};",
  5340. getLLVMStyleWithColumns(60));
  5341. // Trailing commas.
  5342. verifyFormat("vector<int> x = {\n"
  5343. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5344. "};",
  5345. getLLVMStyleWithColumns(39));
  5346. verifyFormat("vector<int> x = {\n"
  5347. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5348. "};",
  5349. getLLVMStyleWithColumns(39));
  5350. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5351. " 1, 1, 1, 1,\n"
  5352. " /**/ /**/};",
  5353. getLLVMStyleWithColumns(39));
  5354. // Trailing comment in the first line.
  5355. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  5356. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  5357. " 111111111, 222222222, 3333333333, 444444444, //\n"
  5358. " 11111111, 22222222, 333333333, 44444444};");
  5359. // Trailing comment in the last line.
  5360. verifyFormat("int aaaaa[] = {\n"
  5361. " 1, 2, 3, // comment\n"
  5362. " 4, 5, 6 // comment\n"
  5363. "};");
  5364. // With nested lists, we should either format one item per line or all nested
  5365. // lists one on line.
  5366. // FIXME: For some nested lists, we can do better.
  5367. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5368. " {aaaaaaaaaaaaaaaaaaa},\n"
  5369. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5370. " {aaaaaaaaaaaaaaaaa}};",
  5371. getLLVMStyleWithColumns(60));
  5372. verifyFormat(
  5373. "SomeStruct my_struct_array = {\n"
  5374. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5375. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5376. " {aaa, aaa},\n"
  5377. " {aaa, aaa},\n"
  5378. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5379. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5380. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5381. // No column layout should be used here.
  5382. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5383. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5384. verifyNoCrash("a<,");
  5385. // No braced initializer here.
  5386. verifyFormat("void f() {\n"
  5387. " struct Dummy {};\n"
  5388. " f(v);\n"
  5389. "}");
  5390. // Long lists should be formatted in columns even if they are nested.
  5391. verifyFormat(
  5392. "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5393. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5394. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5395. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5396. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5397. " 1, 22, 333, 4444, 55555, 666666, 7777777});");
  5398. // Allow "single-column" layout even if that violates the column limit. There
  5399. // isn't going to be a better way.
  5400. verifyFormat("std::vector<int> a = {\n"
  5401. " aaaaaaaa,\n"
  5402. " aaaaaaaa,\n"
  5403. " aaaaaaaa,\n"
  5404. " aaaaaaaa,\n"
  5405. " aaaaaaaaaa,\n"
  5406. " aaaaaaaa,\n"
  5407. " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
  5408. getLLVMStyleWithColumns(30));
  5409. verifyFormat("vector<int> aaaa = {\n"
  5410. " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5411. " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5412. " aaaaaa.aaaaaaa,\n"
  5413. " aaaaaa.aaaaaaa,\n"
  5414. " aaaaaa.aaaaaaa,\n"
  5415. " aaaaaa.aaaaaaa,\n"
  5416. "};");
  5417. // Don't create hanging lists.
  5418. verifyFormat("someFunction(Param, {List1, List2,\n"
  5419. " List3});",
  5420. getLLVMStyleWithColumns(35));
  5421. verifyFormat("someFunction(Param, Param,\n"
  5422. " {List1, List2,\n"
  5423. " List3});",
  5424. getLLVMStyleWithColumns(35));
  5425. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
  5426. " aaaaaaaaaaaaaaaaaaaaaaa);");
  5427. }
  5428. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5429. FormatStyle DoNotMerge = getLLVMStyle();
  5430. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5431. verifyFormat("void f() { return 42; }");
  5432. verifyFormat("void f() {\n"
  5433. " return 42;\n"
  5434. "}",
  5435. DoNotMerge);
  5436. verifyFormat("void f() {\n"
  5437. " // Comment\n"
  5438. "}");
  5439. verifyFormat("{\n"
  5440. "#error {\n"
  5441. " int a;\n"
  5442. "}");
  5443. verifyFormat("{\n"
  5444. " int a;\n"
  5445. "#error {\n"
  5446. "}");
  5447. verifyFormat("void f() {} // comment");
  5448. verifyFormat("void f() { int a; } // comment");
  5449. verifyFormat("void f() {\n"
  5450. "} // comment",
  5451. DoNotMerge);
  5452. verifyFormat("void f() {\n"
  5453. " int a;\n"
  5454. "} // comment",
  5455. DoNotMerge);
  5456. verifyFormat("void f() {\n"
  5457. "} // comment",
  5458. getLLVMStyleWithColumns(15));
  5459. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5460. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5461. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5462. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5463. verifyFormat("class C {\n"
  5464. " C()\n"
  5465. " : iiiiiiii(nullptr),\n"
  5466. " kkkkkkk(nullptr),\n"
  5467. " mmmmmmm(nullptr),\n"
  5468. " nnnnnnn(nullptr) {}\n"
  5469. "};",
  5470. getGoogleStyle());
  5471. FormatStyle NoColumnLimit = getLLVMStyle();
  5472. NoColumnLimit.ColumnLimit = 0;
  5473. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5474. EXPECT_EQ("class C {\n"
  5475. " A() : b(0) {}\n"
  5476. "};",
  5477. format("class C{A():b(0){}};", NoColumnLimit));
  5478. EXPECT_EQ("A()\n"
  5479. " : b(0) {\n"
  5480. "}",
  5481. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5482. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5483. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5484. FormatStyle::SFS_None;
  5485. EXPECT_EQ("A()\n"
  5486. " : b(0) {\n"
  5487. "}",
  5488. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5489. EXPECT_EQ("A()\n"
  5490. " : b(0) {\n"
  5491. "}",
  5492. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5493. verifyFormat("#define A \\\n"
  5494. " void f() { \\\n"
  5495. " int i; \\\n"
  5496. " }",
  5497. getLLVMStyleWithColumns(20));
  5498. verifyFormat("#define A \\\n"
  5499. " void f() { int i; }",
  5500. getLLVMStyleWithColumns(21));
  5501. verifyFormat("#define A \\\n"
  5502. " void f() { \\\n"
  5503. " int i; \\\n"
  5504. " } \\\n"
  5505. " int j;",
  5506. getLLVMStyleWithColumns(22));
  5507. verifyFormat("#define A \\\n"
  5508. " void f() { int i; } \\\n"
  5509. " int j;",
  5510. getLLVMStyleWithColumns(23));
  5511. }
  5512. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5513. FormatStyle MergeInlineOnly = getLLVMStyle();
  5514. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5515. verifyFormat("class C {\n"
  5516. " int f() { return 42; }\n"
  5517. "};",
  5518. MergeInlineOnly);
  5519. verifyFormat("int f() {\n"
  5520. " return 42;\n"
  5521. "}",
  5522. MergeInlineOnly);
  5523. }
  5524. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5525. // Elaborate type variable declarations.
  5526. verifyFormat("struct foo a = {bar};\nint n;");
  5527. verifyFormat("class foo a = {bar};\nint n;");
  5528. verifyFormat("union foo a = {bar};\nint n;");
  5529. // Elaborate types inside function definitions.
  5530. verifyFormat("struct foo f() {}\nint n;");
  5531. verifyFormat("class foo f() {}\nint n;");
  5532. verifyFormat("union foo f() {}\nint n;");
  5533. // Templates.
  5534. verifyFormat("template <class X> void f() {}\nint n;");
  5535. verifyFormat("template <struct X> void f() {}\nint n;");
  5536. verifyFormat("template <union X> void f() {}\nint n;");
  5537. // Actual definitions...
  5538. verifyFormat("struct {\n} n;");
  5539. verifyFormat(
  5540. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5541. verifyFormat("union Z {\n int n;\n} x;");
  5542. verifyFormat("class MACRO Z {\n} n;");
  5543. verifyFormat("class MACRO(X) Z {\n} n;");
  5544. verifyFormat("class __attribute__(X) Z {\n} n;");
  5545. verifyFormat("class __declspec(X) Z {\n} n;");
  5546. verifyFormat("class A##B##C {\n} n;");
  5547. verifyFormat("class alignas(16) Z {\n} n;");
  5548. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  5549. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  5550. // Redefinition from nested context:
  5551. verifyFormat("class A::B::C {\n} n;");
  5552. // Template definitions.
  5553. verifyFormat(
  5554. "template <typename F>\n"
  5555. "Matcher(const Matcher<F> &Other,\n"
  5556. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5557. " !is_same<F, T>::value>::type * = 0)\n"
  5558. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5559. // FIXME: This is still incorrectly handled at the formatter side.
  5560. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5561. verifyFormat("int i = SomeFunction(a<b, a> b);");
  5562. // FIXME:
  5563. // This now gets parsed incorrectly as class definition.
  5564. // verifyFormat("class A<int> f() {\n}\nint n;");
  5565. // Elaborate types where incorrectly parsing the structural element would
  5566. // break the indent.
  5567. verifyFormat("if (true)\n"
  5568. " class X x;\n"
  5569. "else\n"
  5570. " f();\n");
  5571. // This is simply incomplete. Formatting is not important, but must not crash.
  5572. verifyFormat("class A:");
  5573. }
  5574. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5575. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5576. format("#error Leave all white!!!!! space* alone!\n"));
  5577. EXPECT_EQ(
  5578. "#warning Leave all white!!!!! space* alone!\n",
  5579. format("#warning Leave all white!!!!! space* alone!\n"));
  5580. EXPECT_EQ("#error 1", format(" # error 1"));
  5581. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5582. }
  5583. TEST_F(FormatTest, FormatHashIfExpressions) {
  5584. verifyFormat("#if AAAA && BBBB");
  5585. verifyFormat("#if (AAAA && BBBB)");
  5586. verifyFormat("#elif (AAAA && BBBB)");
  5587. // FIXME: Come up with a better indentation for #elif.
  5588. verifyFormat(
  5589. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5590. " defined(BBBBBBBB)\n"
  5591. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5592. " defined(BBBBBBBB)\n"
  5593. "#endif",
  5594. getLLVMStyleWithColumns(65));
  5595. }
  5596. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5597. FormatStyle AllowsMergedIf = getGoogleStyle();
  5598. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5599. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5600. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5601. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5602. EXPECT_EQ("if (true) return 42;",
  5603. format("if (true)\nreturn 42;", AllowsMergedIf));
  5604. FormatStyle ShortMergedIf = AllowsMergedIf;
  5605. ShortMergedIf.ColumnLimit = 25;
  5606. verifyFormat("#define A \\\n"
  5607. " if (true) return 42;",
  5608. ShortMergedIf);
  5609. verifyFormat("#define A \\\n"
  5610. " f(); \\\n"
  5611. " if (true)\n"
  5612. "#define B",
  5613. ShortMergedIf);
  5614. verifyFormat("#define A \\\n"
  5615. " f(); \\\n"
  5616. " if (true)\n"
  5617. "g();",
  5618. ShortMergedIf);
  5619. verifyFormat("{\n"
  5620. "#ifdef A\n"
  5621. " // Comment\n"
  5622. " if (true) continue;\n"
  5623. "#endif\n"
  5624. " // Comment\n"
  5625. " if (true) continue;\n"
  5626. "}",
  5627. ShortMergedIf);
  5628. ShortMergedIf.ColumnLimit = 29;
  5629. verifyFormat("#define A \\\n"
  5630. " if (aaaaaaaaaa) return 1; \\\n"
  5631. " return 2;",
  5632. ShortMergedIf);
  5633. ShortMergedIf.ColumnLimit = 28;
  5634. verifyFormat("#define A \\\n"
  5635. " if (aaaaaaaaaa) \\\n"
  5636. " return 1; \\\n"
  5637. " return 2;",
  5638. ShortMergedIf);
  5639. }
  5640. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5641. verifyFormat("void f(int *a);");
  5642. verifyFormat("void f() { f(fint * b); }");
  5643. verifyFormat("class A {\n void f(int *a);\n};");
  5644. verifyFormat("class A {\n int *a;\n};");
  5645. verifyFormat("namespace a {\n"
  5646. "namespace b {\n"
  5647. "class A {\n"
  5648. " void f() {}\n"
  5649. " int *a;\n"
  5650. "};\n"
  5651. "}\n"
  5652. "}");
  5653. }
  5654. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  5655. verifyFormat("while");
  5656. verifyFormat("operator");
  5657. }
  5658. TEST_F(FormatTest, SkipsDeeplyNestedLines) {
  5659. // This code would be painfully slow to format if we didn't skip it.
  5660. std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
  5661. "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
  5662. "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
  5663. "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
  5664. "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
  5665. "A(1, 1)\n"
  5666. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
  5667. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5668. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5669. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5670. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5671. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5672. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5673. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5674. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
  5675. ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
  5676. // Deeply nested part is untouched, rest is formatted.
  5677. EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
  5678. format(std::string("int i;\n") + Code + "int j;\n",
  5679. getLLVMStyle(), IC_ExpectIncomplete));
  5680. }
  5681. //===----------------------------------------------------------------------===//
  5682. // Objective-C tests.
  5683. //===----------------------------------------------------------------------===//
  5684. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  5685. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  5686. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  5687. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  5688. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  5689. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  5690. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  5691. format("-(NSInteger)Method3:(id)anObject;"));
  5692. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  5693. format("-(NSInteger)Method4:(id)anObject;"));
  5694. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  5695. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  5696. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  5697. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  5698. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  5699. "forAllCells:(BOOL)flag;",
  5700. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  5701. "forAllCells:(BOOL)flag;"));
  5702. // Very long objectiveC method declaration.
  5703. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  5704. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  5705. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  5706. " inRange:(NSRange)range\n"
  5707. " outRange:(NSRange)out_range\n"
  5708. " outRange1:(NSRange)out_range1\n"
  5709. " outRange2:(NSRange)out_range2\n"
  5710. " outRange3:(NSRange)out_range3\n"
  5711. " outRange4:(NSRange)out_range4\n"
  5712. " outRange5:(NSRange)out_range5\n"
  5713. " outRange6:(NSRange)out_range6\n"
  5714. " outRange7:(NSRange)out_range7\n"
  5715. " outRange8:(NSRange)out_range8\n"
  5716. " outRange9:(NSRange)out_range9;");
  5717. // When the function name has to be wrapped.
  5718. FormatStyle Style = getLLVMStyle();
  5719. Style.IndentWrappedFunctionNames = false;
  5720. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  5721. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  5722. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  5723. "}",
  5724. Style);
  5725. Style.IndentWrappedFunctionNames = true;
  5726. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  5727. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  5728. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  5729. "}",
  5730. Style);
  5731. verifyFormat("- (int)sum:(vector<int>)numbers;");
  5732. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  5733. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  5734. // protocol lists (but not for template classes):
  5735. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  5736. verifyFormat("- (int (*)())foo:(int (*)())f;");
  5737. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  5738. // If there's no return type (very rare in practice!), LLVM and Google style
  5739. // agree.
  5740. verifyFormat("- foo;");
  5741. verifyFormat("- foo:(int)f;");
  5742. verifyGoogleFormat("- foo:(int)foo;");
  5743. }
  5744. TEST_F(FormatTest, BreaksStringLiterals) {
  5745. EXPECT_EQ("\"some text \"\n"
  5746. "\"other\";",
  5747. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  5748. EXPECT_EQ("\"some text \"\n"
  5749. "\"other\";",
  5750. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  5751. EXPECT_EQ(
  5752. "#define A \\\n"
  5753. " \"some \" \\\n"
  5754. " \"text \" \\\n"
  5755. " \"other\";",
  5756. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  5757. EXPECT_EQ(
  5758. "#define A \\\n"
  5759. " \"so \" \\\n"
  5760. " \"text \" \\\n"
  5761. " \"other\";",
  5762. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  5763. EXPECT_EQ("\"some text\"",
  5764. format("\"some text\"", getLLVMStyleWithColumns(1)));
  5765. EXPECT_EQ("\"some text\"",
  5766. format("\"some text\"", getLLVMStyleWithColumns(11)));
  5767. EXPECT_EQ("\"some \"\n"
  5768. "\"text\"",
  5769. format("\"some text\"", getLLVMStyleWithColumns(10)));
  5770. EXPECT_EQ("\"some \"\n"
  5771. "\"text\"",
  5772. format("\"some text\"", getLLVMStyleWithColumns(7)));
  5773. EXPECT_EQ("\"some\"\n"
  5774. "\" tex\"\n"
  5775. "\"t\"",
  5776. format("\"some text\"", getLLVMStyleWithColumns(6)));
  5777. EXPECT_EQ("\"some\"\n"
  5778. "\" tex\"\n"
  5779. "\" and\"",
  5780. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  5781. EXPECT_EQ("\"some\"\n"
  5782. "\"/tex\"\n"
  5783. "\"/and\"",
  5784. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  5785. EXPECT_EQ("variable =\n"
  5786. " \"long string \"\n"
  5787. " \"literal\";",
  5788. format("variable = \"long string literal\";",
  5789. getLLVMStyleWithColumns(20)));
  5790. EXPECT_EQ("variable = f(\n"
  5791. " \"long string \"\n"
  5792. " \"literal\",\n"
  5793. " short,\n"
  5794. " loooooooooooooooooooong);",
  5795. format("variable = f(\"long string literal\", short, "
  5796. "loooooooooooooooooooong);",
  5797. getLLVMStyleWithColumns(20)));
  5798. EXPECT_EQ(
  5799. "f(g(\"long string \"\n"
  5800. " \"literal\"),\n"
  5801. " b);",
  5802. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  5803. EXPECT_EQ("f(g(\"long string \"\n"
  5804. " \"literal\",\n"
  5805. " a),\n"
  5806. " b);",
  5807. format("f(g(\"long string literal\", a), b);",
  5808. getLLVMStyleWithColumns(20)));
  5809. EXPECT_EQ(
  5810. "f(\"one two\".split(\n"
  5811. " variable));",
  5812. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  5813. EXPECT_EQ("f(\"one two three four five six \"\n"
  5814. " \"seven\".split(\n"
  5815. " really_looooong_variable));",
  5816. format("f(\"one two three four five six seven\"."
  5817. "split(really_looooong_variable));",
  5818. getLLVMStyleWithColumns(33)));
  5819. EXPECT_EQ("f(\"some \"\n"
  5820. " \"text\",\n"
  5821. " other);",
  5822. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  5823. // Only break as a last resort.
  5824. verifyFormat(
  5825. "aaaaaaaaaaaaaaaaaaaa(\n"
  5826. " aaaaaaaaaaaaaaaaaaaa,\n"
  5827. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  5828. EXPECT_EQ("\"splitmea\"\n"
  5829. "\"trandomp\"\n"
  5830. "\"oint\"",
  5831. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  5832. EXPECT_EQ("\"split/\"\n"
  5833. "\"pathat/\"\n"
  5834. "\"slashes\"",
  5835. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  5836. EXPECT_EQ("\"split/\"\n"
  5837. "\"pathat/\"\n"
  5838. "\"slashes\"",
  5839. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  5840. EXPECT_EQ("\"split at \"\n"
  5841. "\"spaces/at/\"\n"
  5842. "\"slashes.at.any$\"\n"
  5843. "\"non-alphanumeric%\"\n"
  5844. "\"1111111111characte\"\n"
  5845. "\"rs\"",
  5846. format("\"split at "
  5847. "spaces/at/"
  5848. "slashes.at."
  5849. "any$non-"
  5850. "alphanumeric%"
  5851. "1111111111characte"
  5852. "rs\"",
  5853. getLLVMStyleWithColumns(20)));
  5854. // Verify that splitting the strings understands
  5855. // Style::AlwaysBreakBeforeMultilineStrings.
  5856. EXPECT_EQ(
  5857. "aaaaaaaaaaaa(\n"
  5858. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  5859. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  5860. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  5861. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  5862. "aaaaaaaaaaaaaaaaaaaaaa\");",
  5863. getGoogleStyle()));
  5864. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5865. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  5866. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  5867. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  5868. "aaaaaaaaaaaaaaaaaaaaaa\";",
  5869. getGoogleStyle()));
  5870. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5871. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  5872. format("llvm::outs() << "
  5873. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  5874. "aaaaaaaaaaaaaaaaaaa\";"));
  5875. EXPECT_EQ("ffff(\n"
  5876. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5877. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  5878. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  5879. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  5880. getGoogleStyle()));
  5881. FormatStyle Style = getLLVMStyleWithColumns(12);
  5882. Style.BreakStringLiterals = false;
  5883. EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
  5884. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  5885. AlignLeft.AlignEscapedNewlinesLeft = true;
  5886. EXPECT_EQ("#define A \\\n"
  5887. " \"some \" \\\n"
  5888. " \"text \" \\\n"
  5889. " \"other\";",
  5890. format("#define A \"some text other\";", AlignLeft));
  5891. }
  5892. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  5893. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  5894. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  5895. EXPECT_EQ("int i = a(b());",
  5896. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  5897. }
  5898. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  5899. EXPECT_EQ(
  5900. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  5901. "(\n"
  5902. " \"x\t\");",
  5903. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  5904. "aaaaaaa("
  5905. "\"x\t\");"));
  5906. }
  5907. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  5908. EXPECT_EQ(
  5909. "u8\"utf8 string \"\n"
  5910. "u8\"literal\";",
  5911. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  5912. EXPECT_EQ(
  5913. "u\"utf16 string \"\n"
  5914. "u\"literal\";",
  5915. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  5916. EXPECT_EQ(
  5917. "U\"utf32 string \"\n"
  5918. "U\"literal\";",
  5919. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  5920. EXPECT_EQ("L\"wide string \"\n"
  5921. "L\"literal\";",
  5922. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  5923. EXPECT_EQ("@\"NSString \"\n"
  5924. "@\"literal\";",
  5925. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  5926. // This input makes clang-format try to split the incomplete unicode escape
  5927. // sequence, which used to lead to a crasher.
  5928. verifyNoCrash(
  5929. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  5930. getLLVMStyleWithColumns(60));
  5931. }
  5932. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  5933. FormatStyle Style = getGoogleStyleWithColumns(15);
  5934. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  5935. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  5936. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  5937. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  5938. EXPECT_EQ("u8R\"x(raw literal)x\";",
  5939. format("u8R\"x(raw literal)x\";", Style));
  5940. }
  5941. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  5942. FormatStyle Style = getLLVMStyleWithColumns(20);
  5943. EXPECT_EQ(
  5944. "_T(\"aaaaaaaaaaaaaa\")\n"
  5945. "_T(\"aaaaaaaaaaaaaa\")\n"
  5946. "_T(\"aaaaaaaaaaaa\")",
  5947. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  5948. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  5949. " _T(\"aaaaaa\"),\n"
  5950. " z);",
  5951. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  5952. // FIXME: Handle embedded spaces in one iteration.
  5953. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  5954. // "_T(\"aaaaaaaaaaaaa\")\n"
  5955. // "_T(\"aaaaaaaaaaaaa\")\n"
  5956. // "_T(\"a\")",
  5957. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  5958. // getLLVMStyleWithColumns(20)));
  5959. EXPECT_EQ(
  5960. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  5961. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  5962. EXPECT_EQ("f(\n"
  5963. "#if !TEST\n"
  5964. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  5965. "#endif\n"
  5966. " );",
  5967. format("f(\n"
  5968. "#if !TEST\n"
  5969. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  5970. "#endif\n"
  5971. ");"));
  5972. EXPECT_EQ("f(\n"
  5973. "\n"
  5974. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  5975. format("f(\n"
  5976. "\n"
  5977. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  5978. }
  5979. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  5980. EXPECT_EQ(
  5981. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5982. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5983. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  5984. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5985. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5986. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  5987. }
  5988. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  5989. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  5990. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  5991. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  5992. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5993. ")x\",\n"
  5994. " a),\n"
  5995. " b);",
  5996. format("fffffffffff(g(R\"x(\n"
  5997. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5998. ")x\", a), b);",
  5999. getGoogleStyleWithColumns(20)));
  6000. EXPECT_EQ("fffffffffff(\n"
  6001. " g(R\"x(qqq\n"
  6002. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6003. ")x\",\n"
  6004. " a),\n"
  6005. " b);",
  6006. format("fffffffffff(g(R\"x(qqq\n"
  6007. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6008. ")x\", a), b);",
  6009. getGoogleStyleWithColumns(20)));
  6010. EXPECT_EQ("fffffffffff(R\"x(\n"
  6011. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6012. ")x\");",
  6013. format("fffffffffff(R\"x(\n"
  6014. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6015. ")x\");",
  6016. getGoogleStyleWithColumns(20)));
  6017. EXPECT_EQ("fffffffffff(R\"x(\n"
  6018. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6019. ")x\" + bbbbbb);",
  6020. format("fffffffffff(R\"x(\n"
  6021. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6022. ")x\" + bbbbbb);",
  6023. getGoogleStyleWithColumns(20)));
  6024. EXPECT_EQ("fffffffffff(\n"
  6025. " R\"x(\n"
  6026. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6027. ")x\" +\n"
  6028. " bbbbbb);",
  6029. format("fffffffffff(\n"
  6030. " R\"x(\n"
  6031. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6032. ")x\" + bbbbbb);",
  6033. getGoogleStyleWithColumns(20)));
  6034. }
  6035. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6036. verifyFormat("string a = \"unterminated;");
  6037. EXPECT_EQ("function(\"unterminated,\n"
  6038. " OtherParameter);",
  6039. format("function( \"unterminated,\n"
  6040. " OtherParameter);"));
  6041. }
  6042. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6043. FormatStyle Style = getLLVMStyle();
  6044. Style.Standard = FormatStyle::LS_Cpp03;
  6045. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6046. format("#define x(_a) printf(\"foo\"_a);", Style));
  6047. }
  6048. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  6049. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6050. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6051. " \"ddeeefff\");",
  6052. format("someFunction(\"aaabbbcccdddeeefff\");",
  6053. getLLVMStyleWithColumns(25)));
  6054. EXPECT_EQ("someFunction1234567890(\n"
  6055. " \"aaabbbcccdddeeefff\");",
  6056. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6057. getLLVMStyleWithColumns(26)));
  6058. EXPECT_EQ("someFunction1234567890(\n"
  6059. " \"aaabbbcccdddeeeff\"\n"
  6060. " \"f\");",
  6061. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6062. getLLVMStyleWithColumns(25)));
  6063. EXPECT_EQ("someFunction1234567890(\n"
  6064. " \"aaabbbcccdddeeeff\"\n"
  6065. " \"f\");",
  6066. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6067. getLLVMStyleWithColumns(24)));
  6068. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6069. " \"ddde \"\n"
  6070. " \"efff\");",
  6071. format("someFunction(\"aaabbbcc ddde efff\");",
  6072. getLLVMStyleWithColumns(25)));
  6073. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  6074. " \"ddeeefff\");",
  6075. format("someFunction(\"aaabbbccc ddeeefff\");",
  6076. getLLVMStyleWithColumns(25)));
  6077. EXPECT_EQ("someFunction1234567890(\n"
  6078. " \"aaabb \"\n"
  6079. " \"cccdddeeefff\");",
  6080. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  6081. getLLVMStyleWithColumns(25)));
  6082. EXPECT_EQ("#define A \\\n"
  6083. " string s = \\\n"
  6084. " \"123456789\" \\\n"
  6085. " \"0\"; \\\n"
  6086. " int i;",
  6087. format("#define A string s = \"1234567890\"; int i;",
  6088. getLLVMStyleWithColumns(20)));
  6089. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  6090. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6091. " \"dddeeeff\"\n"
  6092. " \"f\");",
  6093. format("someFunction(\"aaabbbcc dddeeefff\");",
  6094. getLLVMStyleWithColumns(25)));
  6095. }
  6096. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  6097. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  6098. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  6099. EXPECT_EQ("\"test\"\n"
  6100. "\"\\n\"",
  6101. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  6102. EXPECT_EQ("\"tes\\\\\"\n"
  6103. "\"n\"",
  6104. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  6105. EXPECT_EQ("\"\\\\\\\\\"\n"
  6106. "\"\\n\"",
  6107. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  6108. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  6109. EXPECT_EQ("\"\\uff01\"\n"
  6110. "\"test\"",
  6111. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  6112. EXPECT_EQ("\"\\Uff01ff02\"",
  6113. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  6114. EXPECT_EQ("\"\\x000000000001\"\n"
  6115. "\"next\"",
  6116. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  6117. EXPECT_EQ("\"\\x000000000001next\"",
  6118. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  6119. EXPECT_EQ("\"\\x000000000001\"",
  6120. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  6121. EXPECT_EQ("\"test\"\n"
  6122. "\"\\000000\"\n"
  6123. "\"000001\"",
  6124. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  6125. EXPECT_EQ("\"test\\000\"\n"
  6126. "\"00000000\"\n"
  6127. "\"1\"",
  6128. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  6129. }
  6130. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  6131. verifyFormat("void f() {\n"
  6132. " return g() {}\n"
  6133. " void h() {}");
  6134. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  6135. "g();\n"
  6136. "}");
  6137. }
  6138. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  6139. verifyFormat(
  6140. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  6141. }
  6142. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  6143. verifyFormat("class X {\n"
  6144. " void f() {\n"
  6145. " }\n"
  6146. "};",
  6147. getLLVMStyleWithColumns(12));
  6148. }
  6149. TEST_F(FormatTest, ConfigurableIndentWidth) {
  6150. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  6151. EightIndent.IndentWidth = 8;
  6152. EightIndent.ContinuationIndentWidth = 8;
  6153. verifyFormat("void f() {\n"
  6154. " someFunction();\n"
  6155. " if (true) {\n"
  6156. " f();\n"
  6157. " }\n"
  6158. "}",
  6159. EightIndent);
  6160. verifyFormat("class X {\n"
  6161. " void f() {\n"
  6162. " }\n"
  6163. "};",
  6164. EightIndent);
  6165. verifyFormat("int x[] = {\n"
  6166. " call(),\n"
  6167. " call()};",
  6168. EightIndent);
  6169. }
  6170. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  6171. verifyFormat("double\n"
  6172. "f();",
  6173. getLLVMStyleWithColumns(8));
  6174. }
  6175. TEST_F(FormatTest, ConfigurableUseOfTab) {
  6176. FormatStyle Tab = getLLVMStyleWithColumns(42);
  6177. Tab.IndentWidth = 8;
  6178. Tab.UseTab = FormatStyle::UT_Always;
  6179. Tab.AlignEscapedNewlinesLeft = true;
  6180. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6181. " bb)\t\t// w\n"
  6182. "\t;",
  6183. format("if (aaaaaaaa &&// q\n"
  6184. "bb)// w\n"
  6185. ";",
  6186. Tab));
  6187. EXPECT_EQ("if (aaa && bbb) // w\n"
  6188. "\t;",
  6189. format("if(aaa&&bbb)// w\n"
  6190. ";",
  6191. Tab));
  6192. verifyFormat("class X {\n"
  6193. "\tvoid f() {\n"
  6194. "\t\tsomeFunction(parameter1,\n"
  6195. "\t\t\t parameter2);\n"
  6196. "\t}\n"
  6197. "};",
  6198. Tab);
  6199. verifyFormat("#define A \\\n"
  6200. "\tvoid f() { \\\n"
  6201. "\t\tsomeFunction( \\\n"
  6202. "\t\t parameter1, \\\n"
  6203. "\t\t parameter2); \\\n"
  6204. "\t}",
  6205. Tab);
  6206. Tab.TabWidth = 4;
  6207. Tab.IndentWidth = 8;
  6208. verifyFormat("class TabWidth4Indent8 {\n"
  6209. "\t\tvoid f() {\n"
  6210. "\t\t\t\tsomeFunction(parameter1,\n"
  6211. "\t\t\t\t\t\t\t parameter2);\n"
  6212. "\t\t}\n"
  6213. "};",
  6214. Tab);
  6215. Tab.TabWidth = 4;
  6216. Tab.IndentWidth = 4;
  6217. verifyFormat("class TabWidth4Indent4 {\n"
  6218. "\tvoid f() {\n"
  6219. "\t\tsomeFunction(parameter1,\n"
  6220. "\t\t\t\t\t parameter2);\n"
  6221. "\t}\n"
  6222. "};",
  6223. Tab);
  6224. Tab.TabWidth = 8;
  6225. Tab.IndentWidth = 4;
  6226. verifyFormat("class TabWidth8Indent4 {\n"
  6227. " void f() {\n"
  6228. "\tsomeFunction(parameter1,\n"
  6229. "\t\t parameter2);\n"
  6230. " }\n"
  6231. "};",
  6232. Tab);
  6233. Tab.TabWidth = 8;
  6234. Tab.IndentWidth = 8;
  6235. EXPECT_EQ("/*\n"
  6236. "\t a\t\tcomment\n"
  6237. "\t in multiple lines\n"
  6238. " */",
  6239. format(" /*\t \t \n"
  6240. " \t \t a\t\tcomment\t \t\n"
  6241. " \t \t in multiple lines\t\n"
  6242. " \t */",
  6243. Tab));
  6244. Tab.UseTab = FormatStyle::UT_ForIndentation;
  6245. verifyFormat("{\n"
  6246. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6247. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6248. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6249. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6250. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6251. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6252. "};",
  6253. Tab);
  6254. verifyFormat("enum AA {\n"
  6255. "\ta1, // Force multiple lines\n"
  6256. "\ta2,\n"
  6257. "\ta3\n"
  6258. "};",
  6259. Tab);
  6260. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6261. " bb) // w\n"
  6262. "\t;",
  6263. format("if (aaaaaaaa &&// q\n"
  6264. "bb)// w\n"
  6265. ";",
  6266. Tab));
  6267. verifyFormat("class X {\n"
  6268. "\tvoid f() {\n"
  6269. "\t\tsomeFunction(parameter1,\n"
  6270. "\t\t parameter2);\n"
  6271. "\t}\n"
  6272. "};",
  6273. Tab);
  6274. verifyFormat("{\n"
  6275. "\tQ(\n"
  6276. "\t {\n"
  6277. "\t\t int a;\n"
  6278. "\t\t someFunction(aaaaaaaa,\n"
  6279. "\t\t bbbbbbb);\n"
  6280. "\t },\n"
  6281. "\t p);\n"
  6282. "}",
  6283. Tab);
  6284. EXPECT_EQ("{\n"
  6285. "\t/* aaaa\n"
  6286. "\t bbbb */\n"
  6287. "}",
  6288. format("{\n"
  6289. "/* aaaa\n"
  6290. " bbbb */\n"
  6291. "}",
  6292. Tab));
  6293. EXPECT_EQ("{\n"
  6294. "\t/*\n"
  6295. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6296. "\t bbbbbbbbbbbbb\n"
  6297. "\t*/\n"
  6298. "}",
  6299. format("{\n"
  6300. "/*\n"
  6301. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6302. "*/\n"
  6303. "}",
  6304. Tab));
  6305. EXPECT_EQ("{\n"
  6306. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6307. "\t// bbbbbbbbbbbbb\n"
  6308. "}",
  6309. format("{\n"
  6310. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6311. "}",
  6312. Tab));
  6313. EXPECT_EQ("{\n"
  6314. "\t/*\n"
  6315. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6316. "\t bbbbbbbbbbbbb\n"
  6317. "\t*/\n"
  6318. "}",
  6319. format("{\n"
  6320. "\t/*\n"
  6321. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6322. "\t*/\n"
  6323. "}",
  6324. Tab));
  6325. EXPECT_EQ("{\n"
  6326. "\t/*\n"
  6327. "\n"
  6328. "\t*/\n"
  6329. "}",
  6330. format("{\n"
  6331. "\t/*\n"
  6332. "\n"
  6333. "\t*/\n"
  6334. "}",
  6335. Tab));
  6336. EXPECT_EQ("{\n"
  6337. "\t/*\n"
  6338. " asdf\n"
  6339. "\t*/\n"
  6340. "}",
  6341. format("{\n"
  6342. "\t/*\n"
  6343. " asdf\n"
  6344. "\t*/\n"
  6345. "}",
  6346. Tab));
  6347. Tab.UseTab = FormatStyle::UT_Never;
  6348. EXPECT_EQ("/*\n"
  6349. " a\t\tcomment\n"
  6350. " in multiple lines\n"
  6351. " */",
  6352. format(" /*\t \t \n"
  6353. " \t \t a\t\tcomment\t \t\n"
  6354. " \t \t in multiple lines\t\n"
  6355. " \t */",
  6356. Tab));
  6357. EXPECT_EQ("/* some\n"
  6358. " comment */",
  6359. format(" \t \t /* some\n"
  6360. " \t \t comment */",
  6361. Tab));
  6362. EXPECT_EQ("int a; /* some\n"
  6363. " comment */",
  6364. format(" \t \t int a; /* some\n"
  6365. " \t \t comment */",
  6366. Tab));
  6367. EXPECT_EQ("int a; /* some\n"
  6368. "comment */",
  6369. format(" \t \t int\ta; /* some\n"
  6370. " \t \t comment */",
  6371. Tab));
  6372. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  6373. " comment */",
  6374. format(" \t \t f(\"\t\t\"); /* some\n"
  6375. " \t \t comment */",
  6376. Tab));
  6377. EXPECT_EQ("{\n"
  6378. " /*\n"
  6379. " * Comment\n"
  6380. " */\n"
  6381. " int i;\n"
  6382. "}",
  6383. format("{\n"
  6384. "\t/*\n"
  6385. "\t * Comment\n"
  6386. "\t */\n"
  6387. "\t int i;\n"
  6388. "}"));
  6389. Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
  6390. Tab.TabWidth = 8;
  6391. Tab.IndentWidth = 8;
  6392. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6393. " bb) // w\n"
  6394. "\t;",
  6395. format("if (aaaaaaaa &&// q\n"
  6396. "bb)// w\n"
  6397. ";",
  6398. Tab));
  6399. EXPECT_EQ("if (aaa && bbb) // w\n"
  6400. "\t;",
  6401. format("if(aaa&&bbb)// w\n"
  6402. ";",
  6403. Tab));
  6404. verifyFormat("class X {\n"
  6405. "\tvoid f() {\n"
  6406. "\t\tsomeFunction(parameter1,\n"
  6407. "\t\t\t parameter2);\n"
  6408. "\t}\n"
  6409. "};",
  6410. Tab);
  6411. verifyFormat("#define A \\\n"
  6412. "\tvoid f() { \\\n"
  6413. "\t\tsomeFunction( \\\n"
  6414. "\t\t parameter1, \\\n"
  6415. "\t\t parameter2); \\\n"
  6416. "\t}",
  6417. Tab);
  6418. Tab.TabWidth = 4;
  6419. Tab.IndentWidth = 8;
  6420. verifyFormat("class TabWidth4Indent8 {\n"
  6421. "\t\tvoid f() {\n"
  6422. "\t\t\t\tsomeFunction(parameter1,\n"
  6423. "\t\t\t\t\t\t\t parameter2);\n"
  6424. "\t\t}\n"
  6425. "};",
  6426. Tab);
  6427. Tab.TabWidth = 4;
  6428. Tab.IndentWidth = 4;
  6429. verifyFormat("class TabWidth4Indent4 {\n"
  6430. "\tvoid f() {\n"
  6431. "\t\tsomeFunction(parameter1,\n"
  6432. "\t\t\t\t\t parameter2);\n"
  6433. "\t}\n"
  6434. "};",
  6435. Tab);
  6436. Tab.TabWidth = 8;
  6437. Tab.IndentWidth = 4;
  6438. verifyFormat("class TabWidth8Indent4 {\n"
  6439. " void f() {\n"
  6440. "\tsomeFunction(parameter1,\n"
  6441. "\t\t parameter2);\n"
  6442. " }\n"
  6443. "};",
  6444. Tab);
  6445. Tab.TabWidth = 8;
  6446. Tab.IndentWidth = 8;
  6447. EXPECT_EQ("/*\n"
  6448. "\t a\t\tcomment\n"
  6449. "\t in multiple lines\n"
  6450. " */",
  6451. format(" /*\t \t \n"
  6452. " \t \t a\t\tcomment\t \t\n"
  6453. " \t \t in multiple lines\t\n"
  6454. " \t */",
  6455. Tab));
  6456. verifyFormat("{\n"
  6457. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6458. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6459. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6460. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6461. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6462. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  6463. "};",
  6464. Tab);
  6465. verifyFormat("enum AA {\n"
  6466. "\ta1, // Force multiple lines\n"
  6467. "\ta2,\n"
  6468. "\ta3\n"
  6469. "};",
  6470. Tab);
  6471. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6472. " bb) // w\n"
  6473. "\t;",
  6474. format("if (aaaaaaaa &&// q\n"
  6475. "bb)// w\n"
  6476. ";",
  6477. Tab));
  6478. verifyFormat("class X {\n"
  6479. "\tvoid f() {\n"
  6480. "\t\tsomeFunction(parameter1,\n"
  6481. "\t\t\t parameter2);\n"
  6482. "\t}\n"
  6483. "};",
  6484. Tab);
  6485. verifyFormat("{\n"
  6486. "\tQ(\n"
  6487. "\t {\n"
  6488. "\t\t int a;\n"
  6489. "\t\t someFunction(aaaaaaaa,\n"
  6490. "\t\t\t\t bbbbbbb);\n"
  6491. "\t },\n"
  6492. "\t p);\n"
  6493. "}",
  6494. Tab);
  6495. EXPECT_EQ("{\n"
  6496. "\t/* aaaa\n"
  6497. "\t bbbb */\n"
  6498. "}",
  6499. format("{\n"
  6500. "/* aaaa\n"
  6501. " bbbb */\n"
  6502. "}",
  6503. Tab));
  6504. EXPECT_EQ("{\n"
  6505. "\t/*\n"
  6506. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6507. "\t bbbbbbbbbbbbb\n"
  6508. "\t*/\n"
  6509. "}",
  6510. format("{\n"
  6511. "/*\n"
  6512. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6513. "*/\n"
  6514. "}",
  6515. Tab));
  6516. EXPECT_EQ("{\n"
  6517. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6518. "\t// bbbbbbbbbbbbb\n"
  6519. "}",
  6520. format("{\n"
  6521. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6522. "}",
  6523. Tab));
  6524. EXPECT_EQ("{\n"
  6525. "\t/*\n"
  6526. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6527. "\t bbbbbbbbbbbbb\n"
  6528. "\t*/\n"
  6529. "}",
  6530. format("{\n"
  6531. "\t/*\n"
  6532. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  6533. "\t*/\n"
  6534. "}",
  6535. Tab));
  6536. EXPECT_EQ("{\n"
  6537. "\t/*\n"
  6538. "\n"
  6539. "\t*/\n"
  6540. "}",
  6541. format("{\n"
  6542. "\t/*\n"
  6543. "\n"
  6544. "\t*/\n"
  6545. "}",
  6546. Tab));
  6547. EXPECT_EQ("{\n"
  6548. "\t/*\n"
  6549. " asdf\n"
  6550. "\t*/\n"
  6551. "}",
  6552. format("{\n"
  6553. "\t/*\n"
  6554. " asdf\n"
  6555. "\t*/\n"
  6556. "}",
  6557. Tab));
  6558. EXPECT_EQ("/*\n"
  6559. "\t a\t\tcomment\n"
  6560. "\t in multiple lines\n"
  6561. " */",
  6562. format(" /*\t \t \n"
  6563. " \t \t a\t\tcomment\t \t\n"
  6564. " \t \t in multiple lines\t\n"
  6565. " \t */",
  6566. Tab));
  6567. EXPECT_EQ("/* some\n"
  6568. " comment */",
  6569. format(" \t \t /* some\n"
  6570. " \t \t comment */",
  6571. Tab));
  6572. EXPECT_EQ("int a; /* some\n"
  6573. " comment */",
  6574. format(" \t \t int a; /* some\n"
  6575. " \t \t comment */",
  6576. Tab));
  6577. EXPECT_EQ("int a; /* some\n"
  6578. "comment */",
  6579. format(" \t \t int\ta; /* some\n"
  6580. " \t \t comment */",
  6581. Tab));
  6582. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  6583. " comment */",
  6584. format(" \t \t f(\"\t\t\"); /* some\n"
  6585. " \t \t comment */",
  6586. Tab));
  6587. EXPECT_EQ("{\n"
  6588. " /*\n"
  6589. " * Comment\n"
  6590. " */\n"
  6591. " int i;\n"
  6592. "}",
  6593. format("{\n"
  6594. "\t/*\n"
  6595. "\t * Comment\n"
  6596. "\t */\n"
  6597. "\t int i;\n"
  6598. "}"));
  6599. Tab.AlignConsecutiveAssignments = true;
  6600. Tab.AlignConsecutiveDeclarations = true;
  6601. Tab.TabWidth = 4;
  6602. Tab.IndentWidth = 4;
  6603. verifyFormat("class Assign {\n"
  6604. "\tvoid f() {\n"
  6605. "\t\tint x = 123;\n"
  6606. "\t\tint random = 4;\n"
  6607. "\t\tstd::string alphabet =\n"
  6608. "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
  6609. "\t}\n"
  6610. "};",
  6611. Tab);
  6612. }
  6613. TEST_F(FormatTest, CalculatesOriginalColumn) {
  6614. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6615. "q\"; /* some\n"
  6616. " comment */",
  6617. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6618. "q\"; /* some\n"
  6619. " comment */",
  6620. getLLVMStyle()));
  6621. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  6622. "/* some\n"
  6623. " comment */",
  6624. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  6625. " /* some\n"
  6626. " comment */",
  6627. getLLVMStyle()));
  6628. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6629. "qqq\n"
  6630. "/* some\n"
  6631. " comment */",
  6632. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6633. "qqq\n"
  6634. " /* some\n"
  6635. " comment */",
  6636. getLLVMStyle()));
  6637. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6638. "wwww; /* some\n"
  6639. " comment */",
  6640. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  6641. "wwww; /* some\n"
  6642. " comment */",
  6643. getLLVMStyle()));
  6644. }
  6645. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  6646. FormatStyle NoSpace = getLLVMStyle();
  6647. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  6648. verifyFormat("while(true)\n"
  6649. " continue;",
  6650. NoSpace);
  6651. verifyFormat("for(;;)\n"
  6652. " continue;",
  6653. NoSpace);
  6654. verifyFormat("if(true)\n"
  6655. " f();\n"
  6656. "else if(true)\n"
  6657. " f();",
  6658. NoSpace);
  6659. verifyFormat("do {\n"
  6660. " do_something();\n"
  6661. "} while(something());",
  6662. NoSpace);
  6663. verifyFormat("switch(x) {\n"
  6664. "default:\n"
  6665. " break;\n"
  6666. "}",
  6667. NoSpace);
  6668. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  6669. verifyFormat("size_t x = sizeof(x);", NoSpace);
  6670. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  6671. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  6672. verifyFormat("alignas(128) char a[128];", NoSpace);
  6673. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  6674. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  6675. verifyFormat("int f() throw(Deprecated);", NoSpace);
  6676. verifyFormat("typedef void (*cb)(int);", NoSpace);
  6677. verifyFormat("T A::operator()();", NoSpace);
  6678. verifyFormat("X A::operator++(T);", NoSpace);
  6679. FormatStyle Space = getLLVMStyle();
  6680. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  6681. verifyFormat("int f ();", Space);
  6682. verifyFormat("void f (int a, T b) {\n"
  6683. " while (true)\n"
  6684. " continue;\n"
  6685. "}",
  6686. Space);
  6687. verifyFormat("if (true)\n"
  6688. " f ();\n"
  6689. "else if (true)\n"
  6690. " f ();",
  6691. Space);
  6692. verifyFormat("do {\n"
  6693. " do_something ();\n"
  6694. "} while (something ());",
  6695. Space);
  6696. verifyFormat("switch (x) {\n"
  6697. "default:\n"
  6698. " break;\n"
  6699. "}",
  6700. Space);
  6701. verifyFormat("A::A () : a (1) {}", Space);
  6702. verifyFormat("void f () __attribute__ ((asdf));", Space);
  6703. verifyFormat("*(&a + 1);\n"
  6704. "&((&a)[1]);\n"
  6705. "a[(b + c) * d];\n"
  6706. "(((a + 1) * 2) + 3) * 4;",
  6707. Space);
  6708. verifyFormat("#define A(x) x", Space);
  6709. verifyFormat("#define A (x) x", Space);
  6710. verifyFormat("#if defined(x)\n"
  6711. "#endif",
  6712. Space);
  6713. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  6714. verifyFormat("size_t x = sizeof (x);", Space);
  6715. verifyFormat("auto f (int x) -> decltype (x);", Space);
  6716. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  6717. verifyFormat("alignas (128) char a[128];", Space);
  6718. verifyFormat("size_t x = alignof (MyType);", Space);
  6719. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  6720. verifyFormat("int f () throw (Deprecated);", Space);
  6721. verifyFormat("typedef void (*cb) (int);", Space);
  6722. verifyFormat("T A::operator() ();", Space);
  6723. verifyFormat("X A::operator++ (T);", Space);
  6724. }
  6725. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  6726. FormatStyle Spaces = getLLVMStyle();
  6727. Spaces.SpacesInParentheses = true;
  6728. verifyFormat("call( x, y, z );", Spaces);
  6729. verifyFormat("call();", Spaces);
  6730. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  6731. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  6732. Spaces);
  6733. verifyFormat("while ( (bool)1 )\n"
  6734. " continue;",
  6735. Spaces);
  6736. verifyFormat("for ( ;; )\n"
  6737. " continue;",
  6738. Spaces);
  6739. verifyFormat("if ( true )\n"
  6740. " f();\n"
  6741. "else if ( true )\n"
  6742. " f();",
  6743. Spaces);
  6744. verifyFormat("do {\n"
  6745. " do_something( (int)i );\n"
  6746. "} while ( something() );",
  6747. Spaces);
  6748. verifyFormat("switch ( x ) {\n"
  6749. "default:\n"
  6750. " break;\n"
  6751. "}",
  6752. Spaces);
  6753. Spaces.SpacesInParentheses = false;
  6754. Spaces.SpacesInCStyleCastParentheses = true;
  6755. verifyFormat("Type *A = ( Type * )P;", Spaces);
  6756. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  6757. verifyFormat("x = ( int32 )y;", Spaces);
  6758. verifyFormat("int a = ( int )(2.0f);", Spaces);
  6759. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  6760. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  6761. verifyFormat("#define x (( int )-1)", Spaces);
  6762. // Run the first set of tests again with:
  6763. Spaces.SpacesInParentheses = false;
  6764. Spaces.SpaceInEmptyParentheses = true;
  6765. Spaces.SpacesInCStyleCastParentheses = true;
  6766. verifyFormat("call(x, y, z);", Spaces);
  6767. verifyFormat("call( );", Spaces);
  6768. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  6769. verifyFormat("while (( bool )1)\n"
  6770. " continue;",
  6771. Spaces);
  6772. verifyFormat("for (;;)\n"
  6773. " continue;",
  6774. Spaces);
  6775. verifyFormat("if (true)\n"
  6776. " f( );\n"
  6777. "else if (true)\n"
  6778. " f( );",
  6779. Spaces);
  6780. verifyFormat("do {\n"
  6781. " do_something(( int )i);\n"
  6782. "} while (something( ));",
  6783. Spaces);
  6784. verifyFormat("switch (x) {\n"
  6785. "default:\n"
  6786. " break;\n"
  6787. "}",
  6788. Spaces);
  6789. // Run the first set of tests again with:
  6790. Spaces.SpaceAfterCStyleCast = true;
  6791. verifyFormat("call(x, y, z);", Spaces);
  6792. verifyFormat("call( );", Spaces);
  6793. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  6794. verifyFormat("while (( bool ) 1)\n"
  6795. " continue;",
  6796. Spaces);
  6797. verifyFormat("for (;;)\n"
  6798. " continue;",
  6799. Spaces);
  6800. verifyFormat("if (true)\n"
  6801. " f( );\n"
  6802. "else if (true)\n"
  6803. " f( );",
  6804. Spaces);
  6805. verifyFormat("do {\n"
  6806. " do_something(( int ) i);\n"
  6807. "} while (something( ));",
  6808. Spaces);
  6809. verifyFormat("switch (x) {\n"
  6810. "default:\n"
  6811. " break;\n"
  6812. "}",
  6813. Spaces);
  6814. // Run subset of tests again with:
  6815. Spaces.SpacesInCStyleCastParentheses = false;
  6816. Spaces.SpaceAfterCStyleCast = true;
  6817. verifyFormat("while ((bool) 1)\n"
  6818. " continue;",
  6819. Spaces);
  6820. verifyFormat("do {\n"
  6821. " do_something((int) i);\n"
  6822. "} while (something( ));",
  6823. Spaces);
  6824. }
  6825. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  6826. verifyFormat("int a[5];");
  6827. verifyFormat("a[3] += 42;");
  6828. FormatStyle Spaces = getLLVMStyle();
  6829. Spaces.SpacesInSquareBrackets = true;
  6830. // Lambdas unchanged.
  6831. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  6832. verifyFormat("return [i, args...] {};", Spaces);
  6833. // Not lambdas.
  6834. verifyFormat("int a[ 5 ];", Spaces);
  6835. verifyFormat("a[ 3 ] += 42;", Spaces);
  6836. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  6837. verifyFormat("double &operator[](int i) { return 0; }\n"
  6838. "int i;",
  6839. Spaces);
  6840. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  6841. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  6842. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  6843. }
  6844. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  6845. verifyFormat("int a = 5;");
  6846. verifyFormat("a += 42;");
  6847. verifyFormat("a or_eq 8;");
  6848. FormatStyle Spaces = getLLVMStyle();
  6849. Spaces.SpaceBeforeAssignmentOperators = false;
  6850. verifyFormat("int a= 5;", Spaces);
  6851. verifyFormat("a+= 42;", Spaces);
  6852. verifyFormat("a or_eq 8;", Spaces);
  6853. }
  6854. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  6855. FormatStyle Alignment = getLLVMStyle();
  6856. Alignment.AlignConsecutiveAssignments = false;
  6857. verifyFormat("int a = 5;\n"
  6858. "int oneTwoThree = 123;",
  6859. Alignment);
  6860. verifyFormat("int a = 5;\n"
  6861. "int oneTwoThree = 123;",
  6862. Alignment);
  6863. Alignment.AlignConsecutiveAssignments = true;
  6864. verifyFormat("int a = 5;\n"
  6865. "int oneTwoThree = 123;",
  6866. Alignment);
  6867. verifyFormat("int a = method();\n"
  6868. "int oneTwoThree = 133;",
  6869. Alignment);
  6870. verifyFormat("a &= 5;\n"
  6871. "bcd *= 5;\n"
  6872. "ghtyf += 5;\n"
  6873. "dvfvdb -= 5;\n"
  6874. "a /= 5;\n"
  6875. "vdsvsv %= 5;\n"
  6876. "sfdbddfbdfbb ^= 5;\n"
  6877. "dvsdsv |= 5;\n"
  6878. "int dsvvdvsdvvv = 123;",
  6879. Alignment);
  6880. verifyFormat("int i = 1, j = 10;\n"
  6881. "something = 2000;",
  6882. Alignment);
  6883. verifyFormat("something = 2000;\n"
  6884. "int i = 1, j = 10;\n",
  6885. Alignment);
  6886. verifyFormat("something = 2000;\n"
  6887. "another = 911;\n"
  6888. "int i = 1, j = 10;\n"
  6889. "oneMore = 1;\n"
  6890. "i = 2;",
  6891. Alignment);
  6892. verifyFormat("int a = 5;\n"
  6893. "int one = 1;\n"
  6894. "method();\n"
  6895. "int oneTwoThree = 123;\n"
  6896. "int oneTwo = 12;",
  6897. Alignment);
  6898. verifyFormat("int oneTwoThree = 123;\n"
  6899. "int oneTwo = 12;\n"
  6900. "method();\n",
  6901. Alignment);
  6902. verifyFormat("int oneTwoThree = 123; // comment\n"
  6903. "int oneTwo = 12; // comment",
  6904. Alignment);
  6905. EXPECT_EQ("int a = 5;\n"
  6906. "\n"
  6907. "int oneTwoThree = 123;",
  6908. format("int a = 5;\n"
  6909. "\n"
  6910. "int oneTwoThree= 123;",
  6911. Alignment));
  6912. EXPECT_EQ("int a = 5;\n"
  6913. "int one = 1;\n"
  6914. "\n"
  6915. "int oneTwoThree = 123;",
  6916. format("int a = 5;\n"
  6917. "int one = 1;\n"
  6918. "\n"
  6919. "int oneTwoThree = 123;",
  6920. Alignment));
  6921. EXPECT_EQ("int a = 5;\n"
  6922. "int one = 1;\n"
  6923. "\n"
  6924. "int oneTwoThree = 123;\n"
  6925. "int oneTwo = 12;",
  6926. format("int a = 5;\n"
  6927. "int one = 1;\n"
  6928. "\n"
  6929. "int oneTwoThree = 123;\n"
  6930. "int oneTwo = 12;",
  6931. Alignment));
  6932. Alignment.AlignEscapedNewlinesLeft = true;
  6933. verifyFormat("#define A \\\n"
  6934. " int aaaa = 12; \\\n"
  6935. " int b = 23; \\\n"
  6936. " int ccc = 234; \\\n"
  6937. " int dddddddddd = 2345;",
  6938. Alignment);
  6939. Alignment.AlignEscapedNewlinesLeft = false;
  6940. verifyFormat("#define A "
  6941. " \\\n"
  6942. " int aaaa = 12; "
  6943. " \\\n"
  6944. " int b = 23; "
  6945. " \\\n"
  6946. " int ccc = 234; "
  6947. " \\\n"
  6948. " int dddddddddd = 2345;",
  6949. Alignment);
  6950. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  6951. "k = 4, int l = 5,\n"
  6952. " int m = 6) {\n"
  6953. " int j = 10;\n"
  6954. " otherThing = 1;\n"
  6955. "}",
  6956. Alignment);
  6957. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  6958. " int i = 1;\n"
  6959. " int j = 2;\n"
  6960. " int big = 10000;\n"
  6961. "}",
  6962. Alignment);
  6963. verifyFormat("class C {\n"
  6964. "public:\n"
  6965. " int i = 1;\n"
  6966. " virtual void f() = 0;\n"
  6967. "};",
  6968. Alignment);
  6969. verifyFormat("int i = 1;\n"
  6970. "if (SomeType t = getSomething()) {\n"
  6971. "}\n"
  6972. "int j = 2;\n"
  6973. "int big = 10000;",
  6974. Alignment);
  6975. verifyFormat("int j = 7;\n"
  6976. "for (int k = 0; k < N; ++k) {\n"
  6977. "}\n"
  6978. "int j = 2;\n"
  6979. "int big = 10000;\n"
  6980. "}",
  6981. Alignment);
  6982. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  6983. verifyFormat("int i = 1;\n"
  6984. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  6985. " = someLooooooooooooooooongFunction();\n"
  6986. "int j = 2;",
  6987. Alignment);
  6988. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  6989. verifyFormat("int i = 1;\n"
  6990. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  6991. " someLooooooooooooooooongFunction();\n"
  6992. "int j = 2;",
  6993. Alignment);
  6994. verifyFormat("auto lambda = []() {\n"
  6995. " auto i = 0;\n"
  6996. " return 0;\n"
  6997. "};\n"
  6998. "int i = 0;\n"
  6999. "auto v = type{\n"
  7000. " i = 1, //\n"
  7001. " (i = 2), //\n"
  7002. " i = 3 //\n"
  7003. "};",
  7004. Alignment);
  7005. // FIXME: Should align all three assignments
  7006. verifyFormat(
  7007. "int i = 1;\n"
  7008. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  7009. " loooooooooooooooooooooongParameterB);\n"
  7010. "int j = 2;",
  7011. Alignment);
  7012. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  7013. " typename B = very_long_type_name_1,\n"
  7014. " typename T_2 = very_long_type_name_2>\n"
  7015. "auto foo() {}\n",
  7016. Alignment);
  7017. verifyFormat("int a, b = 1;\n"
  7018. "int c = 2;\n"
  7019. "int dd = 3;\n",
  7020. Alignment);
  7021. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  7022. "float b[1][] = {{3.f}};\n",
  7023. Alignment);
  7024. }
  7025. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  7026. FormatStyle Alignment = getLLVMStyle();
  7027. Alignment.AlignConsecutiveDeclarations = false;
  7028. verifyFormat("float const a = 5;\n"
  7029. "int oneTwoThree = 123;",
  7030. Alignment);
  7031. verifyFormat("int a = 5;\n"
  7032. "float const oneTwoThree = 123;",
  7033. Alignment);
  7034. Alignment.AlignConsecutiveDeclarations = true;
  7035. verifyFormat("float const a = 5;\n"
  7036. "int oneTwoThree = 123;",
  7037. Alignment);
  7038. verifyFormat("int a = method();\n"
  7039. "float const oneTwoThree = 133;",
  7040. Alignment);
  7041. verifyFormat("int i = 1, j = 10;\n"
  7042. "something = 2000;",
  7043. Alignment);
  7044. verifyFormat("something = 2000;\n"
  7045. "int i = 1, j = 10;\n",
  7046. Alignment);
  7047. verifyFormat("float something = 2000;\n"
  7048. "double another = 911;\n"
  7049. "int i = 1, j = 10;\n"
  7050. "const int *oneMore = 1;\n"
  7051. "unsigned i = 2;",
  7052. Alignment);
  7053. verifyFormat("float a = 5;\n"
  7054. "int one = 1;\n"
  7055. "method();\n"
  7056. "const double oneTwoThree = 123;\n"
  7057. "const unsigned int oneTwo = 12;",
  7058. Alignment);
  7059. verifyFormat("int oneTwoThree{0}; // comment\n"
  7060. "unsigned oneTwo; // comment",
  7061. Alignment);
  7062. EXPECT_EQ("float const a = 5;\n"
  7063. "\n"
  7064. "int oneTwoThree = 123;",
  7065. format("float const a = 5;\n"
  7066. "\n"
  7067. "int oneTwoThree= 123;",
  7068. Alignment));
  7069. EXPECT_EQ("float a = 5;\n"
  7070. "int one = 1;\n"
  7071. "\n"
  7072. "unsigned oneTwoThree = 123;",
  7073. format("float a = 5;\n"
  7074. "int one = 1;\n"
  7075. "\n"
  7076. "unsigned oneTwoThree = 123;",
  7077. Alignment));
  7078. EXPECT_EQ("float a = 5;\n"
  7079. "int one = 1;\n"
  7080. "\n"
  7081. "unsigned oneTwoThree = 123;\n"
  7082. "int oneTwo = 12;",
  7083. format("float a = 5;\n"
  7084. "int one = 1;\n"
  7085. "\n"
  7086. "unsigned oneTwoThree = 123;\n"
  7087. "int oneTwo = 12;",
  7088. Alignment));
  7089. Alignment.AlignConsecutiveAssignments = true;
  7090. verifyFormat("float something = 2000;\n"
  7091. "double another = 911;\n"
  7092. "int i = 1, j = 10;\n"
  7093. "const int *oneMore = 1;\n"
  7094. "unsigned i = 2;",
  7095. Alignment);
  7096. verifyFormat("int oneTwoThree = {0}; // comment\n"
  7097. "unsigned oneTwo = 0; // comment",
  7098. Alignment);
  7099. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  7100. " int const i = 1;\n"
  7101. " int * j = 2;\n"
  7102. " int big = 10000;\n"
  7103. "\n"
  7104. " unsigned oneTwoThree = 123;\n"
  7105. " int oneTwo = 12;\n"
  7106. " method();\n"
  7107. " float k = 2;\n"
  7108. " int ll = 10000;\n"
  7109. "}",
  7110. format("void SomeFunction(int parameter= 0) {\n"
  7111. " int const i= 1;\n"
  7112. " int *j=2;\n"
  7113. " int big = 10000;\n"
  7114. "\n"
  7115. "unsigned oneTwoThree =123;\n"
  7116. "int oneTwo = 12;\n"
  7117. " method();\n"
  7118. "float k= 2;\n"
  7119. "int ll=10000;\n"
  7120. "}",
  7121. Alignment));
  7122. Alignment.AlignConsecutiveAssignments = false;
  7123. Alignment.AlignEscapedNewlinesLeft = true;
  7124. verifyFormat("#define A \\\n"
  7125. " int aaaa = 12; \\\n"
  7126. " float b = 23; \\\n"
  7127. " const int ccc = 234; \\\n"
  7128. " unsigned dddddddddd = 2345;",
  7129. Alignment);
  7130. Alignment.AlignEscapedNewlinesLeft = false;
  7131. Alignment.ColumnLimit = 30;
  7132. verifyFormat("#define A \\\n"
  7133. " int aaaa = 12; \\\n"
  7134. " float b = 23; \\\n"
  7135. " const int ccc = 234; \\\n"
  7136. " int dddddddddd = 2345;",
  7137. Alignment);
  7138. Alignment.ColumnLimit = 80;
  7139. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  7140. "k = 4, int l = 5,\n"
  7141. " int m = 6) {\n"
  7142. " const int j = 10;\n"
  7143. " otherThing = 1;\n"
  7144. "}",
  7145. Alignment);
  7146. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  7147. " int const i = 1;\n"
  7148. " int * j = 2;\n"
  7149. " int big = 10000;\n"
  7150. "}",
  7151. Alignment);
  7152. verifyFormat("class C {\n"
  7153. "public:\n"
  7154. " int i = 1;\n"
  7155. " virtual void f() = 0;\n"
  7156. "};",
  7157. Alignment);
  7158. verifyFormat("float i = 1;\n"
  7159. "if (SomeType t = getSomething()) {\n"
  7160. "}\n"
  7161. "const unsigned j = 2;\n"
  7162. "int big = 10000;",
  7163. Alignment);
  7164. verifyFormat("float j = 7;\n"
  7165. "for (int k = 0; k < N; ++k) {\n"
  7166. "}\n"
  7167. "unsigned j = 2;\n"
  7168. "int big = 10000;\n"
  7169. "}",
  7170. Alignment);
  7171. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  7172. verifyFormat("float i = 1;\n"
  7173. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  7174. " = someLooooooooooooooooongFunction();\n"
  7175. "int j = 2;",
  7176. Alignment);
  7177. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  7178. verifyFormat("int i = 1;\n"
  7179. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  7180. " someLooooooooooooooooongFunction();\n"
  7181. "int j = 2;",
  7182. Alignment);
  7183. Alignment.AlignConsecutiveAssignments = true;
  7184. verifyFormat("auto lambda = []() {\n"
  7185. " auto ii = 0;\n"
  7186. " float j = 0;\n"
  7187. " return 0;\n"
  7188. "};\n"
  7189. "int i = 0;\n"
  7190. "float i2 = 0;\n"
  7191. "auto v = type{\n"
  7192. " i = 1, //\n"
  7193. " (i = 2), //\n"
  7194. " i = 3 //\n"
  7195. "};",
  7196. Alignment);
  7197. Alignment.AlignConsecutiveAssignments = false;
  7198. // FIXME: Should align all three declarations
  7199. verifyFormat(
  7200. "int i = 1;\n"
  7201. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  7202. " loooooooooooooooooooooongParameterB);\n"
  7203. "int j = 2;",
  7204. Alignment);
  7205. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  7206. // We expect declarations and assignments to align, as long as it doesn't
  7207. // exceed the column limit, starting a new alignemnt sequence whenever it
  7208. // happens.
  7209. Alignment.AlignConsecutiveAssignments = true;
  7210. Alignment.ColumnLimit = 30;
  7211. verifyFormat("float ii = 1;\n"
  7212. "unsigned j = 2;\n"
  7213. "int someVerylongVariable = 1;\n"
  7214. "AnotherLongType ll = 123456;\n"
  7215. "VeryVeryLongType k = 2;\n"
  7216. "int myvar = 1;",
  7217. Alignment);
  7218. Alignment.ColumnLimit = 80;
  7219. Alignment.AlignConsecutiveAssignments = false;
  7220. verifyFormat(
  7221. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  7222. " typename LongType, typename B>\n"
  7223. "auto foo() {}\n",
  7224. Alignment);
  7225. verifyFormat("float a, b = 1;\n"
  7226. "int c = 2;\n"
  7227. "int dd = 3;\n",
  7228. Alignment);
  7229. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  7230. "float b[1][] = {{3.f}};\n",
  7231. Alignment);
  7232. Alignment.AlignConsecutiveAssignments = true;
  7233. verifyFormat("float a, b = 1;\n"
  7234. "int c = 2;\n"
  7235. "int dd = 3;\n",
  7236. Alignment);
  7237. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  7238. "float b[1][] = {{3.f}};\n",
  7239. Alignment);
  7240. Alignment.AlignConsecutiveAssignments = false;
  7241. Alignment.ColumnLimit = 30;
  7242. Alignment.BinPackParameters = false;
  7243. verifyFormat("void foo(float a,\n"
  7244. " float b,\n"
  7245. " int c,\n"
  7246. " uint32_t *d) {\n"
  7247. " int * e = 0;\n"
  7248. " float f = 0;\n"
  7249. " double g = 0;\n"
  7250. "}\n"
  7251. "void bar(ino_t a,\n"
  7252. " int b,\n"
  7253. " uint32_t *c,\n"
  7254. " bool d) {}\n",
  7255. Alignment);
  7256. Alignment.BinPackParameters = true;
  7257. Alignment.ColumnLimit = 80;
  7258. }
  7259. TEST_F(FormatTest, LinuxBraceBreaking) {
  7260. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7261. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7262. verifyFormat("namespace a\n"
  7263. "{\n"
  7264. "class A\n"
  7265. "{\n"
  7266. " void f()\n"
  7267. " {\n"
  7268. " if (true) {\n"
  7269. " a();\n"
  7270. " b();\n"
  7271. " } else {\n"
  7272. " a();\n"
  7273. " }\n"
  7274. " }\n"
  7275. " void g() { return; }\n"
  7276. "};\n"
  7277. "struct B {\n"
  7278. " int x;\n"
  7279. "};\n"
  7280. "}\n",
  7281. LinuxBraceStyle);
  7282. verifyFormat("enum X {\n"
  7283. " Y = 0,\n"
  7284. "}\n",
  7285. LinuxBraceStyle);
  7286. verifyFormat("struct S {\n"
  7287. " int Type;\n"
  7288. " union {\n"
  7289. " int x;\n"
  7290. " double y;\n"
  7291. " } Value;\n"
  7292. " class C\n"
  7293. " {\n"
  7294. " MyFavoriteType Value;\n"
  7295. " } Class;\n"
  7296. "}\n",
  7297. LinuxBraceStyle);
  7298. }
  7299. TEST_F(FormatTest, MozillaBraceBreaking) {
  7300. FormatStyle MozillaBraceStyle = getLLVMStyle();
  7301. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  7302. verifyFormat("namespace a {\n"
  7303. "class A\n"
  7304. "{\n"
  7305. " void f()\n"
  7306. " {\n"
  7307. " if (true) {\n"
  7308. " a();\n"
  7309. " b();\n"
  7310. " }\n"
  7311. " }\n"
  7312. " void g() { return; }\n"
  7313. "};\n"
  7314. "enum E\n"
  7315. "{\n"
  7316. " A,\n"
  7317. " // foo\n"
  7318. " B,\n"
  7319. " C\n"
  7320. "};\n"
  7321. "struct B\n"
  7322. "{\n"
  7323. " int x;\n"
  7324. "};\n"
  7325. "}\n",
  7326. MozillaBraceStyle);
  7327. verifyFormat("struct S\n"
  7328. "{\n"
  7329. " int Type;\n"
  7330. " union\n"
  7331. " {\n"
  7332. " int x;\n"
  7333. " double y;\n"
  7334. " } Value;\n"
  7335. " class C\n"
  7336. " {\n"
  7337. " MyFavoriteType Value;\n"
  7338. " } Class;\n"
  7339. "}\n",
  7340. MozillaBraceStyle);
  7341. }
  7342. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7343. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7344. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7345. verifyFormat("namespace a {\n"
  7346. "class A {\n"
  7347. " void f()\n"
  7348. " {\n"
  7349. " if (true) {\n"
  7350. " a();\n"
  7351. " b();\n"
  7352. " }\n"
  7353. " }\n"
  7354. " void g() { return; }\n"
  7355. "};\n"
  7356. "struct B {\n"
  7357. " int x;\n"
  7358. "};\n"
  7359. "}\n",
  7360. StroustrupBraceStyle);
  7361. verifyFormat("void foo()\n"
  7362. "{\n"
  7363. " if (a) {\n"
  7364. " a();\n"
  7365. " }\n"
  7366. " else {\n"
  7367. " b();\n"
  7368. " }\n"
  7369. "}\n",
  7370. StroustrupBraceStyle);
  7371. verifyFormat("#ifdef _DEBUG\n"
  7372. "int foo(int i = 0)\n"
  7373. "#else\n"
  7374. "int foo(int i = 5)\n"
  7375. "#endif\n"
  7376. "{\n"
  7377. " return i;\n"
  7378. "}",
  7379. StroustrupBraceStyle);
  7380. verifyFormat("void foo() {}\n"
  7381. "void bar()\n"
  7382. "#ifdef _DEBUG\n"
  7383. "{\n"
  7384. " foo();\n"
  7385. "}\n"
  7386. "#else\n"
  7387. "{\n"
  7388. "}\n"
  7389. "#endif",
  7390. StroustrupBraceStyle);
  7391. verifyFormat("void foobar() { int i = 5; }\n"
  7392. "#ifdef _DEBUG\n"
  7393. "void bar() {}\n"
  7394. "#else\n"
  7395. "void bar() { foobar(); }\n"
  7396. "#endif",
  7397. StroustrupBraceStyle);
  7398. }
  7399. TEST_F(FormatTest, AllmanBraceBreaking) {
  7400. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7401. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7402. verifyFormat("namespace a\n"
  7403. "{\n"
  7404. "class A\n"
  7405. "{\n"
  7406. " void f()\n"
  7407. " {\n"
  7408. " if (true)\n"
  7409. " {\n"
  7410. " a();\n"
  7411. " b();\n"
  7412. " }\n"
  7413. " }\n"
  7414. " void g() { return; }\n"
  7415. "};\n"
  7416. "struct B\n"
  7417. "{\n"
  7418. " int x;\n"
  7419. "};\n"
  7420. "}",
  7421. AllmanBraceStyle);
  7422. verifyFormat("void f()\n"
  7423. "{\n"
  7424. " if (true)\n"
  7425. " {\n"
  7426. " a();\n"
  7427. " }\n"
  7428. " else if (false)\n"
  7429. " {\n"
  7430. " b();\n"
  7431. " }\n"
  7432. " else\n"
  7433. " {\n"
  7434. " c();\n"
  7435. " }\n"
  7436. "}\n",
  7437. AllmanBraceStyle);
  7438. verifyFormat("void f()\n"
  7439. "{\n"
  7440. " for (int i = 0; i < 10; ++i)\n"
  7441. " {\n"
  7442. " a();\n"
  7443. " }\n"
  7444. " while (false)\n"
  7445. " {\n"
  7446. " b();\n"
  7447. " }\n"
  7448. " do\n"
  7449. " {\n"
  7450. " c();\n"
  7451. " } while (false)\n"
  7452. "}\n",
  7453. AllmanBraceStyle);
  7454. verifyFormat("void f(int a)\n"
  7455. "{\n"
  7456. " switch (a)\n"
  7457. " {\n"
  7458. " case 0:\n"
  7459. " break;\n"
  7460. " case 1:\n"
  7461. " {\n"
  7462. " break;\n"
  7463. " }\n"
  7464. " case 2:\n"
  7465. " {\n"
  7466. " }\n"
  7467. " break;\n"
  7468. " default:\n"
  7469. " break;\n"
  7470. " }\n"
  7471. "}\n",
  7472. AllmanBraceStyle);
  7473. verifyFormat("enum X\n"
  7474. "{\n"
  7475. " Y = 0,\n"
  7476. "}\n",
  7477. AllmanBraceStyle);
  7478. verifyFormat("enum X\n"
  7479. "{\n"
  7480. " Y = 0\n"
  7481. "}\n",
  7482. AllmanBraceStyle);
  7483. verifyFormat("@interface BSApplicationController ()\n"
  7484. "{\n"
  7485. "@private\n"
  7486. " id _extraIvar;\n"
  7487. "}\n"
  7488. "@end\n",
  7489. AllmanBraceStyle);
  7490. verifyFormat("#ifdef _DEBUG\n"
  7491. "int foo(int i = 0)\n"
  7492. "#else\n"
  7493. "int foo(int i = 5)\n"
  7494. "#endif\n"
  7495. "{\n"
  7496. " return i;\n"
  7497. "}",
  7498. AllmanBraceStyle);
  7499. verifyFormat("void foo() {}\n"
  7500. "void bar()\n"
  7501. "#ifdef _DEBUG\n"
  7502. "{\n"
  7503. " foo();\n"
  7504. "}\n"
  7505. "#else\n"
  7506. "{\n"
  7507. "}\n"
  7508. "#endif",
  7509. AllmanBraceStyle);
  7510. verifyFormat("void foobar() { int i = 5; }\n"
  7511. "#ifdef _DEBUG\n"
  7512. "void bar() {}\n"
  7513. "#else\n"
  7514. "void bar() { foobar(); }\n"
  7515. "#endif",
  7516. AllmanBraceStyle);
  7517. // This shouldn't affect ObjC blocks..
  7518. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7519. " // ...\n"
  7520. " int i;\n"
  7521. "}];",
  7522. AllmanBraceStyle);
  7523. verifyFormat("void (^block)(void) = ^{\n"
  7524. " // ...\n"
  7525. " int i;\n"
  7526. "};",
  7527. AllmanBraceStyle);
  7528. // .. or dict literals.
  7529. verifyFormat("void f()\n"
  7530. "{\n"
  7531. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7532. "}",
  7533. AllmanBraceStyle);
  7534. verifyFormat("int f()\n"
  7535. "{ // comment\n"
  7536. " return 42;\n"
  7537. "}",
  7538. AllmanBraceStyle);
  7539. AllmanBraceStyle.ColumnLimit = 19;
  7540. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7541. AllmanBraceStyle.ColumnLimit = 18;
  7542. verifyFormat("void f()\n"
  7543. "{\n"
  7544. " int i;\n"
  7545. "}",
  7546. AllmanBraceStyle);
  7547. AllmanBraceStyle.ColumnLimit = 80;
  7548. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7549. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7550. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7551. verifyFormat("void f(bool b)\n"
  7552. "{\n"
  7553. " if (b)\n"
  7554. " {\n"
  7555. " return;\n"
  7556. " }\n"
  7557. "}\n",
  7558. BreakBeforeBraceShortIfs);
  7559. verifyFormat("void f(bool b)\n"
  7560. "{\n"
  7561. " if (b) return;\n"
  7562. "}\n",
  7563. BreakBeforeBraceShortIfs);
  7564. verifyFormat("void f(bool b)\n"
  7565. "{\n"
  7566. " while (b)\n"
  7567. " {\n"
  7568. " return;\n"
  7569. " }\n"
  7570. "}\n",
  7571. BreakBeforeBraceShortIfs);
  7572. }
  7573. TEST_F(FormatTest, GNUBraceBreaking) {
  7574. FormatStyle GNUBraceStyle = getLLVMStyle();
  7575. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7576. verifyFormat("namespace a\n"
  7577. "{\n"
  7578. "class A\n"
  7579. "{\n"
  7580. " void f()\n"
  7581. " {\n"
  7582. " int a;\n"
  7583. " {\n"
  7584. " int b;\n"
  7585. " }\n"
  7586. " if (true)\n"
  7587. " {\n"
  7588. " a();\n"
  7589. " b();\n"
  7590. " }\n"
  7591. " }\n"
  7592. " void g() { return; }\n"
  7593. "}\n"
  7594. "}",
  7595. GNUBraceStyle);
  7596. verifyFormat("void f()\n"
  7597. "{\n"
  7598. " if (true)\n"
  7599. " {\n"
  7600. " a();\n"
  7601. " }\n"
  7602. " else if (false)\n"
  7603. " {\n"
  7604. " b();\n"
  7605. " }\n"
  7606. " else\n"
  7607. " {\n"
  7608. " c();\n"
  7609. " }\n"
  7610. "}\n",
  7611. GNUBraceStyle);
  7612. verifyFormat("void f()\n"
  7613. "{\n"
  7614. " for (int i = 0; i < 10; ++i)\n"
  7615. " {\n"
  7616. " a();\n"
  7617. " }\n"
  7618. " while (false)\n"
  7619. " {\n"
  7620. " b();\n"
  7621. " }\n"
  7622. " do\n"
  7623. " {\n"
  7624. " c();\n"
  7625. " }\n"
  7626. " while (false);\n"
  7627. "}\n",
  7628. GNUBraceStyle);
  7629. verifyFormat("void f(int a)\n"
  7630. "{\n"
  7631. " switch (a)\n"
  7632. " {\n"
  7633. " case 0:\n"
  7634. " break;\n"
  7635. " case 1:\n"
  7636. " {\n"
  7637. " break;\n"
  7638. " }\n"
  7639. " case 2:\n"
  7640. " {\n"
  7641. " }\n"
  7642. " break;\n"
  7643. " default:\n"
  7644. " break;\n"
  7645. " }\n"
  7646. "}\n",
  7647. GNUBraceStyle);
  7648. verifyFormat("enum X\n"
  7649. "{\n"
  7650. " Y = 0,\n"
  7651. "}\n",
  7652. GNUBraceStyle);
  7653. verifyFormat("@interface BSApplicationController ()\n"
  7654. "{\n"
  7655. "@private\n"
  7656. " id _extraIvar;\n"
  7657. "}\n"
  7658. "@end\n",
  7659. GNUBraceStyle);
  7660. verifyFormat("#ifdef _DEBUG\n"
  7661. "int foo(int i = 0)\n"
  7662. "#else\n"
  7663. "int foo(int i = 5)\n"
  7664. "#endif\n"
  7665. "{\n"
  7666. " return i;\n"
  7667. "}",
  7668. GNUBraceStyle);
  7669. verifyFormat("void foo() {}\n"
  7670. "void bar()\n"
  7671. "#ifdef _DEBUG\n"
  7672. "{\n"
  7673. " foo();\n"
  7674. "}\n"
  7675. "#else\n"
  7676. "{\n"
  7677. "}\n"
  7678. "#endif",
  7679. GNUBraceStyle);
  7680. verifyFormat("void foobar() { int i = 5; }\n"
  7681. "#ifdef _DEBUG\n"
  7682. "void bar() {}\n"
  7683. "#else\n"
  7684. "void bar() { foobar(); }\n"
  7685. "#endif",
  7686. GNUBraceStyle);
  7687. }
  7688. TEST_F(FormatTest, WebKitBraceBreaking) {
  7689. FormatStyle WebKitBraceStyle = getLLVMStyle();
  7690. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  7691. verifyFormat("namespace a {\n"
  7692. "class A {\n"
  7693. " void f()\n"
  7694. " {\n"
  7695. " if (true) {\n"
  7696. " a();\n"
  7697. " b();\n"
  7698. " }\n"
  7699. " }\n"
  7700. " void g() { return; }\n"
  7701. "};\n"
  7702. "enum E {\n"
  7703. " A,\n"
  7704. " // foo\n"
  7705. " B,\n"
  7706. " C\n"
  7707. "};\n"
  7708. "struct B {\n"
  7709. " int x;\n"
  7710. "};\n"
  7711. "}\n",
  7712. WebKitBraceStyle);
  7713. verifyFormat("struct S {\n"
  7714. " int Type;\n"
  7715. " union {\n"
  7716. " int x;\n"
  7717. " double y;\n"
  7718. " } Value;\n"
  7719. " class C {\n"
  7720. " MyFavoriteType Value;\n"
  7721. " } Class;\n"
  7722. "};\n",
  7723. WebKitBraceStyle);
  7724. }
  7725. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  7726. verifyFormat("void f() {\n"
  7727. " try {\n"
  7728. " } catch (const Exception &e) {\n"
  7729. " }\n"
  7730. "}\n",
  7731. getLLVMStyle());
  7732. }
  7733. TEST_F(FormatTest, UnderstandsPragmas) {
  7734. verifyFormat("#pragma omp reduction(| : var)");
  7735. verifyFormat("#pragma omp reduction(+ : var)");
  7736. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  7737. "(including parentheses).",
  7738. format("#pragma mark Any non-hyphenated or hyphenated string "
  7739. "(including parentheses)."));
  7740. }
  7741. TEST_F(FormatTest, UnderstandPragmaOption) {
  7742. verifyFormat("#pragma option -C -A");
  7743. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  7744. }
  7745. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  7746. for (size_t i = 1; i < Styles.size(); ++i) \
  7747. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  7748. << " differs from Style #0"
  7749. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  7750. SmallVector<FormatStyle, 3> Styles;
  7751. Styles.resize(3);
  7752. Styles[0] = getLLVMStyle();
  7753. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  7754. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  7755. EXPECT_ALL_STYLES_EQUAL(Styles);
  7756. Styles[0] = getGoogleStyle();
  7757. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  7758. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  7759. EXPECT_ALL_STYLES_EQUAL(Styles);
  7760. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7761. EXPECT_TRUE(
  7762. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  7763. EXPECT_TRUE(
  7764. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  7765. EXPECT_ALL_STYLES_EQUAL(Styles);
  7766. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  7767. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  7768. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  7769. EXPECT_ALL_STYLES_EQUAL(Styles);
  7770. Styles[0] = getMozillaStyle();
  7771. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  7772. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  7773. EXPECT_ALL_STYLES_EQUAL(Styles);
  7774. Styles[0] = getWebKitStyle();
  7775. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  7776. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  7777. EXPECT_ALL_STYLES_EQUAL(Styles);
  7778. Styles[0] = getGNUStyle();
  7779. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  7780. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  7781. EXPECT_ALL_STYLES_EQUAL(Styles);
  7782. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  7783. }
  7784. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  7785. SmallVector<FormatStyle, 8> Styles;
  7786. Styles.resize(2);
  7787. Styles[0] = getGoogleStyle();
  7788. Styles[1] = getLLVMStyle();
  7789. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7790. EXPECT_ALL_STYLES_EQUAL(Styles);
  7791. Styles.resize(5);
  7792. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7793. Styles[1] = getLLVMStyle();
  7794. Styles[1].Language = FormatStyle::LK_JavaScript;
  7795. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7796. Styles[2] = getLLVMStyle();
  7797. Styles[2].Language = FormatStyle::LK_JavaScript;
  7798. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  7799. "BasedOnStyle: Google",
  7800. &Styles[2])
  7801. .value());
  7802. Styles[3] = getLLVMStyle();
  7803. Styles[3].Language = FormatStyle::LK_JavaScript;
  7804. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  7805. "Language: JavaScript",
  7806. &Styles[3])
  7807. .value());
  7808. Styles[4] = getLLVMStyle();
  7809. Styles[4].Language = FormatStyle::LK_JavaScript;
  7810. EXPECT_EQ(0, parseConfiguration("---\n"
  7811. "BasedOnStyle: LLVM\n"
  7812. "IndentWidth: 123\n"
  7813. "---\n"
  7814. "BasedOnStyle: Google\n"
  7815. "Language: JavaScript",
  7816. &Styles[4])
  7817. .value());
  7818. EXPECT_ALL_STYLES_EQUAL(Styles);
  7819. }
  7820. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  7821. Style.FIELD = false; \
  7822. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  7823. EXPECT_TRUE(Style.FIELD); \
  7824. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  7825. EXPECT_FALSE(Style.FIELD);
  7826. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  7827. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  7828. Style.STRUCT.FIELD = false; \
  7829. EXPECT_EQ(0, \
  7830. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  7831. .value()); \
  7832. EXPECT_TRUE(Style.STRUCT.FIELD); \
  7833. EXPECT_EQ(0, \
  7834. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  7835. .value()); \
  7836. EXPECT_FALSE(Style.STRUCT.FIELD);
  7837. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  7838. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  7839. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  7840. EXPECT_NE(VALUE, Style.FIELD); \
  7841. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  7842. EXPECT_EQ(VALUE, Style.FIELD)
  7843. TEST_F(FormatTest, ParsesConfigurationBools) {
  7844. FormatStyle Style = {};
  7845. Style.Language = FormatStyle::LK_Cpp;
  7846. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  7847. CHECK_PARSE_BOOL(AlignOperands);
  7848. CHECK_PARSE_BOOL(AlignTrailingComments);
  7849. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  7850. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  7851. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  7852. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  7853. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  7854. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  7855. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  7856. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  7857. CHECK_PARSE_BOOL(BinPackArguments);
  7858. CHECK_PARSE_BOOL(BinPackParameters);
  7859. CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
  7860. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  7861. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  7862. CHECK_PARSE_BOOL(BreakStringLiterals);
  7863. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  7864. CHECK_PARSE_BOOL(DerivePointerAlignment);
  7865. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  7866. CHECK_PARSE_BOOL(DisableFormat);
  7867. CHECK_PARSE_BOOL(IndentCaseLabels);
  7868. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  7869. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  7870. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  7871. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  7872. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  7873. CHECK_PARSE_BOOL(ReflowComments);
  7874. CHECK_PARSE_BOOL(SortIncludes);
  7875. CHECK_PARSE_BOOL(SpacesInParentheses);
  7876. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  7877. CHECK_PARSE_BOOL(SpacesInAngles);
  7878. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  7879. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  7880. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  7881. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  7882. CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
  7883. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  7884. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  7885. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  7886. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  7887. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  7888. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  7889. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  7890. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  7891. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  7892. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  7893. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  7894. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  7895. }
  7896. #undef CHECK_PARSE_BOOL
  7897. TEST_F(FormatTest, ParsesConfiguration) {
  7898. FormatStyle Style = {};
  7899. Style.Language = FormatStyle::LK_Cpp;
  7900. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  7901. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  7902. ConstructorInitializerIndentWidth, 1234u);
  7903. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  7904. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  7905. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  7906. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  7907. PenaltyBreakBeforeFirstCallParameter, 1234u);
  7908. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  7909. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  7910. PenaltyReturnTypeOnItsOwnLine, 1234u);
  7911. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  7912. SpacesBeforeTrailingComments, 1234u);
  7913. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  7914. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  7915. CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
  7916. Style.PointerAlignment = FormatStyle::PAS_Middle;
  7917. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  7918. FormatStyle::PAS_Left);
  7919. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  7920. FormatStyle::PAS_Right);
  7921. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  7922. FormatStyle::PAS_Middle);
  7923. // For backward compatibility:
  7924. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  7925. FormatStyle::PAS_Left);
  7926. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  7927. FormatStyle::PAS_Right);
  7928. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  7929. FormatStyle::PAS_Middle);
  7930. Style.Standard = FormatStyle::LS_Auto;
  7931. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  7932. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  7933. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  7934. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  7935. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  7936. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  7937. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  7938. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  7939. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  7940. FormatStyle::BOS_None);
  7941. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  7942. FormatStyle::BOS_All);
  7943. // For backward compatibility:
  7944. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  7945. FormatStyle::BOS_None);
  7946. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  7947. FormatStyle::BOS_All);
  7948. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  7949. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  7950. FormatStyle::BAS_Align);
  7951. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  7952. FormatStyle::BAS_DontAlign);
  7953. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  7954. FormatStyle::BAS_AlwaysBreak);
  7955. // For backward compatibility:
  7956. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  7957. FormatStyle::BAS_DontAlign);
  7958. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  7959. FormatStyle::BAS_Align);
  7960. Style.UseTab = FormatStyle::UT_ForIndentation;
  7961. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  7962. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  7963. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  7964. CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
  7965. FormatStyle::UT_ForContinuationAndIndentation);
  7966. // For backward compatibility:
  7967. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  7968. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  7969. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  7970. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  7971. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  7972. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  7973. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  7974. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  7975. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  7976. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  7977. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  7978. // For backward compatibility:
  7979. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  7980. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  7981. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  7982. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  7983. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7984. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  7985. FormatStyle::SBPO_Never);
  7986. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  7987. FormatStyle::SBPO_Always);
  7988. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  7989. FormatStyle::SBPO_ControlStatements);
  7990. // For backward compatibility:
  7991. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  7992. FormatStyle::SBPO_Never);
  7993. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  7994. FormatStyle::SBPO_ControlStatements);
  7995. Style.ColumnLimit = 123;
  7996. FormatStyle BaseStyle = getLLVMStyle();
  7997. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  7998. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  7999. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8000. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8001. FormatStyle::BS_Attach);
  8002. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8003. FormatStyle::BS_Linux);
  8004. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  8005. FormatStyle::BS_Mozilla);
  8006. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8007. FormatStyle::BS_Stroustrup);
  8008. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8009. FormatStyle::BS_Allman);
  8010. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8011. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  8012. FormatStyle::BS_WebKit);
  8013. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  8014. FormatStyle::BS_Custom);
  8015. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  8016. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  8017. FormatStyle::RTBS_None);
  8018. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  8019. FormatStyle::RTBS_All);
  8020. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  8021. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  8022. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  8023. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  8024. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  8025. AlwaysBreakAfterReturnType,
  8026. FormatStyle::RTBS_TopLevelDefinitions);
  8027. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  8028. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  8029. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  8030. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  8031. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  8032. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  8033. AlwaysBreakAfterDefinitionReturnType,
  8034. FormatStyle::DRTBS_TopLevel);
  8035. Style.NamespaceIndentation = FormatStyle::NI_All;
  8036. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8037. FormatStyle::NI_None);
  8038. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8039. FormatStyle::NI_Inner);
  8040. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8041. FormatStyle::NI_All);
  8042. // FIXME: This is required because parsing a configuration simply overwrites
  8043. // the first N elements of the list instead of resetting it.
  8044. Style.ForEachMacros.clear();
  8045. std::vector<std::string> BoostForeach;
  8046. BoostForeach.push_back("BOOST_FOREACH");
  8047. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8048. std::vector<std::string> BoostAndQForeach;
  8049. BoostAndQForeach.push_back("BOOST_FOREACH");
  8050. BoostAndQForeach.push_back("Q_FOREACH");
  8051. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8052. BoostAndQForeach);
  8053. Style.IncludeCategories.clear();
  8054. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  8055. {".*", 1}};
  8056. CHECK_PARSE("IncludeCategories:\n"
  8057. " - Regex: abc/.*\n"
  8058. " Priority: 2\n"
  8059. " - Regex: .*\n"
  8060. " Priority: 1",
  8061. IncludeCategories, ExpectedCategories);
  8062. CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
  8063. }
  8064. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8065. FormatStyle Style = {};
  8066. Style.Language = FormatStyle::LK_Cpp;
  8067. CHECK_PARSE("Language: Cpp\n"
  8068. "IndentWidth: 12",
  8069. IndentWidth, 12u);
  8070. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8071. "IndentWidth: 34",
  8072. &Style),
  8073. ParseError::Unsuitable);
  8074. EXPECT_EQ(12u, Style.IndentWidth);
  8075. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8076. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8077. Style.Language = FormatStyle::LK_JavaScript;
  8078. CHECK_PARSE("Language: JavaScript\n"
  8079. "IndentWidth: 12",
  8080. IndentWidth, 12u);
  8081. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8082. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8083. "IndentWidth: 34",
  8084. &Style),
  8085. ParseError::Unsuitable);
  8086. EXPECT_EQ(23u, Style.IndentWidth);
  8087. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8088. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8089. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8090. "IndentWidth: 67",
  8091. IndentWidth, 67u);
  8092. CHECK_PARSE("---\n"
  8093. "Language: JavaScript\n"
  8094. "IndentWidth: 12\n"
  8095. "---\n"
  8096. "Language: Cpp\n"
  8097. "IndentWidth: 34\n"
  8098. "...\n",
  8099. IndentWidth, 12u);
  8100. Style.Language = FormatStyle::LK_Cpp;
  8101. CHECK_PARSE("---\n"
  8102. "Language: JavaScript\n"
  8103. "IndentWidth: 12\n"
  8104. "---\n"
  8105. "Language: Cpp\n"
  8106. "IndentWidth: 34\n"
  8107. "...\n",
  8108. IndentWidth, 34u);
  8109. CHECK_PARSE("---\n"
  8110. "IndentWidth: 78\n"
  8111. "---\n"
  8112. "Language: JavaScript\n"
  8113. "IndentWidth: 56\n"
  8114. "...\n",
  8115. IndentWidth, 78u);
  8116. Style.ColumnLimit = 123;
  8117. Style.IndentWidth = 234;
  8118. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8119. Style.TabWidth = 345;
  8120. EXPECT_FALSE(parseConfiguration("---\n"
  8121. "IndentWidth: 456\n"
  8122. "BreakBeforeBraces: Allman\n"
  8123. "---\n"
  8124. "Language: JavaScript\n"
  8125. "IndentWidth: 111\n"
  8126. "TabWidth: 111\n"
  8127. "---\n"
  8128. "Language: Cpp\n"
  8129. "BreakBeforeBraces: Stroustrup\n"
  8130. "TabWidth: 789\n"
  8131. "...\n",
  8132. &Style));
  8133. EXPECT_EQ(123u, Style.ColumnLimit);
  8134. EXPECT_EQ(456u, Style.IndentWidth);
  8135. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8136. EXPECT_EQ(789u, Style.TabWidth);
  8137. EXPECT_EQ(parseConfiguration("---\n"
  8138. "Language: JavaScript\n"
  8139. "IndentWidth: 56\n"
  8140. "---\n"
  8141. "IndentWidth: 78\n"
  8142. "...\n",
  8143. &Style),
  8144. ParseError::Error);
  8145. EXPECT_EQ(parseConfiguration("---\n"
  8146. "Language: JavaScript\n"
  8147. "IndentWidth: 56\n"
  8148. "---\n"
  8149. "Language: JavaScript\n"
  8150. "IndentWidth: 78\n"
  8151. "...\n",
  8152. &Style),
  8153. ParseError::Error);
  8154. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8155. }
  8156. #undef CHECK_PARSE
  8157. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8158. FormatStyle Style = {};
  8159. Style.Language = FormatStyle::LK_JavaScript;
  8160. Style.BreakBeforeTernaryOperators = true;
  8161. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8162. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8163. Style.BreakBeforeTernaryOperators = true;
  8164. EXPECT_EQ(0, parseConfiguration("---\n"
  8165. "BasedOnStyle: Google\n"
  8166. "---\n"
  8167. "Language: JavaScript\n"
  8168. "IndentWidth: 76\n"
  8169. "...\n",
  8170. &Style)
  8171. .value());
  8172. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8173. EXPECT_EQ(76u, Style.IndentWidth);
  8174. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8175. }
  8176. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8177. FormatStyle Style = getLLVMStyle();
  8178. std::string YAML = configurationAsText(Style);
  8179. FormatStyle ParsedStyle = {};
  8180. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8181. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8182. EXPECT_EQ(Style, ParsedStyle);
  8183. }
  8184. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8185. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8186. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8187. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8188. "\"\xef\xee\xf0\xf3...\"",
  8189. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8190. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8191. "\xef\xee\xf0\xf3...\"",
  8192. getLLVMStyleWithColumns(12)));
  8193. }
  8194. TEST_F(FormatTest, HandlesUTF8BOM) {
  8195. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8196. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8197. format("\xef\xbb\xbf#include <iostream>"));
  8198. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8199. format("\xef\xbb\xbf\n#include <iostream>"));
  8200. }
  8201. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8202. #if !defined(_MSC_VER)
  8203. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8204. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8205. getLLVMStyleWithColumns(35));
  8206. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8207. getLLVMStyleWithColumns(31));
  8208. verifyFormat("// Однажды в студёную зимнюю пору...",
  8209. getLLVMStyleWithColumns(36));
  8210. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  8211. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8212. getLLVMStyleWithColumns(39));
  8213. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8214. getLLVMStyleWithColumns(35));
  8215. }
  8216. TEST_F(FormatTest, SplitsUTF8Strings) {
  8217. // Non-printable characters' width is currently considered to be the length in
  8218. // bytes in UTF8. The characters can be displayed in very different manner
  8219. // (zero-width, single width with a substitution glyph, expanded to their code
  8220. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8221. EXPECT_EQ("\"aaaaÄ\"\n"
  8222. "\"\xc2\x8d\";",
  8223. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8224. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8225. "\"\xc2\x8d\";",
  8226. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8227. EXPECT_EQ("\"Однажды, в \"\n"
  8228. "\"студёную \"\n"
  8229. "\"зимнюю \"\n"
  8230. "\"пору,\"",
  8231. format("\"Однажды, в студёную зимнюю пору,\"",
  8232. getLLVMStyleWithColumns(13)));
  8233. EXPECT_EQ(
  8234. "\"一 二 三 \"\n"
  8235. "\"四 五六 \"\n"
  8236. "\"七 八 九 \"\n"
  8237. "\"十\"",
  8238. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  8239. EXPECT_EQ("\"一\t二 \"\n"
  8240. "\"\t三 \"\n"
  8241. "\"四 五\t六 \"\n"
  8242. "\"\t七 \"\n"
  8243. "\"八九十\tqq\"",
  8244. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8245. getLLVMStyleWithColumns(11)));
  8246. // UTF8 character in an escape sequence.
  8247. EXPECT_EQ("\"aaaaaa\"\n"
  8248. "\"\\\xC2\x8D\"",
  8249. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  8250. }
  8251. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8252. EXPECT_EQ("const char *sssss =\n"
  8253. " \"一二三四五六七八\\\n"
  8254. " 九 十\";",
  8255. format("const char *sssss = \"一二三四五六七八\\\n"
  8256. " 九 十\";",
  8257. getLLVMStyleWithColumns(30)));
  8258. }
  8259. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8260. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8261. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8262. EXPECT_EQ("// Я из лесу\n"
  8263. "// вышел; был\n"
  8264. "// сильный\n"
  8265. "// мороз.",
  8266. format("// Я из лесу вышел; был сильный мороз.",
  8267. getLLVMStyleWithColumns(13)));
  8268. EXPECT_EQ("// 一二三\n"
  8269. "// 四五六七\n"
  8270. "// 八 九\n"
  8271. "// 十",
  8272. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8273. }
  8274. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8275. EXPECT_EQ("/* Гляжу,\n"
  8276. " * поднимается\n"
  8277. " * медленно в\n"
  8278. " * гору\n"
  8279. " * Лошадка,\n"
  8280. " * везущая\n"
  8281. " * хворосту\n"
  8282. " * воз. */",
  8283. format("/* Гляжу, поднимается медленно в гору\n"
  8284. " * Лошадка, везущая хворосту воз. */",
  8285. getLLVMStyleWithColumns(13)));
  8286. EXPECT_EQ(
  8287. "/* 一二三\n"
  8288. " * 四五六七\n"
  8289. " * 八 九\n"
  8290. " * 十 */",
  8291. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8292. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8293. " * 𝕓𝕪𝕥𝕖\n"
  8294. " * 𝖀𝕿𝕱-𝟠 */",
  8295. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8296. }
  8297. #endif // _MSC_VER
  8298. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8299. FormatStyle Style = getLLVMStyle();
  8300. Style.ConstructorInitializerIndentWidth = 4;
  8301. verifyFormat(
  8302. "SomeClass::Constructor()\n"
  8303. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8304. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8305. Style);
  8306. Style.ConstructorInitializerIndentWidth = 2;
  8307. verifyFormat(
  8308. "SomeClass::Constructor()\n"
  8309. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8310. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8311. Style);
  8312. Style.ConstructorInitializerIndentWidth = 0;
  8313. verifyFormat(
  8314. "SomeClass::Constructor()\n"
  8315. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8316. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8317. Style);
  8318. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  8319. verifyFormat(
  8320. "SomeLongTemplateVariableName<\n"
  8321. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
  8322. Style);
  8323. verifyFormat(
  8324. "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  8325. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  8326. Style);
  8327. }
  8328. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8329. FormatStyle Style = getLLVMStyle();
  8330. Style.BreakConstructorInitializersBeforeComma = true;
  8331. Style.ConstructorInitializerIndentWidth = 4;
  8332. verifyFormat("SomeClass::Constructor()\n"
  8333. " : a(a)\n"
  8334. " , b(b)\n"
  8335. " , c(c) {}",
  8336. Style);
  8337. verifyFormat("SomeClass::Constructor()\n"
  8338. " : a(a) {}",
  8339. Style);
  8340. Style.ColumnLimit = 0;
  8341. verifyFormat("SomeClass::Constructor()\n"
  8342. " : a(a) {}",
  8343. Style);
  8344. verifyFormat("SomeClass::Constructor() noexcept\n"
  8345. " : a(a) {}",
  8346. Style);
  8347. verifyFormat("SomeClass::Constructor()\n"
  8348. " : a(a)\n"
  8349. " , b(b)\n"
  8350. " , c(c) {}",
  8351. Style);
  8352. verifyFormat("SomeClass::Constructor()\n"
  8353. " : a(a) {\n"
  8354. " foo();\n"
  8355. " bar();\n"
  8356. "}",
  8357. Style);
  8358. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8359. verifyFormat("SomeClass::Constructor()\n"
  8360. " : a(a)\n"
  8361. " , b(b)\n"
  8362. " , c(c) {\n}",
  8363. Style);
  8364. verifyFormat("SomeClass::Constructor()\n"
  8365. " : a(a) {\n}",
  8366. Style);
  8367. Style.ColumnLimit = 80;
  8368. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8369. Style.ConstructorInitializerIndentWidth = 2;
  8370. verifyFormat("SomeClass::Constructor()\n"
  8371. " : a(a)\n"
  8372. " , b(b)\n"
  8373. " , c(c) {}",
  8374. Style);
  8375. Style.ConstructorInitializerIndentWidth = 0;
  8376. verifyFormat("SomeClass::Constructor()\n"
  8377. ": a(a)\n"
  8378. ", b(b)\n"
  8379. ", c(c) {}",
  8380. Style);
  8381. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8382. Style.ConstructorInitializerIndentWidth = 4;
  8383. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8384. verifyFormat(
  8385. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8386. Style);
  8387. verifyFormat(
  8388. "SomeClass::Constructor()\n"
  8389. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8390. Style);
  8391. Style.ConstructorInitializerIndentWidth = 4;
  8392. Style.ColumnLimit = 60;
  8393. verifyFormat("SomeClass::Constructor()\n"
  8394. " : aaaaaaaa(aaaaaaaa)\n"
  8395. " , aaaaaaaa(aaaaaaaa)\n"
  8396. " , aaaaaaaa(aaaaaaaa) {}",
  8397. Style);
  8398. }
  8399. TEST_F(FormatTest, Destructors) {
  8400. verifyFormat("void F(int &i) { i.~int(); }");
  8401. verifyFormat("void F(int &i) { i->~int(); }");
  8402. }
  8403. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8404. FormatStyle Style = getWebKitStyle();
  8405. // Don't indent in outer namespaces.
  8406. verifyFormat("namespace outer {\n"
  8407. "int i;\n"
  8408. "namespace inner {\n"
  8409. " int i;\n"
  8410. "} // namespace inner\n"
  8411. "} // namespace outer\n"
  8412. "namespace other_outer {\n"
  8413. "int i;\n"
  8414. "}",
  8415. Style);
  8416. // Don't indent case labels.
  8417. verifyFormat("switch (variable) {\n"
  8418. "case 1:\n"
  8419. "case 2:\n"
  8420. " doSomething();\n"
  8421. " break;\n"
  8422. "default:\n"
  8423. " ++variable;\n"
  8424. "}",
  8425. Style);
  8426. // Wrap before binary operators.
  8427. EXPECT_EQ("void f()\n"
  8428. "{\n"
  8429. " if (aaaaaaaaaaaaaaaa\n"
  8430. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8431. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8432. " return;\n"
  8433. "}",
  8434. format("void f() {\n"
  8435. "if (aaaaaaaaaaaaaaaa\n"
  8436. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8437. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8438. "return;\n"
  8439. "}",
  8440. Style));
  8441. // Allow functions on a single line.
  8442. verifyFormat("void f() { return; }", Style);
  8443. // Constructor initializers are formatted one per line with the "," on the
  8444. // new line.
  8445. verifyFormat("Constructor()\n"
  8446. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8447. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8448. " aaaaaaaaaaaaaa)\n"
  8449. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8450. "{\n"
  8451. "}",
  8452. Style);
  8453. verifyFormat("SomeClass::Constructor()\n"
  8454. " : a(a)\n"
  8455. "{\n"
  8456. "}",
  8457. Style);
  8458. EXPECT_EQ("SomeClass::Constructor()\n"
  8459. " : a(a)\n"
  8460. "{\n"
  8461. "}",
  8462. format("SomeClass::Constructor():a(a){}", Style));
  8463. verifyFormat("SomeClass::Constructor()\n"
  8464. " : a(a)\n"
  8465. " , b(b)\n"
  8466. " , c(c)\n"
  8467. "{\n"
  8468. "}",
  8469. Style);
  8470. verifyFormat("SomeClass::Constructor()\n"
  8471. " : a(a)\n"
  8472. "{\n"
  8473. " foo();\n"
  8474. " bar();\n"
  8475. "}",
  8476. Style);
  8477. // Access specifiers should be aligned left.
  8478. verifyFormat("class C {\n"
  8479. "public:\n"
  8480. " int i;\n"
  8481. "};",
  8482. Style);
  8483. // Do not align comments.
  8484. verifyFormat("int a; // Do not\n"
  8485. "double b; // align comments.",
  8486. Style);
  8487. // Do not align operands.
  8488. EXPECT_EQ("ASSERT(aaaa\n"
  8489. " || bbbb);",
  8490. format("ASSERT ( aaaa\n||bbbb);", Style));
  8491. // Accept input's line breaks.
  8492. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8493. " || bbbbbbbbbbbbbbb) {\n"
  8494. " i++;\n"
  8495. "}",
  8496. format("if (aaaaaaaaaaaaaaa\n"
  8497. "|| bbbbbbbbbbbbbbb) { i++; }",
  8498. Style));
  8499. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8500. " i++;\n"
  8501. "}",
  8502. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8503. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8504. verifyFormat("#define aNumber 10", Style);
  8505. // However, generally keep the line breaks that the user authored.
  8506. EXPECT_EQ("#define aNumber \\\n"
  8507. " 10",
  8508. format("#define aNumber \\\n"
  8509. " 10",
  8510. Style));
  8511. // Keep empty and one-element array literals on a single line.
  8512. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8513. " copyItems:YES];",
  8514. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8515. "copyItems:YES];",
  8516. Style));
  8517. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8518. " copyItems:YES];",
  8519. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8520. " copyItems:YES];",
  8521. Style));
  8522. // FIXME: This does not seem right, there should be more indentation before
  8523. // the array literal's entries. Nested blocks have the same problem.
  8524. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8525. " @\"a\",\n"
  8526. " @\"a\"\n"
  8527. "]\n"
  8528. " copyItems:YES];",
  8529. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8530. " @\"a\",\n"
  8531. " @\"a\"\n"
  8532. " ]\n"
  8533. " copyItems:YES];",
  8534. Style));
  8535. EXPECT_EQ(
  8536. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8537. " copyItems:YES];",
  8538. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8539. " copyItems:YES];",
  8540. Style));
  8541. verifyFormat("[self.a b:c c:d];", Style);
  8542. EXPECT_EQ("[self.a b:c\n"
  8543. " c:d];",
  8544. format("[self.a b:c\n"
  8545. "c:d];",
  8546. Style));
  8547. }
  8548. TEST_F(FormatTest, FormatsLambdas) {
  8549. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8550. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8551. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8552. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8553. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8554. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8555. verifyFormat("int x = f(*+[] {});");
  8556. verifyFormat("void f() {\n"
  8557. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8558. "}\n");
  8559. verifyFormat("void f() {\n"
  8560. " other(x.begin(), //\n"
  8561. " x.end(), //\n"
  8562. " [&](int, int) { return 1; });\n"
  8563. "}\n");
  8564. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8565. " return 43;\n"
  8566. "});");
  8567. EXPECT_EQ("SomeFunction([]() {\n"
  8568. "#define A a\n"
  8569. " return 43;\n"
  8570. "});",
  8571. format("SomeFunction([](){\n"
  8572. "#define A a\n"
  8573. "return 43;\n"
  8574. "});"));
  8575. verifyFormat("void f() {\n"
  8576. " SomeFunction([](decltype(x), A *a) {});\n"
  8577. "}");
  8578. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8579. " [](const aaaaaaaaaa &a) { return a; });");
  8580. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8581. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8582. "});");
  8583. verifyFormat("Constructor()\n"
  8584. " : Field([] { // comment\n"
  8585. " int i;\n"
  8586. " }) {}");
  8587. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  8588. " return some_parameter.size();\n"
  8589. "};");
  8590. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  8591. " [](const string &s) { return s; };");
  8592. verifyFormat("int i = aaaaaa ? 1 //\n"
  8593. " : [] {\n"
  8594. " return 2; //\n"
  8595. " }();");
  8596. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  8597. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  8598. " return x == 2; // force break\n"
  8599. " });");
  8600. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  8601. " int iiiiiiiiiiii) {\n"
  8602. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  8603. "});",
  8604. getLLVMStyleWithColumns(60));
  8605. verifyFormat("SomeFunction({[&] {\n"
  8606. " // comment\n"
  8607. " },\n"
  8608. " [&] {\n"
  8609. " // comment\n"
  8610. " }});");
  8611. verifyFormat("SomeFunction({[&] {\n"
  8612. " // comment\n"
  8613. "}});");
  8614. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  8615. " [&]() { return true; },\n"
  8616. " aaaaa aaaaaaaaa);");
  8617. // Lambdas with return types.
  8618. verifyFormat("int c = []() -> int { return 2; }();\n");
  8619. verifyFormat("int c = []() -> int * { return 2; }();\n");
  8620. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8621. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8622. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  8623. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  8624. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  8625. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  8626. verifyFormat("[a, a]() -> a<1> {};");
  8627. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8628. " int j) -> int {\n"
  8629. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8630. "};");
  8631. verifyFormat(
  8632. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  8633. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  8634. " return aaaaaaaaaaaaaaaaa;\n"
  8635. " });",
  8636. getLLVMStyleWithColumns(70));
  8637. verifyFormat("[]() //\n"
  8638. " -> int {\n"
  8639. " return 1; //\n"
  8640. "};");
  8641. // Multiple lambdas in the same parentheses change indentation rules.
  8642. verifyFormat("SomeFunction(\n"
  8643. " []() {\n"
  8644. " int i = 42;\n"
  8645. " return i;\n"
  8646. " },\n"
  8647. " []() {\n"
  8648. " int j = 43;\n"
  8649. " return j;\n"
  8650. " });");
  8651. // More complex introducers.
  8652. verifyFormat("return [i, args...] {};");
  8653. // Not lambdas.
  8654. verifyFormat("constexpr char hello[]{\"hello\"};");
  8655. verifyFormat("double &operator[](int i) { return 0; }\n"
  8656. "int i;");
  8657. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8658. verifyFormat("int i = a[a][a]->f();");
  8659. verifyFormat("int i = (*b)[a]->f();");
  8660. // Other corner cases.
  8661. verifyFormat("void f() {\n"
  8662. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8663. " );\n"
  8664. "}");
  8665. // Lambdas created through weird macros.
  8666. verifyFormat("void f() {\n"
  8667. " MACRO((const AA &a) { return 1; });\n"
  8668. " MACRO((AA &a) { return 1; });\n"
  8669. "}");
  8670. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  8671. " doo_dah();\n"
  8672. " doo_dah();\n"
  8673. " })) {\n"
  8674. "}");
  8675. verifyFormat("auto lambda = []() {\n"
  8676. " int a = 2\n"
  8677. "#if A\n"
  8678. " + 2\n"
  8679. "#endif\n"
  8680. " ;\n"
  8681. "};");
  8682. }
  8683. TEST_F(FormatTest, FormatsBlocks) {
  8684. FormatStyle ShortBlocks = getLLVMStyle();
  8685. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8686. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8687. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8688. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8689. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8690. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8691. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8692. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8693. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8694. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8695. verifyFormat("[operation setCompletionBlock:^{\n"
  8696. " [self onOperationDone];\n"
  8697. "}];");
  8698. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8699. " [self onOperationDone];\n"
  8700. "}]};");
  8701. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8702. " f();\n"
  8703. "}];");
  8704. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8705. " return 1;\n"
  8706. "}];");
  8707. verifyFormat("[myObject doSomethingWith:arg1\n"
  8708. " aaa:^int(int *a) {\n"
  8709. " return 1;\n"
  8710. " }\n"
  8711. " bbb:f(a * bbbbbbbb)];");
  8712. verifyFormat("[operation setCompletionBlock:^{\n"
  8713. " [self.delegate newDataAvailable];\n"
  8714. "}];",
  8715. getLLVMStyleWithColumns(60));
  8716. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8717. " NSString *path = [self sessionFilePath];\n"
  8718. " if (path) {\n"
  8719. " // ...\n"
  8720. " }\n"
  8721. "});");
  8722. verifyFormat("[[SessionService sharedService]\n"
  8723. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8724. " if (window) {\n"
  8725. " [self windowDidLoad:window];\n"
  8726. " } else {\n"
  8727. " [self errorLoadingWindow];\n"
  8728. " }\n"
  8729. " }];");
  8730. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8731. " // ...\n"
  8732. "};\n",
  8733. getLLVMStyleWithColumns(40));
  8734. verifyFormat("[[SessionService sharedService]\n"
  8735. " loadWindowWithCompletionBlock: //\n"
  8736. " ^(SessionWindow *window) {\n"
  8737. " if (window) {\n"
  8738. " [self windowDidLoad:window];\n"
  8739. " } else {\n"
  8740. " [self errorLoadingWindow];\n"
  8741. " }\n"
  8742. " }];",
  8743. getLLVMStyleWithColumns(60));
  8744. verifyFormat("[myObject doSomethingWith:arg1\n"
  8745. " firstBlock:^(Foo *a) {\n"
  8746. " // ...\n"
  8747. " int i;\n"
  8748. " }\n"
  8749. " secondBlock:^(Bar *b) {\n"
  8750. " // ...\n"
  8751. " int i;\n"
  8752. " }\n"
  8753. " thirdBlock:^Foo(Bar *b) {\n"
  8754. " // ...\n"
  8755. " int i;\n"
  8756. " }];");
  8757. verifyFormat("[myObject doSomethingWith:arg1\n"
  8758. " firstBlock:-1\n"
  8759. " secondBlock:^(Bar *b) {\n"
  8760. " // ...\n"
  8761. " int i;\n"
  8762. " }];");
  8763. verifyFormat("f(^{\n"
  8764. " @autoreleasepool {\n"
  8765. " if (a) {\n"
  8766. " g();\n"
  8767. " }\n"
  8768. " }\n"
  8769. "});");
  8770. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  8771. verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
  8772. "};");
  8773. FormatStyle FourIndent = getLLVMStyle();
  8774. FourIndent.ObjCBlockIndentWidth = 4;
  8775. verifyFormat("[operation setCompletionBlock:^{\n"
  8776. " [self onOperationDone];\n"
  8777. "}];",
  8778. FourIndent);
  8779. }
  8780. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  8781. FormatStyle ZeroColumn = getLLVMStyle();
  8782. ZeroColumn.ColumnLimit = 0;
  8783. verifyFormat("[[SessionService sharedService] "
  8784. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8785. " if (window) {\n"
  8786. " [self windowDidLoad:window];\n"
  8787. " } else {\n"
  8788. " [self errorLoadingWindow];\n"
  8789. " }\n"
  8790. "}];",
  8791. ZeroColumn);
  8792. EXPECT_EQ("[[SessionService sharedService]\n"
  8793. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8794. " if (window) {\n"
  8795. " [self windowDidLoad:window];\n"
  8796. " } else {\n"
  8797. " [self errorLoadingWindow];\n"
  8798. " }\n"
  8799. " }];",
  8800. format("[[SessionService sharedService]\n"
  8801. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8802. " if (window) {\n"
  8803. " [self windowDidLoad:window];\n"
  8804. " } else {\n"
  8805. " [self errorLoadingWindow];\n"
  8806. " }\n"
  8807. "}];",
  8808. ZeroColumn));
  8809. verifyFormat("[myObject doSomethingWith:arg1\n"
  8810. " firstBlock:^(Foo *a) {\n"
  8811. " // ...\n"
  8812. " int i;\n"
  8813. " }\n"
  8814. " secondBlock:^(Bar *b) {\n"
  8815. " // ...\n"
  8816. " int i;\n"
  8817. " }\n"
  8818. " thirdBlock:^Foo(Bar *b) {\n"
  8819. " // ...\n"
  8820. " int i;\n"
  8821. " }];",
  8822. ZeroColumn);
  8823. verifyFormat("f(^{\n"
  8824. " @autoreleasepool {\n"
  8825. " if (a) {\n"
  8826. " g();\n"
  8827. " }\n"
  8828. " }\n"
  8829. "});",
  8830. ZeroColumn);
  8831. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8832. " // ...\n"
  8833. "};",
  8834. ZeroColumn);
  8835. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  8836. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  8837. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  8838. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  8839. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  8840. " int i;\n"
  8841. "};",
  8842. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  8843. }
  8844. TEST_F(FormatTest, SupportsCRLF) {
  8845. EXPECT_EQ("int a;\r\n"
  8846. "int b;\r\n"
  8847. "int c;\r\n",
  8848. format("int a;\r\n"
  8849. " int b;\r\n"
  8850. " int c;\r\n",
  8851. getLLVMStyle()));
  8852. EXPECT_EQ("int a;\r\n"
  8853. "int b;\r\n"
  8854. "int c;\r\n",
  8855. format("int a;\r\n"
  8856. " int b;\n"
  8857. " int c;\r\n",
  8858. getLLVMStyle()));
  8859. EXPECT_EQ("int a;\n"
  8860. "int b;\n"
  8861. "int c;\n",
  8862. format("int a;\r\n"
  8863. " int b;\n"
  8864. " int c;\n",
  8865. getLLVMStyle()));
  8866. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8867. "\"bbbbbbb\";\r\n",
  8868. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8869. EXPECT_EQ("#define A \\\r\n"
  8870. " b; \\\r\n"
  8871. " c; \\\r\n"
  8872. " d;\r\n",
  8873. format("#define A \\\r\n"
  8874. " b; \\\r\n"
  8875. " c; d; \r\n",
  8876. getGoogleStyle()));
  8877. EXPECT_EQ("/*\r\n"
  8878. "multi line block comments\r\n"
  8879. "should not introduce\r\n"
  8880. "an extra carriage return\r\n"
  8881. "*/\r\n",
  8882. format("/*\r\n"
  8883. "multi line block comments\r\n"
  8884. "should not introduce\r\n"
  8885. "an extra carriage return\r\n"
  8886. "*/\r\n"));
  8887. }
  8888. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  8889. verifyFormat("MY_CLASS(C) {\n"
  8890. " int i;\n"
  8891. " int j;\n"
  8892. "};");
  8893. }
  8894. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  8895. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  8896. TwoIndent.ContinuationIndentWidth = 2;
  8897. EXPECT_EQ("int i =\n"
  8898. " longFunction(\n"
  8899. " arg);",
  8900. format("int i = longFunction(arg);", TwoIndent));
  8901. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  8902. SixIndent.ContinuationIndentWidth = 6;
  8903. EXPECT_EQ("int i =\n"
  8904. " longFunction(\n"
  8905. " arg);",
  8906. format("int i = longFunction(arg);", SixIndent));
  8907. }
  8908. TEST_F(FormatTest, SpacesInAngles) {
  8909. FormatStyle Spaces = getLLVMStyle();
  8910. Spaces.SpacesInAngles = true;
  8911. verifyFormat("static_cast< int >(arg);", Spaces);
  8912. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  8913. verifyFormat("f< int, float >();", Spaces);
  8914. verifyFormat("template <> g() {}", Spaces);
  8915. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  8916. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  8917. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  8918. Spaces);
  8919. Spaces.Standard = FormatStyle::LS_Cpp03;
  8920. Spaces.SpacesInAngles = true;
  8921. verifyFormat("A< A< int > >();", Spaces);
  8922. Spaces.SpacesInAngles = false;
  8923. verifyFormat("A<A<int> >();", Spaces);
  8924. Spaces.Standard = FormatStyle::LS_Cpp11;
  8925. Spaces.SpacesInAngles = true;
  8926. verifyFormat("A< A< int > >();", Spaces);
  8927. Spaces.SpacesInAngles = false;
  8928. verifyFormat("A<A<int>>();", Spaces);
  8929. }
  8930. TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
  8931. FormatStyle Style = getLLVMStyle();
  8932. Style.SpaceAfterTemplateKeyword = false;
  8933. verifyFormat("template<int> void foo();", Style);
  8934. }
  8935. TEST_F(FormatTest, TripleAngleBrackets) {
  8936. verifyFormat("f<<<1, 1>>>();");
  8937. verifyFormat("f<<<1, 1, 1, s>>>();");
  8938. verifyFormat("f<<<a, b, c, d>>>();");
  8939. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  8940. verifyFormat("f<param><<<1, 1>>>();");
  8941. verifyFormat("f<1><<<1, 1>>>();");
  8942. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  8943. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  8944. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  8945. verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
  8946. " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
  8947. }
  8948. TEST_F(FormatTest, MergeLessLessAtEnd) {
  8949. verifyFormat("<<");
  8950. EXPECT_EQ("< < <", format("\\\n<<<"));
  8951. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  8952. "aaallvm::outs() <<");
  8953. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  8954. "aaaallvm::outs()\n <<");
  8955. }
  8956. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  8957. std::string code = "#if A\n"
  8958. "#if B\n"
  8959. "a.\n"
  8960. "#endif\n"
  8961. " a = 1;\n"
  8962. "#else\n"
  8963. "#endif\n"
  8964. "#if C\n"
  8965. "#else\n"
  8966. "#endif\n";
  8967. EXPECT_EQ(code, format(code));
  8968. }
  8969. TEST_F(FormatTest, HandleConflictMarkers) {
  8970. // Git/SVN conflict markers.
  8971. EXPECT_EQ("int a;\n"
  8972. "void f() {\n"
  8973. " callme(some(parameter1,\n"
  8974. "<<<<<<< text by the vcs\n"
  8975. " parameter2),\n"
  8976. "||||||| text by the vcs\n"
  8977. " parameter2),\n"
  8978. " parameter3,\n"
  8979. "======= text by the vcs\n"
  8980. " parameter2, parameter3),\n"
  8981. ">>>>>>> text by the vcs\n"
  8982. " otherparameter);\n",
  8983. format("int a;\n"
  8984. "void f() {\n"
  8985. " callme(some(parameter1,\n"
  8986. "<<<<<<< text by the vcs\n"
  8987. " parameter2),\n"
  8988. "||||||| text by the vcs\n"
  8989. " parameter2),\n"
  8990. " parameter3,\n"
  8991. "======= text by the vcs\n"
  8992. " parameter2,\n"
  8993. " parameter3),\n"
  8994. ">>>>>>> text by the vcs\n"
  8995. " otherparameter);\n"));
  8996. // Perforce markers.
  8997. EXPECT_EQ("void f() {\n"
  8998. " function(\n"
  8999. ">>>> text by the vcs\n"
  9000. " parameter,\n"
  9001. "==== text by the vcs\n"
  9002. " parameter,\n"
  9003. "==== text by the vcs\n"
  9004. " parameter,\n"
  9005. "<<<< text by the vcs\n"
  9006. " parameter);\n",
  9007. format("void f() {\n"
  9008. " function(\n"
  9009. ">>>> text by the vcs\n"
  9010. " parameter,\n"
  9011. "==== text by the vcs\n"
  9012. " parameter,\n"
  9013. "==== text by the vcs\n"
  9014. " parameter,\n"
  9015. "<<<< text by the vcs\n"
  9016. " parameter);\n"));
  9017. EXPECT_EQ("<<<<<<<\n"
  9018. "|||||||\n"
  9019. "=======\n"
  9020. ">>>>>>>",
  9021. format("<<<<<<<\n"
  9022. "|||||||\n"
  9023. "=======\n"
  9024. ">>>>>>>"));
  9025. EXPECT_EQ("<<<<<<<\n"
  9026. "|||||||\n"
  9027. "int i;\n"
  9028. "=======\n"
  9029. ">>>>>>>",
  9030. format("<<<<<<<\n"
  9031. "|||||||\n"
  9032. "int i;\n"
  9033. "=======\n"
  9034. ">>>>>>>"));
  9035. // FIXME: Handle parsing of macros around conflict markers correctly:
  9036. EXPECT_EQ("#define Macro \\\n"
  9037. "<<<<<<<\n"
  9038. "Something \\\n"
  9039. "|||||||\n"
  9040. "Else \\\n"
  9041. "=======\n"
  9042. "Other \\\n"
  9043. ">>>>>>>\n"
  9044. " End int i;\n",
  9045. format("#define Macro \\\n"
  9046. "<<<<<<<\n"
  9047. " Something \\\n"
  9048. "|||||||\n"
  9049. " Else \\\n"
  9050. "=======\n"
  9051. " Other \\\n"
  9052. ">>>>>>>\n"
  9053. " End\n"
  9054. "int i;\n"));
  9055. }
  9056. TEST_F(FormatTest, DisableRegions) {
  9057. EXPECT_EQ("int i;\n"
  9058. "// clang-format off\n"
  9059. " int j;\n"
  9060. "// clang-format on\n"
  9061. "int k;",
  9062. format(" int i;\n"
  9063. " // clang-format off\n"
  9064. " int j;\n"
  9065. " // clang-format on\n"
  9066. " int k;"));
  9067. EXPECT_EQ("int i;\n"
  9068. "/* clang-format off */\n"
  9069. " int j;\n"
  9070. "/* clang-format on */\n"
  9071. "int k;",
  9072. format(" int i;\n"
  9073. " /* clang-format off */\n"
  9074. " int j;\n"
  9075. " /* clang-format on */\n"
  9076. " int k;"));
  9077. // Don't reflow comments within disabled regions.
  9078. EXPECT_EQ(
  9079. "// clang-format off\n"
  9080. "// long long long long long long line\n"
  9081. "/* clang-format on */\n"
  9082. "/* long long long\n"
  9083. " * long long long\n"
  9084. " * line */\n"
  9085. "int i;\n"
  9086. "/* clang-format off */\n"
  9087. "/* long long long long long long line */\n",
  9088. format("// clang-format off\n"
  9089. "// long long long long long long line\n"
  9090. "/* clang-format on */\n"
  9091. "/* long long long long long long line */\n"
  9092. "int i;\n"
  9093. "/* clang-format off */\n"
  9094. "/* long long long long long long line */\n",
  9095. getLLVMStyleWithColumns(20)));
  9096. }
  9097. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  9098. format("? ) =");
  9099. verifyNoCrash("#define a\\\n /**/}");
  9100. }
  9101. TEST_F(FormatTest, FormatsTableGenCode) {
  9102. FormatStyle Style = getLLVMStyle();
  9103. Style.Language = FormatStyle::LK_TableGen;
  9104. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  9105. }
  9106. TEST_F(FormatTest, ArrayOfTemplates) {
  9107. EXPECT_EQ("auto a = new unique_ptr<int>[10];",
  9108. format("auto a = new unique_ptr<int > [ 10];"));
  9109. FormatStyle Spaces = getLLVMStyle();
  9110. Spaces.SpacesInSquareBrackets = true;
  9111. EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
  9112. format("auto a = new unique_ptr<int > [10];", Spaces));
  9113. }
  9114. TEST_F(FormatTest, ArrayAsTemplateType) {
  9115. EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
  9116. format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
  9117. FormatStyle Spaces = getLLVMStyle();
  9118. Spaces.SpacesInSquareBrackets = true;
  9119. EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
  9120. format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
  9121. }
  9122. TEST(FormatStyle, GetStyleOfFile) {
  9123. vfs::InMemoryFileSystem FS;
  9124. // Test 1: format file in the same directory.
  9125. ASSERT_TRUE(
  9126. FS.addFile("/a/.clang-format", 0,
  9127. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
  9128. ASSERT_TRUE(
  9129. FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  9130. auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
  9131. ASSERT_TRUE((bool)Style1);
  9132. ASSERT_EQ(*Style1, getLLVMStyle());
  9133. // Test 2.1: fallback to default.
  9134. ASSERT_TRUE(
  9135. FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  9136. auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
  9137. ASSERT_TRUE((bool)Style2);
  9138. ASSERT_EQ(*Style2, getMozillaStyle());
  9139. // Test 2.2: no format on 'none' fallback style.
  9140. Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
  9141. ASSERT_TRUE((bool)Style2);
  9142. ASSERT_EQ(*Style2, getNoStyle());
  9143. // Test 2.3: format if config is found with no based style while fallback is
  9144. // 'none'.
  9145. ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
  9146. llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
  9147. Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
  9148. ASSERT_TRUE((bool)Style2);
  9149. ASSERT_EQ(*Style2, getLLVMStyle());
  9150. // Test 2.4: format if yaml with no based style, while fallback is 'none'.
  9151. Style2 = getStyle("{}", "a.h", "none", "", &FS);
  9152. ASSERT_TRUE((bool)Style2);
  9153. ASSERT_EQ(*Style2, getLLVMStyle());
  9154. // Test 3: format file in parent directory.
  9155. ASSERT_TRUE(
  9156. FS.addFile("/c/.clang-format", 0,
  9157. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
  9158. ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
  9159. llvm::MemoryBuffer::getMemBuffer("int i;")));
  9160. auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
  9161. ASSERT_TRUE((bool)Style3);
  9162. ASSERT_EQ(*Style3, getGoogleStyle());
  9163. // Test 4: error on invalid fallback style
  9164. auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
  9165. ASSERT_FALSE((bool)Style4);
  9166. llvm::consumeError(Style4.takeError());
  9167. // Test 5: error on invalid yaml on command line
  9168. auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
  9169. ASSERT_FALSE((bool)Style5);
  9170. llvm::consumeError(Style5.takeError());
  9171. // Test 6: error on invalid style
  9172. auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
  9173. ASSERT_FALSE((bool)Style6);
  9174. llvm::consumeError(Style6.takeError());
  9175. // Test 7: found config file, error on parsing it
  9176. ASSERT_TRUE(
  9177. FS.addFile("/d/.clang-format", 0,
  9178. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
  9179. "InvalidKey: InvalidValue")));
  9180. ASSERT_TRUE(
  9181. FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  9182. auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
  9183. ASSERT_FALSE((bool)Style7);
  9184. llvm::consumeError(Style7.takeError());
  9185. }
  9186. TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
  9187. // Column limit is 20.
  9188. std::string Code = "Type *a =\n"
  9189. " new Type();\n"
  9190. "g(iiiii, 0, jjjjj,\n"
  9191. " 0, kkkkk, 0, mm);\n"
  9192. "int bad = format ;";
  9193. std::string Expected = "auto a = new Type();\n"
  9194. "g(iiiii, nullptr,\n"
  9195. " jjjjj, nullptr,\n"
  9196. " kkkkk, nullptr,\n"
  9197. " mm);\n"
  9198. "int bad = format ;";
  9199. FileID ID = Context.createInMemoryFile("format.cpp", Code);
  9200. tooling::Replacements Replaces = toReplacements(
  9201. {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
  9202. "auto "),
  9203. tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
  9204. "nullptr"),
  9205. tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
  9206. "nullptr"),
  9207. tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
  9208. "nullptr")});
  9209. format::FormatStyle Style = format::getLLVMStyle();
  9210. Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
  9211. auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
  9212. EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
  9213. << llvm::toString(FormattedReplaces.takeError()) << "\n";
  9214. auto Result = applyAllReplacements(Code, *FormattedReplaces);
  9215. EXPECT_TRUE(static_cast<bool>(Result));
  9216. EXPECT_EQ(Expected, *Result);
  9217. }
  9218. TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
  9219. std::string Code = "#include \"a.h\"\n"
  9220. "#include \"c.h\"\n"
  9221. "\n"
  9222. "int main() {\n"
  9223. " return 0;\n"
  9224. "}";
  9225. std::string Expected = "#include \"a.h\"\n"
  9226. "#include \"b.h\"\n"
  9227. "#include \"c.h\"\n"
  9228. "\n"
  9229. "int main() {\n"
  9230. " return 0;\n"
  9231. "}";
  9232. FileID ID = Context.createInMemoryFile("fix.cpp", Code);
  9233. tooling::Replacements Replaces = toReplacements(
  9234. {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
  9235. "#include \"b.h\"\n")});
  9236. format::FormatStyle Style = format::getLLVMStyle();
  9237. Style.SortIncludes = true;
  9238. auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
  9239. EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
  9240. << llvm::toString(FormattedReplaces.takeError()) << "\n";
  9241. auto Result = applyAllReplacements(Code, *FormattedReplaces);
  9242. EXPECT_TRUE(static_cast<bool>(Result));
  9243. EXPECT_EQ(Expected, *Result);
  9244. }
  9245. } // end namespace
  9246. } // end namespace format
  9247. } // end namespace clang