FormatTest.cpp 406 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  18. class FormatTest : public ::testing::Test {
  19. protected:
  20. enum IncompleteCheck {
  21. IC_ExpectComplete,
  22. IC_ExpectIncomplete,
  23. IC_DoNotCheck
  24. };
  25. std::string format(llvm::StringRef Code,
  26. const FormatStyle &Style = getLLVMStyle(),
  27. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  28. DEBUG(llvm::errs() << "---\n");
  29. DEBUG(llvm::errs() << Code << "\n\n");
  30. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  31. bool IncompleteFormat = false;
  32. tooling::Replacements Replaces =
  33. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  34. if (CheckIncomplete != IC_DoNotCheck) {
  35. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  36. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  37. }
  38. ReplacementCount = Replaces.size();
  39. std::string Result = applyAllReplacements(Code, Replaces);
  40. EXPECT_NE("", Result);
  41. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  42. return Result;
  43. }
  44. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  45. FormatStyle Style = getLLVMStyle();
  46. Style.ColumnLimit = ColumnLimit;
  47. return Style;
  48. }
  49. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getGoogleStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. void verifyFormat(llvm::StringRef Code,
  55. const FormatStyle &Style = getLLVMStyle()) {
  56. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  57. }
  58. void verifyIncompleteFormat(llvm::StringRef Code,
  59. const FormatStyle &Style = getLLVMStyle()) {
  60. EXPECT_EQ(Code.str(),
  61. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  62. }
  63. void verifyGoogleFormat(llvm::StringRef Code) {
  64. verifyFormat(Code, getGoogleStyle());
  65. }
  66. void verifyIndependentOfContext(llvm::StringRef text) {
  67. verifyFormat(text);
  68. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  69. }
  70. /// \brief Verify that clang-format does not crash on the given input.
  71. void verifyNoCrash(llvm::StringRef Code,
  72. const FormatStyle &Style = getLLVMStyle()) {
  73. format(Code, Style, IC_DoNotCheck);
  74. }
  75. int ReplacementCount;
  76. };
  77. TEST_F(FormatTest, MessUp) {
  78. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  79. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  80. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  81. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  82. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // Basic function tests.
  86. //===----------------------------------------------------------------------===//
  87. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  88. EXPECT_EQ(";", format(";"));
  89. }
  90. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  91. EXPECT_EQ("int i;", format(" int i;"));
  92. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  93. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  94. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  95. }
  96. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  97. EXPECT_EQ("int i;", format("int\ni;"));
  98. }
  99. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  100. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedCall) {
  103. verifyFormat("Method(f1, f2(f3));");
  104. verifyFormat("Method(f1(f2, f3()));");
  105. verifyFormat("Method(f1(f2, (f3())));");
  106. }
  107. TEST_F(FormatTest, NestedNameSpecifiers) {
  108. verifyFormat("vector<::Type> v;");
  109. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  110. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  111. verifyFormat("bool a = 2 < ::SomeFunction();");
  112. }
  113. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  114. EXPECT_EQ("if (a) {\n"
  115. " f();\n"
  116. "}",
  117. format("if(a){f();}"));
  118. EXPECT_EQ(4, ReplacementCount);
  119. EXPECT_EQ("if (a) {\n"
  120. " f();\n"
  121. "}",
  122. format("if (a) {\n"
  123. " f();\n"
  124. "}"));
  125. EXPECT_EQ(0, ReplacementCount);
  126. EXPECT_EQ("/*\r\n"
  127. "\r\n"
  128. "*/\r\n",
  129. format("/*\r\n"
  130. "\r\n"
  131. "*/\r\n"));
  132. EXPECT_EQ(0, ReplacementCount);
  133. }
  134. TEST_F(FormatTest, RemovesEmptyLines) {
  135. EXPECT_EQ("class C {\n"
  136. " int i;\n"
  137. "};",
  138. format("class C {\n"
  139. " int i;\n"
  140. "\n"
  141. "};"));
  142. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  143. EXPECT_EQ("namespace N {\n"
  144. "\n"
  145. "int i;\n"
  146. "}",
  147. format("namespace N {\n"
  148. "\n"
  149. "int i;\n"
  150. "}",
  151. getGoogleStyle()));
  152. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  153. "\n"
  154. "int i;\n"
  155. "}",
  156. format("extern /**/ \"C\" /**/ {\n"
  157. "\n"
  158. "int i;\n"
  159. "}",
  160. getGoogleStyle()));
  161. // ...but do keep inlining and removing empty lines for non-block extern "C"
  162. // functions.
  163. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  164. EXPECT_EQ("extern \"C\" int f() {\n"
  165. " int i = 42;\n"
  166. " return i;\n"
  167. "}",
  168. format("extern \"C\" int f() {\n"
  169. "\n"
  170. " int i = 42;\n"
  171. " return i;\n"
  172. "}",
  173. getGoogleStyle()));
  174. // Remove empty lines at the beginning and end of blocks.
  175. EXPECT_EQ("void f() {\n"
  176. "\n"
  177. " if (a) {\n"
  178. "\n"
  179. " f();\n"
  180. " }\n"
  181. "}",
  182. format("void f() {\n"
  183. "\n"
  184. " if (a) {\n"
  185. "\n"
  186. " f();\n"
  187. "\n"
  188. " }\n"
  189. "\n"
  190. "}",
  191. getLLVMStyle()));
  192. EXPECT_EQ("void f() {\n"
  193. " if (a) {\n"
  194. " f();\n"
  195. " }\n"
  196. "}",
  197. format("void f() {\n"
  198. "\n"
  199. " if (a) {\n"
  200. "\n"
  201. " f();\n"
  202. "\n"
  203. " }\n"
  204. "\n"
  205. "}",
  206. getGoogleStyle()));
  207. // Don't remove empty lines in more complex control statements.
  208. EXPECT_EQ("void f() {\n"
  209. " if (a) {\n"
  210. " f();\n"
  211. "\n"
  212. " } else if (b) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. "\n"
  220. " } else if (b) {\n"
  221. " f();\n"
  222. "\n"
  223. " }\n"
  224. "\n"
  225. "}"));
  226. // FIXME: This is slightly inconsistent.
  227. EXPECT_EQ("namespace {\n"
  228. "int i;\n"
  229. "}",
  230. format("namespace {\n"
  231. "int i;\n"
  232. "\n"
  233. "}"));
  234. EXPECT_EQ("namespace {\n"
  235. "int i;\n"
  236. "\n"
  237. "} // namespace",
  238. format("namespace {\n"
  239. "int i;\n"
  240. "\n"
  241. "} // namespace"));
  242. }
  243. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  244. verifyFormat("x = (a) and (b);");
  245. verifyFormat("x = (a) or (b);");
  246. verifyFormat("x = (a) bitand (b);");
  247. verifyFormat("x = (a) bitor (b);");
  248. verifyFormat("x = (a) not_eq (b);");
  249. verifyFormat("x = (a) and_eq (b);");
  250. verifyFormat("x = (a) or_eq (b);");
  251. verifyFormat("x = (a) xor (b);");
  252. }
  253. //===----------------------------------------------------------------------===//
  254. // Tests for control statements.
  255. //===----------------------------------------------------------------------===//
  256. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  257. verifyFormat("if (true)\n f();\ng();");
  258. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  259. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  260. FormatStyle AllowsMergedIf = getLLVMStyle();
  261. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  262. verifyFormat("if (a)\n"
  263. " // comment\n"
  264. " f();",
  265. AllowsMergedIf);
  266. verifyFormat("if (a)\n"
  267. " ;",
  268. AllowsMergedIf);
  269. verifyFormat("if (a)\n"
  270. " if (b) return;",
  271. AllowsMergedIf);
  272. verifyFormat("if (a) // Can't merge this\n"
  273. " f();\n",
  274. AllowsMergedIf);
  275. verifyFormat("if (a) /* still don't merge */\n"
  276. " f();",
  277. AllowsMergedIf);
  278. verifyFormat("if (a) { // Never merge this\n"
  279. " f();\n"
  280. "}",
  281. AllowsMergedIf);
  282. verifyFormat("if (a) {/* Never merge this */\n"
  283. " f();\n"
  284. "}",
  285. AllowsMergedIf);
  286. AllowsMergedIf.ColumnLimit = 14;
  287. verifyFormat("if (a) return;", AllowsMergedIf);
  288. verifyFormat("if (aaaaaaaaa)\n"
  289. " return;",
  290. AllowsMergedIf);
  291. AllowsMergedIf.ColumnLimit = 13;
  292. verifyFormat("if (a)\n return;", AllowsMergedIf);
  293. }
  294. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  295. FormatStyle AllowsMergedLoops = getLLVMStyle();
  296. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  297. verifyFormat("while (true) continue;", AllowsMergedLoops);
  298. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  299. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  300. verifyFormat("while (true)\n"
  301. " ;",
  302. AllowsMergedLoops);
  303. verifyFormat("for (;;)\n"
  304. " ;",
  305. AllowsMergedLoops);
  306. verifyFormat("for (;;)\n"
  307. " for (;;) continue;",
  308. AllowsMergedLoops);
  309. verifyFormat("for (;;) // Can't merge this\n"
  310. " continue;",
  311. AllowsMergedLoops);
  312. verifyFormat("for (;;) /* still don't merge */\n"
  313. " continue;",
  314. AllowsMergedLoops);
  315. }
  316. TEST_F(FormatTest, FormatShortBracedStatements) {
  317. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  318. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  319. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  320. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  321. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  322. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  323. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  324. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  325. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  326. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  327. verifyFormat("if (true) { //\n"
  328. " f();\n"
  329. "}",
  330. AllowSimpleBracedStatements);
  331. verifyFormat("if (true) {\n"
  332. " f();\n"
  333. " f();\n"
  334. "}",
  335. AllowSimpleBracedStatements);
  336. verifyFormat("if (true) {\n"
  337. " f();\n"
  338. "} else {\n"
  339. " f();\n"
  340. "}",
  341. AllowSimpleBracedStatements);
  342. verifyFormat("template <int> struct A2 {\n"
  343. " struct B {};\n"
  344. "};",
  345. AllowSimpleBracedStatements);
  346. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  347. verifyFormat("if (true) {\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  358. verifyFormat("while (true) {\n"
  359. " f();\n"
  360. "}",
  361. AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. }
  367. TEST_F(FormatTest, ParseIfElse) {
  368. verifyFormat("if (true)\n"
  369. " if (true)\n"
  370. " if (true)\n"
  371. " f();\n"
  372. " else\n"
  373. " g();\n"
  374. " else\n"
  375. " h();\n"
  376. "else\n"
  377. " i();");
  378. verifyFormat("if (true)\n"
  379. " if (true)\n"
  380. " if (true) {\n"
  381. " if (true)\n"
  382. " f();\n"
  383. " } else {\n"
  384. " g();\n"
  385. " }\n"
  386. " else\n"
  387. " h();\n"
  388. "else {\n"
  389. " i();\n"
  390. "}");
  391. verifyFormat("void f() {\n"
  392. " if (a) {\n"
  393. " } else {\n"
  394. " }\n"
  395. "}");
  396. }
  397. TEST_F(FormatTest, ElseIf) {
  398. verifyFormat("if (a) {\n} else if (b) {\n}");
  399. verifyFormat("if (a)\n"
  400. " f();\n"
  401. "else if (b)\n"
  402. " g();\n"
  403. "else\n"
  404. " h();");
  405. verifyFormat("if (a) {\n"
  406. " f();\n"
  407. "}\n"
  408. "// or else ..\n"
  409. "else {\n"
  410. " g()\n"
  411. "}");
  412. verifyFormat("if (a) {\n"
  413. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  415. "}");
  416. verifyFormat("if (a) {\n"
  417. "} else if (\n"
  418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  419. "}",
  420. getLLVMStyleWithColumns(62));
  421. }
  422. TEST_F(FormatTest, FormatsForLoop) {
  423. verifyFormat(
  424. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  425. " ++VeryVeryLongLoopVariable)\n"
  426. " ;");
  427. verifyFormat("for (;;)\n"
  428. " f();");
  429. verifyFormat("for (;;) {\n}");
  430. verifyFormat("for (;;) {\n"
  431. " f();\n"
  432. "}");
  433. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  434. verifyFormat(
  435. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  436. " E = UnwrappedLines.end();\n"
  437. " I != E; ++I) {\n}");
  438. verifyFormat(
  439. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  440. " ++IIIII) {\n}");
  441. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  442. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  443. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  444. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  445. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  446. " E = FD->getDeclsInPrototypeScope().end();\n"
  447. " I != E; ++I) {\n}");
  448. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  449. " I = Container.begin(),\n"
  450. " E = Container.end();\n"
  451. " I != E; ++I) {\n}",
  452. getLLVMStyleWithColumns(76));
  453. verifyFormat(
  454. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  456. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  458. " ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  460. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  461. " ++i) {\n}");
  462. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  463. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  464. "}");
  465. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  466. " aaaaaaaaaa);\n"
  467. " iter; ++iter) {\n"
  468. "}");
  469. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  472. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  473. FormatStyle NoBinPacking = getLLVMStyle();
  474. NoBinPacking.BinPackParameters = false;
  475. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  476. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  477. " aaaaaaaaaaaaaaaa,\n"
  478. " aaaaaaaaaaaaaaaa,\n"
  479. " aaaaaaaaaaaaaaaa);\n"
  480. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  481. "}",
  482. NoBinPacking);
  483. verifyFormat(
  484. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  485. " E = UnwrappedLines.end();\n"
  486. " I != E;\n"
  487. " ++I) {\n}",
  488. NoBinPacking);
  489. }
  490. TEST_F(FormatTest, RangeBasedForLoops) {
  491. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  492. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  493. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  494. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  495. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  496. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  497. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  499. }
  500. TEST_F(FormatTest, ForEachLoops) {
  501. verifyFormat("void f() {\n"
  502. " foreach (Item *item, itemlist) {}\n"
  503. " Q_FOREACH (Item *item, itemlist) {}\n"
  504. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  505. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  506. "}");
  507. // As function-like macros.
  508. verifyFormat("#define foreach(x, y)\n"
  509. "#define Q_FOREACH(x, y)\n"
  510. "#define BOOST_FOREACH(x, y)\n"
  511. "#define UNKNOWN_FOREACH(x, y)\n");
  512. // Not as function-like macros.
  513. verifyFormat("#define foreach (x, y)\n"
  514. "#define Q_FOREACH (x, y)\n"
  515. "#define BOOST_FOREACH (x, y)\n"
  516. "#define UNKNOWN_FOREACH (x, y)\n");
  517. }
  518. TEST_F(FormatTest, FormatsWhileLoop) {
  519. verifyFormat("while (true) {\n}");
  520. verifyFormat("while (true)\n"
  521. " f();");
  522. verifyFormat("while () {\n}");
  523. verifyFormat("while () {\n"
  524. " f();\n"
  525. "}");
  526. }
  527. TEST_F(FormatTest, FormatsDoWhile) {
  528. verifyFormat("do {\n"
  529. " do_something();\n"
  530. "} while (something());");
  531. verifyFormat("do\n"
  532. " do_something();\n"
  533. "while (something());");
  534. }
  535. TEST_F(FormatTest, FormatsSwitchStatement) {
  536. verifyFormat("switch (x) {\n"
  537. "case 1:\n"
  538. " f();\n"
  539. " break;\n"
  540. "case kFoo:\n"
  541. "case ns::kBar:\n"
  542. "case kBaz:\n"
  543. " break;\n"
  544. "default:\n"
  545. " g();\n"
  546. " break;\n"
  547. "}");
  548. verifyFormat("switch (x) {\n"
  549. "case 1: {\n"
  550. " f();\n"
  551. " break;\n"
  552. "}\n"
  553. "case 2: {\n"
  554. " break;\n"
  555. "}\n"
  556. "}");
  557. verifyFormat("switch (x) {\n"
  558. "case 1: {\n"
  559. " f();\n"
  560. " {\n"
  561. " g();\n"
  562. " h();\n"
  563. " }\n"
  564. " break;\n"
  565. "}\n"
  566. "}");
  567. verifyFormat("switch (x) {\n"
  568. "case 1: {\n"
  569. " f();\n"
  570. " if (foo) {\n"
  571. " g();\n"
  572. " h();\n"
  573. " }\n"
  574. " break;\n"
  575. "}\n"
  576. "}");
  577. verifyFormat("switch (x) {\n"
  578. "case 1: {\n"
  579. " f();\n"
  580. " g();\n"
  581. "} break;\n"
  582. "}");
  583. verifyFormat("switch (test)\n"
  584. " ;");
  585. verifyFormat("switch (x) {\n"
  586. "default: {\n"
  587. " // Do nothing.\n"
  588. "}\n"
  589. "}");
  590. verifyFormat("switch (x) {\n"
  591. "// comment\n"
  592. "// if 1, do f()\n"
  593. "case 1:\n"
  594. " f();\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "case 1:\n"
  598. " // Do amazing stuff\n"
  599. " {\n"
  600. " f();\n"
  601. " g();\n"
  602. " }\n"
  603. " break;\n"
  604. "}");
  605. verifyFormat("#define A \\\n"
  606. " switch (x) { \\\n"
  607. " case a: \\\n"
  608. " foo = b; \\\n"
  609. " }",
  610. getLLVMStyleWithColumns(20));
  611. verifyFormat("#define OPERATION_CASE(name) \\\n"
  612. " case OP_name: \\\n"
  613. " return operations::Operation##name\n",
  614. getLLVMStyleWithColumns(40));
  615. verifyFormat("switch (x) {\n"
  616. "case 1:;\n"
  617. "default:;\n"
  618. " int i;\n"
  619. "}");
  620. verifyGoogleFormat("switch (x) {\n"
  621. " case 1:\n"
  622. " f();\n"
  623. " break;\n"
  624. " case kFoo:\n"
  625. " case ns::kBar:\n"
  626. " case kBaz:\n"
  627. " break;\n"
  628. " default:\n"
  629. " g();\n"
  630. " break;\n"
  631. "}");
  632. verifyGoogleFormat("switch (x) {\n"
  633. " case 1: {\n"
  634. " f();\n"
  635. " break;\n"
  636. " }\n"
  637. "}");
  638. verifyGoogleFormat("switch (test)\n"
  639. " ;");
  640. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  641. " case OP_name: \\\n"
  642. " return operations::Operation##name\n");
  643. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  644. " // Get the correction operation class.\n"
  645. " switch (OpCode) {\n"
  646. " CASE(Add);\n"
  647. " CASE(Subtract);\n"
  648. " default:\n"
  649. " return operations::Unknown;\n"
  650. " }\n"
  651. "#undef OPERATION_CASE\n"
  652. "}");
  653. verifyFormat("DEBUG({\n"
  654. " switch (x) {\n"
  655. " case A:\n"
  656. " f();\n"
  657. " break;\n"
  658. " // On B:\n"
  659. " case B:\n"
  660. " g();\n"
  661. " break;\n"
  662. " }\n"
  663. "});");
  664. verifyFormat("switch (a) {\n"
  665. "case (b):\n"
  666. " return;\n"
  667. "}");
  668. verifyFormat("switch (a) {\n"
  669. "case some_namespace::\n"
  670. " some_constant:\n"
  671. " return;\n"
  672. "}",
  673. getLLVMStyleWithColumns(34));
  674. }
  675. TEST_F(FormatTest, CaseRanges) {
  676. verifyFormat("switch (x) {\n"
  677. "case 'A' ... 'Z':\n"
  678. "case 1 ... 5:\n"
  679. " break;\n"
  680. "}");
  681. }
  682. TEST_F(FormatTest, ShortCaseLabels) {
  683. FormatStyle Style = getLLVMStyle();
  684. Style.AllowShortCaseLabelsOnASingleLine = true;
  685. verifyFormat("switch (a) {\n"
  686. "case 1: x = 1; break;\n"
  687. "case 2: return;\n"
  688. "case 3:\n"
  689. "case 4:\n"
  690. "case 5: return;\n"
  691. "case 6: // comment\n"
  692. " return;\n"
  693. "case 7:\n"
  694. " // comment\n"
  695. " return;\n"
  696. "case 8:\n"
  697. " x = 8; // comment\n"
  698. " break;\n"
  699. "default: y = 1; break;\n"
  700. "}",
  701. Style);
  702. verifyFormat("switch (a) {\n"
  703. "#if FOO\n"
  704. "case 0: return 0;\n"
  705. "#endif\n"
  706. "}",
  707. Style);
  708. verifyFormat("switch (a) {\n"
  709. "case 1: {\n"
  710. "}\n"
  711. "case 2: {\n"
  712. " return;\n"
  713. "}\n"
  714. "case 3: {\n"
  715. " x = 1;\n"
  716. " return;\n"
  717. "}\n"
  718. "case 4:\n"
  719. " if (x)\n"
  720. " return;\n"
  721. "}",
  722. Style);
  723. Style.ColumnLimit = 21;
  724. verifyFormat("switch (a) {\n"
  725. "case 1: x = 1; break;\n"
  726. "case 2: return;\n"
  727. "case 3:\n"
  728. "case 4:\n"
  729. "case 5: return;\n"
  730. "default:\n"
  731. " y = 1;\n"
  732. " break;\n"
  733. "}",
  734. Style);
  735. }
  736. TEST_F(FormatTest, FormatsLabels) {
  737. verifyFormat("void f() {\n"
  738. " some_code();\n"
  739. "test_label:\n"
  740. " some_other_code();\n"
  741. " {\n"
  742. " some_more_code();\n"
  743. " another_label:\n"
  744. " some_more_code();\n"
  745. " }\n"
  746. "}");
  747. verifyFormat("{\n"
  748. " some_code();\n"
  749. "test_label:\n"
  750. " some_other_code();\n"
  751. "}");
  752. verifyFormat("{\n"
  753. " some_code();\n"
  754. "test_label:;\n"
  755. " int i = 0;\n"
  756. "}");
  757. }
  758. //===----------------------------------------------------------------------===//
  759. // Tests for comments.
  760. //===----------------------------------------------------------------------===//
  761. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  762. verifyFormat("//* */");
  763. verifyFormat("// line 1\n"
  764. "// line 2\n"
  765. "void f() {}\n");
  766. verifyFormat("void f() {\n"
  767. " // Doesn't do anything\n"
  768. "}");
  769. verifyFormat("SomeObject\n"
  770. " // Calling someFunction on SomeObject\n"
  771. " .someFunction();");
  772. verifyFormat("auto result = SomeObject\n"
  773. " // Calling someFunction on SomeObject\n"
  774. " .someFunction();");
  775. verifyFormat("void f(int i, // some comment (probably for i)\n"
  776. " int j, // some comment (probably for j)\n"
  777. " int k); // some comment (probably for k)");
  778. verifyFormat("void f(int i,\n"
  779. " // some comment (probably for j)\n"
  780. " int j,\n"
  781. " // some comment (probably for k)\n"
  782. " int k);");
  783. verifyFormat("int i // This is a fancy variable\n"
  784. " = 5; // with nicely aligned comment.");
  785. verifyFormat("// Leading comment.\n"
  786. "int a; // Trailing comment.");
  787. verifyFormat("int a; // Trailing comment\n"
  788. " // on 2\n"
  789. " // or 3 lines.\n"
  790. "int b;");
  791. verifyFormat("int a; // Trailing comment\n"
  792. "\n"
  793. "// Leading comment.\n"
  794. "int b;");
  795. verifyFormat("int a; // Comment.\n"
  796. " // More details.\n"
  797. "int bbbb; // Another comment.");
  798. verifyFormat(
  799. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  800. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  801. "int cccccccccccccccccccccccccccccc; // comment\n"
  802. "int ddd; // looooooooooooooooooooooooong comment\n"
  803. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  804. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  805. "int ccccccccccccccccccc; // comment");
  806. verifyFormat("#include \"a\" // comment\n"
  807. "#include \"a/b/c\" // comment");
  808. verifyFormat("#include <a> // comment\n"
  809. "#include <a/b/c> // comment");
  810. EXPECT_EQ("#include \"a\" // comment\n"
  811. "#include \"a/b/c\" // comment",
  812. format("#include \\\n"
  813. " \"a\" // comment\n"
  814. "#include \"a/b/c\" // comment"));
  815. verifyFormat("enum E {\n"
  816. " // comment\n"
  817. " VAL_A, // comment\n"
  818. " VAL_B\n"
  819. "};");
  820. verifyFormat(
  821. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  822. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  823. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  824. " // Comment inside a statement.\n"
  825. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  826. verifyFormat("SomeFunction(a,\n"
  827. " // comment\n"
  828. " b + x);");
  829. verifyFormat("SomeFunction(a, a,\n"
  830. " // comment\n"
  831. " b + x);");
  832. verifyFormat(
  833. "bool aaaaaaaaaaaaa = // comment\n"
  834. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  835. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  836. verifyFormat("int aaaa; // aaaaa\n"
  837. "int aa; // aaaaaaa",
  838. getLLVMStyleWithColumns(20));
  839. EXPECT_EQ("void f() { // This does something ..\n"
  840. "}\n"
  841. "int a; // This is unrelated",
  842. format("void f() { // This does something ..\n"
  843. " }\n"
  844. "int a; // This is unrelated"));
  845. EXPECT_EQ("class C {\n"
  846. " void f() { // This does something ..\n"
  847. " } // awesome..\n"
  848. "\n"
  849. " int a; // This is unrelated\n"
  850. "};",
  851. format("class C{void f() { // This does something ..\n"
  852. " } // awesome..\n"
  853. " \n"
  854. "int a; // This is unrelated\n"
  855. "};"));
  856. EXPECT_EQ("int i; // single line trailing comment",
  857. format("int i;\\\n// single line trailing comment"));
  858. verifyGoogleFormat("int a; // Trailing comment.");
  859. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  860. " parameter));");
  861. verifyGoogleFormat("#endif // HEADER_GUARD");
  862. verifyFormat("const char *test[] = {\n"
  863. " // A\n"
  864. " \"aaaa\",\n"
  865. " // B\n"
  866. " \"aaaaa\"};");
  867. verifyGoogleFormat(
  868. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  869. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  870. EXPECT_EQ("D(a, {\n"
  871. " // test\n"
  872. " int a;\n"
  873. "});",
  874. format("D(a, {\n"
  875. "// test\n"
  876. "int a;\n"
  877. "});"));
  878. EXPECT_EQ("lineWith(); // comment\n"
  879. "// at start\n"
  880. "otherLine();",
  881. format("lineWith(); // comment\n"
  882. "// at start\n"
  883. "otherLine();"));
  884. EXPECT_EQ("lineWith(); // comment\n"
  885. " // at start\n"
  886. "otherLine();",
  887. format("lineWith(); // comment\n"
  888. " // at start\n"
  889. "otherLine();"));
  890. EXPECT_EQ("lineWith(); // comment\n"
  891. "// at start\n"
  892. "otherLine(); // comment",
  893. format("lineWith(); // comment\n"
  894. "// at start\n"
  895. "otherLine(); // comment"));
  896. EXPECT_EQ("lineWith();\n"
  897. "// at start\n"
  898. "otherLine(); // comment",
  899. format("lineWith();\n"
  900. " // at start\n"
  901. "otherLine(); // comment"));
  902. EXPECT_EQ("// first\n"
  903. "// at start\n"
  904. "otherLine(); // comment",
  905. format("// first\n"
  906. " // at start\n"
  907. "otherLine(); // comment"));
  908. EXPECT_EQ("f();\n"
  909. "// first\n"
  910. "// at start\n"
  911. "otherLine(); // comment",
  912. format("f();\n"
  913. "// first\n"
  914. " // at start\n"
  915. "otherLine(); // comment"));
  916. verifyFormat("f(); // comment\n"
  917. "// first\n"
  918. "// at start\n"
  919. "otherLine();");
  920. EXPECT_EQ("f(); // comment\n"
  921. "// first\n"
  922. "// at start\n"
  923. "otherLine();",
  924. format("f(); // comment\n"
  925. "// first\n"
  926. " // at start\n"
  927. "otherLine();"));
  928. EXPECT_EQ("f(); // comment\n"
  929. " // first\n"
  930. "// at start\n"
  931. "otherLine();",
  932. format("f(); // comment\n"
  933. " // first\n"
  934. "// at start\n"
  935. "otherLine();"));
  936. EXPECT_EQ("void f() {\n"
  937. " lineWith(); // comment\n"
  938. " // at start\n"
  939. "}",
  940. format("void f() {\n"
  941. " lineWith(); // comment\n"
  942. " // at start\n"
  943. "}"));
  944. verifyFormat("#define A \\\n"
  945. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  946. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  947. getLLVMStyleWithColumns(60));
  948. verifyFormat(
  949. "#define A \\\n"
  950. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  951. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  952. getLLVMStyleWithColumns(61));
  953. verifyFormat("if ( // This is some comment\n"
  954. " x + 3) {\n"
  955. "}");
  956. EXPECT_EQ("if ( // This is some comment\n"
  957. " // spanning two lines\n"
  958. " x + 3) {\n"
  959. "}",
  960. format("if( // This is some comment\n"
  961. " // spanning two lines\n"
  962. " x + 3) {\n"
  963. "}"));
  964. verifyNoCrash("/\\\n/");
  965. verifyNoCrash("/\\\n* */");
  966. // The 0-character somehow makes the lexer return a proper comment.
  967. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  968. }
  969. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  970. EXPECT_EQ("SomeFunction(a,\n"
  971. " b, // comment\n"
  972. " c);",
  973. format("SomeFunction(a,\n"
  974. " b, // comment\n"
  975. " c);"));
  976. EXPECT_EQ("SomeFunction(a, b,\n"
  977. " // comment\n"
  978. " c);",
  979. format("SomeFunction(a,\n"
  980. " b,\n"
  981. " // comment\n"
  982. " c);"));
  983. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  984. " c);",
  985. format("SomeFunction(a, b, // comment (unclear relation)\n"
  986. " c);"));
  987. EXPECT_EQ("SomeFunction(a, // comment\n"
  988. " b,\n"
  989. " c); // comment",
  990. format("SomeFunction(a, // comment\n"
  991. " b,\n"
  992. " c); // comment"));
  993. }
  994. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  995. EXPECT_EQ("// comment", format("// comment "));
  996. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  997. format("int aaaaaaa, bbbbbbb; // comment ",
  998. getLLVMStyleWithColumns(33)));
  999. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1000. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1001. }
  1002. TEST_F(FormatTest, UnderstandsBlockComments) {
  1003. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1004. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1005. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1006. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1007. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1008. "/* Trailing comment for aa... */\n"
  1009. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1010. EXPECT_EQ(
  1011. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1012. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1013. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1014. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1015. EXPECT_EQ(
  1016. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1017. " aaaaaaaaaaaaaaaaaa,\n"
  1018. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1019. "}",
  1020. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1021. " aaaaaaaaaaaaaaaaaa ,\n"
  1022. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1023. "}"));
  1024. FormatStyle NoBinPacking = getLLVMStyle();
  1025. NoBinPacking.BinPackParameters = false;
  1026. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1027. " /* parameter 2 */ aaaaaa,\n"
  1028. " /* parameter 3 */ aaaaaa,\n"
  1029. " /* parameter 4 */ aaaaaa);",
  1030. NoBinPacking);
  1031. // Aligning block comments in macros.
  1032. verifyGoogleFormat("#define A \\\n"
  1033. " int i; /*a*/ \\\n"
  1034. " int jjj; /*b*/");
  1035. }
  1036. TEST_F(FormatTest, AlignsBlockComments) {
  1037. EXPECT_EQ("/*\n"
  1038. " * Really multi-line\n"
  1039. " * comment.\n"
  1040. " */\n"
  1041. "void f() {}",
  1042. format(" /*\n"
  1043. " * Really multi-line\n"
  1044. " * comment.\n"
  1045. " */\n"
  1046. " void f() {}"));
  1047. EXPECT_EQ("class C {\n"
  1048. " /*\n"
  1049. " * Another multi-line\n"
  1050. " * comment.\n"
  1051. " */\n"
  1052. " void f() {}\n"
  1053. "};",
  1054. format("class C {\n"
  1055. "/*\n"
  1056. " * Another multi-line\n"
  1057. " * comment.\n"
  1058. " */\n"
  1059. "void f() {}\n"
  1060. "};"));
  1061. EXPECT_EQ("/*\n"
  1062. " 1. This is a comment with non-trivial formatting.\n"
  1063. " 1.1. We have to indent/outdent all lines equally\n"
  1064. " 1.1.1. to keep the formatting.\n"
  1065. " */",
  1066. format(" /*\n"
  1067. " 1. This is a comment with non-trivial formatting.\n"
  1068. " 1.1. We have to indent/outdent all lines equally\n"
  1069. " 1.1.1. to keep the formatting.\n"
  1070. " */"));
  1071. EXPECT_EQ("/*\n"
  1072. "Don't try to outdent if there's not enough indentation.\n"
  1073. "*/",
  1074. format(" /*\n"
  1075. " Don't try to outdent if there's not enough indentation.\n"
  1076. " */"));
  1077. EXPECT_EQ("int i; /* Comment with empty...\n"
  1078. " *\n"
  1079. " * line. */",
  1080. format("int i; /* Comment with empty...\n"
  1081. " *\n"
  1082. " * line. */"));
  1083. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1084. "int bar = 0; /* multiline\n"
  1085. " comment 1 */\n"
  1086. "int baz = 0; /* multiline\n"
  1087. " comment 2 */\n"
  1088. "int bzz = 0; /* multiline\n"
  1089. " comment 3 */",
  1090. format("int foobar = 0; /* comment */\n"
  1091. "int bar = 0; /* multiline\n"
  1092. " comment 1 */\n"
  1093. "int baz = 0; /* multiline\n"
  1094. " comment 2 */\n"
  1095. "int bzz = 0; /* multiline\n"
  1096. " comment 3 */"));
  1097. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1098. "int bar = 0; /* multiline\n"
  1099. " comment */\n"
  1100. "int baz = 0; /* multiline\n"
  1101. "comment */",
  1102. format("int foobar = 0; /* comment */\n"
  1103. "int bar = 0; /* multiline\n"
  1104. "comment */\n"
  1105. "int baz = 0; /* multiline\n"
  1106. "comment */"));
  1107. }
  1108. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1109. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1110. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1111. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1112. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1113. EXPECT_EQ(
  1114. "void ffffffffffff(\n"
  1115. " int aaaaaaaa, int bbbbbbbb,\n"
  1116. " int cccccccccccc) { /*\n"
  1117. " aaaaaaaaaa\n"
  1118. " aaaaaaaaaaaaa\n"
  1119. " bbbbbbbbbbbbbb\n"
  1120. " bbbbbbbbbb\n"
  1121. " */\n"
  1122. "}",
  1123. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1124. "{ /*\n"
  1125. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1126. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1127. " */\n"
  1128. "}",
  1129. getLLVMStyleWithColumns(40)));
  1130. }
  1131. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1132. EXPECT_EQ("void ffffffffff(\n"
  1133. " int aaaaa /* test */);",
  1134. format("void ffffffffff(int aaaaa /* test */);",
  1135. getLLVMStyleWithColumns(35)));
  1136. }
  1137. TEST_F(FormatTest, SplitsLongCxxComments) {
  1138. EXPECT_EQ("// A comment that\n"
  1139. "// doesn't fit on\n"
  1140. "// one line",
  1141. format("// A comment that doesn't fit on one line",
  1142. getLLVMStyleWithColumns(20)));
  1143. EXPECT_EQ("/// A comment that\n"
  1144. "/// doesn't fit on\n"
  1145. "/// one line",
  1146. format("/// A comment that doesn't fit on one line",
  1147. getLLVMStyleWithColumns(20)));
  1148. EXPECT_EQ("//! A comment that\n"
  1149. "//! doesn't fit on\n"
  1150. "//! one line",
  1151. format("//! A comment that doesn't fit on one line",
  1152. getLLVMStyleWithColumns(20)));
  1153. EXPECT_EQ("// a b c d\n"
  1154. "// e f g\n"
  1155. "// h i j k",
  1156. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1157. EXPECT_EQ(
  1158. "// a b c d\n"
  1159. "// e f g\n"
  1160. "// h i j k",
  1161. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1162. EXPECT_EQ("if (true) // A comment that\n"
  1163. " // doesn't fit on\n"
  1164. " // one line",
  1165. format("if (true) // A comment that doesn't fit on one line ",
  1166. getLLVMStyleWithColumns(30)));
  1167. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1168. format("// Don't_touch_leading_whitespace",
  1169. getLLVMStyleWithColumns(20)));
  1170. EXPECT_EQ("// Add leading\n"
  1171. "// whitespace",
  1172. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1173. EXPECT_EQ("/// Add leading\n"
  1174. "/// whitespace",
  1175. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1176. EXPECT_EQ("//! Add leading\n"
  1177. "//! whitespace",
  1178. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1179. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1180. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1181. "// limit",
  1182. format("//Even if it makes the line exceed the column limit",
  1183. getLLVMStyleWithColumns(51)));
  1184. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1185. EXPECT_EQ("// aa bb cc dd",
  1186. format("// aa bb cc dd ",
  1187. getLLVMStyleWithColumns(15)));
  1188. EXPECT_EQ("// A comment before\n"
  1189. "// a macro\n"
  1190. "// definition\n"
  1191. "#define a b",
  1192. format("// A comment before a macro definition\n"
  1193. "#define a b",
  1194. getLLVMStyleWithColumns(20)));
  1195. EXPECT_EQ("void ffffff(\n"
  1196. " int aaaaaaaaa, // wwww\n"
  1197. " int bbbbbbbbbb, // xxxxxxx\n"
  1198. " // yyyyyyyyyy\n"
  1199. " int c, int d, int e) {}",
  1200. format("void ffffff(\n"
  1201. " int aaaaaaaaa, // wwww\n"
  1202. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1203. " int c, int d, int e) {}",
  1204. getLLVMStyleWithColumns(40)));
  1205. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1206. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1207. getLLVMStyleWithColumns(20)));
  1208. EXPECT_EQ(
  1209. "#define XXX // a b c d\n"
  1210. " // e f g h",
  1211. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1212. EXPECT_EQ(
  1213. "#define XXX // q w e r\n"
  1214. " // t y u i",
  1215. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1216. }
  1217. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1218. EXPECT_EQ("// A comment\n"
  1219. "// that doesn't\n"
  1220. "// fit on one\n"
  1221. "// line",
  1222. format("// A comment that doesn't fit on one line",
  1223. getLLVMStyleWithColumns(20)));
  1224. EXPECT_EQ("/// A comment\n"
  1225. "/// that doesn't\n"
  1226. "/// fit on one\n"
  1227. "/// line",
  1228. format("/// A comment that doesn't fit on one line",
  1229. getLLVMStyleWithColumns(20)));
  1230. }
  1231. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1232. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1233. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1234. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1235. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1236. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1237. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1238. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1239. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1240. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1241. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1242. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1243. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1244. getLLVMStyleWithColumns(50)));
  1245. // FIXME: One day we might want to implement adjustment of leading whitespace
  1246. // of the consecutive lines in this kind of comment:
  1247. EXPECT_EQ("double\n"
  1248. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1249. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1250. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1251. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1252. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1253. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1254. getLLVMStyleWithColumns(49)));
  1255. }
  1256. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1257. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1258. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1259. EXPECT_EQ(
  1260. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1261. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1262. EXPECT_EQ(
  1263. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1264. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1265. }
  1266. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1267. EXPECT_EQ("if (xxx ==\n"
  1268. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1269. " zzz)\n"
  1270. " q();",
  1271. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1272. " zzz) q();",
  1273. getLLVMStyleWithColumns(40)));
  1274. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1275. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1276. " zzz)\n"
  1277. " q();",
  1278. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1279. " zzz) q();",
  1280. getLLVMStyleWithColumns(40)));
  1281. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1282. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1283. " zzz)\n"
  1284. " q();",
  1285. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1286. " zzz) q();",
  1287. getLLVMStyleWithColumns(40)));
  1288. EXPECT_EQ("fffffffff(\n"
  1289. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1290. " zzz);",
  1291. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1292. " zzz);",
  1293. getLLVMStyleWithColumns(40)));
  1294. }
  1295. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1296. EXPECT_EQ("#define A(x) /* \\\n"
  1297. " a comment \\\n"
  1298. " inside */ \\\n"
  1299. " f();",
  1300. format("#define A(x) /* \\\n"
  1301. " a comment \\\n"
  1302. " inside */ \\\n"
  1303. " f();",
  1304. getLLVMStyleWithColumns(17)));
  1305. EXPECT_EQ("#define A( \\\n"
  1306. " x) /* \\\n"
  1307. " a comment \\\n"
  1308. " inside */ \\\n"
  1309. " f();",
  1310. format("#define A( \\\n"
  1311. " x) /* \\\n"
  1312. " a comment \\\n"
  1313. " inside */ \\\n"
  1314. " f();",
  1315. getLLVMStyleWithColumns(17)));
  1316. }
  1317. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1318. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1319. format("namespace {}\n // Test\n#define A"));
  1320. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1321. format("namespace {}\n /* Test */\n#define A"));
  1322. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1323. format("namespace {}\n /* Test */ #define A"));
  1324. }
  1325. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1326. EXPECT_EQ("/* This is a long\n"
  1327. " * comment that\n"
  1328. " * doesn't\n"
  1329. " * fit on one line.\n"
  1330. " */",
  1331. format("/* "
  1332. "This is a long "
  1333. "comment that "
  1334. "doesn't "
  1335. "fit on one line. */",
  1336. getLLVMStyleWithColumns(20)));
  1337. EXPECT_EQ(
  1338. "/* a b c d\n"
  1339. " * e f g\n"
  1340. " * h i j k\n"
  1341. " */",
  1342. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1343. EXPECT_EQ(
  1344. "/* a b c d\n"
  1345. " * e f g\n"
  1346. " * h i j k\n"
  1347. " */",
  1348. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1349. EXPECT_EQ("/*\n"
  1350. "This is a long\n"
  1351. "comment that doesn't\n"
  1352. "fit on one line.\n"
  1353. "*/",
  1354. format("/*\n"
  1355. "This is a long "
  1356. "comment that doesn't "
  1357. "fit on one line. \n"
  1358. "*/",
  1359. getLLVMStyleWithColumns(20)));
  1360. EXPECT_EQ("/*\n"
  1361. " * This is a long\n"
  1362. " * comment that\n"
  1363. " * doesn't fit on\n"
  1364. " * one line.\n"
  1365. " */",
  1366. format("/* \n"
  1367. " * This is a long "
  1368. " comment that "
  1369. " doesn't fit on "
  1370. " one line. \n"
  1371. " */",
  1372. getLLVMStyleWithColumns(20)));
  1373. EXPECT_EQ("/*\n"
  1374. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1375. " * so_it_should_be_broken\n"
  1376. " * wherever_a_space_occurs\n"
  1377. " */",
  1378. format("/*\n"
  1379. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1380. " so_it_should_be_broken "
  1381. " wherever_a_space_occurs \n"
  1382. " */",
  1383. getLLVMStyleWithColumns(20)));
  1384. EXPECT_EQ("/*\n"
  1385. " * This_comment_can_not_be_broken_into_lines\n"
  1386. " */",
  1387. format("/*\n"
  1388. " * This_comment_can_not_be_broken_into_lines\n"
  1389. " */",
  1390. getLLVMStyleWithColumns(20)));
  1391. EXPECT_EQ("{\n"
  1392. " /*\n"
  1393. " This is another\n"
  1394. " long comment that\n"
  1395. " doesn't fit on one\n"
  1396. " line 1234567890\n"
  1397. " */\n"
  1398. "}",
  1399. format("{\n"
  1400. "/*\n"
  1401. "This is another "
  1402. " long comment that "
  1403. " doesn't fit on one"
  1404. " line 1234567890\n"
  1405. "*/\n"
  1406. "}",
  1407. getLLVMStyleWithColumns(20)));
  1408. EXPECT_EQ("{\n"
  1409. " /*\n"
  1410. " * This i s\n"
  1411. " * another comment\n"
  1412. " * t hat doesn' t\n"
  1413. " * fit on one l i\n"
  1414. " * n e\n"
  1415. " */\n"
  1416. "}",
  1417. format("{\n"
  1418. "/*\n"
  1419. " * This i s"
  1420. " another comment"
  1421. " t hat doesn' t"
  1422. " fit on one l i"
  1423. " n e\n"
  1424. " */\n"
  1425. "}",
  1426. getLLVMStyleWithColumns(20)));
  1427. EXPECT_EQ("/*\n"
  1428. " * This is a long\n"
  1429. " * comment that\n"
  1430. " * doesn't fit on\n"
  1431. " * one line\n"
  1432. " */",
  1433. format(" /*\n"
  1434. " * This is a long comment that doesn't fit on one line\n"
  1435. " */",
  1436. getLLVMStyleWithColumns(20)));
  1437. EXPECT_EQ("{\n"
  1438. " if (something) /* This is a\n"
  1439. " long\n"
  1440. " comment */\n"
  1441. " ;\n"
  1442. "}",
  1443. format("{\n"
  1444. " if (something) /* This is a long comment */\n"
  1445. " ;\n"
  1446. "}",
  1447. getLLVMStyleWithColumns(30)));
  1448. EXPECT_EQ("/* A comment before\n"
  1449. " * a macro\n"
  1450. " * definition */\n"
  1451. "#define a b",
  1452. format("/* A comment before a macro definition */\n"
  1453. "#define a b",
  1454. getLLVMStyleWithColumns(20)));
  1455. EXPECT_EQ("/* some comment\n"
  1456. " * a comment\n"
  1457. "* that we break\n"
  1458. " * another comment\n"
  1459. "* we have to break\n"
  1460. "* a left comment\n"
  1461. " */",
  1462. format(" /* some comment\n"
  1463. " * a comment that we break\n"
  1464. " * another comment we have to break\n"
  1465. "* a left comment\n"
  1466. " */",
  1467. getLLVMStyleWithColumns(20)));
  1468. EXPECT_EQ("/**\n"
  1469. " * multiline block\n"
  1470. " * comment\n"
  1471. " *\n"
  1472. " */",
  1473. format("/**\n"
  1474. " * multiline block comment\n"
  1475. " *\n"
  1476. " */",
  1477. getLLVMStyleWithColumns(20)));
  1478. EXPECT_EQ("/*\n"
  1479. "\n"
  1480. "\n"
  1481. " */\n",
  1482. format(" /* \n"
  1483. " \n"
  1484. " \n"
  1485. " */\n"));
  1486. EXPECT_EQ("/* a a */",
  1487. format("/* a a */", getLLVMStyleWithColumns(15)));
  1488. EXPECT_EQ("/* a a bc */",
  1489. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1490. EXPECT_EQ("/* aaa aaa\n"
  1491. " * aaaaa */",
  1492. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1493. EXPECT_EQ("/* aaa aaa\n"
  1494. " * aaaaa */",
  1495. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1496. }
  1497. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1498. EXPECT_EQ("#define X \\\n"
  1499. " /* \\\n"
  1500. " Test \\\n"
  1501. " Macro comment \\\n"
  1502. " with a long \\\n"
  1503. " line \\\n"
  1504. " */ \\\n"
  1505. " A + B",
  1506. format("#define X \\\n"
  1507. " /*\n"
  1508. " Test\n"
  1509. " Macro comment with a long line\n"
  1510. " */ \\\n"
  1511. " A + B",
  1512. getLLVMStyleWithColumns(20)));
  1513. EXPECT_EQ("#define X \\\n"
  1514. " /* Macro comment \\\n"
  1515. " with a long \\\n"
  1516. " line */ \\\n"
  1517. " A + B",
  1518. format("#define X \\\n"
  1519. " /* Macro comment with a long\n"
  1520. " line */ \\\n"
  1521. " A + B",
  1522. getLLVMStyleWithColumns(20)));
  1523. EXPECT_EQ("#define X \\\n"
  1524. " /* Macro comment \\\n"
  1525. " * with a long \\\n"
  1526. " * line */ \\\n"
  1527. " A + B",
  1528. format("#define X \\\n"
  1529. " /* Macro comment with a long line */ \\\n"
  1530. " A + B",
  1531. getLLVMStyleWithColumns(20)));
  1532. }
  1533. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1534. EXPECT_EQ(
  1535. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1536. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1537. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1538. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1539. " aaaaaaaaaaaaaaaaaaaa};",
  1540. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1541. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1542. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1543. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1544. " aaaaaaaaaaaaaaaaaaaa };"));
  1545. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1546. " bbbbbbbbbbb, ccccccccccc};");
  1547. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1548. " // comment for bb....\n"
  1549. " bbbbbbbbbbb, ccccccccccc};");
  1550. verifyGoogleFormat(
  1551. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1552. " bbbbbbbbbbb, ccccccccccc};");
  1553. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1554. " // comment for bb....\n"
  1555. " bbbbbbbbbbb, ccccccccccc};");
  1556. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1557. " {d, e, f}, // Group #2\n"
  1558. " {g, h, i}}; // Group #3");
  1559. verifyFormat("S s = {{// Group #1\n"
  1560. " a, b, c},\n"
  1561. " {// Group #2\n"
  1562. " d, e, f},\n"
  1563. " {// Group #3\n"
  1564. " g, h, i}};");
  1565. EXPECT_EQ("S s = {\n"
  1566. " // Some comment\n"
  1567. " a,\n"
  1568. "\n"
  1569. " // Comment after empty line\n"
  1570. " b}",
  1571. format("S s = {\n"
  1572. " // Some comment\n"
  1573. " a,\n"
  1574. " \n"
  1575. " // Comment after empty line\n"
  1576. " b\n"
  1577. "}"));
  1578. EXPECT_EQ("S s = {\n"
  1579. " /* Some comment */\n"
  1580. " a,\n"
  1581. "\n"
  1582. " /* Comment after empty line */\n"
  1583. " b}",
  1584. format("S s = {\n"
  1585. " /* Some comment */\n"
  1586. " a,\n"
  1587. " \n"
  1588. " /* Comment after empty line */\n"
  1589. " b\n"
  1590. "}"));
  1591. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1592. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1593. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1594. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1595. }
  1596. TEST_F(FormatTest, IgnoresIf0Contents) {
  1597. EXPECT_EQ("#if 0\n"
  1598. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1599. "#endif\n"
  1600. "void f() {}",
  1601. format("#if 0\n"
  1602. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1603. "#endif\n"
  1604. "void f( ) { }"));
  1605. EXPECT_EQ("#if false\n"
  1606. "void f( ) { }\n"
  1607. "#endif\n"
  1608. "void g() {}\n",
  1609. format("#if false\n"
  1610. "void f( ) { }\n"
  1611. "#endif\n"
  1612. "void g( ) { }\n"));
  1613. EXPECT_EQ("enum E {\n"
  1614. " One,\n"
  1615. " Two,\n"
  1616. "#if 0\n"
  1617. "Three,\n"
  1618. " Four,\n"
  1619. "#endif\n"
  1620. " Five\n"
  1621. "};",
  1622. format("enum E {\n"
  1623. " One,Two,\n"
  1624. "#if 0\n"
  1625. "Three,\n"
  1626. " Four,\n"
  1627. "#endif\n"
  1628. " Five};"));
  1629. EXPECT_EQ("enum F {\n"
  1630. " One,\n"
  1631. "#if 1\n"
  1632. " Two,\n"
  1633. "#if 0\n"
  1634. "Three,\n"
  1635. " Four,\n"
  1636. "#endif\n"
  1637. " Five\n"
  1638. "#endif\n"
  1639. "};",
  1640. format("enum F {\n"
  1641. "One,\n"
  1642. "#if 1\n"
  1643. "Two,\n"
  1644. "#if 0\n"
  1645. "Three,\n"
  1646. " Four,\n"
  1647. "#endif\n"
  1648. "Five\n"
  1649. "#endif\n"
  1650. "};"));
  1651. EXPECT_EQ("enum G {\n"
  1652. " One,\n"
  1653. "#if 0\n"
  1654. "Two,\n"
  1655. "#else\n"
  1656. " Three,\n"
  1657. "#endif\n"
  1658. " Four\n"
  1659. "};",
  1660. format("enum G {\n"
  1661. "One,\n"
  1662. "#if 0\n"
  1663. "Two,\n"
  1664. "#else\n"
  1665. "Three,\n"
  1666. "#endif\n"
  1667. "Four\n"
  1668. "};"));
  1669. EXPECT_EQ("enum H {\n"
  1670. " One,\n"
  1671. "#if 0\n"
  1672. "#ifdef Q\n"
  1673. "Two,\n"
  1674. "#else\n"
  1675. "Three,\n"
  1676. "#endif\n"
  1677. "#endif\n"
  1678. " Four\n"
  1679. "};",
  1680. format("enum H {\n"
  1681. "One,\n"
  1682. "#if 0\n"
  1683. "#ifdef Q\n"
  1684. "Two,\n"
  1685. "#else\n"
  1686. "Three,\n"
  1687. "#endif\n"
  1688. "#endif\n"
  1689. "Four\n"
  1690. "};"));
  1691. EXPECT_EQ("enum I {\n"
  1692. " One,\n"
  1693. "#if /* test */ 0 || 1\n"
  1694. "Two,\n"
  1695. "Three,\n"
  1696. "#endif\n"
  1697. " Four\n"
  1698. "};",
  1699. format("enum I {\n"
  1700. "One,\n"
  1701. "#if /* test */ 0 || 1\n"
  1702. "Two,\n"
  1703. "Three,\n"
  1704. "#endif\n"
  1705. "Four\n"
  1706. "};"));
  1707. EXPECT_EQ("enum J {\n"
  1708. " One,\n"
  1709. "#if 0\n"
  1710. "#if 0\n"
  1711. "Two,\n"
  1712. "#else\n"
  1713. "Three,\n"
  1714. "#endif\n"
  1715. "Four,\n"
  1716. "#endif\n"
  1717. " Five\n"
  1718. "};",
  1719. format("enum J {\n"
  1720. "One,\n"
  1721. "#if 0\n"
  1722. "#if 0\n"
  1723. "Two,\n"
  1724. "#else\n"
  1725. "Three,\n"
  1726. "#endif\n"
  1727. "Four,\n"
  1728. "#endif\n"
  1729. "Five\n"
  1730. "};"));
  1731. }
  1732. //===----------------------------------------------------------------------===//
  1733. // Tests for classes, namespaces, etc.
  1734. //===----------------------------------------------------------------------===//
  1735. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1736. verifyFormat("class A {};");
  1737. }
  1738. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1739. verifyFormat("class A {\n"
  1740. "public:\n"
  1741. "public: // comment\n"
  1742. "protected:\n"
  1743. "private:\n"
  1744. " void f() {}\n"
  1745. "};");
  1746. verifyGoogleFormat("class A {\n"
  1747. " public:\n"
  1748. " protected:\n"
  1749. " private:\n"
  1750. " void f() {}\n"
  1751. "};");
  1752. verifyFormat("class A {\n"
  1753. "public slots:\n"
  1754. " void f() {}\n"
  1755. "public Q_SLOTS:\n"
  1756. " void f() {}\n"
  1757. "signals:\n"
  1758. " void g();\n"
  1759. "};");
  1760. // Don't interpret 'signals' the wrong way.
  1761. verifyFormat("signals.set();");
  1762. verifyFormat("for (Signals signals : f()) {\n}");
  1763. verifyFormat("{\n"
  1764. " signals.set(); // This needs indentation.\n"
  1765. "}");
  1766. }
  1767. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1768. EXPECT_EQ("class A {\n"
  1769. "public:\n"
  1770. " void f();\n"
  1771. "\n"
  1772. "private:\n"
  1773. " void g() {}\n"
  1774. " // test\n"
  1775. "protected:\n"
  1776. " int h;\n"
  1777. "};",
  1778. format("class A {\n"
  1779. "public:\n"
  1780. "void f();\n"
  1781. "private:\n"
  1782. "void g() {}\n"
  1783. "// test\n"
  1784. "protected:\n"
  1785. "int h;\n"
  1786. "};"));
  1787. EXPECT_EQ("class A {\n"
  1788. "protected:\n"
  1789. "public:\n"
  1790. " void f();\n"
  1791. "};",
  1792. format("class A {\n"
  1793. "protected:\n"
  1794. "\n"
  1795. "public:\n"
  1796. "\n"
  1797. " void f();\n"
  1798. "};"));
  1799. // Even ensure proper spacing inside macros.
  1800. EXPECT_EQ("#define B \\\n"
  1801. " class A { \\\n"
  1802. " protected: \\\n"
  1803. " public: \\\n"
  1804. " void f(); \\\n"
  1805. " };",
  1806. format("#define B \\\n"
  1807. " class A { \\\n"
  1808. " protected: \\\n"
  1809. " \\\n"
  1810. " public: \\\n"
  1811. " \\\n"
  1812. " void f(); \\\n"
  1813. " };",
  1814. getGoogleStyle()));
  1815. // But don't remove empty lines after macros ending in access specifiers.
  1816. EXPECT_EQ("#define A private:\n"
  1817. "\n"
  1818. "int i;",
  1819. format("#define A private:\n"
  1820. "\n"
  1821. "int i;"));
  1822. }
  1823. TEST_F(FormatTest, FormatsClasses) {
  1824. verifyFormat("class A : public B {};");
  1825. verifyFormat("class A : public ::B {};");
  1826. verifyFormat(
  1827. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1828. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1829. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1830. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1831. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1832. verifyFormat(
  1833. "class A : public B, public C, public D, public E, public F {};");
  1834. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1835. " public C,\n"
  1836. " public D,\n"
  1837. " public E,\n"
  1838. " public F,\n"
  1839. " public G {};");
  1840. verifyFormat("class\n"
  1841. " ReallyReallyLongClassName {\n"
  1842. " int i;\n"
  1843. "};",
  1844. getLLVMStyleWithColumns(32));
  1845. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1846. " aaaaaaaaaaaaaaaa> {};");
  1847. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1848. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1849. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1850. verifyFormat("template <class R, class C>\n"
  1851. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1852. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1853. verifyFormat("class ::A::B {};");
  1854. }
  1855. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1856. verifyFormat("class A {\n} a, b;");
  1857. verifyFormat("struct A {\n} a, b;");
  1858. verifyFormat("union A {\n} a;");
  1859. }
  1860. TEST_F(FormatTest, FormatsEnum) {
  1861. verifyFormat("enum {\n"
  1862. " Zero,\n"
  1863. " One = 1,\n"
  1864. " Two = One + 1,\n"
  1865. " Three = (One + Two),\n"
  1866. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1867. " Five = (One, Two, Three, Four, 5)\n"
  1868. "};");
  1869. verifyGoogleFormat("enum {\n"
  1870. " Zero,\n"
  1871. " One = 1,\n"
  1872. " Two = One + 1,\n"
  1873. " Three = (One + Two),\n"
  1874. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1875. " Five = (One, Two, Three, Four, 5)\n"
  1876. "};");
  1877. verifyFormat("enum Enum {};");
  1878. verifyFormat("enum {};");
  1879. verifyFormat("enum X E {} d;");
  1880. verifyFormat("enum __attribute__((...)) E {} d;");
  1881. verifyFormat("enum __declspec__((...)) E {} d;");
  1882. verifyFormat("enum {\n"
  1883. " Bar = Foo<int, int>::value\n"
  1884. "};",
  1885. getLLVMStyleWithColumns(30));
  1886. verifyFormat("enum ShortEnum { A, B, C };");
  1887. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1888. EXPECT_EQ("enum KeepEmptyLines {\n"
  1889. " ONE,\n"
  1890. "\n"
  1891. " TWO,\n"
  1892. "\n"
  1893. " THREE\n"
  1894. "}",
  1895. format("enum KeepEmptyLines {\n"
  1896. " ONE,\n"
  1897. "\n"
  1898. " TWO,\n"
  1899. "\n"
  1900. "\n"
  1901. " THREE\n"
  1902. "}"));
  1903. verifyFormat("enum E { // comment\n"
  1904. " ONE,\n"
  1905. " TWO\n"
  1906. "};\n"
  1907. "int i;");
  1908. // Not enums.
  1909. verifyFormat("enum X f() {\n"
  1910. " a();\n"
  1911. " return 42;\n"
  1912. "}");
  1913. verifyFormat("enum X Type::f() {\n"
  1914. " a();\n"
  1915. " return 42;\n"
  1916. "}");
  1917. verifyFormat("enum ::X f() {\n"
  1918. " a();\n"
  1919. " return 42;\n"
  1920. "}");
  1921. verifyFormat("enum ns::X f() {\n"
  1922. " a();\n"
  1923. " return 42;\n"
  1924. "}");
  1925. }
  1926. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1927. verifyFormat("enum Type {\n"
  1928. " One = 0; // These semicolons should be commas.\n"
  1929. " Two = 1;\n"
  1930. "};");
  1931. verifyFormat("namespace n {\n"
  1932. "enum Type {\n"
  1933. " One,\n"
  1934. " Two, // missing };\n"
  1935. " int i;\n"
  1936. "}\n"
  1937. "void g() {}");
  1938. }
  1939. TEST_F(FormatTest, FormatsEnumStruct) {
  1940. verifyFormat("enum struct {\n"
  1941. " Zero,\n"
  1942. " One = 1,\n"
  1943. " Two = One + 1,\n"
  1944. " Three = (One + Two),\n"
  1945. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1946. " Five = (One, Two, Three, Four, 5)\n"
  1947. "};");
  1948. verifyFormat("enum struct Enum {};");
  1949. verifyFormat("enum struct {};");
  1950. verifyFormat("enum struct X E {} d;");
  1951. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1952. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1953. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1954. }
  1955. TEST_F(FormatTest, FormatsEnumClass) {
  1956. verifyFormat("enum class {\n"
  1957. " Zero,\n"
  1958. " One = 1,\n"
  1959. " Two = One + 1,\n"
  1960. " Three = (One + Two),\n"
  1961. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1962. " Five = (One, Two, Three, Four, 5)\n"
  1963. "};");
  1964. verifyFormat("enum class Enum {};");
  1965. verifyFormat("enum class {};");
  1966. verifyFormat("enum class X E {} d;");
  1967. verifyFormat("enum class __attribute__((...)) E {} d;");
  1968. verifyFormat("enum class __declspec__((...)) E {} d;");
  1969. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1970. }
  1971. TEST_F(FormatTest, FormatsEnumTypes) {
  1972. verifyFormat("enum X : int {\n"
  1973. " A, // Force multiple lines.\n"
  1974. " B\n"
  1975. "};");
  1976. verifyFormat("enum X : int { A, B };");
  1977. verifyFormat("enum X : std::uint32_t { A, B };");
  1978. }
  1979. TEST_F(FormatTest, FormatsNSEnums) {
  1980. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1981. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1982. " // Information about someDecentlyLongValue.\n"
  1983. " someDecentlyLongValue,\n"
  1984. " // Information about anotherDecentlyLongValue.\n"
  1985. " anotherDecentlyLongValue,\n"
  1986. " // Information about aThirdDecentlyLongValue.\n"
  1987. " aThirdDecentlyLongValue\n"
  1988. "};");
  1989. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1990. " a = 1,\n"
  1991. " b = 2,\n"
  1992. " c = 3,\n"
  1993. "};");
  1994. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1995. " a = 1,\n"
  1996. " b = 2,\n"
  1997. " c = 3,\n"
  1998. "};");
  1999. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2000. " a = 1,\n"
  2001. " b = 2,\n"
  2002. " c = 3,\n"
  2003. "};");
  2004. }
  2005. TEST_F(FormatTest, FormatsBitfields) {
  2006. verifyFormat("struct Bitfields {\n"
  2007. " unsigned sClass : 8;\n"
  2008. " unsigned ValueKind : 2;\n"
  2009. "};");
  2010. verifyFormat("struct A {\n"
  2011. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2012. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2013. "};");
  2014. verifyFormat("struct MyStruct {\n"
  2015. " uchar data;\n"
  2016. " uchar : 8;\n"
  2017. " uchar : 8;\n"
  2018. " uchar other;\n"
  2019. "};");
  2020. }
  2021. TEST_F(FormatTest, FormatsNamespaces) {
  2022. verifyFormat("namespace some_namespace {\n"
  2023. "class A {};\n"
  2024. "void f() { f(); }\n"
  2025. "}");
  2026. verifyFormat("namespace {\n"
  2027. "class A {};\n"
  2028. "void f() { f(); }\n"
  2029. "}");
  2030. verifyFormat("inline namespace X {\n"
  2031. "class A {};\n"
  2032. "void f() { f(); }\n"
  2033. "}");
  2034. verifyFormat("using namespace some_namespace;\n"
  2035. "class A {};\n"
  2036. "void f() { f(); }");
  2037. // This code is more common than we thought; if we
  2038. // layout this correctly the semicolon will go into
  2039. // its own line, which is undesirable.
  2040. verifyFormat("namespace {};");
  2041. verifyFormat("namespace {\n"
  2042. "class A {};\n"
  2043. "};");
  2044. verifyFormat("namespace {\n"
  2045. "int SomeVariable = 0; // comment\n"
  2046. "} // namespace");
  2047. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2048. "#define HEADER_GUARD\n"
  2049. "namespace my_namespace {\n"
  2050. "int i;\n"
  2051. "} // my_namespace\n"
  2052. "#endif // HEADER_GUARD",
  2053. format("#ifndef HEADER_GUARD\n"
  2054. " #define HEADER_GUARD\n"
  2055. " namespace my_namespace {\n"
  2056. "int i;\n"
  2057. "} // my_namespace\n"
  2058. "#endif // HEADER_GUARD"));
  2059. EXPECT_EQ("namespace A::B {\n"
  2060. "class C {};\n"
  2061. "}",
  2062. format("namespace A::B {\n"
  2063. "class C {};\n"
  2064. "}"));
  2065. FormatStyle Style = getLLVMStyle();
  2066. Style.NamespaceIndentation = FormatStyle::NI_All;
  2067. EXPECT_EQ("namespace out {\n"
  2068. " int i;\n"
  2069. " namespace in {\n"
  2070. " int i;\n"
  2071. " } // namespace\n"
  2072. "} // namespace",
  2073. format("namespace out {\n"
  2074. "int i;\n"
  2075. "namespace in {\n"
  2076. "int i;\n"
  2077. "} // namespace\n"
  2078. "} // namespace",
  2079. Style));
  2080. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2081. EXPECT_EQ("namespace out {\n"
  2082. "int i;\n"
  2083. "namespace in {\n"
  2084. " int i;\n"
  2085. "} // namespace\n"
  2086. "} // namespace",
  2087. format("namespace out {\n"
  2088. "int i;\n"
  2089. "namespace in {\n"
  2090. "int i;\n"
  2091. "} // namespace\n"
  2092. "} // namespace",
  2093. Style));
  2094. }
  2095. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2096. TEST_F(FormatTest, FormatsInlineASM) {
  2097. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2098. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2099. verifyFormat(
  2100. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2101. " \"cpuid\\n\\t\"\n"
  2102. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2103. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2104. " : \"a\"(value));");
  2105. EXPECT_EQ(
  2106. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2107. " __asm {\n"
  2108. " mov edx,[that] // vtable in edx\n"
  2109. " mov eax,methodIndex\n"
  2110. " call [edx][eax*4] // stdcall\n"
  2111. " }\n"
  2112. "}",
  2113. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2114. " __asm {\n"
  2115. " mov edx,[that] // vtable in edx\n"
  2116. " mov eax,methodIndex\n"
  2117. " call [edx][eax*4] // stdcall\n"
  2118. " }\n"
  2119. "}"));
  2120. EXPECT_EQ("_asm {\n"
  2121. " xor eax, eax;\n"
  2122. " cpuid;\n"
  2123. "}",
  2124. format("_asm {\n"
  2125. " xor eax, eax;\n"
  2126. " cpuid;\n"
  2127. "}"));
  2128. verifyFormat("void function() {\n"
  2129. " // comment\n"
  2130. " asm(\"\");\n"
  2131. "}");
  2132. EXPECT_EQ("__asm {\n"
  2133. "}\n"
  2134. "int i;",
  2135. format("__asm {\n"
  2136. "}\n"
  2137. "int i;"));
  2138. }
  2139. TEST_F(FormatTest, FormatTryCatch) {
  2140. verifyFormat("try {\n"
  2141. " throw a * b;\n"
  2142. "} catch (int a) {\n"
  2143. " // Do nothing.\n"
  2144. "} catch (...) {\n"
  2145. " exit(42);\n"
  2146. "}");
  2147. // Function-level try statements.
  2148. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2149. " return 5;\n"
  2150. "}");
  2151. verifyFormat("class A {\n"
  2152. " int a;\n"
  2153. " A() try : a(0) {\n"
  2154. " } catch (...) {\n"
  2155. " throw;\n"
  2156. " }\n"
  2157. "};\n");
  2158. // Incomplete try-catch blocks.
  2159. verifyIncompleteFormat("try {} catch (");
  2160. }
  2161. TEST_F(FormatTest, FormatSEHTryCatch) {
  2162. verifyFormat("__try {\n"
  2163. " int a = b * c;\n"
  2164. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2165. " // Do nothing.\n"
  2166. "}");
  2167. verifyFormat("__try {\n"
  2168. " int a = b * c;\n"
  2169. "} __finally {\n"
  2170. " // Do nothing.\n"
  2171. "}");
  2172. verifyFormat("DEBUG({\n"
  2173. " __try {\n"
  2174. " } __finally {\n"
  2175. " }\n"
  2176. "});\n");
  2177. }
  2178. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2179. verifyFormat("try {\n"
  2180. " f();\n"
  2181. "} catch {\n"
  2182. " g();\n"
  2183. "}");
  2184. verifyFormat("try {\n"
  2185. " f();\n"
  2186. "} catch (A a) MACRO(x) {\n"
  2187. " g();\n"
  2188. "} catch (B b) MACRO(x) {\n"
  2189. " g();\n"
  2190. "}");
  2191. }
  2192. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2193. FormatStyle Style = getLLVMStyle();
  2194. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2195. FormatStyle::BS_WebKit}) {
  2196. Style.BreakBeforeBraces = BraceStyle;
  2197. verifyFormat("try {\n"
  2198. " // something\n"
  2199. "} catch (...) {\n"
  2200. " // something\n"
  2201. "}",
  2202. Style);
  2203. }
  2204. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2205. verifyFormat("try {\n"
  2206. " // something\n"
  2207. "}\n"
  2208. "catch (...) {\n"
  2209. " // something\n"
  2210. "}",
  2211. Style);
  2212. verifyFormat("__try {\n"
  2213. " // something\n"
  2214. "}\n"
  2215. "__finally {\n"
  2216. " // something\n"
  2217. "}",
  2218. Style);
  2219. verifyFormat("@try {\n"
  2220. " // something\n"
  2221. "}\n"
  2222. "@finally {\n"
  2223. " // something\n"
  2224. "}",
  2225. Style);
  2226. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2227. verifyFormat("try\n"
  2228. "{\n"
  2229. " // something\n"
  2230. "}\n"
  2231. "catch (...)\n"
  2232. "{\n"
  2233. " // something\n"
  2234. "}",
  2235. Style);
  2236. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2237. verifyFormat("try\n"
  2238. " {\n"
  2239. " // something\n"
  2240. " }\n"
  2241. "catch (...)\n"
  2242. " {\n"
  2243. " // something\n"
  2244. " }",
  2245. Style);
  2246. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2247. Style.BraceWrapping.BeforeCatch = true;
  2248. verifyFormat("try {\n"
  2249. " // something\n"
  2250. "}\n"
  2251. "catch (...) {\n"
  2252. " // something\n"
  2253. "}",
  2254. Style);
  2255. }
  2256. TEST_F(FormatTest, FormatObjCTryCatch) {
  2257. verifyFormat("@try {\n"
  2258. " f();\n"
  2259. "} @catch (NSException e) {\n"
  2260. " @throw;\n"
  2261. "} @finally {\n"
  2262. " exit(42);\n"
  2263. "}");
  2264. verifyFormat("DEBUG({\n"
  2265. " @try {\n"
  2266. " } @finally {\n"
  2267. " }\n"
  2268. "});\n");
  2269. }
  2270. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2271. FormatStyle Style = getLLVMStyle();
  2272. verifyFormat("@autoreleasepool {\n"
  2273. " f();\n"
  2274. "}\n"
  2275. "@autoreleasepool {\n"
  2276. " f();\n"
  2277. "}\n",
  2278. Style);
  2279. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2280. verifyFormat("@autoreleasepool\n"
  2281. "{\n"
  2282. " f();\n"
  2283. "}\n"
  2284. "@autoreleasepool\n"
  2285. "{\n"
  2286. " f();\n"
  2287. "}\n",
  2288. Style);
  2289. }
  2290. TEST_F(FormatTest, StaticInitializers) {
  2291. verifyFormat("static SomeClass SC = {1, 'a'};");
  2292. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2293. " 100000000, "
  2294. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2295. // Here, everything other than the "}" would fit on a line.
  2296. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2297. " 10000000000000000000000000};");
  2298. EXPECT_EQ("S s = {a,\n"
  2299. "\n"
  2300. " b};",
  2301. format("S s = {\n"
  2302. " a,\n"
  2303. "\n"
  2304. " b\n"
  2305. "};"));
  2306. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2307. // line. However, the formatting looks a bit off and this probably doesn't
  2308. // happen often in practice.
  2309. verifyFormat("static int Variable[1] = {\n"
  2310. " {1000000000000000000000000000000000000}};",
  2311. getLLVMStyleWithColumns(40));
  2312. }
  2313. TEST_F(FormatTest, DesignatedInitializers) {
  2314. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2315. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2316. " .bbbbbbbbbb = 2,\n"
  2317. " .cccccccccc = 3,\n"
  2318. " .dddddddddd = 4,\n"
  2319. " .eeeeeeeeee = 5};");
  2320. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2321. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2322. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2323. " .ccccccccccccccccccccccccccc = 3,\n"
  2324. " .ddddddddddddddddddddddddddd = 4,\n"
  2325. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2326. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2327. }
  2328. TEST_F(FormatTest, NestedStaticInitializers) {
  2329. verifyFormat("static A x = {{{}}};\n");
  2330. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2331. " {init1, init2, init3, init4}}};",
  2332. getLLVMStyleWithColumns(50));
  2333. verifyFormat("somes Status::global_reps[3] = {\n"
  2334. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2335. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2336. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2337. getLLVMStyleWithColumns(60));
  2338. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2339. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2340. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2341. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2342. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2343. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2344. "rect.fTop}};");
  2345. verifyFormat(
  2346. "SomeArrayOfSomeType a = {\n"
  2347. " {{1, 2, 3},\n"
  2348. " {1, 2, 3},\n"
  2349. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2350. " 333333333333333333333333333333},\n"
  2351. " {1, 2, 3},\n"
  2352. " {1, 2, 3}}};");
  2353. verifyFormat(
  2354. "SomeArrayOfSomeType a = {\n"
  2355. " {{1, 2, 3}},\n"
  2356. " {{1, 2, 3}},\n"
  2357. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2358. " 333333333333333333333333333333}},\n"
  2359. " {{1, 2, 3}},\n"
  2360. " {{1, 2, 3}}};");
  2361. verifyFormat("struct {\n"
  2362. " unsigned bit;\n"
  2363. " const char *const name;\n"
  2364. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2365. " {kOsWin, \"Windows\"},\n"
  2366. " {kOsLinux, \"Linux\"},\n"
  2367. " {kOsCrOS, \"Chrome OS\"}};");
  2368. verifyFormat("struct {\n"
  2369. " unsigned bit;\n"
  2370. " const char *const name;\n"
  2371. "} kBitsToOs[] = {\n"
  2372. " {kOsMac, \"Mac\"},\n"
  2373. " {kOsWin, \"Windows\"},\n"
  2374. " {kOsLinux, \"Linux\"},\n"
  2375. " {kOsCrOS, \"Chrome OS\"},\n"
  2376. "};");
  2377. }
  2378. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2379. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2380. " \\\n"
  2381. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2382. }
  2383. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2384. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2385. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2386. // Do break defaulted and deleted functions.
  2387. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2388. " default;",
  2389. getLLVMStyleWithColumns(40));
  2390. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2391. " delete;",
  2392. getLLVMStyleWithColumns(40));
  2393. }
  2394. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2395. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2396. getLLVMStyleWithColumns(40));
  2397. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2398. getLLVMStyleWithColumns(40));
  2399. EXPECT_EQ("#define Q \\\n"
  2400. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2401. " \"aaaaaaaa.cpp\"",
  2402. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2403. getLLVMStyleWithColumns(40)));
  2404. }
  2405. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2406. EXPECT_EQ("# 123 \"A string literal\"",
  2407. format(" # 123 \"A string literal\""));
  2408. }
  2409. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2410. EXPECT_EQ("#;", format("#;"));
  2411. verifyFormat("#\n;\n;\n;");
  2412. }
  2413. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2414. EXPECT_EQ("#line 42 \"test\"\n",
  2415. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2416. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2417. getLLVMStyleWithColumns(12)));
  2418. }
  2419. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2420. EXPECT_EQ("#line 42 \"test\"",
  2421. format("# \\\n line \\\n 42 \\\n \"test\""));
  2422. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2423. }
  2424. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2425. verifyFormat("#define A \\x20");
  2426. verifyFormat("#define A \\ x20");
  2427. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2428. verifyFormat("#define A ''");
  2429. verifyFormat("#define A ''qqq");
  2430. verifyFormat("#define A `qqq");
  2431. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2432. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2433. "\\na : b);",
  2434. format("const char * c = STRINGIFY(\n"
  2435. "\\na : b);"));
  2436. verifyFormat("a\r\\");
  2437. verifyFormat("a\v\\");
  2438. verifyFormat("a\f\\");
  2439. }
  2440. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2441. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2442. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2443. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2444. // FIXME: We never break before the macro name.
  2445. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2446. verifyFormat("#define A A\n#define A A");
  2447. verifyFormat("#define A(X) A\n#define A A");
  2448. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2449. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2450. }
  2451. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2452. EXPECT_EQ("// somecomment\n"
  2453. "#include \"a.h\"\n"
  2454. "#define A( \\\n"
  2455. " A, B)\n"
  2456. "#include \"b.h\"\n"
  2457. "// somecomment\n",
  2458. format(" // somecomment\n"
  2459. " #include \"a.h\"\n"
  2460. "#define A(A,\\\n"
  2461. " B)\n"
  2462. " #include \"b.h\"\n"
  2463. " // somecomment\n",
  2464. getLLVMStyleWithColumns(13)));
  2465. }
  2466. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2467. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2468. EXPECT_EQ("#define A \\\n"
  2469. " c; \\\n"
  2470. " e;\n"
  2471. "f;",
  2472. format("#define A c; e;\n"
  2473. "f;",
  2474. getLLVMStyleWithColumns(14)));
  2475. }
  2476. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2477. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2478. EXPECT_EQ("int x,\n"
  2479. "#define A\n"
  2480. " y;",
  2481. format("int x,\n#define A\ny;"));
  2482. }
  2483. TEST_F(FormatTest, HashInMacroDefinition) {
  2484. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2485. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2486. verifyFormat("#define A \\\n"
  2487. " { \\\n"
  2488. " f(#c); \\\n"
  2489. " }",
  2490. getLLVMStyleWithColumns(11));
  2491. verifyFormat("#define A(X) \\\n"
  2492. " void function##X()",
  2493. getLLVMStyleWithColumns(22));
  2494. verifyFormat("#define A(a, b, c) \\\n"
  2495. " void a##b##c()",
  2496. getLLVMStyleWithColumns(22));
  2497. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2498. }
  2499. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2500. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2501. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2502. }
  2503. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2504. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2505. " \\\n"
  2506. " b;",
  2507. getLLVMStyleWithColumns(25)));
  2508. EXPECT_EQ("#define A \\\n"
  2509. " \\\n"
  2510. " a; \\\n"
  2511. " b;",
  2512. format("#define A \\\n"
  2513. " \\\n"
  2514. " a; \\\n"
  2515. " b;",
  2516. getLLVMStyleWithColumns(11)));
  2517. EXPECT_EQ("#define A \\\n"
  2518. " a; \\\n"
  2519. " \\\n"
  2520. " b;",
  2521. format("#define A \\\n"
  2522. " a; \\\n"
  2523. " \\\n"
  2524. " b;",
  2525. getLLVMStyleWithColumns(11)));
  2526. }
  2527. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2528. verifyIncompleteFormat("#define A :");
  2529. verifyFormat("#define SOMECASES \\\n"
  2530. " case 1: \\\n"
  2531. " case 2\n",
  2532. getLLVMStyleWithColumns(20));
  2533. verifyFormat("#define A template <typename T>");
  2534. verifyIncompleteFormat("#define STR(x) #x\n"
  2535. "f(STR(this_is_a_string_literal{));");
  2536. verifyFormat("#pragma omp threadprivate( \\\n"
  2537. " y)), // expected-warning",
  2538. getLLVMStyleWithColumns(28));
  2539. verifyFormat("#d, = };");
  2540. verifyFormat("#if \"a");
  2541. verifyIncompleteFormat("({\n"
  2542. "#define b \\\n"
  2543. " } \\\n"
  2544. " a\n"
  2545. "a",
  2546. getLLVMStyleWithColumns(15));
  2547. verifyFormat("#define A \\\n"
  2548. " { \\\n"
  2549. " {\n"
  2550. "#define B \\\n"
  2551. " } \\\n"
  2552. " }",
  2553. getLLVMStyleWithColumns(15));
  2554. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2555. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2556. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2557. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2558. }
  2559. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2560. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2561. EXPECT_EQ("class A : public QObject {\n"
  2562. " Q_OBJECT\n"
  2563. "\n"
  2564. " A() {}\n"
  2565. "};",
  2566. format("class A : public QObject {\n"
  2567. " Q_OBJECT\n"
  2568. "\n"
  2569. " A() {\n}\n"
  2570. "} ;"));
  2571. EXPECT_EQ("MACRO\n"
  2572. "/*static*/ int i;",
  2573. format("MACRO\n"
  2574. " /*static*/ int i;"));
  2575. EXPECT_EQ("SOME_MACRO\n"
  2576. "namespace {\n"
  2577. "void f();\n"
  2578. "}",
  2579. format("SOME_MACRO\n"
  2580. " namespace {\n"
  2581. "void f( );\n"
  2582. "}"));
  2583. // Only if the identifier contains at least 5 characters.
  2584. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2585. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2586. // Only if everything is upper case.
  2587. EXPECT_EQ("class A : public QObject {\n"
  2588. " Q_Object A() {}\n"
  2589. "};",
  2590. format("class A : public QObject {\n"
  2591. " Q_Object\n"
  2592. " A() {\n}\n"
  2593. "} ;"));
  2594. // Only if the next line can actually start an unwrapped line.
  2595. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2596. format("SOME_WEIRD_LOG_MACRO\n"
  2597. "<< SomeThing;"));
  2598. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2599. "(n, buffers))\n",
  2600. getChromiumStyle(FormatStyle::LK_Cpp));
  2601. }
  2602. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2603. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2604. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2605. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2606. "class X {};\n"
  2607. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2608. "int *createScopDetectionPass() { return 0; }",
  2609. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2610. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2611. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2612. " class X {};\n"
  2613. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2614. " int *createScopDetectionPass() { return 0; }"));
  2615. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2616. // braces, so that inner block is indented one level more.
  2617. EXPECT_EQ("int q() {\n"
  2618. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2619. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2620. " IPC_END_MESSAGE_MAP()\n"
  2621. "}",
  2622. format("int q() {\n"
  2623. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2624. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2625. " IPC_END_MESSAGE_MAP()\n"
  2626. "}"));
  2627. // Same inside macros.
  2628. EXPECT_EQ("#define LIST(L) \\\n"
  2629. " L(A) \\\n"
  2630. " L(B) \\\n"
  2631. " L(C)",
  2632. format("#define LIST(L) \\\n"
  2633. " L(A) \\\n"
  2634. " L(B) \\\n"
  2635. " L(C)",
  2636. getGoogleStyle()));
  2637. // These must not be recognized as macros.
  2638. EXPECT_EQ("int q() {\n"
  2639. " f(x);\n"
  2640. " f(x) {}\n"
  2641. " f(x)->g();\n"
  2642. " f(x)->*g();\n"
  2643. " f(x).g();\n"
  2644. " f(x) = x;\n"
  2645. " f(x) += x;\n"
  2646. " f(x) -= x;\n"
  2647. " f(x) *= x;\n"
  2648. " f(x) /= x;\n"
  2649. " f(x) %= x;\n"
  2650. " f(x) &= x;\n"
  2651. " f(x) |= x;\n"
  2652. " f(x) ^= x;\n"
  2653. " f(x) >>= x;\n"
  2654. " f(x) <<= x;\n"
  2655. " f(x)[y].z();\n"
  2656. " LOG(INFO) << x;\n"
  2657. " ifstream(x) >> x;\n"
  2658. "}\n",
  2659. format("int q() {\n"
  2660. " f(x)\n;\n"
  2661. " f(x)\n {}\n"
  2662. " f(x)\n->g();\n"
  2663. " f(x)\n->*g();\n"
  2664. " f(x)\n.g();\n"
  2665. " f(x)\n = x;\n"
  2666. " f(x)\n += x;\n"
  2667. " f(x)\n -= x;\n"
  2668. " f(x)\n *= x;\n"
  2669. " f(x)\n /= x;\n"
  2670. " f(x)\n %= x;\n"
  2671. " f(x)\n &= x;\n"
  2672. " f(x)\n |= x;\n"
  2673. " f(x)\n ^= x;\n"
  2674. " f(x)\n >>= x;\n"
  2675. " f(x)\n <<= x;\n"
  2676. " f(x)\n[y].z();\n"
  2677. " LOG(INFO)\n << x;\n"
  2678. " ifstream(x)\n >> x;\n"
  2679. "}\n"));
  2680. EXPECT_EQ("int q() {\n"
  2681. " F(x)\n"
  2682. " if (1) {\n"
  2683. " }\n"
  2684. " F(x)\n"
  2685. " while (1) {\n"
  2686. " }\n"
  2687. " F(x)\n"
  2688. " G(x);\n"
  2689. " F(x)\n"
  2690. " try {\n"
  2691. " Q();\n"
  2692. " } catch (...) {\n"
  2693. " }\n"
  2694. "}\n",
  2695. format("int q() {\n"
  2696. "F(x)\n"
  2697. "if (1) {}\n"
  2698. "F(x)\n"
  2699. "while (1) {}\n"
  2700. "F(x)\n"
  2701. "G(x);\n"
  2702. "F(x)\n"
  2703. "try { Q(); } catch (...) {}\n"
  2704. "}\n"));
  2705. EXPECT_EQ("class A {\n"
  2706. " A() : t(0) {}\n"
  2707. " A(int i) noexcept() : {}\n"
  2708. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2709. " try : t(0) {\n"
  2710. " } catch (...) {\n"
  2711. " }\n"
  2712. "};",
  2713. format("class A {\n"
  2714. " A()\n : t(0) {}\n"
  2715. " A(int i)\n noexcept() : {}\n"
  2716. " A(X x)\n"
  2717. " try : t(0) {} catch (...) {}\n"
  2718. "};"));
  2719. EXPECT_EQ("class SomeClass {\n"
  2720. "public:\n"
  2721. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2722. "};",
  2723. format("class SomeClass {\n"
  2724. "public:\n"
  2725. " SomeClass()\n"
  2726. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2727. "};"));
  2728. EXPECT_EQ("class SomeClass {\n"
  2729. "public:\n"
  2730. " SomeClass()\n"
  2731. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2732. "};",
  2733. format("class SomeClass {\n"
  2734. "public:\n"
  2735. " SomeClass()\n"
  2736. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2737. "};",
  2738. getLLVMStyleWithColumns(40)));
  2739. }
  2740. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2741. verifyFormat("#define A \\\n"
  2742. " f({ \\\n"
  2743. " g(); \\\n"
  2744. " });",
  2745. getLLVMStyleWithColumns(11));
  2746. }
  2747. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2748. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2749. }
  2750. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2751. verifyFormat("{\n { a #c; }\n}");
  2752. }
  2753. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2754. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2755. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2756. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2757. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2758. }
  2759. TEST_F(FormatTest, EscapedNewlines) {
  2760. EXPECT_EQ(
  2761. "#define A \\\n int i; \\\n int j;",
  2762. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2763. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2764. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2765. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2766. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2767. }
  2768. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2769. EXPECT_EQ(
  2770. "int xxxxxxxxx; /* "
  2771. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2772. "zzzzzz\n"
  2773. "0*/",
  2774. format("int xxxxxxxxx; /* "
  2775. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2776. "0*/"));
  2777. }
  2778. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2779. verifyFormat("#define A \\\n"
  2780. " int v( \\\n"
  2781. " a); \\\n"
  2782. " int i;",
  2783. getLLVMStyleWithColumns(11));
  2784. }
  2785. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2786. EXPECT_EQ(
  2787. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2788. " \\\n"
  2789. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2790. "\n"
  2791. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2792. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2793. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2794. "\\\n"
  2795. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2796. " \n"
  2797. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2798. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2799. }
  2800. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2801. EXPECT_EQ("int\n"
  2802. "#define A\n"
  2803. " a;",
  2804. format("int\n#define A\na;"));
  2805. verifyFormat("functionCallTo(\n"
  2806. " someOtherFunction(\n"
  2807. " withSomeParameters, whichInSequence,\n"
  2808. " areLongerThanALine(andAnotherCall,\n"
  2809. "#define A B\n"
  2810. " withMoreParamters,\n"
  2811. " whichStronglyInfluenceTheLayout),\n"
  2812. " andMoreParameters),\n"
  2813. " trailing);",
  2814. getLLVMStyleWithColumns(69));
  2815. verifyFormat("Foo::Foo()\n"
  2816. "#ifdef BAR\n"
  2817. " : baz(0)\n"
  2818. "#endif\n"
  2819. "{\n"
  2820. "}");
  2821. verifyFormat("void f() {\n"
  2822. " if (true)\n"
  2823. "#ifdef A\n"
  2824. " f(42);\n"
  2825. " x();\n"
  2826. "#else\n"
  2827. " g();\n"
  2828. " x();\n"
  2829. "#endif\n"
  2830. "}");
  2831. verifyFormat("void f(param1, param2,\n"
  2832. " param3,\n"
  2833. "#ifdef A\n"
  2834. " param4(param5,\n"
  2835. "#ifdef A1\n"
  2836. " param6,\n"
  2837. "#ifdef A2\n"
  2838. " param7),\n"
  2839. "#else\n"
  2840. " param8),\n"
  2841. " param9,\n"
  2842. "#endif\n"
  2843. " param10,\n"
  2844. "#endif\n"
  2845. " param11)\n"
  2846. "#else\n"
  2847. " param12)\n"
  2848. "#endif\n"
  2849. "{\n"
  2850. " x();\n"
  2851. "}",
  2852. getLLVMStyleWithColumns(28));
  2853. verifyFormat("#if 1\n"
  2854. "int i;");
  2855. verifyFormat("#if 1\n"
  2856. "#endif\n"
  2857. "#if 1\n"
  2858. "#else\n"
  2859. "#endif\n");
  2860. verifyFormat("DEBUG({\n"
  2861. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2862. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2863. "});\n"
  2864. "#if a\n"
  2865. "#else\n"
  2866. "#endif");
  2867. verifyIncompleteFormat("void f(\n"
  2868. "#if A\n"
  2869. " );\n"
  2870. "#else\n"
  2871. "#endif");
  2872. }
  2873. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2874. verifyFormat("#endif\n"
  2875. "#if B");
  2876. }
  2877. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2878. FormatStyle SingleLine = getLLVMStyle();
  2879. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2880. verifyFormat("#if 0\n"
  2881. "#elif 1\n"
  2882. "#endif\n"
  2883. "void foo() {\n"
  2884. " if (test) foo2();\n"
  2885. "}",
  2886. SingleLine);
  2887. }
  2888. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2889. verifyFormat("functionCall({ int i; });");
  2890. verifyFormat("functionCall({\n"
  2891. " int i;\n"
  2892. " int j;\n"
  2893. "});");
  2894. verifyFormat("functionCall(\n"
  2895. " {\n"
  2896. " int i;\n"
  2897. " int j;\n"
  2898. " },\n"
  2899. " aaaa, bbbb, cccc);");
  2900. verifyFormat("functionA(functionB({\n"
  2901. " int i;\n"
  2902. " int j;\n"
  2903. " }),\n"
  2904. " aaaa, bbbb, cccc);");
  2905. verifyFormat("functionCall(\n"
  2906. " {\n"
  2907. " int i;\n"
  2908. " int j;\n"
  2909. " },\n"
  2910. " aaaa, bbbb, // comment\n"
  2911. " cccc);");
  2912. verifyFormat("functionA(functionB({\n"
  2913. " int i;\n"
  2914. " int j;\n"
  2915. " }),\n"
  2916. " aaaa, bbbb, // comment\n"
  2917. " cccc);");
  2918. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2919. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2920. " int i;\n"
  2921. " int j;\n"
  2922. "});");
  2923. verifyFormat(
  2924. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2925. " {\n"
  2926. " int i; // break\n"
  2927. " },\n"
  2928. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2929. " ccccccccccccccccc));");
  2930. verifyFormat("DEBUG({\n"
  2931. " if (a)\n"
  2932. " f();\n"
  2933. "});");
  2934. }
  2935. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2936. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2937. "int i;",
  2938. format(" SOME_MACRO {int i;} int i;"));
  2939. }
  2940. TEST_F(FormatTest, LayoutNestedBlocks) {
  2941. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2942. " struct s {\n"
  2943. " int i;\n"
  2944. " };\n"
  2945. " s kBitsToOs[] = {{10}};\n"
  2946. " for (int i = 0; i < 10; ++i)\n"
  2947. " return;\n"
  2948. "}");
  2949. verifyFormat("call(parameter, {\n"
  2950. " something();\n"
  2951. " // Comment using all columns.\n"
  2952. " somethingelse();\n"
  2953. "});",
  2954. getLLVMStyleWithColumns(40));
  2955. verifyFormat("DEBUG( //\n"
  2956. " { f(); }, a);");
  2957. verifyFormat("DEBUG( //\n"
  2958. " {\n"
  2959. " f(); //\n"
  2960. " },\n"
  2961. " a);");
  2962. EXPECT_EQ("call(parameter, {\n"
  2963. " something();\n"
  2964. " // Comment too\n"
  2965. " // looooooooooong.\n"
  2966. " somethingElse();\n"
  2967. "});",
  2968. format("call(parameter, {\n"
  2969. " something();\n"
  2970. " // Comment too looooooooooong.\n"
  2971. " somethingElse();\n"
  2972. "});",
  2973. getLLVMStyleWithColumns(29)));
  2974. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2975. EXPECT_EQ("DEBUG({ // comment\n"
  2976. " int i;\n"
  2977. "});",
  2978. format("DEBUG({ // comment\n"
  2979. "int i;\n"
  2980. "});"));
  2981. EXPECT_EQ("DEBUG({\n"
  2982. " int i;\n"
  2983. "\n"
  2984. " // comment\n"
  2985. " int j;\n"
  2986. "});",
  2987. format("DEBUG({\n"
  2988. " int i;\n"
  2989. "\n"
  2990. " // comment\n"
  2991. " int j;\n"
  2992. "});"));
  2993. verifyFormat("DEBUG({\n"
  2994. " if (a)\n"
  2995. " return;\n"
  2996. "});");
  2997. verifyGoogleFormat("DEBUG({\n"
  2998. " if (a) return;\n"
  2999. "});");
  3000. FormatStyle Style = getGoogleStyle();
  3001. Style.ColumnLimit = 45;
  3002. verifyFormat("Debug(aaaaa,\n"
  3003. " {\n"
  3004. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3005. " },\n"
  3006. " a);",
  3007. Style);
  3008. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3009. verifyNoCrash("^{v^{a}}");
  3010. }
  3011. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3012. EXPECT_EQ("#define MACRO() \\\n"
  3013. " Debug(aaa, /* force line break */ \\\n"
  3014. " { \\\n"
  3015. " int i; \\\n"
  3016. " int j; \\\n"
  3017. " })",
  3018. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3019. " { int i; int j; })",
  3020. getGoogleStyle()));
  3021. EXPECT_EQ("#define A \\\n"
  3022. " [] { \\\n"
  3023. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3024. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3025. " }",
  3026. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3027. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3028. getGoogleStyle()));
  3029. }
  3030. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3031. EXPECT_EQ("{}", format("{}"));
  3032. verifyFormat("enum E {};");
  3033. verifyFormat("enum E {}");
  3034. }
  3035. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3036. FormatStyle Style = getLLVMStyle();
  3037. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3038. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3039. verifyFormat("FOO_BEGIN\n"
  3040. " FOO_ENTRY\n"
  3041. "FOO_END", Style);
  3042. verifyFormat("FOO_BEGIN\n"
  3043. " NESTED_FOO_BEGIN\n"
  3044. " NESTED_FOO_ENTRY\n"
  3045. " NESTED_FOO_END\n"
  3046. "FOO_END", Style);
  3047. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3048. " int x;\n"
  3049. " x = 1;\n"
  3050. "FOO_END(Baz)", Style);
  3051. }
  3052. //===----------------------------------------------------------------------===//
  3053. // Line break tests.
  3054. //===----------------------------------------------------------------------===//
  3055. TEST_F(FormatTest, PreventConfusingIndents) {
  3056. verifyFormat(
  3057. "void f() {\n"
  3058. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3059. " parameter, parameter, parameter)),\n"
  3060. " SecondLongCall(parameter));\n"
  3061. "}");
  3062. verifyFormat(
  3063. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3064. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3065. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3066. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3067. verifyFormat(
  3068. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3069. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3070. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3071. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3072. verifyFormat(
  3073. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3074. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3075. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3076. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3077. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3078. "#define A Just forcing a new line\n"
  3079. " ddd);");
  3080. }
  3081. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3082. verifyFormat(
  3083. "bool aaaaaaa =\n"
  3084. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3085. " bbbbbbbb();");
  3086. verifyFormat(
  3087. "bool aaaaaaa =\n"
  3088. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3089. " bbbbbbbb();");
  3090. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3091. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3092. " ccccccccc == ddddddddddd;");
  3093. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3094. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3095. " ccccccccc == ddddddddddd;");
  3096. verifyFormat(
  3097. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3098. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3099. " ccccccccc == ddddddddddd;");
  3100. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3101. " aaaaaa) &&\n"
  3102. " bbbbbb && cccccc;");
  3103. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3104. " aaaaaa) >>\n"
  3105. " bbbbbb;");
  3106. verifyFormat("Whitespaces.addUntouchableComment(\n"
  3107. " SourceMgr.getSpellingColumnNumber(\n"
  3108. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3109. " 1);");
  3110. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3111. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3112. " cccccc) {\n}");
  3113. verifyFormat("b = a &&\n"
  3114. " // Comment\n"
  3115. " b.c && d;");
  3116. // If the LHS of a comparison is not a binary expression itself, the
  3117. // additional linebreak confuses many people.
  3118. verifyFormat(
  3119. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3120. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3121. "}");
  3122. verifyFormat(
  3123. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3124. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3125. "}");
  3126. verifyFormat(
  3127. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3129. "}");
  3130. // Even explicit parentheses stress the precedence enough to make the
  3131. // additional break unnecessary.
  3132. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3134. "}");
  3135. // This cases is borderline, but with the indentation it is still readable.
  3136. verifyFormat(
  3137. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3138. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3139. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3140. "}",
  3141. getLLVMStyleWithColumns(75));
  3142. // If the LHS is a binary expression, we should still use the additional break
  3143. // as otherwise the formatting hides the operator precedence.
  3144. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3145. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3146. " 5) {\n"
  3147. "}");
  3148. FormatStyle OnePerLine = getLLVMStyle();
  3149. OnePerLine.BinPackParameters = false;
  3150. verifyFormat(
  3151. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3152. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3153. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3154. OnePerLine);
  3155. }
  3156. TEST_F(FormatTest, ExpressionIndentation) {
  3157. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3158. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3159. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3160. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3161. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3162. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3163. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3164. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3165. " ccccccccccccccccccccccccccccccccccccccccc;");
  3166. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3167. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3168. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3169. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3170. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3171. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3172. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3173. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3174. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3175. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3176. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3177. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3178. verifyFormat("if () {\n"
  3179. "} else if (aaaaa &&\n"
  3180. " bbbbb > // break\n"
  3181. " ccccc) {\n"
  3182. "}");
  3183. // Presence of a trailing comment used to change indentation of b.
  3184. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3185. " b;\n"
  3186. "return aaaaaaaaaaaaaaaaaaa +\n"
  3187. " b; //",
  3188. getLLVMStyleWithColumns(30));
  3189. }
  3190. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3191. // Not sure what the best system is here. Like this, the LHS can be found
  3192. // immediately above an operator (everything with the same or a higher
  3193. // indent). The RHS is aligned right of the operator and so compasses
  3194. // everything until something with the same indent as the operator is found.
  3195. // FIXME: Is this a good system?
  3196. FormatStyle Style = getLLVMStyle();
  3197. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3198. verifyFormat(
  3199. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3200. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3201. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3202. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3203. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3204. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3205. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3206. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3207. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3208. Style);
  3209. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3210. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3211. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3212. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3213. Style);
  3214. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3215. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3216. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3217. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3218. Style);
  3219. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3220. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3221. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3222. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3223. Style);
  3224. verifyFormat("if () {\n"
  3225. "} else if (aaaaa\n"
  3226. " && bbbbb // break\n"
  3227. " > ccccc) {\n"
  3228. "}",
  3229. Style);
  3230. verifyFormat("return (a)\n"
  3231. " // comment\n"
  3232. " + b;",
  3233. Style);
  3234. verifyFormat(
  3235. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3236. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3237. " + cc;",
  3238. Style);
  3239. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3240. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3241. Style);
  3242. // Forced by comments.
  3243. verifyFormat(
  3244. "unsigned ContentSize =\n"
  3245. " sizeof(int16_t) // DWARF ARange version number\n"
  3246. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3247. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3248. " + sizeof(int8_t); // Segment Size (in bytes)");
  3249. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3250. " == boost::fusion::at_c<1>(iiii).second;",
  3251. Style);
  3252. Style.ColumnLimit = 60;
  3253. verifyFormat("zzzzzzzzzz\n"
  3254. " = bbbbbbbbbbbbbbbbb\n"
  3255. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3256. Style);
  3257. }
  3258. TEST_F(FormatTest, NoOperandAlignment) {
  3259. FormatStyle Style = getLLVMStyle();
  3260. Style.AlignOperands = false;
  3261. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3262. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3263. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3264. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3265. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3266. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3267. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3268. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3269. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3270. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3271. Style);
  3272. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3273. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3274. " + cc;",
  3275. Style);
  3276. verifyFormat("int a = aa\n"
  3277. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3278. " * cccccccccccccccccccccccccccccccccccc;",
  3279. Style);
  3280. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3281. verifyFormat("return (a > b\n"
  3282. " // comment1\n"
  3283. " // comment2\n"
  3284. " || c);",
  3285. Style);
  3286. }
  3287. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3288. FormatStyle Style = getLLVMStyle();
  3289. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3290. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3291. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3292. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3293. Style);
  3294. }
  3295. TEST_F(FormatTest, ConstructorInitializers) {
  3296. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3297. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3298. getLLVMStyleWithColumns(45));
  3299. verifyFormat("Constructor()\n"
  3300. " : Inttializer(FitsOnTheLine) {}",
  3301. getLLVMStyleWithColumns(44));
  3302. verifyFormat("Constructor()\n"
  3303. " : Inttializer(FitsOnTheLine) {}",
  3304. getLLVMStyleWithColumns(43));
  3305. verifyFormat("template <typename T>\n"
  3306. "Constructor() : Initializer(FitsOnTheLine) {}",
  3307. getLLVMStyleWithColumns(45));
  3308. verifyFormat(
  3309. "SomeClass::Constructor()\n"
  3310. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3311. verifyFormat(
  3312. "SomeClass::Constructor()\n"
  3313. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3314. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3315. verifyFormat(
  3316. "SomeClass::Constructor()\n"
  3317. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3318. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3319. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3320. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3321. " : aaaaaaaaaa(aaaaaa) {}");
  3322. verifyFormat("Constructor()\n"
  3323. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3324. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3325. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3326. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3327. verifyFormat("Constructor()\n"
  3328. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3329. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3330. verifyFormat("Constructor(int Parameter = 0)\n"
  3331. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3332. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3333. verifyFormat("Constructor()\n"
  3334. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3335. "}",
  3336. getLLVMStyleWithColumns(60));
  3337. verifyFormat("Constructor()\n"
  3338. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3339. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3340. // Here a line could be saved by splitting the second initializer onto two
  3341. // lines, but that is not desirable.
  3342. verifyFormat("Constructor()\n"
  3343. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3344. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3345. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3346. FormatStyle OnePerLine = getLLVMStyle();
  3347. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3348. verifyFormat("SomeClass::Constructor()\n"
  3349. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3350. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3351. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3352. OnePerLine);
  3353. verifyFormat("SomeClass::Constructor()\n"
  3354. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3355. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3356. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3357. OnePerLine);
  3358. verifyFormat("MyClass::MyClass(int var)\n"
  3359. " : some_var_(var), // 4 space indent\n"
  3360. " some_other_var_(var + 1) { // lined up\n"
  3361. "}",
  3362. OnePerLine);
  3363. verifyFormat("Constructor()\n"
  3364. " : aaaaa(aaaaaa),\n"
  3365. " aaaaa(aaaaaa),\n"
  3366. " aaaaa(aaaaaa),\n"
  3367. " aaaaa(aaaaaa),\n"
  3368. " aaaaa(aaaaaa) {}",
  3369. OnePerLine);
  3370. verifyFormat("Constructor()\n"
  3371. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3372. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3373. OnePerLine);
  3374. OnePerLine.ColumnLimit = 60;
  3375. verifyFormat("Constructor()\n"
  3376. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3377. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3378. OnePerLine);
  3379. EXPECT_EQ("Constructor()\n"
  3380. " : // Comment forcing unwanted break.\n"
  3381. " aaaa(aaaa) {}",
  3382. format("Constructor() :\n"
  3383. " // Comment forcing unwanted break.\n"
  3384. " aaaa(aaaa) {}"));
  3385. }
  3386. TEST_F(FormatTest, MemoizationTests) {
  3387. // This breaks if the memoization lookup does not take \c Indent and
  3388. // \c LastSpace into account.
  3389. verifyFormat(
  3390. "extern CFRunLoopTimerRef\n"
  3391. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3392. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3393. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3394. " CFRunLoopTimerContext *context) {}");
  3395. // Deep nesting somewhat works around our memoization.
  3396. verifyFormat(
  3397. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3398. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3399. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3400. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3401. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3402. getLLVMStyleWithColumns(65));
  3403. verifyFormat(
  3404. "aaaaa(\n"
  3405. " aaaaa,\n"
  3406. " aaaaa(\n"
  3407. " aaaaa,\n"
  3408. " aaaaa(\n"
  3409. " aaaaa,\n"
  3410. " aaaaa(\n"
  3411. " aaaaa,\n"
  3412. " aaaaa(\n"
  3413. " aaaaa,\n"
  3414. " aaaaa(\n"
  3415. " aaaaa,\n"
  3416. " aaaaa(\n"
  3417. " aaaaa,\n"
  3418. " aaaaa(\n"
  3419. " aaaaa,\n"
  3420. " aaaaa(\n"
  3421. " aaaaa,\n"
  3422. " aaaaa(\n"
  3423. " aaaaa,\n"
  3424. " aaaaa(\n"
  3425. " aaaaa,\n"
  3426. " aaaaa(\n"
  3427. " aaaaa,\n"
  3428. " aaaaa))))))))))));",
  3429. getLLVMStyleWithColumns(65));
  3430. verifyFormat(
  3431. "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"
  3432. " a),\n"
  3433. " a),\n"
  3434. " a),\n"
  3435. " a),\n"
  3436. " a),\n"
  3437. " a),\n"
  3438. " a),\n"
  3439. " a),\n"
  3440. " a),\n"
  3441. " a),\n"
  3442. " a),\n"
  3443. " a),\n"
  3444. " a),\n"
  3445. " a),\n"
  3446. " a),\n"
  3447. " a),\n"
  3448. " a)",
  3449. getLLVMStyleWithColumns(65));
  3450. // This test takes VERY long when memoization is broken.
  3451. FormatStyle OnePerLine = getLLVMStyle();
  3452. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3453. OnePerLine.BinPackParameters = false;
  3454. std::string input = "Constructor()\n"
  3455. " : aaaa(a,\n";
  3456. for (unsigned i = 0, e = 80; i != e; ++i) {
  3457. input += " a,\n";
  3458. }
  3459. input += " a) {}";
  3460. verifyFormat(input, OnePerLine);
  3461. }
  3462. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3463. verifyFormat(
  3464. "void f() {\n"
  3465. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3466. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3467. " f();\n"
  3468. "}");
  3469. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3470. " Intervals[i - 1].getRange().getLast()) {\n}");
  3471. }
  3472. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3473. // Principially, we break function declarations in a certain order:
  3474. // 1) break amongst arguments.
  3475. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3476. " Cccccccccccccc cccccccccccccc);");
  3477. verifyFormat("template <class TemplateIt>\n"
  3478. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3479. " TemplateIt *stop) {}");
  3480. // 2) break after return type.
  3481. verifyFormat(
  3482. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3483. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3484. getGoogleStyle());
  3485. // 3) break after (.
  3486. verifyFormat(
  3487. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3488. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3489. getGoogleStyle());
  3490. // 4) break before after nested name specifiers.
  3491. verifyFormat(
  3492. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3493. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3494. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3495. getGoogleStyle());
  3496. // However, there are exceptions, if a sufficient amount of lines can be
  3497. // saved.
  3498. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3499. // more adjusting.
  3500. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3501. " Cccccccccccccc cccccccccc,\n"
  3502. " Cccccccccccccc cccccccccc,\n"
  3503. " Cccccccccccccc cccccccccc,\n"
  3504. " Cccccccccccccc cccccccccc);");
  3505. verifyFormat(
  3506. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3507. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3508. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3509. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3510. getGoogleStyle());
  3511. verifyFormat(
  3512. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3513. " Cccccccccccccc cccccccccc,\n"
  3514. " Cccccccccccccc cccccccccc,\n"
  3515. " Cccccccccccccc cccccccccc,\n"
  3516. " Cccccccccccccc cccccccccc,\n"
  3517. " Cccccccccccccc cccccccccc,\n"
  3518. " Cccccccccccccc cccccccccc);");
  3519. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3520. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3521. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3522. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3523. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3524. // Break after multi-line parameters.
  3525. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3526. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3527. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3528. " bbbb bbbb);");
  3529. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3530. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3531. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3532. " int bbbbbbbbbbbbb);");
  3533. // Treat overloaded operators like other functions.
  3534. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3535. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3536. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3537. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3538. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3539. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3540. verifyGoogleFormat(
  3541. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3542. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3543. verifyGoogleFormat(
  3544. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3545. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3546. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3547. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3548. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3549. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3550. verifyGoogleFormat(
  3551. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3552. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3553. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3554. FormatStyle Style = getLLVMStyle();
  3555. Style.PointerAlignment = FormatStyle::PAS_Left;
  3556. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3557. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3558. Style);
  3559. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3560. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3561. Style);
  3562. }
  3563. TEST_F(FormatTest, TrailingReturnType) {
  3564. verifyFormat("auto foo() -> int;\n");
  3565. verifyFormat("struct S {\n"
  3566. " auto bar() const -> int;\n"
  3567. "};");
  3568. verifyFormat("template <size_t Order, typename T>\n"
  3569. "auto load_img(const std::string &filename)\n"
  3570. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3571. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3572. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3573. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3574. verifyFormat("template <typename T>\n"
  3575. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3576. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3577. // Not trailing return types.
  3578. verifyFormat("void f() { auto a = b->c(); }");
  3579. }
  3580. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3581. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3582. // they are not function-like.
  3583. FormatStyle Style = getGoogleStyle();
  3584. Style.ColumnLimit = 47;
  3585. verifyFormat("void someLongFunction(\n"
  3586. " int someLoooooooooooooongParameter) const {\n}",
  3587. getLLVMStyleWithColumns(47));
  3588. verifyFormat("LoooooongReturnType\n"
  3589. "someLoooooooongFunction() const {}",
  3590. getLLVMStyleWithColumns(47));
  3591. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3592. " const {}",
  3593. Style);
  3594. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3595. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3596. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3597. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3598. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3599. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3600. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3601. " aaaaaaaaaaa aaaaa) const override;");
  3602. verifyGoogleFormat(
  3603. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3604. " const override;");
  3605. // Even if the first parameter has to be wrapped.
  3606. verifyFormat("void someLongFunction(\n"
  3607. " int someLongParameter) const {}",
  3608. getLLVMStyleWithColumns(46));
  3609. verifyFormat("void someLongFunction(\n"
  3610. " int someLongParameter) const {}",
  3611. Style);
  3612. verifyFormat("void someLongFunction(\n"
  3613. " int someLongParameter) override {}",
  3614. Style);
  3615. verifyFormat("void someLongFunction(\n"
  3616. " int someLongParameter) OVERRIDE {}",
  3617. Style);
  3618. verifyFormat("void someLongFunction(\n"
  3619. " int someLongParameter) final {}",
  3620. Style);
  3621. verifyFormat("void someLongFunction(\n"
  3622. " int someLongParameter) FINAL {}",
  3623. Style);
  3624. verifyFormat("void someLongFunction(\n"
  3625. " int parameter) const override {}",
  3626. Style);
  3627. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3628. verifyFormat("void someLongFunction(\n"
  3629. " int someLongParameter) const\n"
  3630. "{\n"
  3631. "}",
  3632. Style);
  3633. // Unless these are unknown annotations.
  3634. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3635. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3636. " LONG_AND_UGLY_ANNOTATION;");
  3637. // Breaking before function-like trailing annotations is fine to keep them
  3638. // close to their arguments.
  3639. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3640. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3641. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3642. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3643. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3644. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3645. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3646. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3647. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3648. verifyFormat(
  3649. "void aaaaaaaaaaaaaaaaaa()\n"
  3650. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3651. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3652. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3653. " __attribute__((unused));");
  3654. verifyGoogleFormat(
  3655. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3656. " GUARDED_BY(aaaaaaaaaaaa);");
  3657. verifyGoogleFormat(
  3658. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3659. " GUARDED_BY(aaaaaaaaaaaa);");
  3660. verifyGoogleFormat(
  3661. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3662. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3663. verifyGoogleFormat(
  3664. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3665. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3666. }
  3667. TEST_F(FormatTest, FunctionAnnotations) {
  3668. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3669. "int OldFunction(const string &parameter) {}");
  3670. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3671. "string OldFunction(const string &parameter) {}");
  3672. verifyFormat("template <typename T>\n"
  3673. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3674. "string OldFunction(const string &parameter) {}");
  3675. // Not function annotations.
  3676. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3677. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3678. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3679. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3680. }
  3681. TEST_F(FormatTest, BreaksDesireably) {
  3682. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3683. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3684. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3685. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3686. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3687. "}");
  3688. verifyFormat(
  3689. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3690. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3691. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3692. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3693. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3694. verifyFormat(
  3695. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3696. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3697. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3698. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3699. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3700. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3701. verifyFormat(
  3702. "void f() {\n"
  3703. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3704. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3705. "}");
  3706. verifyFormat(
  3707. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3708. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3709. verifyFormat(
  3710. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3711. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3712. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3713. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3714. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3715. // Indent consistently independent of call expression and unary operator.
  3716. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3717. " dddddddddddddddddddddddddddddd));");
  3718. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3719. " dddddddddddddddddddddddddddddd));");
  3720. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3721. " dddddddddddddddddddddddddddddd));");
  3722. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3723. // taking into account the StopAt value.
  3724. verifyFormat(
  3725. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3726. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3727. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3728. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3729. verifyFormat("{\n {\n {\n"
  3730. " Annotation.SpaceRequiredBefore =\n"
  3731. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3732. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3733. " }\n }\n}");
  3734. // Break on an outer level if there was a break on an inner level.
  3735. EXPECT_EQ("f(g(h(a, // comment\n"
  3736. " b, c),\n"
  3737. " d, e),\n"
  3738. " x, y);",
  3739. format("f(g(h(a, // comment\n"
  3740. " b, c), d, e), x, y);"));
  3741. // Prefer breaking similar line breaks.
  3742. verifyFormat(
  3743. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3744. " NSTrackingMouseEnteredAndExited |\n"
  3745. " NSTrackingActiveAlways;");
  3746. }
  3747. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3748. FormatStyle NoBinPacking = getGoogleStyle();
  3749. NoBinPacking.BinPackParameters = false;
  3750. NoBinPacking.BinPackArguments = true;
  3751. verifyFormat("void f() {\n"
  3752. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3754. "}",
  3755. NoBinPacking);
  3756. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3757. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3758. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3759. NoBinPacking);
  3760. }
  3761. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3762. FormatStyle NoBinPacking = getGoogleStyle();
  3763. NoBinPacking.BinPackParameters = false;
  3764. NoBinPacking.BinPackArguments = false;
  3765. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3766. " aaaaaaaaaaaaaaaaaaaa,\n"
  3767. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3768. NoBinPacking);
  3769. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3770. " aaaaaaaaaaaaa,\n"
  3771. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3772. NoBinPacking);
  3773. verifyFormat(
  3774. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3775. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3776. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3777. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3778. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3779. NoBinPacking);
  3780. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3781. " .aaaaaaaaaaaaaaaaaa();",
  3782. NoBinPacking);
  3783. verifyFormat("void f() {\n"
  3784. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3785. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3786. "}",
  3787. NoBinPacking);
  3788. verifyFormat(
  3789. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3790. " aaaaaaaaaaaa,\n"
  3791. " aaaaaaaaaaaa);",
  3792. NoBinPacking);
  3793. verifyFormat(
  3794. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3795. " ddddddddddddddddddddddddddddd),\n"
  3796. " test);",
  3797. NoBinPacking);
  3798. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3799. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3800. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3801. NoBinPacking);
  3802. verifyFormat("a(\"a\"\n"
  3803. " \"a\",\n"
  3804. " a);");
  3805. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3806. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3807. " aaaaaaaaa,\n"
  3808. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3809. NoBinPacking);
  3810. verifyFormat(
  3811. "void f() {\n"
  3812. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3813. " .aaaaaaa();\n"
  3814. "}",
  3815. NoBinPacking);
  3816. verifyFormat(
  3817. "template <class SomeType, class SomeOtherType>\n"
  3818. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3819. NoBinPacking);
  3820. }
  3821. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3822. FormatStyle Style = getLLVMStyleWithColumns(15);
  3823. Style.ExperimentalAutoDetectBinPacking = true;
  3824. EXPECT_EQ("aaa(aaaa,\n"
  3825. " aaaa,\n"
  3826. " aaaa);\n"
  3827. "aaa(aaaa,\n"
  3828. " aaaa,\n"
  3829. " aaaa);",
  3830. format("aaa(aaaa,\n" // one-per-line
  3831. " aaaa,\n"
  3832. " aaaa );\n"
  3833. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3834. Style));
  3835. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3836. " aaaa);\n"
  3837. "aaa(aaaa, aaaa,\n"
  3838. " aaaa);",
  3839. format("aaa(aaaa, aaaa,\n" // bin-packed
  3840. " aaaa );\n"
  3841. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3842. Style));
  3843. }
  3844. TEST_F(FormatTest, FormatsBuilderPattern) {
  3845. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3846. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3847. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3848. " .StartsWith(\".init\", ORDER_INIT)\n"
  3849. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3850. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3851. " .Default(ORDER_TEXT);\n");
  3852. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3853. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3854. verifyFormat(
  3855. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3856. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3857. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3858. verifyFormat(
  3859. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3860. " aaaaaaaaaaaaaa);");
  3861. verifyFormat(
  3862. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3863. " aaaaaa->aaaaaaaaaaaa()\n"
  3864. " ->aaaaaaaaaaaaaaaa(\n"
  3865. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3866. " ->aaaaaaaaaaaaaaaaa();");
  3867. verifyGoogleFormat(
  3868. "void f() {\n"
  3869. " someo->Add((new util::filetools::Handler(dir))\n"
  3870. " ->OnEvent1(NewPermanentCallback(\n"
  3871. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3872. " ->OnEvent2(NewPermanentCallback(\n"
  3873. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3874. " ->OnEvent3(NewPermanentCallback(\n"
  3875. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3876. " ->OnEvent5(NewPermanentCallback(\n"
  3877. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3878. " ->OnEvent6(NewPermanentCallback(\n"
  3879. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3880. "}");
  3881. verifyFormat(
  3882. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3883. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3884. " .aaaaaaaaaaaaaaa()\n"
  3885. " .aaaaaaaaaaaaaaa()\n"
  3886. " .aaaaaaaaaaaaaaa()\n"
  3887. " .aaaaaaaaaaaaaaa();");
  3888. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3889. " .aaaaaaaaaaaaaaa()\n"
  3890. " .aaaaaaaaaaaaaaa()\n"
  3891. " .aaaaaaaaaaaaaaa();");
  3892. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3893. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3894. " .aaaaaaaaaaaaaaa();");
  3895. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3896. " ->aaaaaaaaaaaaaae(0)\n"
  3897. " ->aaaaaaaaaaaaaaa();");
  3898. // Don't linewrap after very short segments.
  3899. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3900. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3901. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3902. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3903. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3904. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3905. verifyFormat("aaa()\n"
  3906. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3907. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3908. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3909. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3910. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3911. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3912. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3913. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3914. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3915. // Prefer not to break after empty parentheses.
  3916. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3917. " First->LastNewlineOffset);");
  3918. }
  3919. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3920. verifyFormat(
  3921. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3922. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3923. verifyFormat(
  3924. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3925. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3926. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3927. " ccccccccccccccccccccccccc) {\n}");
  3928. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3929. " ccccccccccccccccccccccccc) {\n}");
  3930. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3931. " ccccccccccccccccccccccccc) {\n}");
  3932. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3933. " ccccccccccccccccccccccccc) {\n}");
  3934. verifyFormat(
  3935. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3936. " ccccccccccccccccccccccccc) {\n}");
  3937. verifyFormat(
  3938. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3939. " ccccccccccccccccccccccccc) {\n}");
  3940. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3941. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3942. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3943. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3944. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3945. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3946. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3947. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3948. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3949. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3950. " aaaaaaaaaaaaaaa != aa) {\n}");
  3951. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3952. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3953. " aaaaaaaaaaaaaaa != aa) {\n}");
  3954. }
  3955. TEST_F(FormatTest, BreaksAfterAssignments) {
  3956. verifyFormat(
  3957. "unsigned Cost =\n"
  3958. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3959. " SI->getPointerAddressSpaceee());\n");
  3960. verifyFormat(
  3961. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3962. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3963. verifyFormat(
  3964. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3965. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3966. verifyFormat("unsigned OriginalStartColumn =\n"
  3967. " SourceMgr.getSpellingColumnNumber(\n"
  3968. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3969. " 1;");
  3970. }
  3971. TEST_F(FormatTest, AlignsAfterAssignments) {
  3972. verifyFormat(
  3973. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3974. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3975. verifyFormat(
  3976. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3977. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3978. verifyFormat(
  3979. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3980. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3981. verifyFormat(
  3982. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3983. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3984. verifyFormat(
  3985. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3986. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3987. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3988. }
  3989. TEST_F(FormatTest, AlignsAfterReturn) {
  3990. verifyFormat(
  3991. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3992. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3993. verifyFormat(
  3994. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3995. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3996. verifyFormat(
  3997. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3998. " aaaaaaaaaaaaaaaaaaaaaa();");
  3999. verifyFormat(
  4000. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4001. " aaaaaaaaaaaaaaaaaaaaaa());");
  4002. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4004. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4005. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4006. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4007. verifyFormat("return\n"
  4008. " // true if code is one of a or b.\n"
  4009. " code == a || code == b;");
  4010. }
  4011. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4012. verifyFormat(
  4013. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4014. " aaaaaaaaa aaaaaaa) {}");
  4015. verifyFormat(
  4016. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4017. " aaaaaaaaaaa aaaaaaaaa);");
  4018. verifyFormat(
  4019. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4020. " aaaaaaaaaaaaaaaaaaaaa));");
  4021. FormatStyle Style = getLLVMStyle();
  4022. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4023. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4024. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4025. Style);
  4026. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4027. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4028. Style);
  4029. verifyFormat("SomeLongVariableName->someFunction(\n"
  4030. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4031. Style);
  4032. verifyFormat(
  4033. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4034. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4035. Style);
  4036. verifyFormat(
  4037. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4038. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4039. Style);
  4040. verifyFormat(
  4041. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4042. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4043. Style);
  4044. }
  4045. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4046. FormatStyle Style = getLLVMStyleWithColumns(40);
  4047. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4048. " bbbbbbbbbbbbbbbbbbbbbb);",
  4049. Style);
  4050. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4051. Style.AlignOperands = false;
  4052. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4053. " bbbbbbbbbbbbbbbbbbbbbb);",
  4054. Style);
  4055. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4056. Style.AlignOperands = true;
  4057. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4058. " bbbbbbbbbbbbbbbbbbbbbb);",
  4059. Style);
  4060. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4061. Style.AlignOperands = false;
  4062. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4063. " bbbbbbbbbbbbbbbbbbbbbb);",
  4064. Style);
  4065. }
  4066. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4067. verifyFormat(
  4068. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4069. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4070. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4071. verifyFormat(
  4072. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4073. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4074. verifyFormat(
  4075. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4076. " : aaaaaaaaaaaaa);");
  4077. verifyFormat(
  4078. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4079. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4080. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4081. " aaaaaaaaaaaaa);");
  4082. verifyFormat(
  4083. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4084. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4085. " aaaaaaaaaaaaa);");
  4086. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4087. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4088. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4089. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4090. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4091. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4092. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4093. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4094. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4095. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4096. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4097. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4098. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4100. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4101. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4102. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4103. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4104. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4105. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4106. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4108. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4109. " : aaaaaaaaaaaaaaaa;");
  4110. verifyFormat(
  4111. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4112. " ? aaaaaaaaaaaaaaa\n"
  4113. " : aaaaaaaaaaaaaaa;");
  4114. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4115. " aaaaaaaaa\n"
  4116. " ? b\n"
  4117. " : c);");
  4118. verifyFormat("return aaaa == bbbb\n"
  4119. " // comment\n"
  4120. " ? aaaa\n"
  4121. " : bbbb;");
  4122. verifyFormat("unsigned Indent =\n"
  4123. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4124. " ? IndentForLevel[TheLine.Level]\n"
  4125. " : TheLine * 2,\n"
  4126. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4127. getLLVMStyleWithColumns(70));
  4128. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4129. " ? aaaaaaaaaaaaaaa\n"
  4130. " : bbbbbbbbbbbbbbb //\n"
  4131. " ? ccccccccccccccc\n"
  4132. " : ddddddddddddddd;");
  4133. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4134. " ? aaaaaaaaaaaaaaa\n"
  4135. " : (bbbbbbbbbbbbbbb //\n"
  4136. " ? ccccccccccccccc\n"
  4137. " : ddddddddddddddd);");
  4138. verifyFormat(
  4139. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4140. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4141. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4142. " aaaaaaaaaaaaaaaaaaaaa\n"
  4143. " : aaaaaaaaaa;");
  4144. verifyFormat(
  4145. "aaaaaa = aaaaaaaaaaaa\n"
  4146. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4147. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4148. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4149. FormatStyle NoBinPacking = getLLVMStyle();
  4150. NoBinPacking.BinPackArguments = false;
  4151. verifyFormat(
  4152. "void f() {\n"
  4153. " g(aaa,\n"
  4154. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4155. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4156. " ? aaaaaaaaaaaaaaa\n"
  4157. " : aaaaaaaaaaaaaaa);\n"
  4158. "}",
  4159. NoBinPacking);
  4160. verifyFormat(
  4161. "void f() {\n"
  4162. " g(aaa,\n"
  4163. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4164. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4165. " ?: aaaaaaaaaaaaaaa);\n"
  4166. "}",
  4167. NoBinPacking);
  4168. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4169. " // comment.\n"
  4170. " ccccccccccccccccccccccccccccccccccccccc\n"
  4171. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4172. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4173. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4174. verifyFormat("return a != b\n"
  4175. " // comment\n"
  4176. " ? a = b\n"
  4177. " : a = b;");
  4178. verifyFormat("return a != b\n"
  4179. " // comment\n"
  4180. " ? a = a != b\n"
  4181. " // comment\n"
  4182. " ? a = b\n"
  4183. " : a\n"
  4184. " : a;\n");
  4185. verifyFormat("return a != b\n"
  4186. " // comment\n"
  4187. " ? a\n"
  4188. " : a = a != b\n"
  4189. " // comment\n"
  4190. " ? a = b\n"
  4191. " : a;");
  4192. }
  4193. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4194. FormatStyle Style = getLLVMStyle();
  4195. Style.BreakBeforeTernaryOperators = false;
  4196. Style.ColumnLimit = 70;
  4197. verifyFormat(
  4198. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4199. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4200. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4201. Style);
  4202. verifyFormat(
  4203. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4204. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4205. Style);
  4206. verifyFormat(
  4207. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4208. " aaaaaaaaaaaaa);",
  4209. Style);
  4210. verifyFormat(
  4211. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4212. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4214. " aaaaaaaaaaaaa);",
  4215. Style);
  4216. verifyFormat(
  4217. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4218. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4219. " aaaaaaaaaaaaa);",
  4220. Style);
  4221. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4222. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4223. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4224. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4225. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4226. Style);
  4227. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4228. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4229. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4230. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4231. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4232. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4233. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4234. Style);
  4235. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4236. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4237. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4238. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4239. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4240. Style);
  4241. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4242. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4243. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4244. Style);
  4245. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4246. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4247. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4248. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4249. Style);
  4250. verifyFormat(
  4251. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4252. " aaaaaaaaaaaaaaa :\n"
  4253. " aaaaaaaaaaaaaaa;",
  4254. Style);
  4255. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4256. " aaaaaaaaa ?\n"
  4257. " b :\n"
  4258. " c);",
  4259. Style);
  4260. verifyFormat(
  4261. "unsigned Indent =\n"
  4262. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4263. " IndentForLevel[TheLine.Level] :\n"
  4264. " TheLine * 2,\n"
  4265. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4266. Style);
  4267. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4268. " aaaaaaaaaaaaaaa :\n"
  4269. " bbbbbbbbbbbbbbb ? //\n"
  4270. " ccccccccccccccc :\n"
  4271. " ddddddddddddddd;",
  4272. Style);
  4273. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4274. " aaaaaaaaaaaaaaa :\n"
  4275. " (bbbbbbbbbbbbbbb ? //\n"
  4276. " ccccccccccccccc :\n"
  4277. " ddddddddddddddd);",
  4278. Style);
  4279. }
  4280. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4281. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4282. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4283. verifyFormat("bool a = true, b = false;");
  4284. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4285. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4286. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4287. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4288. verifyFormat(
  4289. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4290. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4291. " d = e && f;");
  4292. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4293. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4294. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4295. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4296. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4297. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4298. FormatStyle Style = getGoogleStyle();
  4299. Style.PointerAlignment = FormatStyle::PAS_Left;
  4300. Style.DerivePointerAlignment = false;
  4301. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4302. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4303. " *b = bbbbbbbbbbbbbbbbbbb;",
  4304. Style);
  4305. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4306. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4307. Style);
  4308. }
  4309. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4310. verifyFormat("arr[foo ? bar : baz];");
  4311. verifyFormat("f()[foo ? bar : baz];");
  4312. verifyFormat("(a + b)[foo ? bar : baz];");
  4313. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4314. }
  4315. TEST_F(FormatTest, AlignsStringLiterals) {
  4316. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4317. " \"short literal\");");
  4318. verifyFormat(
  4319. "looooooooooooooooooooooooongFunction(\n"
  4320. " \"short literal\"\n"
  4321. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4322. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4323. " \" string literals\",\n"
  4324. " and, other, parameters);");
  4325. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4326. " \"5678\";",
  4327. format("fun + \"1243\" /* comment */\n"
  4328. " \"5678\";",
  4329. getLLVMStyleWithColumns(28)));
  4330. EXPECT_EQ(
  4331. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4332. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4333. " \"aaaaaaaaaaaaaaaa\";",
  4334. format("aaaaaa ="
  4335. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4336. "aaaaaaaaaaaaaaaaaaaaa\" "
  4337. "\"aaaaaaaaaaaaaaaa\";"));
  4338. verifyFormat("a = a + \"a\"\n"
  4339. " \"a\"\n"
  4340. " \"a\";");
  4341. verifyFormat("f(\"a\", \"b\"\n"
  4342. " \"c\");");
  4343. verifyFormat(
  4344. "#define LL_FORMAT \"ll\"\n"
  4345. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4346. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4347. verifyFormat("#define A(X) \\\n"
  4348. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4349. " \"ccccc\"",
  4350. getLLVMStyleWithColumns(23));
  4351. verifyFormat("#define A \"def\"\n"
  4352. "f(\"abc\" A \"ghi\"\n"
  4353. " \"jkl\");");
  4354. verifyFormat("f(L\"a\"\n"
  4355. " L\"b\");");
  4356. verifyFormat("#define A(X) \\\n"
  4357. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4358. " L\"ccccc\"",
  4359. getLLVMStyleWithColumns(25));
  4360. verifyFormat("f(@\"a\"\n"
  4361. " @\"b\");");
  4362. verifyFormat("NSString s = @\"a\"\n"
  4363. " @\"b\"\n"
  4364. " @\"c\";");
  4365. verifyFormat("NSString s = @\"a\"\n"
  4366. " \"b\"\n"
  4367. " \"c\";");
  4368. }
  4369. TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) {
  4370. FormatStyle Style = getLLVMStyle();
  4371. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel;
  4372. verifyFormat("class C {\n"
  4373. " int f() { return 1; }\n"
  4374. "};\n"
  4375. "int\n"
  4376. "f() {\n"
  4377. " return 1;\n"
  4378. "}",
  4379. Style);
  4380. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  4381. verifyFormat("class C {\n"
  4382. " int\n"
  4383. " f() {\n"
  4384. " return 1;\n"
  4385. " }\n"
  4386. "};\n"
  4387. "int\n"
  4388. "f() {\n"
  4389. " return 1;\n"
  4390. "}",
  4391. Style);
  4392. verifyFormat("const char *\n"
  4393. "f(void) {\n" // Break here.
  4394. " return \"\";\n"
  4395. "}\n"
  4396. "const char *bar(void);\n", // No break here.
  4397. Style);
  4398. verifyFormat("template <class T>\n"
  4399. "T *\n"
  4400. "f(T &c) {\n" // Break here.
  4401. " return NULL;\n"
  4402. "}\n"
  4403. "template <class T> T *f(T &c);\n", // No break here.
  4404. Style);
  4405. verifyFormat("class C {\n"
  4406. " int\n"
  4407. " operator+() {\n"
  4408. " return 1;\n"
  4409. " }\n"
  4410. " int\n"
  4411. " operator()() {\n"
  4412. " return 1;\n"
  4413. " }\n"
  4414. "};\n",
  4415. Style);
  4416. verifyFormat("void\n"
  4417. "A::operator()() {}\n"
  4418. "void\n"
  4419. "A::operator>>() {}\n"
  4420. "void\n"
  4421. "A::operator+() {}\n",
  4422. Style);
  4423. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4424. Style);
  4425. verifyFormat("void *\n"
  4426. "operator new(std::size_t s) {}",
  4427. Style);
  4428. verifyFormat("void *\n"
  4429. "operator delete[](void *ptr) {}",
  4430. Style);
  4431. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4432. verifyFormat("const char *\n"
  4433. "f(void)\n" // Break here.
  4434. "{\n"
  4435. " return \"\";\n"
  4436. "}\n"
  4437. "const char *bar(void);\n", // No break here.
  4438. Style);
  4439. verifyFormat("template <class T>\n"
  4440. "T *\n" // Problem here: no line break
  4441. "f(T &c)\n" // Break here.
  4442. "{\n"
  4443. " return NULL;\n"
  4444. "}\n"
  4445. "template <class T> T *f(T &c);\n", // No break here.
  4446. Style);
  4447. }
  4448. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4449. FormatStyle NoBreak = getLLVMStyle();
  4450. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4451. FormatStyle Break = getLLVMStyle();
  4452. Break.AlwaysBreakBeforeMultilineStrings = true;
  4453. verifyFormat("aaaa = \"bbbb\"\n"
  4454. " \"cccc\";",
  4455. NoBreak);
  4456. verifyFormat("aaaa =\n"
  4457. " \"bbbb\"\n"
  4458. " \"cccc\";",
  4459. Break);
  4460. verifyFormat("aaaa(\"bbbb\"\n"
  4461. " \"cccc\");",
  4462. NoBreak);
  4463. verifyFormat("aaaa(\n"
  4464. " \"bbbb\"\n"
  4465. " \"cccc\");",
  4466. Break);
  4467. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4468. " \"cccc\");",
  4469. NoBreak);
  4470. verifyFormat("aaaa(qqq,\n"
  4471. " \"bbbb\"\n"
  4472. " \"cccc\");",
  4473. Break);
  4474. verifyFormat("aaaa(qqq,\n"
  4475. " L\"bbbb\"\n"
  4476. " L\"cccc\");",
  4477. Break);
  4478. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4479. " \"bbbb\"));",
  4480. Break);
  4481. verifyFormat("string s = someFunction(\n"
  4482. " \"abc\"\n"
  4483. " \"abc\");",
  4484. Break);
  4485. // As we break before unary operators, breaking right after them is bad.
  4486. verifyFormat("string foo = abc ? \"x\"\n"
  4487. " \"blah blah blah blah blah blah\"\n"
  4488. " : \"y\";",
  4489. Break);
  4490. // Don't break if there is no column gain.
  4491. verifyFormat("f(\"aaaa\"\n"
  4492. " \"bbbb\");",
  4493. Break);
  4494. // Treat literals with escaped newlines like multi-line string literals.
  4495. EXPECT_EQ("x = \"a\\\n"
  4496. "b\\\n"
  4497. "c\";",
  4498. format("x = \"a\\\n"
  4499. "b\\\n"
  4500. "c\";",
  4501. NoBreak));
  4502. EXPECT_EQ("xxxx =\n"
  4503. " \"a\\\n"
  4504. "b\\\n"
  4505. "c\";",
  4506. format("xxxx = \"a\\\n"
  4507. "b\\\n"
  4508. "c\";",
  4509. Break));
  4510. // Exempt ObjC strings for now.
  4511. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4512. " @\"bbbb\";",
  4513. format("NSString *const kString = @\"aaaa\"\n"
  4514. "@\"bbbb\";",
  4515. Break));
  4516. Break.ColumnLimit = 0;
  4517. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4518. }
  4519. TEST_F(FormatTest, AlignsPipes) {
  4520. verifyFormat(
  4521. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4522. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4523. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4524. verifyFormat(
  4525. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4526. " << aaaaaaaaaaaaaaaaaaaa;");
  4527. verifyFormat(
  4528. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4529. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4530. verifyFormat(
  4531. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4532. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4533. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4534. verifyFormat(
  4535. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4536. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4537. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4538. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4539. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4540. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4541. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4542. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4543. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4544. verifyFormat(
  4545. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4546. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4547. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4548. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4549. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4550. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4551. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4552. " << \"}\";");
  4553. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4554. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4555. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4556. verifyFormat(
  4557. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4558. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4559. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4560. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4561. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4562. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4563. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4564. verifyFormat(
  4565. "void f() {\n"
  4566. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4567. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4568. "}");
  4569. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4570. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4571. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4572. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4573. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4574. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4575. verifyFormat("LOG_IF(aaa == //\n"
  4576. " bbb)\n"
  4577. " << a << b;");
  4578. // Breaking before the first "<<" is generally not desirable.
  4579. verifyFormat(
  4580. "llvm::errs()\n"
  4581. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4582. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4583. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4584. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4585. getLLVMStyleWithColumns(70));
  4586. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4587. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4588. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4589. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4590. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4591. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4592. getLLVMStyleWithColumns(70));
  4593. // But sometimes, breaking before the first "<<" is desirable.
  4594. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4595. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4596. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4597. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4598. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4599. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4600. " << BEF << IsTemplate << Description << E->getType();");
  4601. verifyFormat(
  4602. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4603. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4604. // Incomplete string literal.
  4605. EXPECT_EQ("llvm::errs() << \"\n"
  4606. " << a;",
  4607. format("llvm::errs() << \"\n<<a;"));
  4608. verifyFormat("void f() {\n"
  4609. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4610. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4611. "}");
  4612. // Handle 'endl'.
  4613. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4614. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4615. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4616. }
  4617. TEST_F(FormatTest, UnderstandsEquals) {
  4618. verifyFormat(
  4619. "aaaaaaaaaaaaaaaaa =\n"
  4620. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4621. verifyFormat(
  4622. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4623. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4624. verifyFormat(
  4625. "if (a) {\n"
  4626. " f();\n"
  4627. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4628. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4629. "}");
  4630. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4631. " 100000000 + 10000000) {\n}");
  4632. }
  4633. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4634. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4635. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4636. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4637. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4638. verifyFormat(
  4639. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4640. " Parameter2);");
  4641. verifyFormat(
  4642. "ShortObject->shortFunction(\n"
  4643. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4644. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4645. verifyFormat("loooooooooooooongFunction(\n"
  4646. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4647. verifyFormat(
  4648. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4649. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4650. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4651. " .WillRepeatedly(Return(SomeValue));");
  4652. verifyFormat("void f() {\n"
  4653. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4654. " .Times(2)\n"
  4655. " .WillRepeatedly(Return(SomeValue));\n"
  4656. "}");
  4657. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4658. " ccccccccccccccccccccccc);");
  4659. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4660. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4661. " .aaaaa(aaaaa),\n"
  4662. " aaaaaaaaaaaaaaaaaaaaa);");
  4663. verifyFormat("void f() {\n"
  4664. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4665. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4666. "}");
  4667. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4668. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4669. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4670. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4671. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4672. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4673. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4674. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4675. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4676. "}");
  4677. // Here, it is not necessary to wrap at "." or "->".
  4678. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4679. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4680. verifyFormat(
  4681. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4682. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4683. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4684. verifyFormat(
  4685. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4686. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4687. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4688. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4689. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4690. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4691. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4692. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4693. " .a();");
  4694. FormatStyle NoBinPacking = getLLVMStyle();
  4695. NoBinPacking.BinPackParameters = false;
  4696. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4697. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4698. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4699. " aaaaaaaaaaaaaaaaaaa,\n"
  4700. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4701. NoBinPacking);
  4702. // If there is a subsequent call, change to hanging indentation.
  4703. verifyFormat(
  4704. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4705. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4706. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4707. verifyFormat(
  4708. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4709. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4710. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4711. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4712. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4713. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4714. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4715. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4716. }
  4717. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4718. verifyFormat("template <typename T>\n"
  4719. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4720. verifyFormat("template <typename T>\n"
  4721. "// T should be one of {A, B}.\n"
  4722. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4723. verifyFormat(
  4724. "template <typename T>\n"
  4725. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4726. verifyFormat("template <typename T>\n"
  4727. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4728. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4729. verifyFormat(
  4730. "template <typename T>\n"
  4731. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4732. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4733. verifyFormat(
  4734. "template <typename T>\n"
  4735. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4736. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4737. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4738. verifyFormat("template <typename T>\n"
  4739. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4740. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4741. verifyFormat(
  4742. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4743. " typename T4 = char>\n"
  4744. "void f();");
  4745. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4746. " template <typename> class cccccccccccccccccccccc,\n"
  4747. " typename ddddddddddddd>\n"
  4748. "class C {};");
  4749. verifyFormat(
  4750. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4751. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4752. verifyFormat("void f() {\n"
  4753. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4754. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4755. "}");
  4756. verifyFormat("template <typename T> class C {};");
  4757. verifyFormat("template <typename T> void f();");
  4758. verifyFormat("template <typename T> void f() {}");
  4759. verifyFormat(
  4760. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4761. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4762. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4763. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4764. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4765. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4766. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4767. getLLVMStyleWithColumns(72));
  4768. EXPECT_EQ("static_cast<A< //\n"
  4769. " B> *>(\n"
  4770. "\n"
  4771. " );",
  4772. format("static_cast<A<//\n"
  4773. " B>*>(\n"
  4774. "\n"
  4775. " );"));
  4776. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4777. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4778. FormatStyle AlwaysBreak = getLLVMStyle();
  4779. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4780. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4781. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4782. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4783. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4784. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4785. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4786. verifyFormat("template <template <typename> class Fooooooo,\n"
  4787. " template <typename> class Baaaaaaar>\n"
  4788. "struct C {};",
  4789. AlwaysBreak);
  4790. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4791. "struct C {};",
  4792. AlwaysBreak);
  4793. }
  4794. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4795. verifyFormat(
  4796. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4797. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4798. verifyFormat(
  4799. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4800. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4801. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4802. // FIXME: Should we have the extra indent after the second break?
  4803. verifyFormat(
  4804. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4805. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4806. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4807. verifyFormat(
  4808. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4809. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4810. // Breaking at nested name specifiers is generally not desirable.
  4811. verifyFormat(
  4812. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4813. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4814. verifyFormat(
  4815. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4816. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4817. " aaaaaaaaaaaaaaaaaaaaa);",
  4818. getLLVMStyleWithColumns(74));
  4819. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4820. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4821. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4822. }
  4823. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4824. verifyFormat("A<int> a;");
  4825. verifyFormat("A<A<A<int>>> a;");
  4826. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4827. verifyFormat("bool x = a < 1 || 2 > a;");
  4828. verifyFormat("bool x = 5 < f<int>();");
  4829. verifyFormat("bool x = f<int>() > 5;");
  4830. verifyFormat("bool x = 5 < a<int>::x;");
  4831. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4832. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4833. verifyGoogleFormat("A<A<int>> a;");
  4834. verifyGoogleFormat("A<A<A<int>>> a;");
  4835. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4836. verifyGoogleFormat("A<A<int> > a;");
  4837. verifyGoogleFormat("A<A<A<int> > > a;");
  4838. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4839. verifyGoogleFormat("A<::A<int>> a;");
  4840. verifyGoogleFormat("A<::A> a;");
  4841. verifyGoogleFormat("A< ::A> a;");
  4842. verifyGoogleFormat("A< ::A<int> > a;");
  4843. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4844. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4845. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4846. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4847. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  4848. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  4849. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4850. verifyFormat("test >> a >> b;");
  4851. verifyFormat("test << a >> b;");
  4852. verifyFormat("f<int>();");
  4853. verifyFormat("template <typename T> void f() {}");
  4854. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4855. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  4856. "sizeof(char)>::type>;");
  4857. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  4858. // Not template parameters.
  4859. verifyFormat("return a < b && c > d;");
  4860. verifyFormat("void f() {\n"
  4861. " while (a < b && c > d) {\n"
  4862. " }\n"
  4863. "}");
  4864. verifyFormat("template <typename... Types>\n"
  4865. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4866. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4868. getLLVMStyleWithColumns(60));
  4869. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4870. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4871. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  4872. }
  4873. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4874. verifyFormat("COMPARE(a, ==, b);");
  4875. }
  4876. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4877. verifyFormat("int A::*x;");
  4878. verifyFormat("int (S::*func)(void *);");
  4879. verifyFormat("void f() { int (S::*func)(void *); }");
  4880. verifyFormat("typedef bool *(Class::*Member)() const;");
  4881. verifyFormat("void f() {\n"
  4882. " (a->*f)();\n"
  4883. " a->*x;\n"
  4884. " (a.*f)();\n"
  4885. " ((*a).*f)();\n"
  4886. " a.*x;\n"
  4887. "}");
  4888. verifyFormat("void f() {\n"
  4889. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4890. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4891. "}");
  4892. verifyFormat(
  4893. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4894. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4895. FormatStyle Style = getLLVMStyle();
  4896. Style.PointerAlignment = FormatStyle::PAS_Left;
  4897. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4898. }
  4899. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4900. verifyFormat("int a = -2;");
  4901. verifyFormat("f(-1, -2, -3);");
  4902. verifyFormat("a[-1] = 5;");
  4903. verifyFormat("int a = 5 + -2;");
  4904. verifyFormat("if (i == -1) {\n}");
  4905. verifyFormat("if (i != -1) {\n}");
  4906. verifyFormat("if (i > -1) {\n}");
  4907. verifyFormat("if (i < -1) {\n}");
  4908. verifyFormat("++(a->f());");
  4909. verifyFormat("--(a->f());");
  4910. verifyFormat("(a->f())++;");
  4911. verifyFormat("a[42]++;");
  4912. verifyFormat("if (!(a->f())) {\n}");
  4913. verifyFormat("a-- > b;");
  4914. verifyFormat("b ? -a : c;");
  4915. verifyFormat("n * sizeof char16;");
  4916. verifyFormat("n * alignof char16;", getGoogleStyle());
  4917. verifyFormat("sizeof(char);");
  4918. verifyFormat("alignof(char);", getGoogleStyle());
  4919. verifyFormat("return -1;");
  4920. verifyFormat("switch (a) {\n"
  4921. "case -1:\n"
  4922. " break;\n"
  4923. "}");
  4924. verifyFormat("#define X -1");
  4925. verifyFormat("#define X -kConstant");
  4926. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4927. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4928. verifyFormat("int a = /* confusing comment */ -1;");
  4929. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4930. verifyFormat("int a = i /* confusing comment */++;");
  4931. }
  4932. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4933. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4934. " aaaaa)) {\n"
  4935. "}");
  4936. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4937. " aaaaa));");
  4938. verifyFormat("*aaa = aaaaaaa( // break\n"
  4939. " bbbbbb);");
  4940. }
  4941. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4942. verifyFormat("bool operator<();");
  4943. verifyFormat("bool operator>();");
  4944. verifyFormat("bool operator=();");
  4945. verifyFormat("bool operator==();");
  4946. verifyFormat("bool operator!=();");
  4947. verifyFormat("int operator+();");
  4948. verifyFormat("int operator++();");
  4949. verifyFormat("bool operator();");
  4950. verifyFormat("bool operator()();");
  4951. verifyFormat("bool operator[]();");
  4952. verifyFormat("operator bool();");
  4953. verifyFormat("operator int();");
  4954. verifyFormat("operator void *();");
  4955. verifyFormat("operator SomeType<int>();");
  4956. verifyFormat("operator SomeType<int, int>();");
  4957. verifyFormat("operator SomeType<SomeType<int>>();");
  4958. verifyFormat("void *operator new(std::size_t size);");
  4959. verifyFormat("void *operator new[](std::size_t size);");
  4960. verifyFormat("void operator delete(void *ptr);");
  4961. verifyFormat("void operator delete[](void *ptr);");
  4962. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4963. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4964. verifyFormat(
  4965. "ostream &operator<<(ostream &OutputStream,\n"
  4966. " SomeReallyLongType WithSomeReallyLongValue);");
  4967. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4968. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4969. " return left.group < right.group;\n"
  4970. "}");
  4971. verifyFormat("SomeType &operator=(const SomeType &S);");
  4972. verifyFormat("f.template operator()<int>();");
  4973. verifyGoogleFormat("operator void*();");
  4974. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4975. verifyGoogleFormat("operator ::A();");
  4976. verifyFormat("using A::operator+;");
  4977. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  4978. "int i;");
  4979. }
  4980. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  4981. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  4982. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  4983. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  4984. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  4985. verifyFormat("Deleted &operator=(const Deleted &) &;");
  4986. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  4987. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  4988. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  4989. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  4990. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  4991. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  4992. FormatStyle AlignLeft = getLLVMStyle();
  4993. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  4994. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  4995. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  4996. AlignLeft);
  4997. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  4998. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  4999. FormatStyle Spaces = getLLVMStyle();
  5000. Spaces.SpacesInCStyleCastParentheses = true;
  5001. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5002. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5003. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5004. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5005. Spaces.SpacesInCStyleCastParentheses = false;
  5006. Spaces.SpacesInParentheses = true;
  5007. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5008. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5009. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5010. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5011. }
  5012. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5013. verifyFormat("void f() {\n"
  5014. " A *a = new A;\n"
  5015. " A *a = new (placement) A;\n"
  5016. " delete a;\n"
  5017. " delete (A *)a;\n"
  5018. "}");
  5019. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5020. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5021. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5022. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5023. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5024. verifyFormat("delete[] h->p;");
  5025. }
  5026. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5027. verifyFormat("int *f(int *a) {}");
  5028. verifyFormat("int main(int argc, char **argv) {}");
  5029. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5030. verifyIndependentOfContext("f(a, *a);");
  5031. verifyFormat("void g() { f(*a); }");
  5032. verifyIndependentOfContext("int a = b * 10;");
  5033. verifyIndependentOfContext("int a = 10 * b;");
  5034. verifyIndependentOfContext("int a = b * c;");
  5035. verifyIndependentOfContext("int a += b * c;");
  5036. verifyIndependentOfContext("int a -= b * c;");
  5037. verifyIndependentOfContext("int a *= b * c;");
  5038. verifyIndependentOfContext("int a /= b * c;");
  5039. verifyIndependentOfContext("int a = *b;");
  5040. verifyIndependentOfContext("int a = *b * c;");
  5041. verifyIndependentOfContext("int a = b * *c;");
  5042. verifyIndependentOfContext("int a = b * (10);");
  5043. verifyIndependentOfContext("S << b * (10);");
  5044. verifyIndependentOfContext("return 10 * b;");
  5045. verifyIndependentOfContext("return *b * *c;");
  5046. verifyIndependentOfContext("return a & ~b;");
  5047. verifyIndependentOfContext("f(b ? *c : *d);");
  5048. verifyIndependentOfContext("int a = b ? *c : *d;");
  5049. verifyIndependentOfContext("*b = a;");
  5050. verifyIndependentOfContext("a * ~b;");
  5051. verifyIndependentOfContext("a * !b;");
  5052. verifyIndependentOfContext("a * +b;");
  5053. verifyIndependentOfContext("a * -b;");
  5054. verifyIndependentOfContext("a * ++b;");
  5055. verifyIndependentOfContext("a * --b;");
  5056. verifyIndependentOfContext("a[4] * b;");
  5057. verifyIndependentOfContext("a[a * a] = 1;");
  5058. verifyIndependentOfContext("f() * b;");
  5059. verifyIndependentOfContext("a * [self dostuff];");
  5060. verifyIndependentOfContext("int x = a * (a + b);");
  5061. verifyIndependentOfContext("(a *)(a + b);");
  5062. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5063. verifyIndependentOfContext("int *pa = (int *)&a;");
  5064. verifyIndependentOfContext("return sizeof(int **);");
  5065. verifyIndependentOfContext("return sizeof(int ******);");
  5066. verifyIndependentOfContext("return (int **&)a;");
  5067. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5068. verifyFormat("void f(Type (*parameter)[10]) {}");
  5069. verifyFormat("void f(Type (&parameter)[10]) {}");
  5070. verifyGoogleFormat("return sizeof(int**);");
  5071. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5072. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5073. verifyFormat("auto a = [](int **&, int ***) {};");
  5074. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5075. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5076. verifyFormat("[](const decltype(*a) &value) {}");
  5077. verifyFormat("decltype(a * b) F();");
  5078. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5079. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5080. verifyIndependentOfContext("int i{a * b};");
  5081. verifyIndependentOfContext("aaa && aaa->f();");
  5082. verifyIndependentOfContext("int x = ~*p;");
  5083. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5084. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5085. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5086. verifyFormat("void f() { f(a, c * d); }");
  5087. verifyFormat("void f() { f(new a(), c * d); }");
  5088. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5089. verifyIndependentOfContext("A<int *> a;");
  5090. verifyIndependentOfContext("A<int **> a;");
  5091. verifyIndependentOfContext("A<int *, int *> a;");
  5092. verifyIndependentOfContext("A<int *[]> a;");
  5093. verifyIndependentOfContext(
  5094. "const char *const p = reinterpret_cast<const char *const>(q);");
  5095. verifyIndependentOfContext("A<int **, int **> a;");
  5096. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5097. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5098. verifyFormat("for (; a && b;) {\n}");
  5099. verifyFormat("bool foo = true && [] { return false; }();");
  5100. verifyFormat(
  5101. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5102. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5103. verifyGoogleFormat("**outparam = 1;");
  5104. verifyGoogleFormat("*outparam = a * b;");
  5105. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5106. verifyGoogleFormat("A<int*> a;");
  5107. verifyGoogleFormat("A<int**> a;");
  5108. verifyGoogleFormat("A<int*, int*> a;");
  5109. verifyGoogleFormat("A<int**, int**> a;");
  5110. verifyGoogleFormat("f(b ? *c : *d);");
  5111. verifyGoogleFormat("int a = b ? *c : *d;");
  5112. verifyGoogleFormat("Type* t = **x;");
  5113. verifyGoogleFormat("Type* t = *++*x;");
  5114. verifyGoogleFormat("*++*x;");
  5115. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5116. verifyGoogleFormat("Type* t = x++ * y;");
  5117. verifyGoogleFormat(
  5118. "const char* const p = reinterpret_cast<const char* const>(q);");
  5119. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5120. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5121. verifyGoogleFormat("template <typename T>\n"
  5122. "void f(int i = 0, SomeType** temps = NULL);");
  5123. FormatStyle Left = getLLVMStyle();
  5124. Left.PointerAlignment = FormatStyle::PAS_Left;
  5125. verifyFormat("x = *a(x) = *a(y);", Left);
  5126. verifyFormat("for (;; * = b) {\n}", Left);
  5127. verifyIndependentOfContext("a = *(x + y);");
  5128. verifyIndependentOfContext("a = &(x + y);");
  5129. verifyIndependentOfContext("*(x + y).call();");
  5130. verifyIndependentOfContext("&(x + y)->call();");
  5131. verifyFormat("void f() { &(*I).first; }");
  5132. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5133. verifyFormat(
  5134. "int *MyValues = {\n"
  5135. " *A, // Operator detection might be confused by the '{'\n"
  5136. " *BB // Operator detection might be confused by previous comment\n"
  5137. "};");
  5138. verifyIndependentOfContext("if (int *a = &b)");
  5139. verifyIndependentOfContext("if (int &a = *b)");
  5140. verifyIndependentOfContext("if (a & b[i])");
  5141. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5142. verifyIndependentOfContext("if (*b[i])");
  5143. verifyIndependentOfContext("if (int *a = (&b))");
  5144. verifyIndependentOfContext("while (int *a = &b)");
  5145. verifyIndependentOfContext("size = sizeof *a;");
  5146. verifyIndependentOfContext("if (a && (b = c))");
  5147. verifyFormat("void f() {\n"
  5148. " for (const int &v : Values) {\n"
  5149. " }\n"
  5150. "}");
  5151. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5152. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5153. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5154. verifyFormat("#define A (!a * b)");
  5155. verifyFormat("#define MACRO \\\n"
  5156. " int *i = a * b; \\\n"
  5157. " void f(a *b);",
  5158. getLLVMStyleWithColumns(19));
  5159. verifyIndependentOfContext("A = new SomeType *[Length];");
  5160. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5161. verifyIndependentOfContext("T **t = new T *;");
  5162. verifyIndependentOfContext("T **t = new T *();");
  5163. verifyGoogleFormat("A = new SomeType*[Length]();");
  5164. verifyGoogleFormat("A = new SomeType*[Length];");
  5165. verifyGoogleFormat("T** t = new T*;");
  5166. verifyGoogleFormat("T** t = new T*();");
  5167. FormatStyle PointerLeft = getLLVMStyle();
  5168. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5169. verifyFormat("delete *x;", PointerLeft);
  5170. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5171. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5172. verifyFormat("template <bool a, bool b> "
  5173. "typename t::if<x && y>::type f() {}");
  5174. verifyFormat("template <int *y> f() {}");
  5175. verifyFormat("vector<int *> v;");
  5176. verifyFormat("vector<int *const> v;");
  5177. verifyFormat("vector<int *const **const *> v;");
  5178. verifyFormat("vector<int *volatile> v;");
  5179. verifyFormat("vector<a * b> v;");
  5180. verifyFormat("foo<b && false>();");
  5181. verifyFormat("foo<b & 1>();");
  5182. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5183. verifyFormat(
  5184. "template <class T, class = typename std::enable_if<\n"
  5185. " std::is_integral<T>::value &&\n"
  5186. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5187. "void F();",
  5188. getLLVMStyleWithColumns(76));
  5189. verifyFormat(
  5190. "template <class T,\n"
  5191. " class = typename ::std::enable_if<\n"
  5192. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5193. "void F();",
  5194. getGoogleStyleWithColumns(68));
  5195. verifyIndependentOfContext("MACRO(int *i);");
  5196. verifyIndependentOfContext("MACRO(auto *a);");
  5197. verifyIndependentOfContext("MACRO(const A *a);");
  5198. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5199. // FIXME: Is there a way to make this work?
  5200. // verifyIndependentOfContext("MACRO(A *a);");
  5201. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5202. verifyFormat("return options != nullptr && operator==(*options);");
  5203. EXPECT_EQ("#define OP(x) \\\n"
  5204. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5205. " return s << a.DebugString(); \\\n"
  5206. " }",
  5207. format("#define OP(x) \\\n"
  5208. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5209. " return s << a.DebugString(); \\\n"
  5210. " }",
  5211. getLLVMStyleWithColumns(50)));
  5212. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5213. // foo<x> d > v; doesn't make sense.
  5214. verifyFormat("foo<a<b && c> d> v;");
  5215. FormatStyle PointerMiddle = getLLVMStyle();
  5216. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5217. verifyFormat("delete *x;", PointerMiddle);
  5218. verifyFormat("int * x;", PointerMiddle);
  5219. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5220. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5221. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5222. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5223. verifyFormat("A<int *> a;", PointerMiddle);
  5224. verifyFormat("A<int **> a;", PointerMiddle);
  5225. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5226. verifyFormat("A<int * []> a;", PointerMiddle);
  5227. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5228. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5229. verifyFormat("T ** t = new T *;", PointerMiddle);
  5230. // Member function reference qualifiers aren't binary operators.
  5231. verifyFormat("string // break\n"
  5232. "operator()() & {}");
  5233. verifyFormat("string // break\n"
  5234. "operator()() && {}");
  5235. verifyGoogleFormat("template <typename T>\n"
  5236. "auto x() & -> int {}");
  5237. }
  5238. TEST_F(FormatTest, UnderstandsAttributes) {
  5239. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5240. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5241. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5242. FormatStyle AfterType = getLLVMStyle();
  5243. AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  5244. verifyFormat("__attribute__((nodebug)) void\n"
  5245. "foo() {}\n",
  5246. AfterType);
  5247. }
  5248. TEST_F(FormatTest, UnderstandsEllipsis) {
  5249. verifyFormat("int printf(const char *fmt, ...);");
  5250. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5251. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5252. FormatStyle PointersLeft = getLLVMStyle();
  5253. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5254. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5255. }
  5256. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5257. EXPECT_EQ("int *a;\n"
  5258. "int *a;\n"
  5259. "int *a;",
  5260. format("int *a;\n"
  5261. "int* a;\n"
  5262. "int *a;",
  5263. getGoogleStyle()));
  5264. EXPECT_EQ("int* a;\n"
  5265. "int* a;\n"
  5266. "int* a;",
  5267. format("int* a;\n"
  5268. "int* a;\n"
  5269. "int *a;",
  5270. getGoogleStyle()));
  5271. EXPECT_EQ("int *a;\n"
  5272. "int *a;\n"
  5273. "int *a;",
  5274. format("int *a;\n"
  5275. "int * a;\n"
  5276. "int * a;",
  5277. getGoogleStyle()));
  5278. EXPECT_EQ("auto x = [] {\n"
  5279. " int *a;\n"
  5280. " int *a;\n"
  5281. " int *a;\n"
  5282. "};",
  5283. format("auto x=[]{int *a;\n"
  5284. "int * a;\n"
  5285. "int * a;};",
  5286. getGoogleStyle()));
  5287. }
  5288. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5289. verifyFormat("int f(int &&a) {}");
  5290. verifyFormat("int f(int a, char &&b) {}");
  5291. verifyFormat("void f() { int &&a = b; }");
  5292. verifyGoogleFormat("int f(int a, char&& b) {}");
  5293. verifyGoogleFormat("void f() { int&& a = b; }");
  5294. verifyIndependentOfContext("A<int &&> a;");
  5295. verifyIndependentOfContext("A<int &&, int &&> a;");
  5296. verifyGoogleFormat("A<int&&> a;");
  5297. verifyGoogleFormat("A<int&&, int&&> a;");
  5298. // Not rvalue references:
  5299. verifyFormat("template <bool B, bool C> class A {\n"
  5300. " static_assert(B && C, \"Something is wrong\");\n"
  5301. "};");
  5302. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5303. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5304. verifyFormat("#define A(a, b) (a && b)");
  5305. }
  5306. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5307. verifyFormat("void f() {\n"
  5308. " x[aaaaaaaaa -\n"
  5309. " b] = 23;\n"
  5310. "}",
  5311. getLLVMStyleWithColumns(15));
  5312. }
  5313. TEST_F(FormatTest, FormatsCasts) {
  5314. verifyFormat("Type *A = static_cast<Type *>(P);");
  5315. verifyFormat("Type *A = (Type *)P;");
  5316. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5317. verifyFormat("int a = (int)(2.0f);");
  5318. verifyFormat("int a = (int)2.0f;");
  5319. verifyFormat("x[(int32)y];");
  5320. verifyFormat("x = (int32)y;");
  5321. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5322. verifyFormat("int a = (int)*b;");
  5323. verifyFormat("int a = (int)2.0f;");
  5324. verifyFormat("int a = (int)~0;");
  5325. verifyFormat("int a = (int)++a;");
  5326. verifyFormat("int a = (int)sizeof(int);");
  5327. verifyFormat("int a = (int)+2;");
  5328. verifyFormat("my_int a = (my_int)2.0f;");
  5329. verifyFormat("my_int a = (my_int)sizeof(int);");
  5330. verifyFormat("return (my_int)aaa;");
  5331. verifyFormat("#define x ((int)-1)");
  5332. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5333. verifyFormat("#define p(q) ((int *)&q)");
  5334. verifyFormat("fn(a)(b) + 1;");
  5335. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5336. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5337. verifyFormat("my_int a = (my_int)~0;");
  5338. verifyFormat("my_int a = (my_int)++a;");
  5339. verifyFormat("my_int a = (my_int)-2;");
  5340. verifyFormat("my_int a = (my_int)1;");
  5341. verifyFormat("my_int a = (my_int *)1;");
  5342. verifyFormat("my_int a = (const my_int)-1;");
  5343. verifyFormat("my_int a = (const my_int *)-1;");
  5344. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5345. verifyFormat("my_int a = (ns::my_int)-2;");
  5346. verifyFormat("case (my_int)ONE:");
  5347. // FIXME: single value wrapped with paren will be treated as cast.
  5348. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5349. verifyFormat("{ (void)F; }");
  5350. // Don't break after a cast's
  5351. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5352. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5353. " bbbbbbbbbbbbbbbbbbbbbb);");
  5354. // These are not casts.
  5355. verifyFormat("void f(int *) {}");
  5356. verifyFormat("f(foo)->b;");
  5357. verifyFormat("f(foo).b;");
  5358. verifyFormat("f(foo)(b);");
  5359. verifyFormat("f(foo)[b];");
  5360. verifyFormat("[](foo) { return 4; }(bar);");
  5361. verifyFormat("(*funptr)(foo)[4];");
  5362. verifyFormat("funptrs[4](foo)[4];");
  5363. verifyFormat("void f(int *);");
  5364. verifyFormat("void f(int *) = 0;");
  5365. verifyFormat("void f(SmallVector<int>) {}");
  5366. verifyFormat("void f(SmallVector<int>);");
  5367. verifyFormat("void f(SmallVector<int>) = 0;");
  5368. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5369. verifyFormat("int a = sizeof(int) * b;");
  5370. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5371. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5372. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5373. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5374. // These are not casts, but at some point were confused with casts.
  5375. verifyFormat("virtual void foo(int *) override;");
  5376. verifyFormat("virtual void foo(char &) const;");
  5377. verifyFormat("virtual void foo(int *a, char *) const;");
  5378. verifyFormat("int a = sizeof(int *) + b;");
  5379. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5380. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5381. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5382. // FIXME: The indentation here is not ideal.
  5383. verifyFormat(
  5384. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5385. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5386. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5387. }
  5388. TEST_F(FormatTest, FormatsFunctionTypes) {
  5389. verifyFormat("A<bool()> a;");
  5390. verifyFormat("A<SomeType()> a;");
  5391. verifyFormat("A<void (*)(int, std::string)> a;");
  5392. verifyFormat("A<void *(int)>;");
  5393. verifyFormat("void *(*a)(int *, SomeType *);");
  5394. verifyFormat("int (*func)(void *);");
  5395. verifyFormat("void f() { int (*func)(void *); }");
  5396. verifyFormat("template <class CallbackClass>\n"
  5397. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5398. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5399. verifyGoogleFormat("void* (*a)(int);");
  5400. verifyGoogleFormat(
  5401. "template <class CallbackClass>\n"
  5402. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5403. // Other constructs can look somewhat like function types:
  5404. verifyFormat("A<sizeof(*x)> a;");
  5405. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5406. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5407. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5408. }
  5409. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5410. verifyFormat("A (*foo_)[6];");
  5411. verifyFormat("vector<int> (*foo_)[6];");
  5412. }
  5413. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5414. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5415. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5416. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5417. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5418. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5419. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5420. // Different ways of ()-initializiation.
  5421. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5422. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5423. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5424. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5425. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5426. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5427. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5428. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5429. }
  5430. TEST_F(FormatTest, BreaksLongDeclarations) {
  5431. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5432. " AnotherNameForTheLongType;");
  5433. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5434. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5435. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5436. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5437. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5438. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5439. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5440. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5441. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5442. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5443. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5444. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5445. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5446. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5447. FormatStyle Indented = getLLVMStyle();
  5448. Indented.IndentWrappedFunctionNames = true;
  5449. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5450. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5451. Indented);
  5452. verifyFormat(
  5453. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5454. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5455. Indented);
  5456. verifyFormat(
  5457. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5458. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5459. Indented);
  5460. verifyFormat(
  5461. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5462. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5463. Indented);
  5464. // FIXME: Without the comment, this breaks after "(".
  5465. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5466. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5467. getGoogleStyle());
  5468. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5469. " int LoooooooooooooooooooongParam2) {}");
  5470. verifyFormat(
  5471. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5472. " SourceLocation L, IdentifierIn *II,\n"
  5473. " Type *T) {}");
  5474. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5475. "ReallyReaaallyLongFunctionName(\n"
  5476. " const std::string &SomeParameter,\n"
  5477. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5478. " &ReallyReallyLongParameterName,\n"
  5479. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5480. " &AnotherLongParameterName) {}");
  5481. verifyFormat("template <typename A>\n"
  5482. "SomeLoooooooooooooooooooooongType<\n"
  5483. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5484. "Function() {}");
  5485. verifyGoogleFormat(
  5486. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5487. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5488. verifyGoogleFormat(
  5489. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5490. " SourceLocation L) {}");
  5491. verifyGoogleFormat(
  5492. "some_namespace::LongReturnType\n"
  5493. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5494. " int first_long_parameter, int second_parameter) {}");
  5495. verifyGoogleFormat("template <typename T>\n"
  5496. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5497. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5498. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5499. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5500. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5501. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5502. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5503. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5504. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5505. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5506. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5507. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5508. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5509. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5510. }
  5511. TEST_F(FormatTest, FormatsArrays) {
  5512. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5513. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5514. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5515. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5516. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5517. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5518. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5519. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5520. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5521. verifyFormat(
  5522. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5523. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5524. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5525. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5526. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5527. verifyFormat(
  5528. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5529. " .aaaaaaa[0]\n"
  5530. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5531. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5532. }
  5533. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5534. verifyFormat("(a)->b();");
  5535. verifyFormat("--a;");
  5536. }
  5537. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5538. verifyFormat("#include <string>\n"
  5539. "#include <a/b/c.h>\n"
  5540. "#include \"a/b/string\"\n"
  5541. "#include \"string.h\"\n"
  5542. "#include \"string.h\"\n"
  5543. "#include <a-a>\n"
  5544. "#include < path with space >\n"
  5545. "#include_next <test.h>"
  5546. "#include \"abc.h\" // this is included for ABC\n"
  5547. "#include \"some long include\" // with a comment\n"
  5548. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5549. getLLVMStyleWithColumns(35));
  5550. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5551. EXPECT_EQ("#include <a>", format("#include<a>"));
  5552. verifyFormat("#import <string>");
  5553. verifyFormat("#import <a/b/c.h>");
  5554. verifyFormat("#import \"a/b/string\"");
  5555. verifyFormat("#import \"string.h\"");
  5556. verifyFormat("#import \"string.h\"");
  5557. verifyFormat("#if __has_include(<strstream>)\n"
  5558. "#include <strstream>\n"
  5559. "#endif");
  5560. verifyFormat("#define MY_IMPORT <a/b>");
  5561. // Protocol buffer definition or missing "#".
  5562. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5563. getLLVMStyleWithColumns(30));
  5564. FormatStyle Style = getLLVMStyle();
  5565. Style.AlwaysBreakBeforeMultilineStrings = true;
  5566. Style.ColumnLimit = 0;
  5567. verifyFormat("#import \"abc.h\"", Style);
  5568. // But 'import' might also be a regular C++ namespace.
  5569. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5570. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5571. }
  5572. //===----------------------------------------------------------------------===//
  5573. // Error recovery tests.
  5574. //===----------------------------------------------------------------------===//
  5575. TEST_F(FormatTest, IncompleteParameterLists) {
  5576. FormatStyle NoBinPacking = getLLVMStyle();
  5577. NoBinPacking.BinPackParameters = false;
  5578. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5579. " double *min_x,\n"
  5580. " double *max_x,\n"
  5581. " double *min_y,\n"
  5582. " double *max_y,\n"
  5583. " double *min_z,\n"
  5584. " double *max_z, ) {}",
  5585. NoBinPacking);
  5586. }
  5587. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5588. verifyFormat("void f() { return; }\n42");
  5589. verifyFormat("void f() {\n"
  5590. " if (0)\n"
  5591. " return;\n"
  5592. "}\n"
  5593. "42");
  5594. verifyFormat("void f() { return }\n42");
  5595. verifyFormat("void f() {\n"
  5596. " if (0)\n"
  5597. " return\n"
  5598. "}\n"
  5599. "42");
  5600. }
  5601. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5602. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5603. EXPECT_EQ("void f() {\n"
  5604. " if (a)\n"
  5605. " return\n"
  5606. "}",
  5607. format("void f ( ) { if ( a ) return }"));
  5608. EXPECT_EQ("namespace N {\n"
  5609. "void f()\n"
  5610. "}",
  5611. format("namespace N { void f() }"));
  5612. EXPECT_EQ("namespace N {\n"
  5613. "void f() {}\n"
  5614. "void g()\n"
  5615. "}",
  5616. format("namespace N { void f( ) { } void g( ) }"));
  5617. }
  5618. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5619. verifyFormat("int aaaaaaaa =\n"
  5620. " // Overlylongcomment\n"
  5621. " b;",
  5622. getLLVMStyleWithColumns(20));
  5623. verifyFormat("function(\n"
  5624. " ShortArgument,\n"
  5625. " LoooooooooooongArgument);\n",
  5626. getLLVMStyleWithColumns(20));
  5627. }
  5628. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5629. verifyFormat("public:");
  5630. verifyFormat("class A {\n"
  5631. "public\n"
  5632. " void f() {}\n"
  5633. "};");
  5634. verifyFormat("public\n"
  5635. "int qwerty;");
  5636. verifyFormat("public\n"
  5637. "B {}");
  5638. verifyFormat("public\n"
  5639. "{}");
  5640. verifyFormat("public\n"
  5641. "B { int x; }");
  5642. }
  5643. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5644. verifyFormat("{");
  5645. verifyFormat("#})");
  5646. verifyNoCrash("(/**/[:!] ?[).");
  5647. }
  5648. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5649. verifyFormat("do {\n}");
  5650. verifyFormat("do {\n}\n"
  5651. "f();");
  5652. verifyFormat("do {\n}\n"
  5653. "wheeee(fun);");
  5654. verifyFormat("do {\n"
  5655. " f();\n"
  5656. "}");
  5657. }
  5658. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5659. verifyFormat("if {\n foo;\n foo();\n}");
  5660. verifyFormat("switch {\n foo;\n foo();\n}");
  5661. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5662. verifyFormat("while {\n foo;\n foo();\n}");
  5663. verifyFormat("do {\n foo;\n foo();\n} while;");
  5664. }
  5665. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5666. verifyIncompleteFormat("namespace {\n"
  5667. "class Foo { Foo (\n"
  5668. "};\n"
  5669. "} // comment");
  5670. }
  5671. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5672. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5673. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5674. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5675. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5676. EXPECT_EQ("{\n"
  5677. " {\n"
  5678. " breakme(\n"
  5679. " qwe);\n"
  5680. " }\n",
  5681. format("{\n"
  5682. " {\n"
  5683. " breakme(qwe);\n"
  5684. "}\n",
  5685. getLLVMStyleWithColumns(10)));
  5686. }
  5687. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5688. verifyFormat("int x = {\n"
  5689. " avariable,\n"
  5690. " b(alongervariable)};",
  5691. getLLVMStyleWithColumns(25));
  5692. }
  5693. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5694. verifyFormat("return (a)(b){1, 2, 3};");
  5695. }
  5696. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5697. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5698. verifyFormat("vector<int> x{\n"
  5699. " 1, 2, 3, 4,\n"
  5700. "};");
  5701. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5702. verifyFormat("f({1, 2});");
  5703. verifyFormat("auto v = Foo{-1};");
  5704. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5705. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5706. verifyFormat("new vector<int>{1, 2, 3};");
  5707. verifyFormat("new int[3]{1, 2, 3};");
  5708. verifyFormat("new int{1};");
  5709. verifyFormat("return {arg1, arg2};");
  5710. verifyFormat("return {arg1, SomeType{parameter}};");
  5711. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5712. verifyFormat("new T{arg1, arg2};");
  5713. verifyFormat("f(MyMap[{composite, key}]);");
  5714. verifyFormat("class Class {\n"
  5715. " T member = {arg1, arg2};\n"
  5716. "};");
  5717. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5718. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5719. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5720. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5721. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5722. verifyFormat("auto i = decltype(x){};");
  5723. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5724. verifyFormat("Node n{1, Node{1000}, //\n"
  5725. " 2};");
  5726. verifyFormat("Aaaa aaaaaaa{\n"
  5727. " {\n"
  5728. " aaaa,\n"
  5729. " },\n"
  5730. "};");
  5731. verifyFormat("class C : public D {\n"
  5732. " SomeClass SC{2};\n"
  5733. "};");
  5734. verifyFormat("class C : public A {\n"
  5735. " class D : public B {\n"
  5736. " void f() { int i{2}; }\n"
  5737. " };\n"
  5738. "};");
  5739. verifyFormat("#define A {a, a},");
  5740. // In combination with BinPackArguments = false.
  5741. FormatStyle NoBinPacking = getLLVMStyle();
  5742. NoBinPacking.BinPackArguments = false;
  5743. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5744. " bbbbb,\n"
  5745. " ccccc,\n"
  5746. " ddddd,\n"
  5747. " eeeee,\n"
  5748. " ffffff,\n"
  5749. " ggggg,\n"
  5750. " hhhhhh,\n"
  5751. " iiiiii,\n"
  5752. " jjjjjj,\n"
  5753. " kkkkkk};",
  5754. NoBinPacking);
  5755. verifyFormat("const Aaaaaa aaaaa = {\n"
  5756. " aaaaa,\n"
  5757. " bbbbb,\n"
  5758. " ccccc,\n"
  5759. " ddddd,\n"
  5760. " eeeee,\n"
  5761. " ffffff,\n"
  5762. " ggggg,\n"
  5763. " hhhhhh,\n"
  5764. " iiiiii,\n"
  5765. " jjjjjj,\n"
  5766. " kkkkkk,\n"
  5767. "};",
  5768. NoBinPacking);
  5769. verifyFormat(
  5770. "const Aaaaaa aaaaa = {\n"
  5771. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5772. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5773. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5774. "};",
  5775. NoBinPacking);
  5776. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5777. // this might be utterly useless in real code.
  5778. verifyFormat("Constructor::Constructor()\n"
  5779. " : some_value{ //\n"
  5780. " aaaaaaa, //\n"
  5781. " bbbbbbb} {}");
  5782. // In braced lists, the first comment is always assumed to belong to the
  5783. // first element. Thus, it can be moved to the next or previous line as
  5784. // appropriate.
  5785. EXPECT_EQ("function({// First element:\n"
  5786. " 1,\n"
  5787. " // Second element:\n"
  5788. " 2});",
  5789. format("function({\n"
  5790. " // First element:\n"
  5791. " 1,\n"
  5792. " // Second element:\n"
  5793. " 2});"));
  5794. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5795. " // First element:\n"
  5796. " 1,\n"
  5797. " // Second element:\n"
  5798. " 2};",
  5799. format("std::vector<int> MyNumbers{// First element:\n"
  5800. " 1,\n"
  5801. " // Second element:\n"
  5802. " 2};",
  5803. getLLVMStyleWithColumns(30)));
  5804. // A trailing comma should still lead to an enforced line break.
  5805. EXPECT_EQ("vector<int> SomeVector = {\n"
  5806. " // aaa\n"
  5807. " 1, 2,\n"
  5808. "};",
  5809. format("vector<int> SomeVector = { // aaa\n"
  5810. " 1, 2, };"));
  5811. FormatStyle ExtraSpaces = getLLVMStyle();
  5812. ExtraSpaces.Cpp11BracedListStyle = false;
  5813. ExtraSpaces.ColumnLimit = 75;
  5814. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5815. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5816. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5817. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5818. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5819. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5820. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5821. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5822. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5823. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5824. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5825. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5826. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5827. verifyFormat("class Class {\n"
  5828. " T member = { arg1, arg2 };\n"
  5829. "};",
  5830. ExtraSpaces);
  5831. verifyFormat(
  5832. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5833. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5834. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5835. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5836. ExtraSpaces);
  5837. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5838. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5839. ExtraSpaces);
  5840. verifyFormat(
  5841. "someFunction(OtherParam,\n"
  5842. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5843. " param1, param2,\n"
  5844. " // comment 2\n"
  5845. " param3, param4 });",
  5846. ExtraSpaces);
  5847. verifyFormat(
  5848. "std::this_thread::sleep_for(\n"
  5849. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5850. ExtraSpaces);
  5851. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5852. " aaaaaaa,\n"
  5853. " aaaaaaaaaa,\n"
  5854. " aaaaa,\n"
  5855. " aaaaaaaaaaaaaaa,\n"
  5856. " aaa,\n"
  5857. " aaaaaaaaaa,\n"
  5858. " a,\n"
  5859. " aaaaaaaaaaaaaaaaaaaaa,\n"
  5860. " aaaaaaaaaaaa,\n"
  5861. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  5862. " aaaaaaa,\n"
  5863. " a};");
  5864. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5865. }
  5866. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5867. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5868. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5869. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5870. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5871. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5872. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5873. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  5874. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5875. " 1, 22, 333, 4444, 55555, //\n"
  5876. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5877. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5878. verifyFormat(
  5879. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5880. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5881. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5882. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5883. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5884. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5885. " 7777777};");
  5886. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5887. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5888. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5889. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5890. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5891. " // Separating comment.\n"
  5892. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5893. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5894. " // Leading comment\n"
  5895. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5896. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5897. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5898. " 1, 1, 1, 1};",
  5899. getLLVMStyleWithColumns(39));
  5900. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5901. " 1, 1, 1, 1};",
  5902. getLLVMStyleWithColumns(38));
  5903. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5904. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5905. getLLVMStyleWithColumns(43));
  5906. verifyFormat(
  5907. "static unsigned SomeValues[10][3] = {\n"
  5908. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  5909. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  5910. verifyFormat("static auto fields = new vector<string>{\n"
  5911. " \"aaaaaaaaaaaaa\",\n"
  5912. " \"aaaaaaaaaaaaa\",\n"
  5913. " \"aaaaaaaaaaaa\",\n"
  5914. " \"aaaaaaaaaaaaaa\",\n"
  5915. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  5916. " \"aaaaaaaaaaaa\",\n"
  5917. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  5918. "};");
  5919. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  5920. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  5921. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  5922. " 3, cccccccccccccccccccccc};",
  5923. getLLVMStyleWithColumns(60));
  5924. // Trailing commas.
  5925. verifyFormat("vector<int> x = {\n"
  5926. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5927. "};",
  5928. getLLVMStyleWithColumns(39));
  5929. verifyFormat("vector<int> x = {\n"
  5930. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5931. "};",
  5932. getLLVMStyleWithColumns(39));
  5933. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5934. " 1, 1, 1, 1,\n"
  5935. " /**/ /**/};",
  5936. getLLVMStyleWithColumns(39));
  5937. // Trailing comment in the first line.
  5938. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  5939. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  5940. " 111111111, 222222222, 3333333333, 444444444, //\n"
  5941. " 11111111, 22222222, 333333333, 44444444};");
  5942. // Trailing comment in the last line.
  5943. verifyFormat("int aaaaa[] = {\n"
  5944. " 1, 2, 3, // comment\n"
  5945. " 4, 5, 6 // comment\n"
  5946. "};");
  5947. // With nested lists, we should either format one item per line or all nested
  5948. // lists one on line.
  5949. // FIXME: For some nested lists, we can do better.
  5950. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5951. " {aaaaaaaaaaaaaaaaaaa},\n"
  5952. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5953. " {aaaaaaaaaaaaaaaaa}};",
  5954. getLLVMStyleWithColumns(60));
  5955. verifyFormat(
  5956. "SomeStruct my_struct_array = {\n"
  5957. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5958. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5959. " {aaa, aaa},\n"
  5960. " {aaa, aaa},\n"
  5961. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5962. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5963. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5964. // No column layout should be used here.
  5965. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5966. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5967. verifyNoCrash("a<,");
  5968. }
  5969. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5970. FormatStyle DoNotMerge = getLLVMStyle();
  5971. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5972. verifyFormat("void f() { return 42; }");
  5973. verifyFormat("void f() {\n"
  5974. " return 42;\n"
  5975. "}",
  5976. DoNotMerge);
  5977. verifyFormat("void f() {\n"
  5978. " // Comment\n"
  5979. "}");
  5980. verifyFormat("{\n"
  5981. "#error {\n"
  5982. " int a;\n"
  5983. "}");
  5984. verifyFormat("{\n"
  5985. " int a;\n"
  5986. "#error {\n"
  5987. "}");
  5988. verifyFormat("void f() {} // comment");
  5989. verifyFormat("void f() { int a; } // comment");
  5990. verifyFormat("void f() {\n"
  5991. "} // comment",
  5992. DoNotMerge);
  5993. verifyFormat("void f() {\n"
  5994. " int a;\n"
  5995. "} // comment",
  5996. DoNotMerge);
  5997. verifyFormat("void f() {\n"
  5998. "} // comment",
  5999. getLLVMStyleWithColumns(15));
  6000. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6001. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6002. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6003. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6004. verifyFormat("class C {\n"
  6005. " C()\n"
  6006. " : iiiiiiii(nullptr),\n"
  6007. " kkkkkkk(nullptr),\n"
  6008. " mmmmmmm(nullptr),\n"
  6009. " nnnnnnn(nullptr) {}\n"
  6010. "};",
  6011. getGoogleStyle());
  6012. FormatStyle NoColumnLimit = getLLVMStyle();
  6013. NoColumnLimit.ColumnLimit = 0;
  6014. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6015. EXPECT_EQ("class C {\n"
  6016. " A() : b(0) {}\n"
  6017. "};",
  6018. format("class C{A():b(0){}};", NoColumnLimit));
  6019. EXPECT_EQ("A()\n"
  6020. " : b(0) {\n"
  6021. "}",
  6022. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6023. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6024. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6025. FormatStyle::SFS_None;
  6026. EXPECT_EQ("A()\n"
  6027. " : b(0) {\n"
  6028. "}",
  6029. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6030. EXPECT_EQ("A()\n"
  6031. " : b(0) {\n"
  6032. "}",
  6033. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6034. verifyFormat("#define A \\\n"
  6035. " void f() { \\\n"
  6036. " int i; \\\n"
  6037. " }",
  6038. getLLVMStyleWithColumns(20));
  6039. verifyFormat("#define A \\\n"
  6040. " void f() { int i; }",
  6041. getLLVMStyleWithColumns(21));
  6042. verifyFormat("#define A \\\n"
  6043. " void f() { \\\n"
  6044. " int i; \\\n"
  6045. " } \\\n"
  6046. " int j;",
  6047. getLLVMStyleWithColumns(22));
  6048. verifyFormat("#define A \\\n"
  6049. " void f() { int i; } \\\n"
  6050. " int j;",
  6051. getLLVMStyleWithColumns(23));
  6052. }
  6053. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6054. FormatStyle MergeInlineOnly = getLLVMStyle();
  6055. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6056. verifyFormat("class C {\n"
  6057. " int f() { return 42; }\n"
  6058. "};",
  6059. MergeInlineOnly);
  6060. verifyFormat("int f() {\n"
  6061. " return 42;\n"
  6062. "}",
  6063. MergeInlineOnly);
  6064. }
  6065. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6066. // Elaborate type variable declarations.
  6067. verifyFormat("struct foo a = {bar};\nint n;");
  6068. verifyFormat("class foo a = {bar};\nint n;");
  6069. verifyFormat("union foo a = {bar};\nint n;");
  6070. // Elaborate types inside function definitions.
  6071. verifyFormat("struct foo f() {}\nint n;");
  6072. verifyFormat("class foo f() {}\nint n;");
  6073. verifyFormat("union foo f() {}\nint n;");
  6074. // Templates.
  6075. verifyFormat("template <class X> void f() {}\nint n;");
  6076. verifyFormat("template <struct X> void f() {}\nint n;");
  6077. verifyFormat("template <union X> void f() {}\nint n;");
  6078. // Actual definitions...
  6079. verifyFormat("struct {\n} n;");
  6080. verifyFormat(
  6081. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6082. verifyFormat("union Z {\n int n;\n} x;");
  6083. verifyFormat("class MACRO Z {\n} n;");
  6084. verifyFormat("class MACRO(X) Z {\n} n;");
  6085. verifyFormat("class __attribute__(X) Z {\n} n;");
  6086. verifyFormat("class __declspec(X) Z {\n} n;");
  6087. verifyFormat("class A##B##C {\n} n;");
  6088. verifyFormat("class alignas(16) Z {\n} n;");
  6089. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6090. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6091. // Redefinition from nested context:
  6092. verifyFormat("class A::B::C {\n} n;");
  6093. // Template definitions.
  6094. verifyFormat(
  6095. "template <typename F>\n"
  6096. "Matcher(const Matcher<F> &Other,\n"
  6097. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6098. " !is_same<F, T>::value>::type * = 0)\n"
  6099. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6100. // FIXME: This is still incorrectly handled at the formatter side.
  6101. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6102. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6103. // FIXME:
  6104. // This now gets parsed incorrectly as class definition.
  6105. // verifyFormat("class A<int> f() {\n}\nint n;");
  6106. // Elaborate types where incorrectly parsing the structural element would
  6107. // break the indent.
  6108. verifyFormat("if (true)\n"
  6109. " class X x;\n"
  6110. "else\n"
  6111. " f();\n");
  6112. // This is simply incomplete. Formatting is not important, but must not crash.
  6113. verifyFormat("class A:");
  6114. }
  6115. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6116. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6117. format("#error Leave all white!!!!! space* alone!\n"));
  6118. EXPECT_EQ(
  6119. "#warning Leave all white!!!!! space* alone!\n",
  6120. format("#warning Leave all white!!!!! space* alone!\n"));
  6121. EXPECT_EQ("#error 1", format(" # error 1"));
  6122. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6123. }
  6124. TEST_F(FormatTest, FormatHashIfExpressions) {
  6125. verifyFormat("#if AAAA && BBBB");
  6126. verifyFormat("#if (AAAA && BBBB)");
  6127. verifyFormat("#elif (AAAA && BBBB)");
  6128. // FIXME: Come up with a better indentation for #elif.
  6129. verifyFormat(
  6130. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6131. " defined(BBBBBBBB)\n"
  6132. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6133. " defined(BBBBBBBB)\n"
  6134. "#endif",
  6135. getLLVMStyleWithColumns(65));
  6136. }
  6137. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6138. FormatStyle AllowsMergedIf = getGoogleStyle();
  6139. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6140. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6141. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6142. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6143. EXPECT_EQ("if (true) return 42;",
  6144. format("if (true)\nreturn 42;", AllowsMergedIf));
  6145. FormatStyle ShortMergedIf = AllowsMergedIf;
  6146. ShortMergedIf.ColumnLimit = 25;
  6147. verifyFormat("#define A \\\n"
  6148. " if (true) return 42;",
  6149. ShortMergedIf);
  6150. verifyFormat("#define A \\\n"
  6151. " f(); \\\n"
  6152. " if (true)\n"
  6153. "#define B",
  6154. ShortMergedIf);
  6155. verifyFormat("#define A \\\n"
  6156. " f(); \\\n"
  6157. " if (true)\n"
  6158. "g();",
  6159. ShortMergedIf);
  6160. verifyFormat("{\n"
  6161. "#ifdef A\n"
  6162. " // Comment\n"
  6163. " if (true) continue;\n"
  6164. "#endif\n"
  6165. " // Comment\n"
  6166. " if (true) continue;\n"
  6167. "}",
  6168. ShortMergedIf);
  6169. ShortMergedIf.ColumnLimit = 29;
  6170. verifyFormat("#define A \\\n"
  6171. " if (aaaaaaaaaa) return 1; \\\n"
  6172. " return 2;",
  6173. ShortMergedIf);
  6174. ShortMergedIf.ColumnLimit = 28;
  6175. verifyFormat("#define A \\\n"
  6176. " if (aaaaaaaaaa) \\\n"
  6177. " return 1; \\\n"
  6178. " return 2;",
  6179. ShortMergedIf);
  6180. }
  6181. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6182. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6183. " f();\n"
  6184. "}");
  6185. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6186. " f();\n"
  6187. "} /* another comment */ else /* comment #3 */ {\n"
  6188. " g();\n"
  6189. "}");
  6190. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6191. " f();\n"
  6192. "}");
  6193. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6194. " f();\n"
  6195. "}");
  6196. verifyFormat("do /* a comment in a strange place */ {\n"
  6197. " f();\n"
  6198. "} /* another comment */ while (0);");
  6199. }
  6200. TEST_F(FormatTest, BlockComments) {
  6201. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6202. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6203. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6204. EXPECT_EQ("#define A /*123*/ \\\n"
  6205. " b\n"
  6206. "/* */\n"
  6207. "someCall(\n"
  6208. " parameter);",
  6209. format("#define A /*123*/ b\n"
  6210. "/* */\n"
  6211. "someCall(parameter);",
  6212. getLLVMStyleWithColumns(15)));
  6213. EXPECT_EQ("#define A\n"
  6214. "/* */ someCall(\n"
  6215. " parameter);",
  6216. format("#define A\n"
  6217. "/* */someCall(parameter);",
  6218. getLLVMStyleWithColumns(15)));
  6219. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6220. EXPECT_EQ("/*\n"
  6221. "*\n"
  6222. " * aaaaaa\n"
  6223. " * aaaaaa\n"
  6224. "*/",
  6225. format("/*\n"
  6226. "*\n"
  6227. " * aaaaaa aaaaaa\n"
  6228. "*/",
  6229. getLLVMStyleWithColumns(10)));
  6230. EXPECT_EQ("/*\n"
  6231. "**\n"
  6232. "* aaaaaa\n"
  6233. "*aaaaaa\n"
  6234. "*/",
  6235. format("/*\n"
  6236. "**\n"
  6237. "* aaaaaa aaaaaa\n"
  6238. "*/",
  6239. getLLVMStyleWithColumns(10)));
  6240. FormatStyle NoBinPacking = getLLVMStyle();
  6241. NoBinPacking.BinPackParameters = false;
  6242. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6243. " 2, /* comment 2 */\n"
  6244. " 3, /* comment 3 */\n"
  6245. " aaaa,\n"
  6246. " bbbb);",
  6247. format("someFunction (1, /* comment 1 */\n"
  6248. " 2, /* comment 2 */ \n"
  6249. " 3, /* comment 3 */\n"
  6250. "aaaa, bbbb );",
  6251. NoBinPacking));
  6252. verifyFormat(
  6253. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6254. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6255. EXPECT_EQ(
  6256. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6257. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6258. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6259. format(
  6260. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6261. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6262. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6263. EXPECT_EQ(
  6264. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6265. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6266. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6267. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6268. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6269. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6270. verifyFormat("void f(int * /* unused */) {}");
  6271. EXPECT_EQ("/*\n"
  6272. " **\n"
  6273. " */",
  6274. format("/*\n"
  6275. " **\n"
  6276. " */"));
  6277. EXPECT_EQ("/*\n"
  6278. " *q\n"
  6279. " */",
  6280. format("/*\n"
  6281. " *q\n"
  6282. " */"));
  6283. EXPECT_EQ("/*\n"
  6284. " * q\n"
  6285. " */",
  6286. format("/*\n"
  6287. " * q\n"
  6288. " */"));
  6289. EXPECT_EQ("/*\n"
  6290. " **/",
  6291. format("/*\n"
  6292. " **/"));
  6293. EXPECT_EQ("/*\n"
  6294. " ***/",
  6295. format("/*\n"
  6296. " ***/"));
  6297. }
  6298. TEST_F(FormatTest, BlockCommentsInMacros) {
  6299. EXPECT_EQ("#define A \\\n"
  6300. " { \\\n"
  6301. " /* one line */ \\\n"
  6302. " someCall();",
  6303. format("#define A { \\\n"
  6304. " /* one line */ \\\n"
  6305. " someCall();",
  6306. getLLVMStyleWithColumns(20)));
  6307. EXPECT_EQ("#define A \\\n"
  6308. " { \\\n"
  6309. " /* previous */ \\\n"
  6310. " /* one line */ \\\n"
  6311. " someCall();",
  6312. format("#define A { \\\n"
  6313. " /* previous */ \\\n"
  6314. " /* one line */ \\\n"
  6315. " someCall();",
  6316. getLLVMStyleWithColumns(20)));
  6317. }
  6318. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6319. EXPECT_EQ("a = {\n"
  6320. " 1111 /* */\n"
  6321. "};",
  6322. format("a = {1111 /* */\n"
  6323. "};",
  6324. getLLVMStyleWithColumns(15)));
  6325. EXPECT_EQ("a = {\n"
  6326. " 1111 /* */\n"
  6327. "};",
  6328. format("a = {1111 /* */\n"
  6329. "};",
  6330. getLLVMStyleWithColumns(15)));
  6331. // FIXME: The formatting is still wrong here.
  6332. EXPECT_EQ("a = {\n"
  6333. " 1111 /* a\n"
  6334. " */\n"
  6335. "};",
  6336. format("a = {1111 /* a */\n"
  6337. "};",
  6338. getLLVMStyleWithColumns(15)));
  6339. }
  6340. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6341. // FIXME: This is not what we want...
  6342. verifyFormat("{\n"
  6343. "// a"
  6344. "// b");
  6345. }
  6346. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6347. verifyFormat("void f(int *a);");
  6348. verifyFormat("void f() { f(fint * b); }");
  6349. verifyFormat("class A {\n void f(int *a);\n};");
  6350. verifyFormat("class A {\n int *a;\n};");
  6351. verifyFormat("namespace a {\n"
  6352. "namespace b {\n"
  6353. "class A {\n"
  6354. " void f() {}\n"
  6355. " int *a;\n"
  6356. "};\n"
  6357. "}\n"
  6358. "}");
  6359. }
  6360. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6361. verifyFormat("while");
  6362. verifyFormat("operator");
  6363. }
  6364. //===----------------------------------------------------------------------===//
  6365. // Objective-C tests.
  6366. //===----------------------------------------------------------------------===//
  6367. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6368. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6369. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6370. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6371. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6372. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6373. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6374. format("-(NSInteger)Method3:(id)anObject;"));
  6375. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6376. format("-(NSInteger)Method4:(id)anObject;"));
  6377. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6378. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6379. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6380. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6381. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6382. "forAllCells:(BOOL)flag;",
  6383. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6384. "forAllCells:(BOOL)flag;"));
  6385. // Very long objectiveC method declaration.
  6386. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6387. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6388. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6389. " inRange:(NSRange)range\n"
  6390. " outRange:(NSRange)out_range\n"
  6391. " outRange1:(NSRange)out_range1\n"
  6392. " outRange2:(NSRange)out_range2\n"
  6393. " outRange3:(NSRange)out_range3\n"
  6394. " outRange4:(NSRange)out_range4\n"
  6395. " outRange5:(NSRange)out_range5\n"
  6396. " outRange6:(NSRange)out_range6\n"
  6397. " outRange7:(NSRange)out_range7\n"
  6398. " outRange8:(NSRange)out_range8\n"
  6399. " outRange9:(NSRange)out_range9;");
  6400. // When the function name has to be wrapped.
  6401. FormatStyle Style = getLLVMStyle();
  6402. Style.IndentWrappedFunctionNames = false;
  6403. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6404. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6405. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6406. "}",
  6407. Style);
  6408. Style.IndentWrappedFunctionNames = true;
  6409. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6410. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6411. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6412. "}",
  6413. Style);
  6414. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6415. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6416. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6417. // protocol lists (but not for template classes):
  6418. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6419. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6420. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6421. // If there's no return type (very rare in practice!), LLVM and Google style
  6422. // agree.
  6423. verifyFormat("- foo;");
  6424. verifyFormat("- foo:(int)f;");
  6425. verifyGoogleFormat("- foo:(int)foo;");
  6426. }
  6427. TEST_F(FormatTest, FormatObjCInterface) {
  6428. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6429. "@public\n"
  6430. " int field1;\n"
  6431. "@protected\n"
  6432. " int field2;\n"
  6433. "@private\n"
  6434. " int field3;\n"
  6435. "@package\n"
  6436. " int field4;\n"
  6437. "}\n"
  6438. "+ (id)init;\n"
  6439. "@end");
  6440. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6441. " @public\n"
  6442. " int field1;\n"
  6443. " @protected\n"
  6444. " int field2;\n"
  6445. " @private\n"
  6446. " int field3;\n"
  6447. " @package\n"
  6448. " int field4;\n"
  6449. "}\n"
  6450. "+ (id)init;\n"
  6451. "@end");
  6452. verifyFormat("@interface /* wait for it */ Foo\n"
  6453. "+ (id)init;\n"
  6454. "// Look, a comment!\n"
  6455. "- (int)answerWith:(int)i;\n"
  6456. "@end");
  6457. verifyFormat("@interface Foo\n"
  6458. "@end\n"
  6459. "@interface Bar\n"
  6460. "@end");
  6461. verifyFormat("@interface Foo : Bar\n"
  6462. "+ (id)init;\n"
  6463. "@end");
  6464. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6465. "+ (id)init;\n"
  6466. "@end");
  6467. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6468. "+ (id)init;\n"
  6469. "@end");
  6470. verifyFormat("@interface Foo (HackStuff)\n"
  6471. "+ (id)init;\n"
  6472. "@end");
  6473. verifyFormat("@interface Foo ()\n"
  6474. "+ (id)init;\n"
  6475. "@end");
  6476. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6477. "+ (id)init;\n"
  6478. "@end");
  6479. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6480. "+ (id)init;\n"
  6481. "@end");
  6482. verifyFormat("@interface Foo {\n"
  6483. " int _i;\n"
  6484. "}\n"
  6485. "+ (id)init;\n"
  6486. "@end");
  6487. verifyFormat("@interface Foo : Bar {\n"
  6488. " int _i;\n"
  6489. "}\n"
  6490. "+ (id)init;\n"
  6491. "@end");
  6492. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6493. " int _i;\n"
  6494. "}\n"
  6495. "+ (id)init;\n"
  6496. "@end");
  6497. verifyFormat("@interface Foo (HackStuff) {\n"
  6498. " int _i;\n"
  6499. "}\n"
  6500. "+ (id)init;\n"
  6501. "@end");
  6502. verifyFormat("@interface Foo () {\n"
  6503. " int _i;\n"
  6504. "}\n"
  6505. "+ (id)init;\n"
  6506. "@end");
  6507. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6508. " int _i;\n"
  6509. "}\n"
  6510. "+ (id)init;\n"
  6511. "@end");
  6512. FormatStyle OnePerLine = getGoogleStyle();
  6513. OnePerLine.BinPackParameters = false;
  6514. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6515. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6516. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6517. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6518. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6519. "}",
  6520. OnePerLine);
  6521. }
  6522. TEST_F(FormatTest, FormatObjCImplementation) {
  6523. verifyFormat("@implementation Foo : NSObject {\n"
  6524. "@public\n"
  6525. " int field1;\n"
  6526. "@protected\n"
  6527. " int field2;\n"
  6528. "@private\n"
  6529. " int field3;\n"
  6530. "@package\n"
  6531. " int field4;\n"
  6532. "}\n"
  6533. "+ (id)init {\n}\n"
  6534. "@end");
  6535. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6536. " @public\n"
  6537. " int field1;\n"
  6538. " @protected\n"
  6539. " int field2;\n"
  6540. " @private\n"
  6541. " int field3;\n"
  6542. " @package\n"
  6543. " int field4;\n"
  6544. "}\n"
  6545. "+ (id)init {\n}\n"
  6546. "@end");
  6547. verifyFormat("@implementation Foo\n"
  6548. "+ (id)init {\n"
  6549. " if (true)\n"
  6550. " return nil;\n"
  6551. "}\n"
  6552. "// Look, a comment!\n"
  6553. "- (int)answerWith:(int)i {\n"
  6554. " return i;\n"
  6555. "}\n"
  6556. "+ (int)answerWith:(int)i {\n"
  6557. " return i;\n"
  6558. "}\n"
  6559. "@end");
  6560. verifyFormat("@implementation Foo\n"
  6561. "@end\n"
  6562. "@implementation Bar\n"
  6563. "@end");
  6564. EXPECT_EQ("@implementation Foo : Bar\n"
  6565. "+ (id)init {\n}\n"
  6566. "- (void)foo {\n}\n"
  6567. "@end",
  6568. format("@implementation Foo : Bar\n"
  6569. "+(id)init{}\n"
  6570. "-(void)foo{}\n"
  6571. "@end"));
  6572. verifyFormat("@implementation Foo {\n"
  6573. " int _i;\n"
  6574. "}\n"
  6575. "+ (id)init {\n}\n"
  6576. "@end");
  6577. verifyFormat("@implementation Foo : Bar {\n"
  6578. " int _i;\n"
  6579. "}\n"
  6580. "+ (id)init {\n}\n"
  6581. "@end");
  6582. verifyFormat("@implementation Foo (HackStuff)\n"
  6583. "+ (id)init {\n}\n"
  6584. "@end");
  6585. verifyFormat("@implementation ObjcClass\n"
  6586. "- (void)method;\n"
  6587. "{}\n"
  6588. "@end");
  6589. }
  6590. TEST_F(FormatTest, FormatObjCProtocol) {
  6591. verifyFormat("@protocol Foo\n"
  6592. "@property(weak) id delegate;\n"
  6593. "- (NSUInteger)numberOfThings;\n"
  6594. "@end");
  6595. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6596. "- (NSUInteger)numberOfThings;\n"
  6597. "@end");
  6598. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6599. "- (NSUInteger)numberOfThings;\n"
  6600. "@end");
  6601. verifyFormat("@protocol Foo;\n"
  6602. "@protocol Bar;\n");
  6603. verifyFormat("@protocol Foo\n"
  6604. "@end\n"
  6605. "@protocol Bar\n"
  6606. "@end");
  6607. verifyFormat("@protocol myProtocol\n"
  6608. "- (void)mandatoryWithInt:(int)i;\n"
  6609. "@optional\n"
  6610. "- (void)optional;\n"
  6611. "@required\n"
  6612. "- (void)required;\n"
  6613. "@optional\n"
  6614. "@property(assign) int madProp;\n"
  6615. "@end\n");
  6616. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6617. " int *looooooooooooooooooooooooooooongNumber;\n"
  6618. "@property(nonatomic, assign, readonly)\n"
  6619. " NSString *looooooooooooooooooooooooooooongName;");
  6620. verifyFormat("@implementation PR18406\n"
  6621. "}\n"
  6622. "@end");
  6623. }
  6624. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6625. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6626. " rect:(NSRect)theRect\n"
  6627. " interval:(float)theInterval {\n"
  6628. "}");
  6629. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6630. " longKeyword:(NSRect)theRect\n"
  6631. " evenLongerKeyword:(float)theInterval\n"
  6632. " error:(NSError **)theError {\n"
  6633. "}");
  6634. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6635. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6636. " NS_DESIGNATED_INITIALIZER;",
  6637. getLLVMStyleWithColumns(60));
  6638. // Continuation indent width should win over aligning colons if the function
  6639. // name is long.
  6640. FormatStyle continuationStyle = getGoogleStyle();
  6641. continuationStyle.ColumnLimit = 40;
  6642. continuationStyle.IndentWrappedFunctionNames = true;
  6643. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6644. " dontAlignNamef:(NSRect)theRect {\n"
  6645. "}",
  6646. continuationStyle);
  6647. // Make sure we don't break aligning for short parameter names.
  6648. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6649. " aShortf:(NSRect)theRect {\n"
  6650. "}",
  6651. continuationStyle);
  6652. }
  6653. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6654. verifyFormat("[foo bar:baz];");
  6655. verifyFormat("return [foo bar:baz];");
  6656. verifyFormat("return (a)[foo bar:baz];");
  6657. verifyFormat("f([foo bar:baz]);");
  6658. verifyFormat("f(2, [foo bar:baz]);");
  6659. verifyFormat("f(2, a ? b : c);");
  6660. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6661. // Unary operators.
  6662. verifyFormat("int a = +[foo bar:baz];");
  6663. verifyFormat("int a = -[foo bar:baz];");
  6664. verifyFormat("int a = ![foo bar:baz];");
  6665. verifyFormat("int a = ~[foo bar:baz];");
  6666. verifyFormat("int a = ++[foo bar:baz];");
  6667. verifyFormat("int a = --[foo bar:baz];");
  6668. verifyFormat("int a = sizeof [foo bar:baz];");
  6669. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6670. verifyFormat("int a = &[foo bar:baz];");
  6671. verifyFormat("int a = *[foo bar:baz];");
  6672. // FIXME: Make casts work, without breaking f()[4].
  6673. // verifyFormat("int a = (int)[foo bar:baz];");
  6674. // verifyFormat("return (int)[foo bar:baz];");
  6675. // verifyFormat("(void)[foo bar:baz];");
  6676. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6677. // Binary operators.
  6678. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6679. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6680. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6681. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6682. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6683. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6684. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6685. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6686. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6687. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6688. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6689. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6690. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6691. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6692. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6693. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6694. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6695. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6696. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6697. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6698. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6699. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6700. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6701. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6702. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6703. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6704. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6705. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6706. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6707. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6708. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6709. // Whew!
  6710. verifyFormat("return in[42];");
  6711. verifyFormat("for (auto v : in[1]) {\n}");
  6712. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  6713. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  6714. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  6715. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  6716. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  6717. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6718. "}");
  6719. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6720. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  6721. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  6722. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6723. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6724. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6725. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6726. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6727. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6728. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6729. verifyFormat("arr[[self indexForFoo:a]];");
  6730. verifyFormat("throw [self errorFor:a];");
  6731. verifyFormat("@throw [self errorFor:a];");
  6732. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6733. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6734. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6735. // This tests that the formatter doesn't break after "backing" but before ":",
  6736. // which would be at 80 columns.
  6737. verifyFormat(
  6738. "void f() {\n"
  6739. " if ((self = [super initWithContentRect:contentRect\n"
  6740. " styleMask:styleMask ?: otherMask\n"
  6741. " backing:NSBackingStoreBuffered\n"
  6742. " defer:YES]))");
  6743. verifyFormat(
  6744. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6745. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6746. verifyFormat("[myObj short:arg1 // Force line break\n"
  6747. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6748. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6749. " error:arg4];");
  6750. verifyFormat(
  6751. "void f() {\n"
  6752. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6753. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6754. " pos.width(), pos.height())\n"
  6755. " styleMask:NSBorderlessWindowMask\n"
  6756. " backing:NSBackingStoreBuffered\n"
  6757. " defer:NO]);\n"
  6758. "}");
  6759. verifyFormat(
  6760. "void f() {\n"
  6761. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6762. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6763. " pos.width(), pos.height())\n"
  6764. " syeMask:NSBorderlessWindowMask\n"
  6765. " bking:NSBackingStoreBuffered\n"
  6766. " der:NO]);\n"
  6767. "}",
  6768. getLLVMStyleWithColumns(70));
  6769. verifyFormat(
  6770. "void f() {\n"
  6771. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6772. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6773. " pos.width(), pos.height())\n"
  6774. " styleMask:NSBorderlessWindowMask\n"
  6775. " backing:NSBackingStoreBuffered\n"
  6776. " defer:NO]);\n"
  6777. "}",
  6778. getChromiumStyle(FormatStyle::LK_Cpp));
  6779. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6780. " with:contentsNativeView];");
  6781. verifyFormat(
  6782. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6783. " owner:nillllll];");
  6784. verifyFormat(
  6785. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6786. " forType:kBookmarkButtonDragType];");
  6787. verifyFormat("[defaultCenter addObserver:self\n"
  6788. " selector:@selector(willEnterFullscreen)\n"
  6789. " name:kWillEnterFullscreenNotification\n"
  6790. " object:nil];");
  6791. verifyFormat("[image_rep drawInRect:drawRect\n"
  6792. " fromRect:NSZeroRect\n"
  6793. " operation:NSCompositeCopy\n"
  6794. " fraction:1.0\n"
  6795. " respectFlipped:NO\n"
  6796. " hints:nil];");
  6797. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6798. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6799. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  6800. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6801. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  6802. " aaaaaaaaaaaaaaaaaaaaaa];");
  6803. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  6804. " .aaaaaaaa];", // FIXME: Indentation seems off.
  6805. getLLVMStyleWithColumns(60));
  6806. verifyFormat(
  6807. "scoped_nsobject<NSTextField> message(\n"
  6808. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6809. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6810. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6811. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6812. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6813. " aaaa:bbb];");
  6814. verifyFormat("[self param:function( //\n"
  6815. " parameter)]");
  6816. verifyFormat(
  6817. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6818. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6819. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6820. // FIXME: This violates the column limit.
  6821. verifyFormat(
  6822. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6823. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  6824. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  6825. getLLVMStyleWithColumns(60));
  6826. // Variadic parameters.
  6827. verifyFormat(
  6828. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6829. verifyFormat(
  6830. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6831. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6832. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6833. verifyFormat("[self // break\n"
  6834. " a:a\n"
  6835. " aaa:aaa];");
  6836. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6837. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6838. }
  6839. TEST_F(FormatTest, ObjCAt) {
  6840. verifyFormat("@autoreleasepool");
  6841. verifyFormat("@catch");
  6842. verifyFormat("@class");
  6843. verifyFormat("@compatibility_alias");
  6844. verifyFormat("@defs");
  6845. verifyFormat("@dynamic");
  6846. verifyFormat("@encode");
  6847. verifyFormat("@end");
  6848. verifyFormat("@finally");
  6849. verifyFormat("@implementation");
  6850. verifyFormat("@import");
  6851. verifyFormat("@interface");
  6852. verifyFormat("@optional");
  6853. verifyFormat("@package");
  6854. verifyFormat("@private");
  6855. verifyFormat("@property");
  6856. verifyFormat("@protected");
  6857. verifyFormat("@protocol");
  6858. verifyFormat("@public");
  6859. verifyFormat("@required");
  6860. verifyFormat("@selector");
  6861. verifyFormat("@synchronized");
  6862. verifyFormat("@synthesize");
  6863. verifyFormat("@throw");
  6864. verifyFormat("@try");
  6865. EXPECT_EQ("@interface", format("@ interface"));
  6866. // The precise formatting of this doesn't matter, nobody writes code like
  6867. // this.
  6868. verifyFormat("@ /*foo*/ interface");
  6869. }
  6870. TEST_F(FormatTest, ObjCSnippets) {
  6871. verifyFormat("@autoreleasepool {\n"
  6872. " foo();\n"
  6873. "}");
  6874. verifyFormat("@class Foo, Bar;");
  6875. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6876. verifyFormat("@dynamic textColor;");
  6877. verifyFormat("char *buf1 = @encode(int *);");
  6878. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6879. verifyFormat("char *buf1 = @encode(int **);");
  6880. verifyFormat("Protocol *proto = @protocol(p1);");
  6881. verifyFormat("SEL s = @selector(foo:);");
  6882. verifyFormat("@synchronized(self) {\n"
  6883. " f();\n"
  6884. "}");
  6885. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6886. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6887. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6888. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6889. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6890. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6891. getMozillaStyle());
  6892. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6893. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6894. getWebKitStyle());
  6895. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6896. verifyFormat("@import foo.bar;\n"
  6897. "@import baz;");
  6898. }
  6899. TEST_F(FormatTest, ObjCForIn) {
  6900. verifyFormat("- (void)test {\n"
  6901. " for (NSString *n in arrayOfStrings) {\n"
  6902. " foo(n);\n"
  6903. " }\n"
  6904. "}");
  6905. verifyFormat("- (void)test {\n"
  6906. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  6907. " foo(n);\n"
  6908. " }\n"
  6909. "}");
  6910. }
  6911. TEST_F(FormatTest, ObjCLiterals) {
  6912. verifyFormat("@\"String\"");
  6913. verifyFormat("@1");
  6914. verifyFormat("@+4.8");
  6915. verifyFormat("@-4");
  6916. verifyFormat("@1LL");
  6917. verifyFormat("@.5");
  6918. verifyFormat("@'c'");
  6919. verifyFormat("@true");
  6920. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6921. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6922. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6923. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6924. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6925. }
  6926. TEST_F(FormatTest, ObjCDictLiterals) {
  6927. verifyFormat("@{");
  6928. verifyFormat("@{}");
  6929. verifyFormat("@{@\"one\" : @1}");
  6930. verifyFormat("return @{@\"one\" : @1;");
  6931. verifyFormat("@{@\"one\" : @1}");
  6932. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6933. verifyFormat("@{\n"
  6934. " @\"one\" : @{@2 : @1},\n"
  6935. "}");
  6936. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6937. verifyIncompleteFormat("[self setDict:@{}");
  6938. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  6939. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6940. verifyFormat(
  6941. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6942. verifyFormat(
  6943. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6944. verifyFormat("NSDictionary *d = @{\n"
  6945. " @\"nam\" : NSUserNam(),\n"
  6946. " @\"dte\" : [NSDate date],\n"
  6947. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6948. "};");
  6949. verifyFormat(
  6950. "@{\n"
  6951. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6952. "regularFont,\n"
  6953. "};");
  6954. verifyGoogleFormat(
  6955. "@{\n"
  6956. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6957. "regularFont,\n"
  6958. "};");
  6959. verifyFormat(
  6960. "@{\n"
  6961. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6962. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6963. "};");
  6964. // We should try to be robust in case someone forgets the "@".
  6965. verifyFormat("NSDictionary *d = {\n"
  6966. " @\"nam\" : NSUserNam(),\n"
  6967. " @\"dte\" : [NSDate date],\n"
  6968. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6969. "};");
  6970. verifyFormat("NSMutableDictionary *dictionary =\n"
  6971. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6972. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6973. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6974. " cccccccccccccccc : ccccccccccccccc\n"
  6975. " }];");
  6976. // Ensure that casts before the key are kept on the same line as the key.
  6977. verifyFormat(
  6978. "NSDictionary *d = @{\n"
  6979. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6980. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  6981. "};");
  6982. }
  6983. TEST_F(FormatTest, ObjCArrayLiterals) {
  6984. verifyIncompleteFormat("@[");
  6985. verifyFormat("@[]");
  6986. verifyFormat(
  6987. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6988. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6989. verifyFormat("NSArray *array = @[ [foo description] ];");
  6990. verifyFormat(
  6991. "NSArray *some_variable = @[\n"
  6992. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6993. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6994. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6995. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6996. "];");
  6997. verifyFormat("NSArray *some_variable = @[\n"
  6998. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6999. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7000. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7001. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7002. "];");
  7003. verifyGoogleFormat("NSArray *some_variable = @[\n"
  7004. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7005. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7006. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7007. " @\"aaaaaaaaaaaaaaaaa\"\n"
  7008. "];");
  7009. verifyFormat("NSArray *array = @[\n"
  7010. " @\"a\",\n"
  7011. " @\"a\",\n" // Trailing comma -> one per line.
  7012. "];");
  7013. // We should try to be robust in case someone forgets the "@".
  7014. verifyFormat("NSArray *some_variable = [\n"
  7015. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7016. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7017. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7018. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7019. "];");
  7020. verifyFormat(
  7021. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7022. " index:(NSUInteger)index\n"
  7023. " nonDigitAttributes:\n"
  7024. " (NSDictionary *)noDigitAttributes;");
  7025. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7026. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7027. "]];");
  7028. }
  7029. TEST_F(FormatTest, BreaksStringLiterals) {
  7030. EXPECT_EQ("\"some text \"\n"
  7031. "\"other\";",
  7032. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7033. EXPECT_EQ("\"some text \"\n"
  7034. "\"other\";",
  7035. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7036. EXPECT_EQ(
  7037. "#define A \\\n"
  7038. " \"some \" \\\n"
  7039. " \"text \" \\\n"
  7040. " \"other\";",
  7041. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7042. EXPECT_EQ(
  7043. "#define A \\\n"
  7044. " \"so \" \\\n"
  7045. " \"text \" \\\n"
  7046. " \"other\";",
  7047. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7048. EXPECT_EQ("\"some text\"",
  7049. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7050. EXPECT_EQ("\"some text\"",
  7051. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7052. EXPECT_EQ("\"some \"\n"
  7053. "\"text\"",
  7054. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7055. EXPECT_EQ("\"some \"\n"
  7056. "\"text\"",
  7057. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7058. EXPECT_EQ("\"some\"\n"
  7059. "\" tex\"\n"
  7060. "\"t\"",
  7061. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7062. EXPECT_EQ("\"some\"\n"
  7063. "\" tex\"\n"
  7064. "\" and\"",
  7065. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7066. EXPECT_EQ("\"some\"\n"
  7067. "\"/tex\"\n"
  7068. "\"/and\"",
  7069. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7070. EXPECT_EQ("variable =\n"
  7071. " \"long string \"\n"
  7072. " \"literal\";",
  7073. format("variable = \"long string literal\";",
  7074. getLLVMStyleWithColumns(20)));
  7075. EXPECT_EQ("variable = f(\n"
  7076. " \"long string \"\n"
  7077. " \"literal\",\n"
  7078. " short,\n"
  7079. " loooooooooooooooooooong);",
  7080. format("variable = f(\"long string literal\", short, "
  7081. "loooooooooooooooooooong);",
  7082. getLLVMStyleWithColumns(20)));
  7083. EXPECT_EQ(
  7084. "f(g(\"long string \"\n"
  7085. " \"literal\"),\n"
  7086. " b);",
  7087. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7088. EXPECT_EQ("f(g(\"long string \"\n"
  7089. " \"literal\",\n"
  7090. " a),\n"
  7091. " b);",
  7092. format("f(g(\"long string literal\", a), b);",
  7093. getLLVMStyleWithColumns(20)));
  7094. EXPECT_EQ(
  7095. "f(\"one two\".split(\n"
  7096. " variable));",
  7097. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7098. EXPECT_EQ("f(\"one two three four five six \"\n"
  7099. " \"seven\".split(\n"
  7100. " really_looooong_variable));",
  7101. format("f(\"one two three four five six seven\"."
  7102. "split(really_looooong_variable));",
  7103. getLLVMStyleWithColumns(33)));
  7104. EXPECT_EQ("f(\"some \"\n"
  7105. " \"text\",\n"
  7106. " other);",
  7107. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7108. // Only break as a last resort.
  7109. verifyFormat(
  7110. "aaaaaaaaaaaaaaaaaaaa(\n"
  7111. " aaaaaaaaaaaaaaaaaaaa,\n"
  7112. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7113. EXPECT_EQ("\"splitmea\"\n"
  7114. "\"trandomp\"\n"
  7115. "\"oint\"",
  7116. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7117. EXPECT_EQ("\"split/\"\n"
  7118. "\"pathat/\"\n"
  7119. "\"slashes\"",
  7120. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7121. EXPECT_EQ("\"split/\"\n"
  7122. "\"pathat/\"\n"
  7123. "\"slashes\"",
  7124. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7125. EXPECT_EQ("\"split at \"\n"
  7126. "\"spaces/at/\"\n"
  7127. "\"slashes.at.any$\"\n"
  7128. "\"non-alphanumeric%\"\n"
  7129. "\"1111111111characte\"\n"
  7130. "\"rs\"",
  7131. format("\"split at "
  7132. "spaces/at/"
  7133. "slashes.at."
  7134. "any$non-"
  7135. "alphanumeric%"
  7136. "1111111111characte"
  7137. "rs\"",
  7138. getLLVMStyleWithColumns(20)));
  7139. // Verify that splitting the strings understands
  7140. // Style::AlwaysBreakBeforeMultilineStrings.
  7141. EXPECT_EQ(
  7142. "aaaaaaaaaaaa(\n"
  7143. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7144. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7145. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7146. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7147. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7148. getGoogleStyle()));
  7149. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7150. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7151. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7152. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7153. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7154. getGoogleStyle()));
  7155. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7156. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7157. format("llvm::outs() << "
  7158. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7159. "aaaaaaaaaaaaaaaaaaa\";"));
  7160. EXPECT_EQ("ffff(\n"
  7161. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7162. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7163. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7164. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7165. getGoogleStyle()));
  7166. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7167. AlignLeft.AlignEscapedNewlinesLeft = true;
  7168. EXPECT_EQ("#define A \\\n"
  7169. " \"some \" \\\n"
  7170. " \"text \" \\\n"
  7171. " \"other\";",
  7172. format("#define A \"some text other\";", AlignLeft));
  7173. }
  7174. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7175. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7176. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7177. EXPECT_EQ("int i = a(b());",
  7178. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7179. }
  7180. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7181. EXPECT_EQ(
  7182. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7183. "(\n"
  7184. " \"x\t\");",
  7185. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7186. "aaaaaaa("
  7187. "\"x\t\");"));
  7188. }
  7189. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7190. EXPECT_EQ(
  7191. "u8\"utf8 string \"\n"
  7192. "u8\"literal\";",
  7193. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7194. EXPECT_EQ(
  7195. "u\"utf16 string \"\n"
  7196. "u\"literal\";",
  7197. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7198. EXPECT_EQ(
  7199. "U\"utf32 string \"\n"
  7200. "U\"literal\";",
  7201. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7202. EXPECT_EQ("L\"wide string \"\n"
  7203. "L\"literal\";",
  7204. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7205. EXPECT_EQ("@\"NSString \"\n"
  7206. "@\"literal\";",
  7207. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7208. // This input makes clang-format try to split the incomplete unicode escape
  7209. // sequence, which used to lead to a crasher.
  7210. verifyNoCrash(
  7211. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7212. getLLVMStyleWithColumns(60));
  7213. }
  7214. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7215. FormatStyle Style = getGoogleStyleWithColumns(15);
  7216. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7217. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7218. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7219. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7220. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7221. format("u8R\"x(raw literal)x\";", Style));
  7222. }
  7223. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7224. FormatStyle Style = getLLVMStyleWithColumns(20);
  7225. EXPECT_EQ(
  7226. "_T(\"aaaaaaaaaaaaaa\")\n"
  7227. "_T(\"aaaaaaaaaaaaaa\")\n"
  7228. "_T(\"aaaaaaaaaaaa\")",
  7229. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7230. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7231. " _T(\"aaaaaa\"),\n"
  7232. " z);",
  7233. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7234. // FIXME: Handle embedded spaces in one iteration.
  7235. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7236. // "_T(\"aaaaaaaaaaaaa\")\n"
  7237. // "_T(\"aaaaaaaaaaaaa\")\n"
  7238. // "_T(\"a\")",
  7239. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7240. // getLLVMStyleWithColumns(20)));
  7241. EXPECT_EQ(
  7242. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7243. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7244. EXPECT_EQ("f(\n"
  7245. "#if !TEST\n"
  7246. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7247. "#endif\n"
  7248. " );",
  7249. format("f(\n"
  7250. "#if !TEST\n"
  7251. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7252. "#endif\n"
  7253. ");"));
  7254. EXPECT_EQ("f(\n"
  7255. "\n"
  7256. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7257. format("f(\n"
  7258. "\n"
  7259. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7260. }
  7261. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7262. EXPECT_EQ(
  7263. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7264. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7265. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7266. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7267. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7268. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7269. }
  7270. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7271. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7272. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7273. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7274. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7275. ")x\",\n"
  7276. " a),\n"
  7277. " b);",
  7278. format("fffffffffff(g(R\"x(\n"
  7279. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7280. ")x\", a), b);",
  7281. getGoogleStyleWithColumns(20)));
  7282. EXPECT_EQ("fffffffffff(\n"
  7283. " g(R\"x(qqq\n"
  7284. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7285. ")x\",\n"
  7286. " a),\n"
  7287. " b);",
  7288. format("fffffffffff(g(R\"x(qqq\n"
  7289. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7290. ")x\", a), b);",
  7291. getGoogleStyleWithColumns(20)));
  7292. EXPECT_EQ("fffffffffff(R\"x(\n"
  7293. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7294. ")x\");",
  7295. format("fffffffffff(R\"x(\n"
  7296. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7297. ")x\");",
  7298. getGoogleStyleWithColumns(20)));
  7299. EXPECT_EQ("fffffffffff(R\"x(\n"
  7300. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7301. ")x\" + bbbbbb);",
  7302. format("fffffffffff(R\"x(\n"
  7303. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7304. ")x\" + bbbbbb);",
  7305. getGoogleStyleWithColumns(20)));
  7306. EXPECT_EQ("fffffffffff(\n"
  7307. " R\"x(\n"
  7308. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7309. ")x\" +\n"
  7310. " bbbbbb);",
  7311. format("fffffffffff(\n"
  7312. " R\"x(\n"
  7313. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7314. ")x\" + bbbbbb);",
  7315. getGoogleStyleWithColumns(20)));
  7316. }
  7317. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7318. verifyFormat("string a = \"unterminated;");
  7319. EXPECT_EQ("function(\"unterminated,\n"
  7320. " OtherParameter);",
  7321. format("function( \"unterminated,\n"
  7322. " OtherParameter);"));
  7323. }
  7324. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7325. FormatStyle Style = getLLVMStyle();
  7326. Style.Standard = FormatStyle::LS_Cpp03;
  7327. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7328. format("#define x(_a) printf(\"foo\"_a);", Style));
  7329. }
  7330. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7331. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7332. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7333. " \"ddeeefff\");",
  7334. format("someFunction(\"aaabbbcccdddeeefff\");",
  7335. getLLVMStyleWithColumns(25)));
  7336. EXPECT_EQ("someFunction1234567890(\n"
  7337. " \"aaabbbcccdddeeefff\");",
  7338. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7339. getLLVMStyleWithColumns(26)));
  7340. EXPECT_EQ("someFunction1234567890(\n"
  7341. " \"aaabbbcccdddeeeff\"\n"
  7342. " \"f\");",
  7343. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7344. getLLVMStyleWithColumns(25)));
  7345. EXPECT_EQ("someFunction1234567890(\n"
  7346. " \"aaabbbcccdddeeeff\"\n"
  7347. " \"f\");",
  7348. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7349. getLLVMStyleWithColumns(24)));
  7350. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7351. " \"ddde \"\n"
  7352. " \"efff\");",
  7353. format("someFunction(\"aaabbbcc ddde efff\");",
  7354. getLLVMStyleWithColumns(25)));
  7355. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7356. " \"ddeeefff\");",
  7357. format("someFunction(\"aaabbbccc ddeeefff\");",
  7358. getLLVMStyleWithColumns(25)));
  7359. EXPECT_EQ("someFunction1234567890(\n"
  7360. " \"aaabb \"\n"
  7361. " \"cccdddeeefff\");",
  7362. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7363. getLLVMStyleWithColumns(25)));
  7364. EXPECT_EQ("#define A \\\n"
  7365. " string s = \\\n"
  7366. " \"123456789\" \\\n"
  7367. " \"0\"; \\\n"
  7368. " int i;",
  7369. format("#define A string s = \"1234567890\"; int i;",
  7370. getLLVMStyleWithColumns(20)));
  7371. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7372. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7373. " \"dddeeeff\"\n"
  7374. " \"f\");",
  7375. format("someFunction(\"aaabbbcc dddeeefff\");",
  7376. getLLVMStyleWithColumns(25)));
  7377. }
  7378. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7379. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7380. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7381. EXPECT_EQ("\"test\"\n"
  7382. "\"\\n\"",
  7383. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7384. EXPECT_EQ("\"tes\\\\\"\n"
  7385. "\"n\"",
  7386. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7387. EXPECT_EQ("\"\\\\\\\\\"\n"
  7388. "\"\\n\"",
  7389. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7390. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7391. EXPECT_EQ("\"\\uff01\"\n"
  7392. "\"test\"",
  7393. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7394. EXPECT_EQ("\"\\Uff01ff02\"",
  7395. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7396. EXPECT_EQ("\"\\x000000000001\"\n"
  7397. "\"next\"",
  7398. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7399. EXPECT_EQ("\"\\x000000000001next\"",
  7400. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7401. EXPECT_EQ("\"\\x000000000001\"",
  7402. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7403. EXPECT_EQ("\"test\"\n"
  7404. "\"\\000000\"\n"
  7405. "\"000001\"",
  7406. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7407. EXPECT_EQ("\"test\\000\"\n"
  7408. "\"00000000\"\n"
  7409. "\"1\"",
  7410. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7411. }
  7412. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7413. verifyFormat("void f() {\n"
  7414. " return g() {}\n"
  7415. " void h() {}");
  7416. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7417. "g();\n"
  7418. "}");
  7419. }
  7420. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7421. verifyFormat(
  7422. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7423. }
  7424. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7425. verifyFormat("class X {\n"
  7426. " void f() {\n"
  7427. " }\n"
  7428. "};",
  7429. getLLVMStyleWithColumns(12));
  7430. }
  7431. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7432. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7433. EightIndent.IndentWidth = 8;
  7434. EightIndent.ContinuationIndentWidth = 8;
  7435. verifyFormat("void f() {\n"
  7436. " someFunction();\n"
  7437. " if (true) {\n"
  7438. " f();\n"
  7439. " }\n"
  7440. "}",
  7441. EightIndent);
  7442. verifyFormat("class X {\n"
  7443. " void f() {\n"
  7444. " }\n"
  7445. "};",
  7446. EightIndent);
  7447. verifyFormat("int x[] = {\n"
  7448. " call(),\n"
  7449. " call()};",
  7450. EightIndent);
  7451. }
  7452. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7453. verifyFormat("double\n"
  7454. "f();",
  7455. getLLVMStyleWithColumns(8));
  7456. }
  7457. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7458. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7459. Tab.IndentWidth = 8;
  7460. Tab.UseTab = FormatStyle::UT_Always;
  7461. Tab.AlignEscapedNewlinesLeft = true;
  7462. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7463. " bb)\t\t// w\n"
  7464. "\t;",
  7465. format("if (aaaaaaaa &&// q\n"
  7466. "bb)// w\n"
  7467. ";",
  7468. Tab));
  7469. EXPECT_EQ("if (aaa && bbb) // w\n"
  7470. "\t;",
  7471. format("if(aaa&&bbb)// w\n"
  7472. ";",
  7473. Tab));
  7474. verifyFormat("class X {\n"
  7475. "\tvoid f() {\n"
  7476. "\t\tsomeFunction(parameter1,\n"
  7477. "\t\t\t parameter2);\n"
  7478. "\t}\n"
  7479. "};",
  7480. Tab);
  7481. verifyFormat("#define A \\\n"
  7482. "\tvoid f() { \\\n"
  7483. "\t\tsomeFunction( \\\n"
  7484. "\t\t parameter1, \\\n"
  7485. "\t\t parameter2); \\\n"
  7486. "\t}",
  7487. Tab);
  7488. Tab.TabWidth = 4;
  7489. Tab.IndentWidth = 8;
  7490. verifyFormat("class TabWidth4Indent8 {\n"
  7491. "\t\tvoid f() {\n"
  7492. "\t\t\t\tsomeFunction(parameter1,\n"
  7493. "\t\t\t\t\t\t\t parameter2);\n"
  7494. "\t\t}\n"
  7495. "};",
  7496. Tab);
  7497. Tab.TabWidth = 4;
  7498. Tab.IndentWidth = 4;
  7499. verifyFormat("class TabWidth4Indent4 {\n"
  7500. "\tvoid f() {\n"
  7501. "\t\tsomeFunction(parameter1,\n"
  7502. "\t\t\t\t\t parameter2);\n"
  7503. "\t}\n"
  7504. "};",
  7505. Tab);
  7506. Tab.TabWidth = 8;
  7507. Tab.IndentWidth = 4;
  7508. verifyFormat("class TabWidth8Indent4 {\n"
  7509. " void f() {\n"
  7510. "\tsomeFunction(parameter1,\n"
  7511. "\t\t parameter2);\n"
  7512. " }\n"
  7513. "};",
  7514. Tab);
  7515. Tab.TabWidth = 8;
  7516. Tab.IndentWidth = 8;
  7517. EXPECT_EQ("/*\n"
  7518. "\t a\t\tcomment\n"
  7519. "\t in multiple lines\n"
  7520. " */",
  7521. format(" /*\t \t \n"
  7522. " \t \t a\t\tcomment\t \t\n"
  7523. " \t \t in multiple lines\t\n"
  7524. " \t */",
  7525. Tab));
  7526. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7527. verifyFormat("{\n"
  7528. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7529. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7530. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7531. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7532. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7533. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7534. "};",
  7535. Tab);
  7536. verifyFormat("enum A {\n"
  7537. "\ta1, // Force multiple lines\n"
  7538. "\ta2,\n"
  7539. "\ta3\n"
  7540. "};",
  7541. Tab);
  7542. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7543. " bb) // w\n"
  7544. "\t;",
  7545. format("if (aaaaaaaa &&// q\n"
  7546. "bb)// w\n"
  7547. ";",
  7548. Tab));
  7549. verifyFormat("class X {\n"
  7550. "\tvoid f() {\n"
  7551. "\t\tsomeFunction(parameter1,\n"
  7552. "\t\t parameter2);\n"
  7553. "\t}\n"
  7554. "};",
  7555. Tab);
  7556. verifyFormat("{\n"
  7557. "\tQ(\n"
  7558. "\t {\n"
  7559. "\t\t int a;\n"
  7560. "\t\t someFunction(aaaaaaaa,\n"
  7561. "\t\t bbbbbbb);\n"
  7562. "\t },\n"
  7563. "\t p);\n"
  7564. "}",
  7565. Tab);
  7566. EXPECT_EQ("{\n"
  7567. "\t/* aaaa\n"
  7568. "\t bbbb */\n"
  7569. "}",
  7570. format("{\n"
  7571. "/* aaaa\n"
  7572. " bbbb */\n"
  7573. "}",
  7574. Tab));
  7575. EXPECT_EQ("{\n"
  7576. "\t/*\n"
  7577. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7578. "\t bbbbbbbbbbbbb\n"
  7579. "\t*/\n"
  7580. "}",
  7581. format("{\n"
  7582. "/*\n"
  7583. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7584. "*/\n"
  7585. "}",
  7586. Tab));
  7587. EXPECT_EQ("{\n"
  7588. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7589. "\t// bbbbbbbbbbbbb\n"
  7590. "}",
  7591. format("{\n"
  7592. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7593. "}",
  7594. Tab));
  7595. EXPECT_EQ("{\n"
  7596. "\t/*\n"
  7597. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7598. "\t bbbbbbbbbbbbb\n"
  7599. "\t*/\n"
  7600. "}",
  7601. format("{\n"
  7602. "\t/*\n"
  7603. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7604. "\t*/\n"
  7605. "}",
  7606. Tab));
  7607. EXPECT_EQ("{\n"
  7608. "\t/*\n"
  7609. "\n"
  7610. "\t*/\n"
  7611. "}",
  7612. format("{\n"
  7613. "\t/*\n"
  7614. "\n"
  7615. "\t*/\n"
  7616. "}",
  7617. Tab));
  7618. EXPECT_EQ("{\n"
  7619. "\t/*\n"
  7620. " asdf\n"
  7621. "\t*/\n"
  7622. "}",
  7623. format("{\n"
  7624. "\t/*\n"
  7625. " asdf\n"
  7626. "\t*/\n"
  7627. "}",
  7628. Tab));
  7629. Tab.UseTab = FormatStyle::UT_Never;
  7630. EXPECT_EQ("/*\n"
  7631. " a\t\tcomment\n"
  7632. " in multiple lines\n"
  7633. " */",
  7634. format(" /*\t \t \n"
  7635. " \t \t a\t\tcomment\t \t\n"
  7636. " \t \t in multiple lines\t\n"
  7637. " \t */",
  7638. Tab));
  7639. EXPECT_EQ("/* some\n"
  7640. " comment */",
  7641. format(" \t \t /* some\n"
  7642. " \t \t comment */",
  7643. Tab));
  7644. EXPECT_EQ("int a; /* some\n"
  7645. " comment */",
  7646. format(" \t \t int a; /* some\n"
  7647. " \t \t comment */",
  7648. Tab));
  7649. EXPECT_EQ("int a; /* some\n"
  7650. "comment */",
  7651. format(" \t \t int\ta; /* some\n"
  7652. " \t \t comment */",
  7653. Tab));
  7654. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7655. " comment */",
  7656. format(" \t \t f(\"\t\t\"); /* some\n"
  7657. " \t \t comment */",
  7658. Tab));
  7659. EXPECT_EQ("{\n"
  7660. " /*\n"
  7661. " * Comment\n"
  7662. " */\n"
  7663. " int i;\n"
  7664. "}",
  7665. format("{\n"
  7666. "\t/*\n"
  7667. "\t * Comment\n"
  7668. "\t */\n"
  7669. "\t int i;\n"
  7670. "}"));
  7671. }
  7672. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7673. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7674. "q\"; /* some\n"
  7675. " comment */",
  7676. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7677. "q\"; /* some\n"
  7678. " comment */",
  7679. getLLVMStyle()));
  7680. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7681. "/* some\n"
  7682. " comment */",
  7683. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7684. " /* some\n"
  7685. " comment */",
  7686. getLLVMStyle()));
  7687. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7688. "qqq\n"
  7689. "/* some\n"
  7690. " comment */",
  7691. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7692. "qqq\n"
  7693. " /* some\n"
  7694. " comment */",
  7695. getLLVMStyle()));
  7696. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7697. "wwww; /* some\n"
  7698. " comment */",
  7699. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7700. "wwww; /* some\n"
  7701. " comment */",
  7702. getLLVMStyle()));
  7703. }
  7704. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7705. FormatStyle NoSpace = getLLVMStyle();
  7706. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7707. verifyFormat("while(true)\n"
  7708. " continue;",
  7709. NoSpace);
  7710. verifyFormat("for(;;)\n"
  7711. " continue;",
  7712. NoSpace);
  7713. verifyFormat("if(true)\n"
  7714. " f();\n"
  7715. "else if(true)\n"
  7716. " f();",
  7717. NoSpace);
  7718. verifyFormat("do {\n"
  7719. " do_something();\n"
  7720. "} while(something());",
  7721. NoSpace);
  7722. verifyFormat("switch(x) {\n"
  7723. "default:\n"
  7724. " break;\n"
  7725. "}",
  7726. NoSpace);
  7727. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7728. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7729. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7730. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7731. verifyFormat("alignas(128) char a[128];", NoSpace);
  7732. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7733. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7734. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7735. verifyFormat("typedef void (*cb)(int);", NoSpace);
  7736. verifyFormat("T A::operator()();", NoSpace);
  7737. verifyFormat("X A::operator++(T);", NoSpace);
  7738. FormatStyle Space = getLLVMStyle();
  7739. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7740. verifyFormat("int f ();", Space);
  7741. verifyFormat("void f (int a, T b) {\n"
  7742. " while (true)\n"
  7743. " continue;\n"
  7744. "}",
  7745. Space);
  7746. verifyFormat("if (true)\n"
  7747. " f ();\n"
  7748. "else if (true)\n"
  7749. " f ();",
  7750. Space);
  7751. verifyFormat("do {\n"
  7752. " do_something ();\n"
  7753. "} while (something ());",
  7754. Space);
  7755. verifyFormat("switch (x) {\n"
  7756. "default:\n"
  7757. " break;\n"
  7758. "}",
  7759. Space);
  7760. verifyFormat("A::A () : a (1) {}", Space);
  7761. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7762. verifyFormat("*(&a + 1);\n"
  7763. "&((&a)[1]);\n"
  7764. "a[(b + c) * d];\n"
  7765. "(((a + 1) * 2) + 3) * 4;",
  7766. Space);
  7767. verifyFormat("#define A(x) x", Space);
  7768. verifyFormat("#define A (x) x", Space);
  7769. verifyFormat("#if defined(x)\n"
  7770. "#endif",
  7771. Space);
  7772. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7773. verifyFormat("size_t x = sizeof (x);", Space);
  7774. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7775. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7776. verifyFormat("alignas (128) char a[128];", Space);
  7777. verifyFormat("size_t x = alignof (MyType);", Space);
  7778. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7779. verifyFormat("int f () throw (Deprecated);", Space);
  7780. verifyFormat("typedef void (*cb) (int);", Space);
  7781. verifyFormat("T A::operator() ();", Space);
  7782. verifyFormat("X A::operator++ (T);", Space);
  7783. }
  7784. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7785. FormatStyle Spaces = getLLVMStyle();
  7786. Spaces.SpacesInParentheses = true;
  7787. verifyFormat("call( x, y, z );", Spaces);
  7788. verifyFormat("call();", Spaces);
  7789. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  7790. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  7791. Spaces);
  7792. verifyFormat("while ( (bool)1 )\n"
  7793. " continue;",
  7794. Spaces);
  7795. verifyFormat("for ( ;; )\n"
  7796. " continue;",
  7797. Spaces);
  7798. verifyFormat("if ( true )\n"
  7799. " f();\n"
  7800. "else if ( true )\n"
  7801. " f();",
  7802. Spaces);
  7803. verifyFormat("do {\n"
  7804. " do_something( (int)i );\n"
  7805. "} while ( something() );",
  7806. Spaces);
  7807. verifyFormat("switch ( x ) {\n"
  7808. "default:\n"
  7809. " break;\n"
  7810. "}",
  7811. Spaces);
  7812. Spaces.SpacesInParentheses = false;
  7813. Spaces.SpacesInCStyleCastParentheses = true;
  7814. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7815. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7816. verifyFormat("x = ( int32 )y;", Spaces);
  7817. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7818. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7819. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7820. verifyFormat("#define x (( int )-1)", Spaces);
  7821. // Run the first set of tests again with:
  7822. Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
  7823. Spaces.SpacesInCStyleCastParentheses = true;
  7824. verifyFormat("call(x, y, z);", Spaces);
  7825. verifyFormat("call( );", Spaces);
  7826. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  7827. verifyFormat("while (( bool )1)\n"
  7828. " continue;",
  7829. Spaces);
  7830. verifyFormat("for (;;)\n"
  7831. " continue;",
  7832. Spaces);
  7833. verifyFormat("if (true)\n"
  7834. " f( );\n"
  7835. "else if (true)\n"
  7836. " f( );",
  7837. Spaces);
  7838. verifyFormat("do {\n"
  7839. " do_something(( int )i);\n"
  7840. "} while (something( ));",
  7841. Spaces);
  7842. verifyFormat("switch (x) {\n"
  7843. "default:\n"
  7844. " break;\n"
  7845. "}",
  7846. Spaces);
  7847. // Run the first set of tests again with:
  7848. Spaces.SpaceAfterCStyleCast = true;
  7849. verifyFormat("call(x, y, z);", Spaces);
  7850. verifyFormat("call( );", Spaces);
  7851. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  7852. verifyFormat("while (( bool ) 1)\n"
  7853. " continue;",
  7854. Spaces);
  7855. verifyFormat("for (;;)\n"
  7856. " continue;",
  7857. Spaces);
  7858. verifyFormat("if (true)\n"
  7859. " f( );\n"
  7860. "else if (true)\n"
  7861. " f( );",
  7862. Spaces);
  7863. verifyFormat("do {\n"
  7864. " do_something(( int ) i);\n"
  7865. "} while (something( ));",
  7866. Spaces);
  7867. verifyFormat("switch (x) {\n"
  7868. "default:\n"
  7869. " break;\n"
  7870. "}",
  7871. Spaces);
  7872. // Run subset of tests again with:
  7873. Spaces.SpacesInCStyleCastParentheses = false;
  7874. Spaces.SpaceAfterCStyleCast = true;
  7875. verifyFormat("while ((bool) 1)\n"
  7876. " continue;",
  7877. Spaces);
  7878. verifyFormat("do {\n"
  7879. " do_something((int) i);\n"
  7880. "} while (something( ));",
  7881. Spaces);
  7882. }
  7883. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7884. verifyFormat("int a[5];");
  7885. verifyFormat("a[3] += 42;");
  7886. FormatStyle Spaces = getLLVMStyle();
  7887. Spaces.SpacesInSquareBrackets = true;
  7888. // Lambdas unchanged.
  7889. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7890. verifyFormat("return [i, args...] {};", Spaces);
  7891. // Not lambdas.
  7892. verifyFormat("int a[ 5 ];", Spaces);
  7893. verifyFormat("a[ 3 ] += 42;", Spaces);
  7894. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7895. verifyFormat("double &operator[](int i) { return 0; }\n"
  7896. "int i;",
  7897. Spaces);
  7898. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7899. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7900. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7901. }
  7902. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7903. verifyFormat("int a = 5;");
  7904. verifyFormat("a += 42;");
  7905. verifyFormat("a or_eq 8;");
  7906. FormatStyle Spaces = getLLVMStyle();
  7907. Spaces.SpaceBeforeAssignmentOperators = false;
  7908. verifyFormat("int a= 5;", Spaces);
  7909. verifyFormat("a+= 42;", Spaces);
  7910. verifyFormat("a or_eq 8;", Spaces);
  7911. }
  7912. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  7913. FormatStyle Alignment = getLLVMStyle();
  7914. Alignment.AlignConsecutiveAssignments = false;
  7915. verifyFormat("int a = 5;\n"
  7916. "int oneTwoThree = 123;",
  7917. Alignment);
  7918. verifyFormat("int a = 5;\n"
  7919. "int oneTwoThree = 123;",
  7920. Alignment);
  7921. Alignment.AlignConsecutiveAssignments = true;
  7922. verifyFormat("int a = 5;\n"
  7923. "int oneTwoThree = 123;",
  7924. Alignment);
  7925. verifyFormat("int a = method();\n"
  7926. "int oneTwoThree = 133;",
  7927. Alignment);
  7928. verifyFormat("a &= 5;\n"
  7929. "bcd *= 5;\n"
  7930. "ghtyf += 5;\n"
  7931. "dvfvdb -= 5;\n"
  7932. "a /= 5;\n"
  7933. "vdsvsv %= 5;\n"
  7934. "sfdbddfbdfbb ^= 5;\n"
  7935. "dvsdsv |= 5;\n"
  7936. "int dsvvdvsdvvv = 123;",
  7937. Alignment);
  7938. verifyFormat("int i = 1, j = 10;\n"
  7939. "something = 2000;",
  7940. Alignment);
  7941. verifyFormat("something = 2000;\n"
  7942. "int i = 1, j = 10;\n",
  7943. Alignment);
  7944. verifyFormat("something = 2000;\n"
  7945. "another = 911;\n"
  7946. "int i = 1, j = 10;\n"
  7947. "oneMore = 1;\n"
  7948. "i = 2;",
  7949. Alignment);
  7950. verifyFormat("int a = 5;\n"
  7951. "int one = 1;\n"
  7952. "method();\n"
  7953. "int oneTwoThree = 123;\n"
  7954. "int oneTwo = 12;",
  7955. Alignment);
  7956. verifyFormat("int oneTwoThree = 123;\n"
  7957. "int oneTwo = 12;\n"
  7958. "method();\n",
  7959. Alignment);
  7960. verifyFormat("int oneTwoThree = 123; // comment\n"
  7961. "int oneTwo = 12; // comment",
  7962. Alignment);
  7963. EXPECT_EQ("int a = 5;\n"
  7964. "\n"
  7965. "int oneTwoThree = 123;",
  7966. format("int a = 5;\n"
  7967. "\n"
  7968. "int oneTwoThree= 123;",
  7969. Alignment));
  7970. EXPECT_EQ("int a = 5;\n"
  7971. "int one = 1;\n"
  7972. "\n"
  7973. "int oneTwoThree = 123;",
  7974. format("int a = 5;\n"
  7975. "int one = 1;\n"
  7976. "\n"
  7977. "int oneTwoThree = 123;",
  7978. Alignment));
  7979. EXPECT_EQ("int a = 5;\n"
  7980. "int one = 1;\n"
  7981. "\n"
  7982. "int oneTwoThree = 123;\n"
  7983. "int oneTwo = 12;",
  7984. format("int a = 5;\n"
  7985. "int one = 1;\n"
  7986. "\n"
  7987. "int oneTwoThree = 123;\n"
  7988. "int oneTwo = 12;",
  7989. Alignment));
  7990. Alignment.AlignEscapedNewlinesLeft = true;
  7991. verifyFormat("#define A \\\n"
  7992. " int aaaa = 12; \\\n"
  7993. " int b = 23; \\\n"
  7994. " int ccc = 234; \\\n"
  7995. " int dddddddddd = 2345;",
  7996. Alignment);
  7997. Alignment.AlignEscapedNewlinesLeft = false;
  7998. verifyFormat("#define A "
  7999. " \\\n"
  8000. " int aaaa = 12; "
  8001. " \\\n"
  8002. " int b = 23; "
  8003. " \\\n"
  8004. " int ccc = 234; "
  8005. " \\\n"
  8006. " int dddddddddd = 2345;",
  8007. Alignment);
  8008. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8009. "k = 4, int l = 5,\n"
  8010. " int m = 6) {\n"
  8011. " int j = 10;\n"
  8012. " otherThing = 1;\n"
  8013. "}",
  8014. Alignment);
  8015. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8016. " int i = 1;\n"
  8017. " int j = 2;\n"
  8018. " int big = 10000;\n"
  8019. "}",
  8020. Alignment);
  8021. verifyFormat("class C {\n"
  8022. "public:\n"
  8023. " int i = 1;\n"
  8024. " virtual void f() = 0;\n"
  8025. "};",
  8026. Alignment);
  8027. verifyFormat("int i = 1;\n"
  8028. "if (SomeType t = getSomething()) {\n"
  8029. "}\n"
  8030. "int j = 2;\n"
  8031. "int big = 10000;",
  8032. Alignment);
  8033. verifyFormat("int j = 7;\n"
  8034. "for (int k = 0; k < N; ++k) {\n"
  8035. "}\n"
  8036. "int j = 2;\n"
  8037. "int big = 10000;\n"
  8038. "}",
  8039. Alignment);
  8040. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8041. verifyFormat("int i = 1;\n"
  8042. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8043. " = someLooooooooooooooooongFunction();\n"
  8044. "int j = 2;",
  8045. Alignment);
  8046. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8047. verifyFormat("int i = 1;\n"
  8048. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8049. " someLooooooooooooooooongFunction();\n"
  8050. "int j = 2;",
  8051. Alignment);
  8052. verifyFormat("auto lambda = []() {\n"
  8053. " auto i = 0;\n"
  8054. " return 0;\n"
  8055. "};\n"
  8056. "int i = 0;\n"
  8057. "auto v = type{\n"
  8058. " i = 1, //\n"
  8059. " (i = 2), //\n"
  8060. " i = 3 //\n"
  8061. "};",
  8062. Alignment);
  8063. // FIXME: Should align all three assignments
  8064. verifyFormat(
  8065. "int i = 1;\n"
  8066. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8067. " loooooooooooooooooooooongParameterB);\n"
  8068. "int j = 2;",
  8069. Alignment);
  8070. }
  8071. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8072. FormatStyle Alignment = getLLVMStyle();
  8073. Alignment.AlignConsecutiveDeclarations = false;
  8074. verifyFormat("float const a = 5;\n"
  8075. "int oneTwoThree = 123;",
  8076. Alignment);
  8077. verifyFormat("int a = 5;\n"
  8078. "float const oneTwoThree = 123;",
  8079. Alignment);
  8080. Alignment.AlignConsecutiveDeclarations = true;
  8081. verifyFormat("float const a = 5;\n"
  8082. "int oneTwoThree = 123;",
  8083. Alignment);
  8084. verifyFormat("int a = method();\n"
  8085. "float const oneTwoThree = 133;",
  8086. Alignment);
  8087. verifyFormat("int i = 1, j = 10;\n"
  8088. "something = 2000;",
  8089. Alignment);
  8090. verifyFormat("something = 2000;\n"
  8091. "int i = 1, j = 10;\n",
  8092. Alignment);
  8093. verifyFormat("float something = 2000;\n"
  8094. "double another = 911;\n"
  8095. "int i = 1, j = 10;\n"
  8096. "const int *oneMore = 1;\n"
  8097. "unsigned i = 2;",
  8098. Alignment);
  8099. verifyFormat("float a = 5;\n"
  8100. "int one = 1;\n"
  8101. "method();\n"
  8102. "const double oneTwoThree = 123;\n"
  8103. "const unsigned int oneTwo = 12;",
  8104. Alignment);
  8105. verifyFormat("int oneTwoThree{0}; // comment\n"
  8106. "unsigned oneTwo; // comment",
  8107. Alignment);
  8108. EXPECT_EQ("float const a = 5;\n"
  8109. "\n"
  8110. "int oneTwoThree = 123;",
  8111. format("float const a = 5;\n"
  8112. "\n"
  8113. "int oneTwoThree= 123;",
  8114. Alignment));
  8115. EXPECT_EQ("float a = 5;\n"
  8116. "int one = 1;\n"
  8117. "\n"
  8118. "unsigned oneTwoThree = 123;",
  8119. format("float a = 5;\n"
  8120. "int one = 1;\n"
  8121. "\n"
  8122. "unsigned oneTwoThree = 123;",
  8123. Alignment));
  8124. EXPECT_EQ("float a = 5;\n"
  8125. "int one = 1;\n"
  8126. "\n"
  8127. "unsigned oneTwoThree = 123;\n"
  8128. "int oneTwo = 12;",
  8129. format("float a = 5;\n"
  8130. "int one = 1;\n"
  8131. "\n"
  8132. "unsigned oneTwoThree = 123;\n"
  8133. "int oneTwo = 12;",
  8134. Alignment));
  8135. Alignment.AlignConsecutiveAssignments = true;
  8136. verifyFormat("float something = 2000;\n"
  8137. "double another = 911;\n"
  8138. "int i = 1, j = 10;\n"
  8139. "const int *oneMore = 1;\n"
  8140. "unsigned i = 2;",
  8141. Alignment);
  8142. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8143. "unsigned oneTwo = 0; // comment",
  8144. Alignment);
  8145. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8146. " int const i = 1;\n"
  8147. " int * j = 2;\n"
  8148. " int big = 10000;\n"
  8149. "\n"
  8150. " unsigned oneTwoThree = 123;\n"
  8151. " int oneTwo = 12;\n"
  8152. " method();\n"
  8153. " float k = 2;\n"
  8154. " int ll = 10000;\n"
  8155. "}",
  8156. format("void SomeFunction(int parameter= 0) {\n"
  8157. " int const i= 1;\n"
  8158. " int *j=2;\n"
  8159. " int big = 10000;\n"
  8160. "\n"
  8161. "unsigned oneTwoThree =123;\n"
  8162. "int oneTwo = 12;\n"
  8163. " method();\n"
  8164. "float k= 2;\n"
  8165. "int ll=10000;\n"
  8166. "}",
  8167. Alignment));
  8168. Alignment.AlignConsecutiveAssignments = false;
  8169. Alignment.AlignEscapedNewlinesLeft = true;
  8170. verifyFormat("#define A \\\n"
  8171. " int aaaa = 12; \\\n"
  8172. " float b = 23; \\\n"
  8173. " const int ccc = 234; \\\n"
  8174. " unsigned dddddddddd = 2345;",
  8175. Alignment);
  8176. Alignment.AlignEscapedNewlinesLeft = false;
  8177. Alignment.ColumnLimit = 30;
  8178. verifyFormat("#define A \\\n"
  8179. " int aaaa = 12; \\\n"
  8180. " float b = 23; \\\n"
  8181. " const int ccc = 234; \\\n"
  8182. " int dddddddddd = 2345;",
  8183. Alignment);
  8184. Alignment.ColumnLimit = 80;
  8185. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8186. "k = 4, int l = 5,\n"
  8187. " int m = 6) {\n"
  8188. " const int j = 10;\n"
  8189. " otherThing = 1;\n"
  8190. "}",
  8191. Alignment);
  8192. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8193. " int const i = 1;\n"
  8194. " int * j = 2;\n"
  8195. " int big = 10000;\n"
  8196. "}",
  8197. Alignment);
  8198. verifyFormat("class C {\n"
  8199. "public:\n"
  8200. " int i = 1;\n"
  8201. " virtual void f() = 0;\n"
  8202. "};",
  8203. Alignment);
  8204. verifyFormat("float i = 1;\n"
  8205. "if (SomeType t = getSomething()) {\n"
  8206. "}\n"
  8207. "const unsigned j = 2;\n"
  8208. "int big = 10000;",
  8209. Alignment);
  8210. verifyFormat("float j = 7;\n"
  8211. "for (int k = 0; k < N; ++k) {\n"
  8212. "}\n"
  8213. "unsigned j = 2;\n"
  8214. "int big = 10000;\n"
  8215. "}",
  8216. Alignment);
  8217. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8218. verifyFormat("float i = 1;\n"
  8219. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8220. " = someLooooooooooooooooongFunction();\n"
  8221. "int j = 2;",
  8222. Alignment);
  8223. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8224. verifyFormat("int i = 1;\n"
  8225. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8226. " someLooooooooooooooooongFunction();\n"
  8227. "int j = 2;",
  8228. Alignment);
  8229. Alignment.AlignConsecutiveAssignments = true;
  8230. verifyFormat("auto lambda = []() {\n"
  8231. " auto ii = 0;\n"
  8232. " float j = 0;\n"
  8233. " return 0;\n"
  8234. "};\n"
  8235. "int i = 0;\n"
  8236. "float i2 = 0;\n"
  8237. "auto v = type{\n"
  8238. " i = 1, //\n"
  8239. " (i = 2), //\n"
  8240. " i = 3 //\n"
  8241. "};",
  8242. Alignment);
  8243. Alignment.AlignConsecutiveAssignments = false;
  8244. // FIXME: Should align all three declarations
  8245. verifyFormat(
  8246. "int i = 1;\n"
  8247. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8248. " loooooooooooooooooooooongParameterB);\n"
  8249. "int j = 2;",
  8250. Alignment);
  8251. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8252. // We expect declarations and assignments to align, as long as it doesn't
  8253. // exceed the column limit, starting a new alignemnt sequence whenever it
  8254. // happens.
  8255. Alignment.AlignConsecutiveAssignments = true;
  8256. Alignment.ColumnLimit = 30;
  8257. verifyFormat("float ii = 1;\n"
  8258. "unsigned j = 2;\n"
  8259. "int someVerylongVariable = 1;\n"
  8260. "AnotherLongType ll = 123456;\n"
  8261. "VeryVeryLongType k = 2;\n"
  8262. "int myvar = 1;",
  8263. Alignment);
  8264. Alignment.ColumnLimit = 80;
  8265. }
  8266. TEST_F(FormatTest, LinuxBraceBreaking) {
  8267. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8268. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8269. verifyFormat("namespace a\n"
  8270. "{\n"
  8271. "class A\n"
  8272. "{\n"
  8273. " void f()\n"
  8274. " {\n"
  8275. " if (true) {\n"
  8276. " a();\n"
  8277. " b();\n"
  8278. " }\n"
  8279. " }\n"
  8280. " void g() { return; }\n"
  8281. "};\n"
  8282. "struct B {\n"
  8283. " int x;\n"
  8284. "};\n"
  8285. "}\n",
  8286. LinuxBraceStyle);
  8287. verifyFormat("enum X {\n"
  8288. " Y = 0,\n"
  8289. "}\n",
  8290. LinuxBraceStyle);
  8291. verifyFormat("struct S {\n"
  8292. " int Type;\n"
  8293. " union {\n"
  8294. " int x;\n"
  8295. " double y;\n"
  8296. " } Value;\n"
  8297. " class C\n"
  8298. " {\n"
  8299. " MyFavoriteType Value;\n"
  8300. " } Class;\n"
  8301. "}\n",
  8302. LinuxBraceStyle);
  8303. }
  8304. TEST_F(FormatTest, MozillaBraceBreaking) {
  8305. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8306. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8307. verifyFormat("namespace a {\n"
  8308. "class A\n"
  8309. "{\n"
  8310. " void f()\n"
  8311. " {\n"
  8312. " if (true) {\n"
  8313. " a();\n"
  8314. " b();\n"
  8315. " }\n"
  8316. " }\n"
  8317. " void g() { return; }\n"
  8318. "};\n"
  8319. "enum E\n"
  8320. "{\n"
  8321. " A,\n"
  8322. " // foo\n"
  8323. " B,\n"
  8324. " C\n"
  8325. "};\n"
  8326. "struct B\n"
  8327. "{\n"
  8328. " int x;\n"
  8329. "};\n"
  8330. "}\n",
  8331. MozillaBraceStyle);
  8332. verifyFormat("struct S\n"
  8333. "{\n"
  8334. " int Type;\n"
  8335. " union\n"
  8336. " {\n"
  8337. " int x;\n"
  8338. " double y;\n"
  8339. " } Value;\n"
  8340. " class C\n"
  8341. " {\n"
  8342. " MyFavoriteType Value;\n"
  8343. " } Class;\n"
  8344. "}\n",
  8345. MozillaBraceStyle);
  8346. }
  8347. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8348. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8349. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8350. verifyFormat("namespace a {\n"
  8351. "class A {\n"
  8352. " void f()\n"
  8353. " {\n"
  8354. " if (true) {\n"
  8355. " a();\n"
  8356. " b();\n"
  8357. " }\n"
  8358. " }\n"
  8359. " void g() { return; }\n"
  8360. "};\n"
  8361. "struct B {\n"
  8362. " int x;\n"
  8363. "};\n"
  8364. "}\n",
  8365. StroustrupBraceStyle);
  8366. verifyFormat("void foo()\n"
  8367. "{\n"
  8368. " if (a) {\n"
  8369. " a();\n"
  8370. " }\n"
  8371. " else {\n"
  8372. " b();\n"
  8373. " }\n"
  8374. "}\n",
  8375. StroustrupBraceStyle);
  8376. verifyFormat("#ifdef _DEBUG\n"
  8377. "int foo(int i = 0)\n"
  8378. "#else\n"
  8379. "int foo(int i = 5)\n"
  8380. "#endif\n"
  8381. "{\n"
  8382. " return i;\n"
  8383. "}",
  8384. StroustrupBraceStyle);
  8385. verifyFormat("void foo() {}\n"
  8386. "void bar()\n"
  8387. "#ifdef _DEBUG\n"
  8388. "{\n"
  8389. " foo();\n"
  8390. "}\n"
  8391. "#else\n"
  8392. "{\n"
  8393. "}\n"
  8394. "#endif",
  8395. StroustrupBraceStyle);
  8396. verifyFormat("void foobar() { int i = 5; }\n"
  8397. "#ifdef _DEBUG\n"
  8398. "void bar() {}\n"
  8399. "#else\n"
  8400. "void bar() { foobar(); }\n"
  8401. "#endif",
  8402. StroustrupBraceStyle);
  8403. }
  8404. TEST_F(FormatTest, AllmanBraceBreaking) {
  8405. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8406. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8407. verifyFormat("namespace a\n"
  8408. "{\n"
  8409. "class A\n"
  8410. "{\n"
  8411. " void f()\n"
  8412. " {\n"
  8413. " if (true)\n"
  8414. " {\n"
  8415. " a();\n"
  8416. " b();\n"
  8417. " }\n"
  8418. " }\n"
  8419. " void g() { return; }\n"
  8420. "};\n"
  8421. "struct B\n"
  8422. "{\n"
  8423. " int x;\n"
  8424. "};\n"
  8425. "}",
  8426. AllmanBraceStyle);
  8427. verifyFormat("void f()\n"
  8428. "{\n"
  8429. " if (true)\n"
  8430. " {\n"
  8431. " a();\n"
  8432. " }\n"
  8433. " else if (false)\n"
  8434. " {\n"
  8435. " b();\n"
  8436. " }\n"
  8437. " else\n"
  8438. " {\n"
  8439. " c();\n"
  8440. " }\n"
  8441. "}\n",
  8442. AllmanBraceStyle);
  8443. verifyFormat("void f()\n"
  8444. "{\n"
  8445. " for (int i = 0; i < 10; ++i)\n"
  8446. " {\n"
  8447. " a();\n"
  8448. " }\n"
  8449. " while (false)\n"
  8450. " {\n"
  8451. " b();\n"
  8452. " }\n"
  8453. " do\n"
  8454. " {\n"
  8455. " c();\n"
  8456. " } while (false)\n"
  8457. "}\n",
  8458. AllmanBraceStyle);
  8459. verifyFormat("void f(int a)\n"
  8460. "{\n"
  8461. " switch (a)\n"
  8462. " {\n"
  8463. " case 0:\n"
  8464. " break;\n"
  8465. " case 1:\n"
  8466. " {\n"
  8467. " break;\n"
  8468. " }\n"
  8469. " case 2:\n"
  8470. " {\n"
  8471. " }\n"
  8472. " break;\n"
  8473. " default:\n"
  8474. " break;\n"
  8475. " }\n"
  8476. "}\n",
  8477. AllmanBraceStyle);
  8478. verifyFormat("enum X\n"
  8479. "{\n"
  8480. " Y = 0,\n"
  8481. "}\n",
  8482. AllmanBraceStyle);
  8483. verifyFormat("enum X\n"
  8484. "{\n"
  8485. " Y = 0\n"
  8486. "}\n",
  8487. AllmanBraceStyle);
  8488. verifyFormat("@interface BSApplicationController ()\n"
  8489. "{\n"
  8490. "@private\n"
  8491. " id _extraIvar;\n"
  8492. "}\n"
  8493. "@end\n",
  8494. AllmanBraceStyle);
  8495. verifyFormat("#ifdef _DEBUG\n"
  8496. "int foo(int i = 0)\n"
  8497. "#else\n"
  8498. "int foo(int i = 5)\n"
  8499. "#endif\n"
  8500. "{\n"
  8501. " return i;\n"
  8502. "}",
  8503. AllmanBraceStyle);
  8504. verifyFormat("void foo() {}\n"
  8505. "void bar()\n"
  8506. "#ifdef _DEBUG\n"
  8507. "{\n"
  8508. " foo();\n"
  8509. "}\n"
  8510. "#else\n"
  8511. "{\n"
  8512. "}\n"
  8513. "#endif",
  8514. AllmanBraceStyle);
  8515. verifyFormat("void foobar() { int i = 5; }\n"
  8516. "#ifdef _DEBUG\n"
  8517. "void bar() {}\n"
  8518. "#else\n"
  8519. "void bar() { foobar(); }\n"
  8520. "#endif",
  8521. AllmanBraceStyle);
  8522. // This shouldn't affect ObjC blocks..
  8523. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  8524. " // ...\n"
  8525. " int i;\n"
  8526. "}];",
  8527. AllmanBraceStyle);
  8528. verifyFormat("void (^block)(void) = ^{\n"
  8529. " // ...\n"
  8530. " int i;\n"
  8531. "};",
  8532. AllmanBraceStyle);
  8533. // .. or dict literals.
  8534. verifyFormat("void f()\n"
  8535. "{\n"
  8536. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  8537. "}",
  8538. AllmanBraceStyle);
  8539. verifyFormat("int f()\n"
  8540. "{ // comment\n"
  8541. " return 42;\n"
  8542. "}",
  8543. AllmanBraceStyle);
  8544. AllmanBraceStyle.ColumnLimit = 19;
  8545. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  8546. AllmanBraceStyle.ColumnLimit = 18;
  8547. verifyFormat("void f()\n"
  8548. "{\n"
  8549. " int i;\n"
  8550. "}",
  8551. AllmanBraceStyle);
  8552. AllmanBraceStyle.ColumnLimit = 80;
  8553. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  8554. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  8555. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  8556. verifyFormat("void f(bool b)\n"
  8557. "{\n"
  8558. " if (b)\n"
  8559. " {\n"
  8560. " return;\n"
  8561. " }\n"
  8562. "}\n",
  8563. BreakBeforeBraceShortIfs);
  8564. verifyFormat("void f(bool b)\n"
  8565. "{\n"
  8566. " if (b) return;\n"
  8567. "}\n",
  8568. BreakBeforeBraceShortIfs);
  8569. verifyFormat("void f(bool b)\n"
  8570. "{\n"
  8571. " while (b)\n"
  8572. " {\n"
  8573. " return;\n"
  8574. " }\n"
  8575. "}\n",
  8576. BreakBeforeBraceShortIfs);
  8577. }
  8578. TEST_F(FormatTest, GNUBraceBreaking) {
  8579. FormatStyle GNUBraceStyle = getLLVMStyle();
  8580. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  8581. verifyFormat("namespace a\n"
  8582. "{\n"
  8583. "class A\n"
  8584. "{\n"
  8585. " void f()\n"
  8586. " {\n"
  8587. " int a;\n"
  8588. " {\n"
  8589. " int b;\n"
  8590. " }\n"
  8591. " if (true)\n"
  8592. " {\n"
  8593. " a();\n"
  8594. " b();\n"
  8595. " }\n"
  8596. " }\n"
  8597. " void g() { return; }\n"
  8598. "}\n"
  8599. "}",
  8600. GNUBraceStyle);
  8601. verifyFormat("void f()\n"
  8602. "{\n"
  8603. " if (true)\n"
  8604. " {\n"
  8605. " a();\n"
  8606. " }\n"
  8607. " else if (false)\n"
  8608. " {\n"
  8609. " b();\n"
  8610. " }\n"
  8611. " else\n"
  8612. " {\n"
  8613. " c();\n"
  8614. " }\n"
  8615. "}\n",
  8616. GNUBraceStyle);
  8617. verifyFormat("void f()\n"
  8618. "{\n"
  8619. " for (int i = 0; i < 10; ++i)\n"
  8620. " {\n"
  8621. " a();\n"
  8622. " }\n"
  8623. " while (false)\n"
  8624. " {\n"
  8625. " b();\n"
  8626. " }\n"
  8627. " do\n"
  8628. " {\n"
  8629. " c();\n"
  8630. " }\n"
  8631. " while (false);\n"
  8632. "}\n",
  8633. GNUBraceStyle);
  8634. verifyFormat("void f(int a)\n"
  8635. "{\n"
  8636. " switch (a)\n"
  8637. " {\n"
  8638. " case 0:\n"
  8639. " break;\n"
  8640. " case 1:\n"
  8641. " {\n"
  8642. " break;\n"
  8643. " }\n"
  8644. " case 2:\n"
  8645. " {\n"
  8646. " }\n"
  8647. " break;\n"
  8648. " default:\n"
  8649. " break;\n"
  8650. " }\n"
  8651. "}\n",
  8652. GNUBraceStyle);
  8653. verifyFormat("enum X\n"
  8654. "{\n"
  8655. " Y = 0,\n"
  8656. "}\n",
  8657. GNUBraceStyle);
  8658. verifyFormat("@interface BSApplicationController ()\n"
  8659. "{\n"
  8660. "@private\n"
  8661. " id _extraIvar;\n"
  8662. "}\n"
  8663. "@end\n",
  8664. GNUBraceStyle);
  8665. verifyFormat("#ifdef _DEBUG\n"
  8666. "int foo(int i = 0)\n"
  8667. "#else\n"
  8668. "int foo(int i = 5)\n"
  8669. "#endif\n"
  8670. "{\n"
  8671. " return i;\n"
  8672. "}",
  8673. GNUBraceStyle);
  8674. verifyFormat("void foo() {}\n"
  8675. "void bar()\n"
  8676. "#ifdef _DEBUG\n"
  8677. "{\n"
  8678. " foo();\n"
  8679. "}\n"
  8680. "#else\n"
  8681. "{\n"
  8682. "}\n"
  8683. "#endif",
  8684. GNUBraceStyle);
  8685. verifyFormat("void foobar() { int i = 5; }\n"
  8686. "#ifdef _DEBUG\n"
  8687. "void bar() {}\n"
  8688. "#else\n"
  8689. "void bar() { foobar(); }\n"
  8690. "#endif",
  8691. GNUBraceStyle);
  8692. }
  8693. TEST_F(FormatTest, WebKitBraceBreaking) {
  8694. FormatStyle WebKitBraceStyle = getLLVMStyle();
  8695. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  8696. verifyFormat("namespace a {\n"
  8697. "class A {\n"
  8698. " void f()\n"
  8699. " {\n"
  8700. " if (true) {\n"
  8701. " a();\n"
  8702. " b();\n"
  8703. " }\n"
  8704. " }\n"
  8705. " void g() { return; }\n"
  8706. "};\n"
  8707. "enum E {\n"
  8708. " A,\n"
  8709. " // foo\n"
  8710. " B,\n"
  8711. " C\n"
  8712. "};\n"
  8713. "struct B {\n"
  8714. " int x;\n"
  8715. "};\n"
  8716. "}\n",
  8717. WebKitBraceStyle);
  8718. verifyFormat("struct S {\n"
  8719. " int Type;\n"
  8720. " union {\n"
  8721. " int x;\n"
  8722. " double y;\n"
  8723. " } Value;\n"
  8724. " class C {\n"
  8725. " MyFavoriteType Value;\n"
  8726. " } Class;\n"
  8727. "};\n",
  8728. WebKitBraceStyle);
  8729. }
  8730. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  8731. verifyFormat("void f() {\n"
  8732. " try {\n"
  8733. " } catch (const Exception &e) {\n"
  8734. " }\n"
  8735. "}\n",
  8736. getLLVMStyle());
  8737. }
  8738. TEST_F(FormatTest, UnderstandsPragmas) {
  8739. verifyFormat("#pragma omp reduction(| : var)");
  8740. verifyFormat("#pragma omp reduction(+ : var)");
  8741. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  8742. "(including parentheses).",
  8743. format("#pragma mark Any non-hyphenated or hyphenated string "
  8744. "(including parentheses)."));
  8745. }
  8746. TEST_F(FormatTest, UnderstandPragmaOption) {
  8747. verifyFormat("#pragma option -C -A");
  8748. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  8749. }
  8750. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  8751. for (size_t i = 1; i < Styles.size(); ++i) \
  8752. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  8753. << " differs from Style #0"
  8754. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  8755. SmallVector<FormatStyle, 3> Styles;
  8756. Styles.resize(3);
  8757. Styles[0] = getLLVMStyle();
  8758. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  8759. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  8760. EXPECT_ALL_STYLES_EQUAL(Styles);
  8761. Styles[0] = getGoogleStyle();
  8762. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  8763. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  8764. EXPECT_ALL_STYLES_EQUAL(Styles);
  8765. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8766. EXPECT_TRUE(
  8767. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  8768. EXPECT_TRUE(
  8769. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  8770. EXPECT_ALL_STYLES_EQUAL(Styles);
  8771. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  8772. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  8773. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  8774. EXPECT_ALL_STYLES_EQUAL(Styles);
  8775. Styles[0] = getMozillaStyle();
  8776. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  8777. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  8778. EXPECT_ALL_STYLES_EQUAL(Styles);
  8779. Styles[0] = getWebKitStyle();
  8780. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  8781. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  8782. EXPECT_ALL_STYLES_EQUAL(Styles);
  8783. Styles[0] = getGNUStyle();
  8784. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  8785. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  8786. EXPECT_ALL_STYLES_EQUAL(Styles);
  8787. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  8788. }
  8789. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  8790. SmallVector<FormatStyle, 8> Styles;
  8791. Styles.resize(2);
  8792. Styles[0] = getGoogleStyle();
  8793. Styles[1] = getLLVMStyle();
  8794. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8795. EXPECT_ALL_STYLES_EQUAL(Styles);
  8796. Styles.resize(5);
  8797. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8798. Styles[1] = getLLVMStyle();
  8799. Styles[1].Language = FormatStyle::LK_JavaScript;
  8800. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8801. Styles[2] = getLLVMStyle();
  8802. Styles[2].Language = FormatStyle::LK_JavaScript;
  8803. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  8804. "BasedOnStyle: Google",
  8805. &Styles[2])
  8806. .value());
  8807. Styles[3] = getLLVMStyle();
  8808. Styles[3].Language = FormatStyle::LK_JavaScript;
  8809. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  8810. "Language: JavaScript",
  8811. &Styles[3])
  8812. .value());
  8813. Styles[4] = getLLVMStyle();
  8814. Styles[4].Language = FormatStyle::LK_JavaScript;
  8815. EXPECT_EQ(0, parseConfiguration("---\n"
  8816. "BasedOnStyle: LLVM\n"
  8817. "IndentWidth: 123\n"
  8818. "---\n"
  8819. "BasedOnStyle: Google\n"
  8820. "Language: JavaScript",
  8821. &Styles[4])
  8822. .value());
  8823. EXPECT_ALL_STYLES_EQUAL(Styles);
  8824. }
  8825. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  8826. Style.FIELD = false; \
  8827. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  8828. EXPECT_TRUE(Style.FIELD); \
  8829. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  8830. EXPECT_FALSE(Style.FIELD);
  8831. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  8832. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  8833. Style.STRUCT.FIELD = false; \
  8834. EXPECT_EQ(0, \
  8835. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  8836. .value()); \
  8837. EXPECT_TRUE(Style.STRUCT.FIELD); \
  8838. EXPECT_EQ(0, \
  8839. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  8840. .value()); \
  8841. EXPECT_FALSE(Style.STRUCT.FIELD);
  8842. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  8843. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  8844. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  8845. EXPECT_NE(VALUE, Style.FIELD); \
  8846. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  8847. EXPECT_EQ(VALUE, Style.FIELD)
  8848. TEST_F(FormatTest, ParsesConfigurationBools) {
  8849. FormatStyle Style = {};
  8850. Style.Language = FormatStyle::LK_Cpp;
  8851. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  8852. CHECK_PARSE_BOOL(AlignOperands);
  8853. CHECK_PARSE_BOOL(AlignTrailingComments);
  8854. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  8855. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  8856. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  8857. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  8858. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  8859. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  8860. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  8861. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  8862. CHECK_PARSE_BOOL(BinPackArguments);
  8863. CHECK_PARSE_BOOL(BinPackParameters);
  8864. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  8865. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  8866. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  8867. CHECK_PARSE_BOOL(DerivePointerAlignment);
  8868. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  8869. CHECK_PARSE_BOOL(DisableFormat);
  8870. CHECK_PARSE_BOOL(IndentCaseLabels);
  8871. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  8872. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  8873. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  8874. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  8875. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  8876. CHECK_PARSE_BOOL(SortIncludes);
  8877. CHECK_PARSE_BOOL(SpacesInParentheses);
  8878. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  8879. CHECK_PARSE_BOOL(SpacesInAngles);
  8880. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  8881. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  8882. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  8883. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  8884. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  8885. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  8886. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  8887. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  8888. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  8889. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  8890. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  8891. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  8892. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  8893. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  8894. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  8895. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  8896. }
  8897. #undef CHECK_PARSE_BOOL
  8898. TEST_F(FormatTest, ParsesConfiguration) {
  8899. FormatStyle Style = {};
  8900. Style.Language = FormatStyle::LK_Cpp;
  8901. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  8902. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  8903. ConstructorInitializerIndentWidth, 1234u);
  8904. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  8905. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  8906. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  8907. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  8908. PenaltyBreakBeforeFirstCallParameter, 1234u);
  8909. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  8910. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  8911. PenaltyReturnTypeOnItsOwnLine, 1234u);
  8912. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  8913. SpacesBeforeTrailingComments, 1234u);
  8914. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  8915. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  8916. Style.PointerAlignment = FormatStyle::PAS_Middle;
  8917. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  8918. FormatStyle::PAS_Left);
  8919. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  8920. FormatStyle::PAS_Right);
  8921. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  8922. FormatStyle::PAS_Middle);
  8923. // For backward compatibility:
  8924. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  8925. FormatStyle::PAS_Left);
  8926. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  8927. FormatStyle::PAS_Right);
  8928. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  8929. FormatStyle::PAS_Middle);
  8930. Style.Standard = FormatStyle::LS_Auto;
  8931. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  8932. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  8933. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  8934. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  8935. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8936. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8937. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8938. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8939. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8940. FormatStyle::BOS_None);
  8941. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8942. FormatStyle::BOS_All);
  8943. // For backward compatibility:
  8944. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8945. FormatStyle::BOS_None);
  8946. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8947. FormatStyle::BOS_All);
  8948. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  8949. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  8950. FormatStyle::BAS_Align);
  8951. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  8952. FormatStyle::BAS_DontAlign);
  8953. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  8954. FormatStyle::BAS_AlwaysBreak);
  8955. // For backward compatibility:
  8956. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  8957. FormatStyle::BAS_DontAlign);
  8958. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  8959. FormatStyle::BAS_Align);
  8960. Style.UseTab = FormatStyle::UT_ForIndentation;
  8961. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8962. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8963. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8964. // For backward compatibility:
  8965. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8966. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8967. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8968. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8969. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8970. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8971. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8972. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  8973. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  8974. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8975. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8976. // For backward compatibility:
  8977. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8978. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8979. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8980. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8981. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8982. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8983. FormatStyle::SBPO_Never);
  8984. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8985. FormatStyle::SBPO_Always);
  8986. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8987. FormatStyle::SBPO_ControlStatements);
  8988. // For backward compatibility:
  8989. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8990. FormatStyle::SBPO_Never);
  8991. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8992. FormatStyle::SBPO_ControlStatements);
  8993. Style.ColumnLimit = 123;
  8994. FormatStyle BaseStyle = getLLVMStyle();
  8995. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8996. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8997. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8998. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8999. FormatStyle::BS_Attach);
  9000. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9001. FormatStyle::BS_Linux);
  9002. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9003. FormatStyle::BS_Mozilla);
  9004. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9005. FormatStyle::BS_Stroustrup);
  9006. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9007. FormatStyle::BS_Allman);
  9008. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9009. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9010. FormatStyle::BS_WebKit);
  9011. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9012. FormatStyle::BS_Custom);
  9013. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9014. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9015. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9016. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9017. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9018. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9019. AlwaysBreakAfterDefinitionReturnType,
  9020. FormatStyle::DRTBS_TopLevel);
  9021. Style.NamespaceIndentation = FormatStyle::NI_All;
  9022. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9023. FormatStyle::NI_None);
  9024. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9025. FormatStyle::NI_Inner);
  9026. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9027. FormatStyle::NI_All);
  9028. // FIXME: This is required because parsing a configuration simply overwrites
  9029. // the first N elements of the list instead of resetting it.
  9030. Style.ForEachMacros.clear();
  9031. std::vector<std::string> BoostForeach;
  9032. BoostForeach.push_back("BOOST_FOREACH");
  9033. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9034. std::vector<std::string> BoostAndQForeach;
  9035. BoostAndQForeach.push_back("BOOST_FOREACH");
  9036. BoostAndQForeach.push_back("Q_FOREACH");
  9037. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9038. BoostAndQForeach);
  9039. Style.IncludeCategories.clear();
  9040. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9041. {".*", 1}};
  9042. CHECK_PARSE("IncludeCategories:\n"
  9043. " - Regex: abc/.*\n"
  9044. " Priority: 2\n"
  9045. " - Regex: .*\n"
  9046. " Priority: 1",
  9047. IncludeCategories, ExpectedCategories);
  9048. }
  9049. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9050. FormatStyle Style = {};
  9051. Style.Language = FormatStyle::LK_Cpp;
  9052. CHECK_PARSE("Language: Cpp\n"
  9053. "IndentWidth: 12",
  9054. IndentWidth, 12u);
  9055. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9056. "IndentWidth: 34",
  9057. &Style),
  9058. ParseError::Unsuitable);
  9059. EXPECT_EQ(12u, Style.IndentWidth);
  9060. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9061. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9062. Style.Language = FormatStyle::LK_JavaScript;
  9063. CHECK_PARSE("Language: JavaScript\n"
  9064. "IndentWidth: 12",
  9065. IndentWidth, 12u);
  9066. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9067. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9068. "IndentWidth: 34",
  9069. &Style),
  9070. ParseError::Unsuitable);
  9071. EXPECT_EQ(23u, Style.IndentWidth);
  9072. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9073. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9074. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9075. "IndentWidth: 67",
  9076. IndentWidth, 67u);
  9077. CHECK_PARSE("---\n"
  9078. "Language: JavaScript\n"
  9079. "IndentWidth: 12\n"
  9080. "---\n"
  9081. "Language: Cpp\n"
  9082. "IndentWidth: 34\n"
  9083. "...\n",
  9084. IndentWidth, 12u);
  9085. Style.Language = FormatStyle::LK_Cpp;
  9086. CHECK_PARSE("---\n"
  9087. "Language: JavaScript\n"
  9088. "IndentWidth: 12\n"
  9089. "---\n"
  9090. "Language: Cpp\n"
  9091. "IndentWidth: 34\n"
  9092. "...\n",
  9093. IndentWidth, 34u);
  9094. CHECK_PARSE("---\n"
  9095. "IndentWidth: 78\n"
  9096. "---\n"
  9097. "Language: JavaScript\n"
  9098. "IndentWidth: 56\n"
  9099. "...\n",
  9100. IndentWidth, 78u);
  9101. Style.ColumnLimit = 123;
  9102. Style.IndentWidth = 234;
  9103. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9104. Style.TabWidth = 345;
  9105. EXPECT_FALSE(parseConfiguration("---\n"
  9106. "IndentWidth: 456\n"
  9107. "BreakBeforeBraces: Allman\n"
  9108. "---\n"
  9109. "Language: JavaScript\n"
  9110. "IndentWidth: 111\n"
  9111. "TabWidth: 111\n"
  9112. "---\n"
  9113. "Language: Cpp\n"
  9114. "BreakBeforeBraces: Stroustrup\n"
  9115. "TabWidth: 789\n"
  9116. "...\n",
  9117. &Style));
  9118. EXPECT_EQ(123u, Style.ColumnLimit);
  9119. EXPECT_EQ(456u, Style.IndentWidth);
  9120. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9121. EXPECT_EQ(789u, Style.TabWidth);
  9122. EXPECT_EQ(parseConfiguration("---\n"
  9123. "Language: JavaScript\n"
  9124. "IndentWidth: 56\n"
  9125. "---\n"
  9126. "IndentWidth: 78\n"
  9127. "...\n",
  9128. &Style),
  9129. ParseError::Error);
  9130. EXPECT_EQ(parseConfiguration("---\n"
  9131. "Language: JavaScript\n"
  9132. "IndentWidth: 56\n"
  9133. "---\n"
  9134. "Language: JavaScript\n"
  9135. "IndentWidth: 78\n"
  9136. "...\n",
  9137. &Style),
  9138. ParseError::Error);
  9139. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9140. }
  9141. #undef CHECK_PARSE
  9142. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9143. FormatStyle Style = {};
  9144. Style.Language = FormatStyle::LK_JavaScript;
  9145. Style.BreakBeforeTernaryOperators = true;
  9146. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9147. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9148. Style.BreakBeforeTernaryOperators = true;
  9149. EXPECT_EQ(0, parseConfiguration("---\n"
  9150. "BasedOnStyle: Google\n"
  9151. "---\n"
  9152. "Language: JavaScript\n"
  9153. "IndentWidth: 76\n"
  9154. "...\n",
  9155. &Style)
  9156. .value());
  9157. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9158. EXPECT_EQ(76u, Style.IndentWidth);
  9159. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9160. }
  9161. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9162. FormatStyle Style = getLLVMStyle();
  9163. std::string YAML = configurationAsText(Style);
  9164. FormatStyle ParsedStyle = {};
  9165. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9166. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9167. EXPECT_EQ(Style, ParsedStyle);
  9168. }
  9169. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9170. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9171. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9172. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9173. "\"\xef\xee\xf0\xf3...\"",
  9174. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9175. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9176. "\xef\xee\xf0\xf3...\"",
  9177. getLLVMStyleWithColumns(12)));
  9178. }
  9179. TEST_F(FormatTest, HandlesUTF8BOM) {
  9180. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9181. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9182. format("\xef\xbb\xbf#include <iostream>"));
  9183. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9184. format("\xef\xbb\xbf\n#include <iostream>"));
  9185. }
  9186. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9187. #if !defined(_MSC_VER)
  9188. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9189. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9190. getLLVMStyleWithColumns(35));
  9191. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9192. getLLVMStyleWithColumns(31));
  9193. verifyFormat("// Однажды в студёную зимнюю пору...",
  9194. getLLVMStyleWithColumns(36));
  9195. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9196. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9197. getLLVMStyleWithColumns(39));
  9198. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9199. getLLVMStyleWithColumns(35));
  9200. }
  9201. TEST_F(FormatTest, SplitsUTF8Strings) {
  9202. // Non-printable characters' width is currently considered to be the length in
  9203. // bytes in UTF8. The characters can be displayed in very different manner
  9204. // (zero-width, single width with a substitution glyph, expanded to their code
  9205. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9206. EXPECT_EQ("\"aaaaÄ\"\n"
  9207. "\"\xc2\x8d\";",
  9208. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9209. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9210. "\"\xc2\x8d\";",
  9211. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9212. EXPECT_EQ("\"Однажды, в \"\n"
  9213. "\"студёную \"\n"
  9214. "\"зимнюю \"\n"
  9215. "\"пору,\"",
  9216. format("\"Однажды, в студёную зимнюю пору,\"",
  9217. getLLVMStyleWithColumns(13)));
  9218. EXPECT_EQ(
  9219. "\"一 二 三 \"\n"
  9220. "\"四 五六 \"\n"
  9221. "\"七 八 九 \"\n"
  9222. "\"十\"",
  9223. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9224. EXPECT_EQ("\"一\t二 \"\n"
  9225. "\"\t三 \"\n"
  9226. "\"四 五\t六 \"\n"
  9227. "\"\t七 \"\n"
  9228. "\"八九十\tqq\"",
  9229. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9230. getLLVMStyleWithColumns(11)));
  9231. // UTF8 character in an escape sequence.
  9232. EXPECT_EQ("\"aaaaaa\"\n"
  9233. "\"\\\xC2\x8D\"",
  9234. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9235. }
  9236. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9237. EXPECT_EQ("const char *sssss =\n"
  9238. " \"一二三四五六七八\\\n"
  9239. " 九 十\";",
  9240. format("const char *sssss = \"一二三四五六七八\\\n"
  9241. " 九 十\";",
  9242. getLLVMStyleWithColumns(30)));
  9243. }
  9244. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9245. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9246. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9247. EXPECT_EQ("// Я из лесу\n"
  9248. "// вышел; был\n"
  9249. "// сильный\n"
  9250. "// мороз.",
  9251. format("// Я из лесу вышел; был сильный мороз.",
  9252. getLLVMStyleWithColumns(13)));
  9253. EXPECT_EQ("// 一二三\n"
  9254. "// 四五六七\n"
  9255. "// 八 九\n"
  9256. "// 十",
  9257. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9258. }
  9259. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9260. EXPECT_EQ("/* Гляжу,\n"
  9261. " * поднимается\n"
  9262. " * медленно в\n"
  9263. " * гору\n"
  9264. " * Лошадка,\n"
  9265. " * везущая\n"
  9266. " * хворосту\n"
  9267. " * воз. */",
  9268. format("/* Гляжу, поднимается медленно в гору\n"
  9269. " * Лошадка, везущая хворосту воз. */",
  9270. getLLVMStyleWithColumns(13)));
  9271. EXPECT_EQ(
  9272. "/* 一二三\n"
  9273. " * 四五六七\n"
  9274. " * 八 九\n"
  9275. " * 十 */",
  9276. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9277. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9278. " * 𝕓𝕪𝕥𝕖\n"
  9279. " * 𝖀𝕿𝕱-𝟠 */",
  9280. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9281. }
  9282. #endif // _MSC_VER
  9283. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9284. FormatStyle Style = getLLVMStyle();
  9285. Style.ConstructorInitializerIndentWidth = 4;
  9286. verifyFormat(
  9287. "SomeClass::Constructor()\n"
  9288. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9289. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9290. Style);
  9291. Style.ConstructorInitializerIndentWidth = 2;
  9292. verifyFormat(
  9293. "SomeClass::Constructor()\n"
  9294. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9295. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9296. Style);
  9297. Style.ConstructorInitializerIndentWidth = 0;
  9298. verifyFormat(
  9299. "SomeClass::Constructor()\n"
  9300. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9301. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9302. Style);
  9303. }
  9304. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9305. FormatStyle Style = getLLVMStyle();
  9306. Style.BreakConstructorInitializersBeforeComma = true;
  9307. Style.ConstructorInitializerIndentWidth = 4;
  9308. verifyFormat("SomeClass::Constructor()\n"
  9309. " : a(a)\n"
  9310. " , b(b)\n"
  9311. " , c(c) {}",
  9312. Style);
  9313. verifyFormat("SomeClass::Constructor()\n"
  9314. " : a(a) {}",
  9315. Style);
  9316. Style.ColumnLimit = 0;
  9317. verifyFormat("SomeClass::Constructor()\n"
  9318. " : a(a) {}",
  9319. Style);
  9320. verifyFormat("SomeClass::Constructor()\n"
  9321. " : a(a)\n"
  9322. " , b(b)\n"
  9323. " , c(c) {}",
  9324. Style);
  9325. verifyFormat("SomeClass::Constructor()\n"
  9326. " : a(a) {\n"
  9327. " foo();\n"
  9328. " bar();\n"
  9329. "}",
  9330. Style);
  9331. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9332. verifyFormat("SomeClass::Constructor()\n"
  9333. " : a(a)\n"
  9334. " , b(b)\n"
  9335. " , c(c) {\n}",
  9336. Style);
  9337. verifyFormat("SomeClass::Constructor()\n"
  9338. " : a(a) {\n}",
  9339. Style);
  9340. Style.ColumnLimit = 80;
  9341. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9342. Style.ConstructorInitializerIndentWidth = 2;
  9343. verifyFormat("SomeClass::Constructor()\n"
  9344. " : a(a)\n"
  9345. " , b(b)\n"
  9346. " , c(c) {}",
  9347. Style);
  9348. Style.ConstructorInitializerIndentWidth = 0;
  9349. verifyFormat("SomeClass::Constructor()\n"
  9350. ": a(a)\n"
  9351. ", b(b)\n"
  9352. ", c(c) {}",
  9353. Style);
  9354. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9355. Style.ConstructorInitializerIndentWidth = 4;
  9356. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9357. verifyFormat(
  9358. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9359. Style);
  9360. verifyFormat(
  9361. "SomeClass::Constructor()\n"
  9362. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9363. Style);
  9364. Style.ConstructorInitializerIndentWidth = 4;
  9365. Style.ColumnLimit = 60;
  9366. verifyFormat("SomeClass::Constructor()\n"
  9367. " : aaaaaaaa(aaaaaaaa)\n"
  9368. " , aaaaaaaa(aaaaaaaa)\n"
  9369. " , aaaaaaaa(aaaaaaaa) {}",
  9370. Style);
  9371. }
  9372. TEST_F(FormatTest, Destructors) {
  9373. verifyFormat("void F(int &i) { i.~int(); }");
  9374. verifyFormat("void F(int &i) { i->~int(); }");
  9375. }
  9376. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9377. FormatStyle Style = getWebKitStyle();
  9378. // Don't indent in outer namespaces.
  9379. verifyFormat("namespace outer {\n"
  9380. "int i;\n"
  9381. "namespace inner {\n"
  9382. " int i;\n"
  9383. "} // namespace inner\n"
  9384. "} // namespace outer\n"
  9385. "namespace other_outer {\n"
  9386. "int i;\n"
  9387. "}",
  9388. Style);
  9389. // Don't indent case labels.
  9390. verifyFormat("switch (variable) {\n"
  9391. "case 1:\n"
  9392. "case 2:\n"
  9393. " doSomething();\n"
  9394. " break;\n"
  9395. "default:\n"
  9396. " ++variable;\n"
  9397. "}",
  9398. Style);
  9399. // Wrap before binary operators.
  9400. EXPECT_EQ("void f()\n"
  9401. "{\n"
  9402. " if (aaaaaaaaaaaaaaaa\n"
  9403. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9404. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9405. " return;\n"
  9406. "}",
  9407. format("void f() {\n"
  9408. "if (aaaaaaaaaaaaaaaa\n"
  9409. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9410. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9411. "return;\n"
  9412. "}",
  9413. Style));
  9414. // Allow functions on a single line.
  9415. verifyFormat("void f() { return; }", Style);
  9416. // Constructor initializers are formatted one per line with the "," on the
  9417. // new line.
  9418. verifyFormat("Constructor()\n"
  9419. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  9420. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  9421. " aaaaaaaaaaaaaa)\n"
  9422. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  9423. "{\n"
  9424. "}",
  9425. Style);
  9426. verifyFormat("SomeClass::Constructor()\n"
  9427. " : a(a)\n"
  9428. "{\n"
  9429. "}",
  9430. Style);
  9431. EXPECT_EQ("SomeClass::Constructor()\n"
  9432. " : a(a)\n"
  9433. "{\n"
  9434. "}",
  9435. format("SomeClass::Constructor():a(a){}", Style));
  9436. verifyFormat("SomeClass::Constructor()\n"
  9437. " : a(a)\n"
  9438. " , b(b)\n"
  9439. " , c(c)\n"
  9440. "{\n"
  9441. "}",
  9442. Style);
  9443. verifyFormat("SomeClass::Constructor()\n"
  9444. " : a(a)\n"
  9445. "{\n"
  9446. " foo();\n"
  9447. " bar();\n"
  9448. "}",
  9449. Style);
  9450. // Access specifiers should be aligned left.
  9451. verifyFormat("class C {\n"
  9452. "public:\n"
  9453. " int i;\n"
  9454. "};",
  9455. Style);
  9456. // Do not align comments.
  9457. verifyFormat("int a; // Do not\n"
  9458. "double b; // align comments.",
  9459. Style);
  9460. // Do not align operands.
  9461. EXPECT_EQ("ASSERT(aaaa\n"
  9462. " || bbbb);",
  9463. format("ASSERT ( aaaa\n||bbbb);", Style));
  9464. // Accept input's line breaks.
  9465. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  9466. " || bbbbbbbbbbbbbbb) {\n"
  9467. " i++;\n"
  9468. "}",
  9469. format("if (aaaaaaaaaaaaaaa\n"
  9470. "|| bbbbbbbbbbbbbbb) { i++; }",
  9471. Style));
  9472. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  9473. " i++;\n"
  9474. "}",
  9475. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  9476. // Don't automatically break all macro definitions (llvm.org/PR17842).
  9477. verifyFormat("#define aNumber 10", Style);
  9478. // However, generally keep the line breaks that the user authored.
  9479. EXPECT_EQ("#define aNumber \\\n"
  9480. " 10",
  9481. format("#define aNumber \\\n"
  9482. " 10",
  9483. Style));
  9484. // Keep empty and one-element array literals on a single line.
  9485. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  9486. " copyItems:YES];",
  9487. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  9488. "copyItems:YES];",
  9489. Style));
  9490. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  9491. " copyItems:YES];",
  9492. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  9493. " copyItems:YES];",
  9494. Style));
  9495. // FIXME: This does not seem right, there should be more indentation before
  9496. // the array literal's entries. Nested blocks have the same problem.
  9497. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9498. " @\"a\",\n"
  9499. " @\"a\"\n"
  9500. "]\n"
  9501. " copyItems:YES];",
  9502. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9503. " @\"a\",\n"
  9504. " @\"a\"\n"
  9505. " ]\n"
  9506. " copyItems:YES];",
  9507. Style));
  9508. EXPECT_EQ(
  9509. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9510. " copyItems:YES];",
  9511. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9512. " copyItems:YES];",
  9513. Style));
  9514. verifyFormat("[self.a b:c c:d];", Style);
  9515. EXPECT_EQ("[self.a b:c\n"
  9516. " c:d];",
  9517. format("[self.a b:c\n"
  9518. "c:d];",
  9519. Style));
  9520. }
  9521. TEST_F(FormatTest, FormatsLambdas) {
  9522. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  9523. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  9524. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  9525. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  9526. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  9527. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  9528. verifyFormat("void f() {\n"
  9529. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  9530. "}\n");
  9531. verifyFormat("void f() {\n"
  9532. " other(x.begin(), //\n"
  9533. " x.end(), //\n"
  9534. " [&](int, int) { return 1; });\n"
  9535. "}\n");
  9536. verifyFormat("SomeFunction([]() { // A cool function...\n"
  9537. " return 43;\n"
  9538. "});");
  9539. EXPECT_EQ("SomeFunction([]() {\n"
  9540. "#define A a\n"
  9541. " return 43;\n"
  9542. "});",
  9543. format("SomeFunction([](){\n"
  9544. "#define A a\n"
  9545. "return 43;\n"
  9546. "});"));
  9547. verifyFormat("void f() {\n"
  9548. " SomeFunction([](decltype(x), A *a) {});\n"
  9549. "}");
  9550. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  9551. " [](const aaaaaaaaaa &a) { return a; });");
  9552. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  9553. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  9554. "});");
  9555. verifyFormat("Constructor()\n"
  9556. " : Field([] { // comment\n"
  9557. " int i;\n"
  9558. " }) {}");
  9559. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  9560. " return some_parameter.size();\n"
  9561. "};");
  9562. verifyFormat("int i = aaaaaa ? 1 //\n"
  9563. " : [] {\n"
  9564. " return 2; //\n"
  9565. " }();");
  9566. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  9567. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  9568. " return x == 2; // force break\n"
  9569. " });");
  9570. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  9571. " int iiiiiiiiiiii) {\n"
  9572. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  9573. "});",
  9574. getLLVMStyleWithColumns(60));
  9575. verifyFormat("SomeFunction({[&] {\n"
  9576. " // comment\n"
  9577. " },\n"
  9578. " [&] {\n"
  9579. " // comment\n"
  9580. " }});");
  9581. verifyFormat("SomeFunction({[&] {\n"
  9582. " // comment\n"
  9583. "}});");
  9584. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  9585. " [&]() { return true; },\n"
  9586. " aaaaa aaaaaaaaa);");
  9587. // Lambdas with return types.
  9588. verifyFormat("int c = []() -> int { return 2; }();\n");
  9589. verifyFormat("int c = []() -> int * { return 2; }();\n");
  9590. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  9591. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  9592. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  9593. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  9594. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  9595. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  9596. verifyFormat("[a, a]() -> a<1> {};");
  9597. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  9598. " int j) -> int {\n"
  9599. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  9600. "};");
  9601. verifyFormat(
  9602. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  9603. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  9604. " return aaaaaaaaaaaaaaaaa;\n"
  9605. " });",
  9606. getLLVMStyleWithColumns(70));
  9607. // Multiple lambdas in the same parentheses change indentation rules.
  9608. verifyFormat("SomeFunction(\n"
  9609. " []() {\n"
  9610. " int i = 42;\n"
  9611. " return i;\n"
  9612. " },\n"
  9613. " []() {\n"
  9614. " int j = 43;\n"
  9615. " return j;\n"
  9616. " });");
  9617. // More complex introducers.
  9618. verifyFormat("return [i, args...] {};");
  9619. // Not lambdas.
  9620. verifyFormat("constexpr char hello[]{\"hello\"};");
  9621. verifyFormat("double &operator[](int i) { return 0; }\n"
  9622. "int i;");
  9623. verifyFormat("std::unique_ptr<int[]> foo() {}");
  9624. verifyFormat("int i = a[a][a]->f();");
  9625. verifyFormat("int i = (*b)[a]->f();");
  9626. // Other corner cases.
  9627. verifyFormat("void f() {\n"
  9628. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  9629. " );\n"
  9630. "}");
  9631. // Lambdas created through weird macros.
  9632. verifyFormat("void f() {\n"
  9633. " MACRO((const AA &a) { return 1; });\n"
  9634. "}");
  9635. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  9636. " doo_dah();\n"
  9637. " doo_dah();\n"
  9638. " })) {\n"
  9639. "}");
  9640. verifyFormat("auto lambda = []() {\n"
  9641. " int a = 2\n"
  9642. "#if A\n"
  9643. " + 2\n"
  9644. "#endif\n"
  9645. " ;\n"
  9646. "};");
  9647. }
  9648. TEST_F(FormatTest, FormatsBlocks) {
  9649. FormatStyle ShortBlocks = getLLVMStyle();
  9650. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  9651. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  9652. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  9653. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  9654. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  9655. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  9656. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  9657. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  9658. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  9659. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  9660. verifyFormat("[operation setCompletionBlock:^{\n"
  9661. " [self onOperationDone];\n"
  9662. "}];");
  9663. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  9664. " [self onOperationDone];\n"
  9665. "}]};");
  9666. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  9667. " f();\n"
  9668. "}];");
  9669. verifyFormat("int a = [operation block:^int(int *i) {\n"
  9670. " return 1;\n"
  9671. "}];");
  9672. verifyFormat("[myObject doSomethingWith:arg1\n"
  9673. " aaa:^int(int *a) {\n"
  9674. " return 1;\n"
  9675. " }\n"
  9676. " bbb:f(a * bbbbbbbb)];");
  9677. verifyFormat("[operation setCompletionBlock:^{\n"
  9678. " [self.delegate newDataAvailable];\n"
  9679. "}];",
  9680. getLLVMStyleWithColumns(60));
  9681. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  9682. " NSString *path = [self sessionFilePath];\n"
  9683. " if (path) {\n"
  9684. " // ...\n"
  9685. " }\n"
  9686. "});");
  9687. verifyFormat("[[SessionService sharedService]\n"
  9688. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9689. " if (window) {\n"
  9690. " [self windowDidLoad:window];\n"
  9691. " } else {\n"
  9692. " [self errorLoadingWindow];\n"
  9693. " }\n"
  9694. " }];");
  9695. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9696. " // ...\n"
  9697. "};\n",
  9698. getLLVMStyleWithColumns(40));
  9699. verifyFormat("[[SessionService sharedService]\n"
  9700. " loadWindowWithCompletionBlock: //\n"
  9701. " ^(SessionWindow *window) {\n"
  9702. " if (window) {\n"
  9703. " [self windowDidLoad:window];\n"
  9704. " } else {\n"
  9705. " [self errorLoadingWindow];\n"
  9706. " }\n"
  9707. " }];",
  9708. getLLVMStyleWithColumns(60));
  9709. verifyFormat("[myObject doSomethingWith:arg1\n"
  9710. " firstBlock:^(Foo *a) {\n"
  9711. " // ...\n"
  9712. " int i;\n"
  9713. " }\n"
  9714. " secondBlock:^(Bar *b) {\n"
  9715. " // ...\n"
  9716. " int i;\n"
  9717. " }\n"
  9718. " thirdBlock:^Foo(Bar *b) {\n"
  9719. " // ...\n"
  9720. " int i;\n"
  9721. " }];");
  9722. verifyFormat("[myObject doSomethingWith:arg1\n"
  9723. " firstBlock:-1\n"
  9724. " secondBlock:^(Bar *b) {\n"
  9725. " // ...\n"
  9726. " int i;\n"
  9727. " }];");
  9728. verifyFormat("f(^{\n"
  9729. " @autoreleasepool {\n"
  9730. " if (a) {\n"
  9731. " g();\n"
  9732. " }\n"
  9733. " }\n"
  9734. "});");
  9735. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  9736. FormatStyle FourIndent = getLLVMStyle();
  9737. FourIndent.ObjCBlockIndentWidth = 4;
  9738. verifyFormat("[operation setCompletionBlock:^{\n"
  9739. " [self onOperationDone];\n"
  9740. "}];",
  9741. FourIndent);
  9742. }
  9743. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  9744. FormatStyle ZeroColumn = getLLVMStyle();
  9745. ZeroColumn.ColumnLimit = 0;
  9746. verifyFormat("[[SessionService sharedService] "
  9747. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9748. " if (window) {\n"
  9749. " [self windowDidLoad:window];\n"
  9750. " } else {\n"
  9751. " [self errorLoadingWindow];\n"
  9752. " }\n"
  9753. "}];",
  9754. ZeroColumn);
  9755. EXPECT_EQ("[[SessionService sharedService]\n"
  9756. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9757. " if (window) {\n"
  9758. " [self windowDidLoad:window];\n"
  9759. " } else {\n"
  9760. " [self errorLoadingWindow];\n"
  9761. " }\n"
  9762. " }];",
  9763. format("[[SessionService sharedService]\n"
  9764. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9765. " if (window) {\n"
  9766. " [self windowDidLoad:window];\n"
  9767. " } else {\n"
  9768. " [self errorLoadingWindow];\n"
  9769. " }\n"
  9770. "}];",
  9771. ZeroColumn));
  9772. verifyFormat("[myObject doSomethingWith:arg1\n"
  9773. " firstBlock:^(Foo *a) {\n"
  9774. " // ...\n"
  9775. " int i;\n"
  9776. " }\n"
  9777. " secondBlock:^(Bar *b) {\n"
  9778. " // ...\n"
  9779. " int i;\n"
  9780. " }\n"
  9781. " thirdBlock:^Foo(Bar *b) {\n"
  9782. " // ...\n"
  9783. " int i;\n"
  9784. " }];",
  9785. ZeroColumn);
  9786. verifyFormat("f(^{\n"
  9787. " @autoreleasepool {\n"
  9788. " if (a) {\n"
  9789. " g();\n"
  9790. " }\n"
  9791. " }\n"
  9792. "});",
  9793. ZeroColumn);
  9794. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9795. " // ...\n"
  9796. "};",
  9797. ZeroColumn);
  9798. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  9799. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  9800. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  9801. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  9802. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  9803. " int i;\n"
  9804. "};",
  9805. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  9806. }
  9807. TEST_F(FormatTest, SupportsCRLF) {
  9808. EXPECT_EQ("int a;\r\n"
  9809. "int b;\r\n"
  9810. "int c;\r\n",
  9811. format("int a;\r\n"
  9812. " int b;\r\n"
  9813. " int c;\r\n",
  9814. getLLVMStyle()));
  9815. EXPECT_EQ("int a;\r\n"
  9816. "int b;\r\n"
  9817. "int c;\r\n",
  9818. format("int a;\r\n"
  9819. " int b;\n"
  9820. " int c;\r\n",
  9821. getLLVMStyle()));
  9822. EXPECT_EQ("int a;\n"
  9823. "int b;\n"
  9824. "int c;\n",
  9825. format("int a;\r\n"
  9826. " int b;\n"
  9827. " int c;\n",
  9828. getLLVMStyle()));
  9829. EXPECT_EQ("\"aaaaaaa \"\r\n"
  9830. "\"bbbbbbb\";\r\n",
  9831. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  9832. EXPECT_EQ("#define A \\\r\n"
  9833. " b; \\\r\n"
  9834. " c; \\\r\n"
  9835. " d;\r\n",
  9836. format("#define A \\\r\n"
  9837. " b; \\\r\n"
  9838. " c; d; \r\n",
  9839. getGoogleStyle()));
  9840. EXPECT_EQ("/*\r\n"
  9841. "multi line block comments\r\n"
  9842. "should not introduce\r\n"
  9843. "an extra carriage return\r\n"
  9844. "*/\r\n",
  9845. format("/*\r\n"
  9846. "multi line block comments\r\n"
  9847. "should not introduce\r\n"
  9848. "an extra carriage return\r\n"
  9849. "*/\r\n"));
  9850. }
  9851. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  9852. verifyFormat("MY_CLASS(C) {\n"
  9853. " int i;\n"
  9854. " int j;\n"
  9855. "};");
  9856. }
  9857. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  9858. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  9859. TwoIndent.ContinuationIndentWidth = 2;
  9860. EXPECT_EQ("int i =\n"
  9861. " longFunction(\n"
  9862. " arg);",
  9863. format("int i = longFunction(arg);", TwoIndent));
  9864. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  9865. SixIndent.ContinuationIndentWidth = 6;
  9866. EXPECT_EQ("int i =\n"
  9867. " longFunction(\n"
  9868. " arg);",
  9869. format("int i = longFunction(arg);", SixIndent));
  9870. }
  9871. TEST_F(FormatTest, SpacesInAngles) {
  9872. FormatStyle Spaces = getLLVMStyle();
  9873. Spaces.SpacesInAngles = true;
  9874. verifyFormat("static_cast< int >(arg);", Spaces);
  9875. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  9876. verifyFormat("f< int, float >();", Spaces);
  9877. verifyFormat("template <> g() {}", Spaces);
  9878. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  9879. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  9880. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  9881. Spaces);
  9882. Spaces.Standard = FormatStyle::LS_Cpp03;
  9883. Spaces.SpacesInAngles = true;
  9884. verifyFormat("A< A< int > >();", Spaces);
  9885. Spaces.SpacesInAngles = false;
  9886. verifyFormat("A<A<int> >();", Spaces);
  9887. Spaces.Standard = FormatStyle::LS_Cpp11;
  9888. Spaces.SpacesInAngles = true;
  9889. verifyFormat("A< A< int > >();", Spaces);
  9890. Spaces.SpacesInAngles = false;
  9891. verifyFormat("A<A<int>>();", Spaces);
  9892. }
  9893. TEST_F(FormatTest, TripleAngleBrackets) {
  9894. verifyFormat("f<<<1, 1>>>();");
  9895. verifyFormat("f<<<1, 1, 1, s>>>();");
  9896. verifyFormat("f<<<a, b, c, d>>>();");
  9897. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  9898. verifyFormat("f<param><<<1, 1>>>();");
  9899. verifyFormat("f<1><<<1, 1>>>();");
  9900. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  9901. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9902. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  9903. }
  9904. TEST_F(FormatTest, MergeLessLessAtEnd) {
  9905. verifyFormat("<<");
  9906. EXPECT_EQ("< < <", format("\\\n<<<"));
  9907. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9908. "aaallvm::outs() <<");
  9909. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9910. "aaaallvm::outs()\n <<");
  9911. }
  9912. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  9913. std::string code = "#if A\n"
  9914. "#if B\n"
  9915. "a.\n"
  9916. "#endif\n"
  9917. " a = 1;\n"
  9918. "#else\n"
  9919. "#endif\n"
  9920. "#if C\n"
  9921. "#else\n"
  9922. "#endif\n";
  9923. EXPECT_EQ(code, format(code));
  9924. }
  9925. TEST_F(FormatTest, HandleConflictMarkers) {
  9926. // Git/SVN conflict markers.
  9927. EXPECT_EQ("int a;\n"
  9928. "void f() {\n"
  9929. " callme(some(parameter1,\n"
  9930. "<<<<<<< text by the vcs\n"
  9931. " parameter2),\n"
  9932. "||||||| text by the vcs\n"
  9933. " parameter2),\n"
  9934. " parameter3,\n"
  9935. "======= text by the vcs\n"
  9936. " parameter2, parameter3),\n"
  9937. ">>>>>>> text by the vcs\n"
  9938. " otherparameter);\n",
  9939. format("int a;\n"
  9940. "void f() {\n"
  9941. " callme(some(parameter1,\n"
  9942. "<<<<<<< text by the vcs\n"
  9943. " parameter2),\n"
  9944. "||||||| text by the vcs\n"
  9945. " parameter2),\n"
  9946. " parameter3,\n"
  9947. "======= text by the vcs\n"
  9948. " parameter2,\n"
  9949. " parameter3),\n"
  9950. ">>>>>>> text by the vcs\n"
  9951. " otherparameter);\n"));
  9952. // Perforce markers.
  9953. EXPECT_EQ("void f() {\n"
  9954. " function(\n"
  9955. ">>>> text by the vcs\n"
  9956. " parameter,\n"
  9957. "==== text by the vcs\n"
  9958. " parameter,\n"
  9959. "==== text by the vcs\n"
  9960. " parameter,\n"
  9961. "<<<< text by the vcs\n"
  9962. " parameter);\n",
  9963. format("void f() {\n"
  9964. " function(\n"
  9965. ">>>> text by the vcs\n"
  9966. " parameter,\n"
  9967. "==== text by the vcs\n"
  9968. " parameter,\n"
  9969. "==== text by the vcs\n"
  9970. " parameter,\n"
  9971. "<<<< text by the vcs\n"
  9972. " parameter);\n"));
  9973. EXPECT_EQ("<<<<<<<\n"
  9974. "|||||||\n"
  9975. "=======\n"
  9976. ">>>>>>>",
  9977. format("<<<<<<<\n"
  9978. "|||||||\n"
  9979. "=======\n"
  9980. ">>>>>>>"));
  9981. EXPECT_EQ("<<<<<<<\n"
  9982. "|||||||\n"
  9983. "int i;\n"
  9984. "=======\n"
  9985. ">>>>>>>",
  9986. format("<<<<<<<\n"
  9987. "|||||||\n"
  9988. "int i;\n"
  9989. "=======\n"
  9990. ">>>>>>>"));
  9991. // FIXME: Handle parsing of macros around conflict markers correctly:
  9992. EXPECT_EQ("#define Macro \\\n"
  9993. "<<<<<<<\n"
  9994. "Something \\\n"
  9995. "|||||||\n"
  9996. "Else \\\n"
  9997. "=======\n"
  9998. "Other \\\n"
  9999. ">>>>>>>\n"
  10000. " End int i;\n",
  10001. format("#define Macro \\\n"
  10002. "<<<<<<<\n"
  10003. " Something \\\n"
  10004. "|||||||\n"
  10005. " Else \\\n"
  10006. "=======\n"
  10007. " Other \\\n"
  10008. ">>>>>>>\n"
  10009. " End\n"
  10010. "int i;\n"));
  10011. }
  10012. TEST_F(FormatTest, DisableRegions) {
  10013. EXPECT_EQ("int i;\n"
  10014. "// clang-format off\n"
  10015. " int j;\n"
  10016. "// clang-format on\n"
  10017. "int k;",
  10018. format(" int i;\n"
  10019. " // clang-format off\n"
  10020. " int j;\n"
  10021. " // clang-format on\n"
  10022. " int k;"));
  10023. EXPECT_EQ("int i;\n"
  10024. "/* clang-format off */\n"
  10025. " int j;\n"
  10026. "/* clang-format on */\n"
  10027. "int k;",
  10028. format(" int i;\n"
  10029. " /* clang-format off */\n"
  10030. " int j;\n"
  10031. " /* clang-format on */\n"
  10032. " int k;"));
  10033. }
  10034. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10035. format("? ) =");
  10036. verifyNoCrash("#define a\\\n /**/}");
  10037. }
  10038. } // end namespace
  10039. } // end namespace format
  10040. } // end namespace clang