FormatTest.cpp 247 KB

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