FormatTest.cpp 418 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  18. class FormatTest : public ::testing::Test {
  19. protected:
  20. enum IncompleteCheck {
  21. IC_ExpectComplete,
  22. IC_ExpectIncomplete,
  23. IC_DoNotCheck
  24. };
  25. std::string format(llvm::StringRef Code,
  26. const FormatStyle &Style = getLLVMStyle(),
  27. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  28. DEBUG(llvm::errs() << "---\n");
  29. DEBUG(llvm::errs() << Code << "\n\n");
  30. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  31. bool IncompleteFormat = false;
  32. tooling::Replacements Replaces =
  33. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  34. if (CheckIncomplete != IC_DoNotCheck) {
  35. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  36. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  37. }
  38. ReplacementCount = Replaces.size();
  39. std::string Result = applyAllReplacements(Code, Replaces);
  40. EXPECT_NE("", Result);
  41. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  42. return Result;
  43. }
  44. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  45. FormatStyle Style = getLLVMStyle();
  46. Style.ColumnLimit = ColumnLimit;
  47. return Style;
  48. }
  49. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getGoogleStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. void verifyFormat(llvm::StringRef Code,
  55. const FormatStyle &Style = getLLVMStyle()) {
  56. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  57. }
  58. void verifyIncompleteFormat(llvm::StringRef Code,
  59. const FormatStyle &Style = getLLVMStyle()) {
  60. EXPECT_EQ(Code.str(),
  61. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  62. }
  63. void verifyGoogleFormat(llvm::StringRef Code) {
  64. verifyFormat(Code, getGoogleStyle());
  65. }
  66. void verifyIndependentOfContext(llvm::StringRef text) {
  67. verifyFormat(text);
  68. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  69. }
  70. /// \brief Verify that clang-format does not crash on the given input.
  71. void verifyNoCrash(llvm::StringRef Code,
  72. const FormatStyle &Style = getLLVMStyle()) {
  73. format(Code, Style, IC_DoNotCheck);
  74. }
  75. int ReplacementCount;
  76. };
  77. TEST_F(FormatTest, MessUp) {
  78. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  79. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  80. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  81. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  82. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // Basic function tests.
  86. //===----------------------------------------------------------------------===//
  87. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  88. EXPECT_EQ(";", format(";"));
  89. }
  90. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  91. EXPECT_EQ("int i;", format(" int i;"));
  92. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  93. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  94. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  95. }
  96. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  97. EXPECT_EQ("int i;", format("int\ni;"));
  98. }
  99. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  100. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedCall) {
  103. verifyFormat("Method(f1, f2(f3));");
  104. verifyFormat("Method(f1(f2, f3()));");
  105. verifyFormat("Method(f1(f2, (f3())));");
  106. }
  107. TEST_F(FormatTest, NestedNameSpecifiers) {
  108. verifyFormat("vector<::Type> v;");
  109. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  110. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  111. verifyFormat("bool a = 2 < ::SomeFunction();");
  112. }
  113. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  114. EXPECT_EQ("if (a) {\n"
  115. " f();\n"
  116. "}",
  117. format("if(a){f();}"));
  118. EXPECT_EQ(4, ReplacementCount);
  119. EXPECT_EQ("if (a) {\n"
  120. " f();\n"
  121. "}",
  122. format("if (a) {\n"
  123. " f();\n"
  124. "}"));
  125. EXPECT_EQ(0, ReplacementCount);
  126. EXPECT_EQ("/*\r\n"
  127. "\r\n"
  128. "*/\r\n",
  129. format("/*\r\n"
  130. "\r\n"
  131. "*/\r\n"));
  132. EXPECT_EQ(0, ReplacementCount);
  133. }
  134. TEST_F(FormatTest, RemovesEmptyLines) {
  135. EXPECT_EQ("class C {\n"
  136. " int i;\n"
  137. "};",
  138. format("class C {\n"
  139. " int i;\n"
  140. "\n"
  141. "};"));
  142. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  143. EXPECT_EQ("namespace N {\n"
  144. "\n"
  145. "int i;\n"
  146. "}",
  147. format("namespace N {\n"
  148. "\n"
  149. "int i;\n"
  150. "}",
  151. getGoogleStyle()));
  152. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  153. "\n"
  154. "int i;\n"
  155. "}",
  156. format("extern /**/ \"C\" /**/ {\n"
  157. "\n"
  158. "int i;\n"
  159. "}",
  160. getGoogleStyle()));
  161. // ...but do keep inlining and removing empty lines for non-block extern "C"
  162. // functions.
  163. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  164. EXPECT_EQ("extern \"C\" int f() {\n"
  165. " int i = 42;\n"
  166. " return i;\n"
  167. "}",
  168. format("extern \"C\" int f() {\n"
  169. "\n"
  170. " int i = 42;\n"
  171. " return i;\n"
  172. "}",
  173. getGoogleStyle()));
  174. // Remove empty lines at the beginning and end of blocks.
  175. EXPECT_EQ("void f() {\n"
  176. "\n"
  177. " if (a) {\n"
  178. "\n"
  179. " f();\n"
  180. " }\n"
  181. "}",
  182. format("void f() {\n"
  183. "\n"
  184. " if (a) {\n"
  185. "\n"
  186. " f();\n"
  187. "\n"
  188. " }\n"
  189. "\n"
  190. "}",
  191. getLLVMStyle()));
  192. EXPECT_EQ("void f() {\n"
  193. " if (a) {\n"
  194. " f();\n"
  195. " }\n"
  196. "}",
  197. format("void f() {\n"
  198. "\n"
  199. " if (a) {\n"
  200. "\n"
  201. " f();\n"
  202. "\n"
  203. " }\n"
  204. "\n"
  205. "}",
  206. getGoogleStyle()));
  207. // Don't remove empty lines in more complex control statements.
  208. EXPECT_EQ("void f() {\n"
  209. " if (a) {\n"
  210. " f();\n"
  211. "\n"
  212. " } else if (b) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. "\n"
  220. " } else if (b) {\n"
  221. " f();\n"
  222. "\n"
  223. " }\n"
  224. "\n"
  225. "}"));
  226. // FIXME: This is slightly inconsistent.
  227. EXPECT_EQ("namespace {\n"
  228. "int i;\n"
  229. "}",
  230. format("namespace {\n"
  231. "int i;\n"
  232. "\n"
  233. "}"));
  234. EXPECT_EQ("namespace {\n"
  235. "int i;\n"
  236. "\n"
  237. "} // namespace",
  238. format("namespace {\n"
  239. "int i;\n"
  240. "\n"
  241. "} // namespace"));
  242. }
  243. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  244. verifyFormat("x = (a) and (b);");
  245. verifyFormat("x = (a) or (b);");
  246. verifyFormat("x = (a) bitand (b);");
  247. verifyFormat("x = (a) bitor (b);");
  248. verifyFormat("x = (a) not_eq (b);");
  249. verifyFormat("x = (a) and_eq (b);");
  250. verifyFormat("x = (a) or_eq (b);");
  251. verifyFormat("x = (a) xor (b);");
  252. }
  253. //===----------------------------------------------------------------------===//
  254. // Tests for control statements.
  255. //===----------------------------------------------------------------------===//
  256. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  257. verifyFormat("if (true)\n f();\ng();");
  258. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  259. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  260. FormatStyle AllowsMergedIf = getLLVMStyle();
  261. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  262. verifyFormat("if (a)\n"
  263. " // comment\n"
  264. " f();",
  265. AllowsMergedIf);
  266. verifyFormat("if (a)\n"
  267. " ;",
  268. AllowsMergedIf);
  269. verifyFormat("if (a)\n"
  270. " if (b) return;",
  271. AllowsMergedIf);
  272. verifyFormat("if (a) // Can't merge this\n"
  273. " f();\n",
  274. AllowsMergedIf);
  275. verifyFormat("if (a) /* still don't merge */\n"
  276. " f();",
  277. AllowsMergedIf);
  278. verifyFormat("if (a) { // Never merge this\n"
  279. " f();\n"
  280. "}",
  281. AllowsMergedIf);
  282. verifyFormat("if (a) { /* Never merge this */\n"
  283. " f();\n"
  284. "}",
  285. AllowsMergedIf);
  286. AllowsMergedIf.ColumnLimit = 14;
  287. verifyFormat("if (a) return;", AllowsMergedIf);
  288. verifyFormat("if (aaaaaaaaa)\n"
  289. " return;",
  290. AllowsMergedIf);
  291. AllowsMergedIf.ColumnLimit = 13;
  292. verifyFormat("if (a)\n return;", AllowsMergedIf);
  293. }
  294. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  295. FormatStyle AllowsMergedLoops = getLLVMStyle();
  296. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  297. verifyFormat("while (true) continue;", AllowsMergedLoops);
  298. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  299. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  300. verifyFormat("while (true)\n"
  301. " ;",
  302. AllowsMergedLoops);
  303. verifyFormat("for (;;)\n"
  304. " ;",
  305. AllowsMergedLoops);
  306. verifyFormat("for (;;)\n"
  307. " for (;;) continue;",
  308. AllowsMergedLoops);
  309. verifyFormat("for (;;) // Can't merge this\n"
  310. " continue;",
  311. AllowsMergedLoops);
  312. verifyFormat("for (;;) /* still don't merge */\n"
  313. " continue;",
  314. AllowsMergedLoops);
  315. }
  316. TEST_F(FormatTest, FormatShortBracedStatements) {
  317. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  318. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  319. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  320. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  321. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  322. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  323. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  324. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  325. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  326. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  327. verifyFormat("if (true) { //\n"
  328. " f();\n"
  329. "}",
  330. AllowSimpleBracedStatements);
  331. verifyFormat("if (true) {\n"
  332. " f();\n"
  333. " f();\n"
  334. "}",
  335. AllowSimpleBracedStatements);
  336. verifyFormat("if (true) {\n"
  337. " f();\n"
  338. "} else {\n"
  339. " f();\n"
  340. "}",
  341. AllowSimpleBracedStatements);
  342. verifyFormat("template <int> struct A2 {\n"
  343. " struct B {};\n"
  344. "};",
  345. AllowSimpleBracedStatements);
  346. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  347. verifyFormat("if (true) {\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  358. verifyFormat("while (true) {\n"
  359. " f();\n"
  360. "}",
  361. AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. }
  367. TEST_F(FormatTest, ParseIfElse) {
  368. verifyFormat("if (true)\n"
  369. " if (true)\n"
  370. " if (true)\n"
  371. " f();\n"
  372. " else\n"
  373. " g();\n"
  374. " else\n"
  375. " h();\n"
  376. "else\n"
  377. " i();");
  378. verifyFormat("if (true)\n"
  379. " if (true)\n"
  380. " if (true) {\n"
  381. " if (true)\n"
  382. " f();\n"
  383. " } else {\n"
  384. " g();\n"
  385. " }\n"
  386. " else\n"
  387. " h();\n"
  388. "else {\n"
  389. " i();\n"
  390. "}");
  391. verifyFormat("void f() {\n"
  392. " if (a) {\n"
  393. " } else {\n"
  394. " }\n"
  395. "}");
  396. }
  397. TEST_F(FormatTest, ElseIf) {
  398. verifyFormat("if (a) {\n} else if (b) {\n}");
  399. verifyFormat("if (a)\n"
  400. " f();\n"
  401. "else if (b)\n"
  402. " g();\n"
  403. "else\n"
  404. " h();");
  405. verifyFormat("if (a) {\n"
  406. " f();\n"
  407. "}\n"
  408. "// or else ..\n"
  409. "else {\n"
  410. " g()\n"
  411. "}");
  412. verifyFormat("if (a) {\n"
  413. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  415. "}");
  416. verifyFormat("if (a) {\n"
  417. "} else if (\n"
  418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  419. "}",
  420. getLLVMStyleWithColumns(62));
  421. }
  422. TEST_F(FormatTest, FormatsForLoop) {
  423. verifyFormat(
  424. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  425. " ++VeryVeryLongLoopVariable)\n"
  426. " ;");
  427. verifyFormat("for (;;)\n"
  428. " f();");
  429. verifyFormat("for (;;) {\n}");
  430. verifyFormat("for (;;) {\n"
  431. " f();\n"
  432. "}");
  433. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  434. verifyFormat(
  435. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  436. " E = UnwrappedLines.end();\n"
  437. " I != E; ++I) {\n}");
  438. verifyFormat(
  439. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  440. " ++IIIII) {\n}");
  441. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  442. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  443. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  444. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  445. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  446. " E = FD->getDeclsInPrototypeScope().end();\n"
  447. " I != E; ++I) {\n}");
  448. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  449. " I = Container.begin(),\n"
  450. " E = Container.end();\n"
  451. " I != E; ++I) {\n}",
  452. getLLVMStyleWithColumns(76));
  453. verifyFormat(
  454. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  456. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  458. " ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  460. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  461. " ++i) {\n}");
  462. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  463. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  464. "}");
  465. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  466. " aaaaaaaaaa);\n"
  467. " iter; ++iter) {\n"
  468. "}");
  469. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  472. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  473. FormatStyle NoBinPacking = getLLVMStyle();
  474. NoBinPacking.BinPackParameters = false;
  475. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  476. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  477. " aaaaaaaaaaaaaaaa,\n"
  478. " aaaaaaaaaaaaaaaa,\n"
  479. " aaaaaaaaaaaaaaaa);\n"
  480. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  481. "}",
  482. NoBinPacking);
  483. verifyFormat(
  484. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  485. " E = UnwrappedLines.end();\n"
  486. " I != E;\n"
  487. " ++I) {\n}",
  488. NoBinPacking);
  489. }
  490. TEST_F(FormatTest, RangeBasedForLoops) {
  491. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  492. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  493. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  494. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  495. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  496. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  497. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  499. }
  500. TEST_F(FormatTest, ForEachLoops) {
  501. verifyFormat("void f() {\n"
  502. " foreach (Item *item, itemlist) {}\n"
  503. " Q_FOREACH (Item *item, itemlist) {}\n"
  504. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  505. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  506. "}");
  507. // As function-like macros.
  508. verifyFormat("#define foreach(x, y)\n"
  509. "#define Q_FOREACH(x, y)\n"
  510. "#define BOOST_FOREACH(x, y)\n"
  511. "#define UNKNOWN_FOREACH(x, y)\n");
  512. // Not as function-like macros.
  513. verifyFormat("#define foreach (x, y)\n"
  514. "#define Q_FOREACH (x, y)\n"
  515. "#define BOOST_FOREACH (x, y)\n"
  516. "#define UNKNOWN_FOREACH (x, y)\n");
  517. }
  518. TEST_F(FormatTest, FormatsWhileLoop) {
  519. verifyFormat("while (true) {\n}");
  520. verifyFormat("while (true)\n"
  521. " f();");
  522. verifyFormat("while () {\n}");
  523. verifyFormat("while () {\n"
  524. " f();\n"
  525. "}");
  526. }
  527. TEST_F(FormatTest, FormatsDoWhile) {
  528. verifyFormat("do {\n"
  529. " do_something();\n"
  530. "} while (something());");
  531. verifyFormat("do\n"
  532. " do_something();\n"
  533. "while (something());");
  534. }
  535. TEST_F(FormatTest, FormatsSwitchStatement) {
  536. verifyFormat("switch (x) {\n"
  537. "case 1:\n"
  538. " f();\n"
  539. " break;\n"
  540. "case kFoo:\n"
  541. "case ns::kBar:\n"
  542. "case kBaz:\n"
  543. " break;\n"
  544. "default:\n"
  545. " g();\n"
  546. " break;\n"
  547. "}");
  548. verifyFormat("switch (x) {\n"
  549. "case 1: {\n"
  550. " f();\n"
  551. " break;\n"
  552. "}\n"
  553. "case 2: {\n"
  554. " break;\n"
  555. "}\n"
  556. "}");
  557. verifyFormat("switch (x) {\n"
  558. "case 1: {\n"
  559. " f();\n"
  560. " {\n"
  561. " g();\n"
  562. " h();\n"
  563. " }\n"
  564. " break;\n"
  565. "}\n"
  566. "}");
  567. verifyFormat("switch (x) {\n"
  568. "case 1: {\n"
  569. " f();\n"
  570. " if (foo) {\n"
  571. " g();\n"
  572. " h();\n"
  573. " }\n"
  574. " break;\n"
  575. "}\n"
  576. "}");
  577. verifyFormat("switch (x) {\n"
  578. "case 1: {\n"
  579. " f();\n"
  580. " g();\n"
  581. "} break;\n"
  582. "}");
  583. verifyFormat("switch (test)\n"
  584. " ;");
  585. verifyFormat("switch (x) {\n"
  586. "default: {\n"
  587. " // Do nothing.\n"
  588. "}\n"
  589. "}");
  590. verifyFormat("switch (x) {\n"
  591. "// comment\n"
  592. "// if 1, do f()\n"
  593. "case 1:\n"
  594. " f();\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "case 1:\n"
  598. " // Do amazing stuff\n"
  599. " {\n"
  600. " f();\n"
  601. " g();\n"
  602. " }\n"
  603. " break;\n"
  604. "}");
  605. verifyFormat("#define A \\\n"
  606. " switch (x) { \\\n"
  607. " case a: \\\n"
  608. " foo = b; \\\n"
  609. " }",
  610. getLLVMStyleWithColumns(20));
  611. verifyFormat("#define OPERATION_CASE(name) \\\n"
  612. " case OP_name: \\\n"
  613. " return operations::Operation##name\n",
  614. getLLVMStyleWithColumns(40));
  615. verifyFormat("switch (x) {\n"
  616. "case 1:;\n"
  617. "default:;\n"
  618. " int i;\n"
  619. "}");
  620. verifyGoogleFormat("switch (x) {\n"
  621. " case 1:\n"
  622. " f();\n"
  623. " break;\n"
  624. " case kFoo:\n"
  625. " case ns::kBar:\n"
  626. " case kBaz:\n"
  627. " break;\n"
  628. " default:\n"
  629. " g();\n"
  630. " break;\n"
  631. "}");
  632. verifyGoogleFormat("switch (x) {\n"
  633. " case 1: {\n"
  634. " f();\n"
  635. " break;\n"
  636. " }\n"
  637. "}");
  638. verifyGoogleFormat("switch (test)\n"
  639. " ;");
  640. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  641. " case OP_name: \\\n"
  642. " return operations::Operation##name\n");
  643. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  644. " // Get the correction operation class.\n"
  645. " switch (OpCode) {\n"
  646. " CASE(Add);\n"
  647. " CASE(Subtract);\n"
  648. " default:\n"
  649. " return operations::Unknown;\n"
  650. " }\n"
  651. "#undef OPERATION_CASE\n"
  652. "}");
  653. verifyFormat("DEBUG({\n"
  654. " switch (x) {\n"
  655. " case A:\n"
  656. " f();\n"
  657. " break;\n"
  658. " // On B:\n"
  659. " case B:\n"
  660. " g();\n"
  661. " break;\n"
  662. " }\n"
  663. "});");
  664. verifyFormat("switch (a) {\n"
  665. "case (b):\n"
  666. " return;\n"
  667. "}");
  668. verifyFormat("switch (a) {\n"
  669. "case some_namespace::\n"
  670. " some_constant:\n"
  671. " return;\n"
  672. "}",
  673. getLLVMStyleWithColumns(34));
  674. }
  675. TEST_F(FormatTest, CaseRanges) {
  676. verifyFormat("switch (x) {\n"
  677. "case 'A' ... 'Z':\n"
  678. "case 1 ... 5:\n"
  679. " break;\n"
  680. "}");
  681. }
  682. TEST_F(FormatTest, ShortCaseLabels) {
  683. FormatStyle Style = getLLVMStyle();
  684. Style.AllowShortCaseLabelsOnASingleLine = true;
  685. verifyFormat("switch (a) {\n"
  686. "case 1: x = 1; break;\n"
  687. "case 2: return;\n"
  688. "case 3:\n"
  689. "case 4:\n"
  690. "case 5: return;\n"
  691. "case 6: // comment\n"
  692. " return;\n"
  693. "case 7:\n"
  694. " // comment\n"
  695. " return;\n"
  696. "case 8:\n"
  697. " x = 8; // comment\n"
  698. " break;\n"
  699. "default: y = 1; break;\n"
  700. "}",
  701. Style);
  702. verifyFormat("switch (a) {\n"
  703. "#if FOO\n"
  704. "case 0: return 0;\n"
  705. "#endif\n"
  706. "}",
  707. Style);
  708. verifyFormat("switch (a) {\n"
  709. "case 1: {\n"
  710. "}\n"
  711. "case 2: {\n"
  712. " return;\n"
  713. "}\n"
  714. "case 3: {\n"
  715. " x = 1;\n"
  716. " return;\n"
  717. "}\n"
  718. "case 4:\n"
  719. " if (x)\n"
  720. " return;\n"
  721. "}",
  722. Style);
  723. Style.ColumnLimit = 21;
  724. verifyFormat("switch (a) {\n"
  725. "case 1: x = 1; break;\n"
  726. "case 2: return;\n"
  727. "case 3:\n"
  728. "case 4:\n"
  729. "case 5: return;\n"
  730. "default:\n"
  731. " y = 1;\n"
  732. " break;\n"
  733. "}",
  734. Style);
  735. }
  736. TEST_F(FormatTest, FormatsLabels) {
  737. verifyFormat("void f() {\n"
  738. " some_code();\n"
  739. "test_label:\n"
  740. " some_other_code();\n"
  741. " {\n"
  742. " some_more_code();\n"
  743. " another_label:\n"
  744. " some_more_code();\n"
  745. " }\n"
  746. "}");
  747. verifyFormat("{\n"
  748. " some_code();\n"
  749. "test_label:\n"
  750. " some_other_code();\n"
  751. "}");
  752. verifyFormat("{\n"
  753. " some_code();\n"
  754. "test_label:;\n"
  755. " int i = 0;\n"
  756. "}");
  757. }
  758. //===----------------------------------------------------------------------===//
  759. // Tests for comments.
  760. //===----------------------------------------------------------------------===//
  761. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  762. verifyFormat("//* */");
  763. verifyFormat("// line 1\n"
  764. "// line 2\n"
  765. "void f() {}\n");
  766. verifyFormat("void f() {\n"
  767. " // Doesn't do anything\n"
  768. "}");
  769. verifyFormat("SomeObject\n"
  770. " // Calling someFunction on SomeObject\n"
  771. " .someFunction();");
  772. verifyFormat("auto result = SomeObject\n"
  773. " // Calling someFunction on SomeObject\n"
  774. " .someFunction();");
  775. verifyFormat("void f(int i, // some comment (probably for i)\n"
  776. " int j, // some comment (probably for j)\n"
  777. " int k); // some comment (probably for k)");
  778. verifyFormat("void f(int i,\n"
  779. " // some comment (probably for j)\n"
  780. " int j,\n"
  781. " // some comment (probably for k)\n"
  782. " int k);");
  783. verifyFormat("int i // This is a fancy variable\n"
  784. " = 5; // with nicely aligned comment.");
  785. verifyFormat("// Leading comment.\n"
  786. "int a; // Trailing comment.");
  787. verifyFormat("int a; // Trailing comment\n"
  788. " // on 2\n"
  789. " // or 3 lines.\n"
  790. "int b;");
  791. verifyFormat("int a; // Trailing comment\n"
  792. "\n"
  793. "// Leading comment.\n"
  794. "int b;");
  795. verifyFormat("int a; // Comment.\n"
  796. " // More details.\n"
  797. "int bbbb; // Another comment.");
  798. verifyFormat(
  799. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  800. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  801. "int cccccccccccccccccccccccccccccc; // comment\n"
  802. "int ddd; // looooooooooooooooooooooooong comment\n"
  803. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  804. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  805. "int ccccccccccccccccccc; // comment");
  806. verifyFormat("#include \"a\" // comment\n"
  807. "#include \"a/b/c\" // comment");
  808. verifyFormat("#include <a> // comment\n"
  809. "#include <a/b/c> // comment");
  810. EXPECT_EQ("#include \"a\" // comment\n"
  811. "#include \"a/b/c\" // comment",
  812. format("#include \\\n"
  813. " \"a\" // comment\n"
  814. "#include \"a/b/c\" // comment"));
  815. verifyFormat("enum E {\n"
  816. " // comment\n"
  817. " VAL_A, // comment\n"
  818. " VAL_B\n"
  819. "};");
  820. verifyFormat(
  821. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  822. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  823. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  824. " // Comment inside a statement.\n"
  825. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  826. verifyFormat("SomeFunction(a,\n"
  827. " // comment\n"
  828. " b + x);");
  829. verifyFormat("SomeFunction(a, a,\n"
  830. " // comment\n"
  831. " b + x);");
  832. verifyFormat(
  833. "bool aaaaaaaaaaaaa = // comment\n"
  834. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  835. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  836. verifyFormat("int aaaa; // aaaaa\n"
  837. "int aa; // aaaaaaa",
  838. getLLVMStyleWithColumns(20));
  839. EXPECT_EQ("void f() { // This does something ..\n"
  840. "}\n"
  841. "int a; // This is unrelated",
  842. format("void f() { // This does something ..\n"
  843. " }\n"
  844. "int a; // This is unrelated"));
  845. EXPECT_EQ("class C {\n"
  846. " void f() { // This does something ..\n"
  847. " } // awesome..\n"
  848. "\n"
  849. " int a; // This is unrelated\n"
  850. "};",
  851. format("class C{void f() { // This does something ..\n"
  852. " } // awesome..\n"
  853. " \n"
  854. "int a; // This is unrelated\n"
  855. "};"));
  856. EXPECT_EQ("int i; // single line trailing comment",
  857. format("int i;\\\n// single line trailing comment"));
  858. verifyGoogleFormat("int a; // Trailing comment.");
  859. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  860. " parameter));");
  861. verifyGoogleFormat("#endif // HEADER_GUARD");
  862. verifyFormat("const char *test[] = {\n"
  863. " // A\n"
  864. " \"aaaa\",\n"
  865. " // B\n"
  866. " \"aaaaa\"};");
  867. verifyGoogleFormat(
  868. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  869. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  870. EXPECT_EQ("D(a, {\n"
  871. " // test\n"
  872. " int a;\n"
  873. "});",
  874. format("D(a, {\n"
  875. "// test\n"
  876. "int a;\n"
  877. "});"));
  878. EXPECT_EQ("lineWith(); // comment\n"
  879. "// at start\n"
  880. "otherLine();",
  881. format("lineWith(); // comment\n"
  882. "// at start\n"
  883. "otherLine();"));
  884. EXPECT_EQ("lineWith(); // comment\n"
  885. "/*\n"
  886. " * at start */\n"
  887. "otherLine();",
  888. format("lineWith(); // comment\n"
  889. "/*\n"
  890. " * at start */\n"
  891. "otherLine();"));
  892. EXPECT_EQ("lineWith(); // comment\n"
  893. " // at start\n"
  894. "otherLine();",
  895. format("lineWith(); // comment\n"
  896. " // at start\n"
  897. "otherLine();"));
  898. EXPECT_EQ("lineWith(); // comment\n"
  899. "// at start\n"
  900. "otherLine(); // comment",
  901. format("lineWith(); // comment\n"
  902. "// at start\n"
  903. "otherLine(); // comment"));
  904. EXPECT_EQ("lineWith();\n"
  905. "// at start\n"
  906. "otherLine(); // comment",
  907. format("lineWith();\n"
  908. " // at start\n"
  909. "otherLine(); // comment"));
  910. EXPECT_EQ("// first\n"
  911. "// at start\n"
  912. "otherLine(); // comment",
  913. format("// first\n"
  914. " // at start\n"
  915. "otherLine(); // comment"));
  916. EXPECT_EQ("f();\n"
  917. "// first\n"
  918. "// at start\n"
  919. "otherLine(); // comment",
  920. format("f();\n"
  921. "// first\n"
  922. " // at start\n"
  923. "otherLine(); // comment"));
  924. verifyFormat("f(); // comment\n"
  925. "// first\n"
  926. "// at start\n"
  927. "otherLine();");
  928. EXPECT_EQ("f(); // comment\n"
  929. "// first\n"
  930. "// at start\n"
  931. "otherLine();",
  932. format("f(); // comment\n"
  933. "// first\n"
  934. " // at start\n"
  935. "otherLine();"));
  936. EXPECT_EQ("f(); // comment\n"
  937. " // first\n"
  938. "// at start\n"
  939. "otherLine();",
  940. format("f(); // comment\n"
  941. " // first\n"
  942. "// at start\n"
  943. "otherLine();"));
  944. EXPECT_EQ("void f() {\n"
  945. " lineWith(); // comment\n"
  946. " // at start\n"
  947. "}",
  948. format("void f() {\n"
  949. " lineWith(); // comment\n"
  950. " // at start\n"
  951. "}"));
  952. EXPECT_EQ("int xy; // a\n"
  953. "int z; // b",
  954. format("int xy; // a\n"
  955. "int z; //b"));
  956. EXPECT_EQ("int xy; // a\n"
  957. "int z; // bb",
  958. format("int xy; // a\n"
  959. "int z; //bb",
  960. getLLVMStyleWithColumns(12)));
  961. verifyFormat("#define A \\\n"
  962. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  963. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  964. getLLVMStyleWithColumns(60));
  965. verifyFormat(
  966. "#define A \\\n"
  967. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  968. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  969. getLLVMStyleWithColumns(61));
  970. verifyFormat("if ( // This is some comment\n"
  971. " x + 3) {\n"
  972. "}");
  973. EXPECT_EQ("if ( // This is some comment\n"
  974. " // spanning two lines\n"
  975. " x + 3) {\n"
  976. "}",
  977. format("if( // This is some comment\n"
  978. " // spanning two lines\n"
  979. " x + 3) {\n"
  980. "}"));
  981. verifyNoCrash("/\\\n/");
  982. verifyNoCrash("/\\\n* */");
  983. // The 0-character somehow makes the lexer return a proper comment.
  984. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  985. }
  986. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  987. EXPECT_EQ("SomeFunction(a,\n"
  988. " b, // comment\n"
  989. " c);",
  990. format("SomeFunction(a,\n"
  991. " b, // comment\n"
  992. " c);"));
  993. EXPECT_EQ("SomeFunction(a, b,\n"
  994. " // comment\n"
  995. " c);",
  996. format("SomeFunction(a,\n"
  997. " b,\n"
  998. " // comment\n"
  999. " c);"));
  1000. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  1001. " c);",
  1002. format("SomeFunction(a, b, // comment (unclear relation)\n"
  1003. " c);"));
  1004. EXPECT_EQ("SomeFunction(a, // comment\n"
  1005. " b,\n"
  1006. " c); // comment",
  1007. format("SomeFunction(a, // comment\n"
  1008. " b,\n"
  1009. " c); // comment"));
  1010. }
  1011. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1012. EXPECT_EQ("// comment", format("// comment "));
  1013. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1014. format("int aaaaaaa, bbbbbbb; // comment ",
  1015. getLLVMStyleWithColumns(33)));
  1016. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1017. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1018. }
  1019. TEST_F(FormatTest, UnderstandsBlockComments) {
  1020. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1021. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1022. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1023. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1024. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1025. "/* Trailing comment for aa... */\n"
  1026. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1027. EXPECT_EQ(
  1028. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1029. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1030. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1031. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1032. EXPECT_EQ(
  1033. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1034. " aaaaaaaaaaaaaaaaaa,\n"
  1035. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1036. "}",
  1037. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1038. " aaaaaaaaaaaaaaaaaa ,\n"
  1039. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1040. "}"));
  1041. FormatStyle NoBinPacking = getLLVMStyle();
  1042. NoBinPacking.BinPackParameters = false;
  1043. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1044. " /* parameter 2 */ aaaaaa,\n"
  1045. " /* parameter 3 */ aaaaaa,\n"
  1046. " /* parameter 4 */ aaaaaa);",
  1047. NoBinPacking);
  1048. // Aligning block comments in macros.
  1049. verifyGoogleFormat("#define A \\\n"
  1050. " int i; /*a*/ \\\n"
  1051. " int jjj; /*b*/");
  1052. }
  1053. TEST_F(FormatTest, AlignsBlockComments) {
  1054. EXPECT_EQ("/*\n"
  1055. " * Really multi-line\n"
  1056. " * comment.\n"
  1057. " */\n"
  1058. "void f() {}",
  1059. format(" /*\n"
  1060. " * Really multi-line\n"
  1061. " * comment.\n"
  1062. " */\n"
  1063. " void f() {}"));
  1064. EXPECT_EQ("class C {\n"
  1065. " /*\n"
  1066. " * Another multi-line\n"
  1067. " * comment.\n"
  1068. " */\n"
  1069. " void f() {}\n"
  1070. "};",
  1071. format("class C {\n"
  1072. "/*\n"
  1073. " * Another multi-line\n"
  1074. " * comment.\n"
  1075. " */\n"
  1076. "void f() {}\n"
  1077. "};"));
  1078. EXPECT_EQ("/*\n"
  1079. " 1. This is a comment with non-trivial formatting.\n"
  1080. " 1.1. We have to indent/outdent all lines equally\n"
  1081. " 1.1.1. to keep the formatting.\n"
  1082. " */",
  1083. format(" /*\n"
  1084. " 1. This is a comment with non-trivial formatting.\n"
  1085. " 1.1. We have to indent/outdent all lines equally\n"
  1086. " 1.1.1. to keep the formatting.\n"
  1087. " */"));
  1088. EXPECT_EQ("/*\n"
  1089. "Don't try to outdent if there's not enough indentation.\n"
  1090. "*/",
  1091. format(" /*\n"
  1092. " Don't try to outdent if there's not enough indentation.\n"
  1093. " */"));
  1094. EXPECT_EQ("int i; /* Comment with empty...\n"
  1095. " *\n"
  1096. " * line. */",
  1097. format("int i; /* Comment with empty...\n"
  1098. " *\n"
  1099. " * line. */"));
  1100. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1101. "int bar = 0; /* multiline\n"
  1102. " comment 1 */\n"
  1103. "int baz = 0; /* multiline\n"
  1104. " comment 2 */\n"
  1105. "int bzz = 0; /* multiline\n"
  1106. " comment 3 */",
  1107. format("int foobar = 0; /* comment */\n"
  1108. "int bar = 0; /* multiline\n"
  1109. " comment 1 */\n"
  1110. "int baz = 0; /* multiline\n"
  1111. " comment 2 */\n"
  1112. "int bzz = 0; /* multiline\n"
  1113. " comment 3 */"));
  1114. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1115. "int bar = 0; /* multiline\n"
  1116. " comment */\n"
  1117. "int baz = 0; /* multiline\n"
  1118. "comment */",
  1119. format("int foobar = 0; /* comment */\n"
  1120. "int bar = 0; /* multiline\n"
  1121. "comment */\n"
  1122. "int baz = 0; /* multiline\n"
  1123. "comment */"));
  1124. }
  1125. TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
  1126. FormatStyle Style = getLLVMStyleWithColumns(20);
  1127. Style.ReflowComments = false;
  1128. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  1129. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  1130. }
  1131. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1132. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1134. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1136. EXPECT_EQ(
  1137. "void ffffffffffff(\n"
  1138. " int aaaaaaaa, int bbbbbbbb,\n"
  1139. " int cccccccccccc) { /*\n"
  1140. " aaaaaaaaaa\n"
  1141. " aaaaaaaaaaaaa\n"
  1142. " bbbbbbbbbbbbbb\n"
  1143. " bbbbbbbbbb\n"
  1144. " */\n"
  1145. "}",
  1146. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1147. "{ /*\n"
  1148. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1149. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1150. " */\n"
  1151. "}",
  1152. getLLVMStyleWithColumns(40)));
  1153. }
  1154. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1155. EXPECT_EQ("void ffffffffff(\n"
  1156. " int aaaaa /* test */);",
  1157. format("void ffffffffff(int aaaaa /* test */);",
  1158. getLLVMStyleWithColumns(35)));
  1159. }
  1160. TEST_F(FormatTest, SplitsLongCxxComments) {
  1161. EXPECT_EQ("// A comment that\n"
  1162. "// doesn't fit on\n"
  1163. "// one line",
  1164. format("// A comment that doesn't fit on one line",
  1165. getLLVMStyleWithColumns(20)));
  1166. EXPECT_EQ("/// A comment that\n"
  1167. "/// doesn't fit on\n"
  1168. "/// one line",
  1169. format("/// A comment that doesn't fit on one line",
  1170. getLLVMStyleWithColumns(20)));
  1171. EXPECT_EQ("//! A comment that\n"
  1172. "//! doesn't fit on\n"
  1173. "//! one line",
  1174. format("//! A comment that doesn't fit on one line",
  1175. getLLVMStyleWithColumns(20)));
  1176. EXPECT_EQ("// a b c d\n"
  1177. "// e f g\n"
  1178. "// h i j k",
  1179. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1180. EXPECT_EQ(
  1181. "// a b c d\n"
  1182. "// e f g\n"
  1183. "// h i j k",
  1184. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1185. EXPECT_EQ("if (true) // A comment that\n"
  1186. " // doesn't fit on\n"
  1187. " // one line",
  1188. format("if (true) // A comment that doesn't fit on one line ",
  1189. getLLVMStyleWithColumns(30)));
  1190. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1191. format("// Don't_touch_leading_whitespace",
  1192. getLLVMStyleWithColumns(20)));
  1193. EXPECT_EQ("// Add leading\n"
  1194. "// whitespace",
  1195. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1196. EXPECT_EQ("/// Add leading\n"
  1197. "/// whitespace",
  1198. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1199. EXPECT_EQ("//! Add leading\n"
  1200. "//! whitespace",
  1201. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1202. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1203. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1204. "// limit",
  1205. format("//Even if it makes the line exceed the column limit",
  1206. getLLVMStyleWithColumns(51)));
  1207. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1208. EXPECT_EQ("// aa bb cc dd",
  1209. format("// aa bb cc dd ",
  1210. getLLVMStyleWithColumns(15)));
  1211. EXPECT_EQ("// A comment before\n"
  1212. "// a macro\n"
  1213. "// definition\n"
  1214. "#define a b",
  1215. format("// A comment before a macro definition\n"
  1216. "#define a b",
  1217. getLLVMStyleWithColumns(20)));
  1218. EXPECT_EQ("void ffffff(\n"
  1219. " int aaaaaaaaa, // wwww\n"
  1220. " int bbbbbbbbbb, // xxxxxxx\n"
  1221. " // yyyyyyyyyy\n"
  1222. " int c, int d, int e) {}",
  1223. format("void ffffff(\n"
  1224. " int aaaaaaaaa, // wwww\n"
  1225. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1226. " int c, int d, int e) {}",
  1227. getLLVMStyleWithColumns(40)));
  1228. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1229. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1230. getLLVMStyleWithColumns(20)));
  1231. EXPECT_EQ(
  1232. "#define XXX // a b c d\n"
  1233. " // e f g h",
  1234. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1235. EXPECT_EQ(
  1236. "#define XXX // q w e r\n"
  1237. " // t y u i",
  1238. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1239. }
  1240. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1241. EXPECT_EQ("// A comment\n"
  1242. "// that doesn't\n"
  1243. "// fit on one\n"
  1244. "// line",
  1245. format("// A comment that doesn't fit on one line",
  1246. getLLVMStyleWithColumns(20)));
  1247. EXPECT_EQ("/// A comment\n"
  1248. "/// that doesn't\n"
  1249. "/// fit on one\n"
  1250. "/// line",
  1251. format("/// A comment that doesn't fit on one line",
  1252. getLLVMStyleWithColumns(20)));
  1253. }
  1254. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1255. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1256. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1257. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1258. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1259. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1260. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1261. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1262. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1263. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1264. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1265. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1266. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1267. getLLVMStyleWithColumns(50)));
  1268. // FIXME: One day we might want to implement adjustment of leading whitespace
  1269. // of the consecutive lines in this kind of comment:
  1270. EXPECT_EQ("double\n"
  1271. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1272. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1273. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1274. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1275. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1276. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1277. getLLVMStyleWithColumns(49)));
  1278. }
  1279. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1280. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1281. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1282. EXPECT_EQ(
  1283. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1284. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1285. EXPECT_EQ(
  1286. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1287. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1288. }
  1289. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1290. EXPECT_EQ("if (xxx ==\n"
  1291. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1292. " zzz)\n"
  1293. " q();",
  1294. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1295. " zzz) q();",
  1296. getLLVMStyleWithColumns(40)));
  1297. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1298. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1299. " zzz)\n"
  1300. " q();",
  1301. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1302. " zzz) q();",
  1303. getLLVMStyleWithColumns(40)));
  1304. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1305. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1306. " zzz)\n"
  1307. " q();",
  1308. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1309. " zzz) q();",
  1310. getLLVMStyleWithColumns(40)));
  1311. EXPECT_EQ("fffffffff(\n"
  1312. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1313. " zzz);",
  1314. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1315. " zzz);",
  1316. getLLVMStyleWithColumns(40)));
  1317. }
  1318. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1319. EXPECT_EQ("#define A(x) /* \\\n"
  1320. " a comment \\\n"
  1321. " inside */ \\\n"
  1322. " f();",
  1323. format("#define A(x) /* \\\n"
  1324. " a comment \\\n"
  1325. " inside */ \\\n"
  1326. " f();",
  1327. getLLVMStyleWithColumns(17)));
  1328. EXPECT_EQ("#define A( \\\n"
  1329. " x) /* \\\n"
  1330. " a comment \\\n"
  1331. " inside */ \\\n"
  1332. " f();",
  1333. format("#define A( \\\n"
  1334. " x) /* \\\n"
  1335. " a comment \\\n"
  1336. " inside */ \\\n"
  1337. " f();",
  1338. getLLVMStyleWithColumns(17)));
  1339. }
  1340. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1341. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1342. format("namespace {}\n // Test\n#define A"));
  1343. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1344. format("namespace {}\n /* Test */\n#define A"));
  1345. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1346. format("namespace {}\n /* Test */ #define A"));
  1347. }
  1348. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1349. EXPECT_EQ("/* This is a long\n"
  1350. " * comment that\n"
  1351. " * doesn't\n"
  1352. " * fit on one line.\n"
  1353. " */",
  1354. format("/* "
  1355. "This is a long "
  1356. "comment that "
  1357. "doesn't "
  1358. "fit on one line. */",
  1359. getLLVMStyleWithColumns(20)));
  1360. EXPECT_EQ(
  1361. "/* a b c d\n"
  1362. " * e f g\n"
  1363. " * h i j k\n"
  1364. " */",
  1365. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1366. EXPECT_EQ(
  1367. "/* a b c d\n"
  1368. " * e f g\n"
  1369. " * h i j k\n"
  1370. " */",
  1371. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1372. EXPECT_EQ("/*\n"
  1373. "This is a long\n"
  1374. "comment that doesn't\n"
  1375. "fit on one line.\n"
  1376. "*/",
  1377. format("/*\n"
  1378. "This is a long "
  1379. "comment that doesn't "
  1380. "fit on one line. \n"
  1381. "*/",
  1382. getLLVMStyleWithColumns(20)));
  1383. EXPECT_EQ("/*\n"
  1384. " * This is a long\n"
  1385. " * comment that\n"
  1386. " * doesn't fit on\n"
  1387. " * one line.\n"
  1388. " */",
  1389. format("/* \n"
  1390. " * This is a long "
  1391. " comment that "
  1392. " doesn't fit on "
  1393. " one line. \n"
  1394. " */",
  1395. getLLVMStyleWithColumns(20)));
  1396. EXPECT_EQ("/*\n"
  1397. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1398. " * so_it_should_be_broken\n"
  1399. " * wherever_a_space_occurs\n"
  1400. " */",
  1401. format("/*\n"
  1402. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1403. " so_it_should_be_broken "
  1404. " wherever_a_space_occurs \n"
  1405. " */",
  1406. getLLVMStyleWithColumns(20)));
  1407. EXPECT_EQ("/*\n"
  1408. " * This_comment_can_not_be_broken_into_lines\n"
  1409. " */",
  1410. format("/*\n"
  1411. " * This_comment_can_not_be_broken_into_lines\n"
  1412. " */",
  1413. getLLVMStyleWithColumns(20)));
  1414. EXPECT_EQ("{\n"
  1415. " /*\n"
  1416. " This is another\n"
  1417. " long comment that\n"
  1418. " doesn't fit on one\n"
  1419. " line 1234567890\n"
  1420. " */\n"
  1421. "}",
  1422. format("{\n"
  1423. "/*\n"
  1424. "This is another "
  1425. " long comment that "
  1426. " doesn't fit on one"
  1427. " line 1234567890\n"
  1428. "*/\n"
  1429. "}",
  1430. getLLVMStyleWithColumns(20)));
  1431. EXPECT_EQ("{\n"
  1432. " /*\n"
  1433. " * This i s\n"
  1434. " * another comment\n"
  1435. " * t hat doesn' t\n"
  1436. " * fit on one l i\n"
  1437. " * n e\n"
  1438. " */\n"
  1439. "}",
  1440. format("{\n"
  1441. "/*\n"
  1442. " * This i s"
  1443. " another comment"
  1444. " t hat doesn' t"
  1445. " fit on one l i"
  1446. " n e\n"
  1447. " */\n"
  1448. "}",
  1449. getLLVMStyleWithColumns(20)));
  1450. EXPECT_EQ("/*\n"
  1451. " * This is a long\n"
  1452. " * comment that\n"
  1453. " * doesn't fit on\n"
  1454. " * one line\n"
  1455. " */",
  1456. format(" /*\n"
  1457. " * This is a long comment that doesn't fit on one line\n"
  1458. " */",
  1459. getLLVMStyleWithColumns(20)));
  1460. EXPECT_EQ("{\n"
  1461. " if (something) /* This is a\n"
  1462. " long\n"
  1463. " comment */\n"
  1464. " ;\n"
  1465. "}",
  1466. format("{\n"
  1467. " if (something) /* This is a long comment */\n"
  1468. " ;\n"
  1469. "}",
  1470. getLLVMStyleWithColumns(30)));
  1471. EXPECT_EQ("/* A comment before\n"
  1472. " * a macro\n"
  1473. " * definition */\n"
  1474. "#define a b",
  1475. format("/* A comment before a macro definition */\n"
  1476. "#define a b",
  1477. getLLVMStyleWithColumns(20)));
  1478. EXPECT_EQ("/* some comment\n"
  1479. " * a comment\n"
  1480. "* that we break\n"
  1481. " * another comment\n"
  1482. "* we have to break\n"
  1483. "* a left comment\n"
  1484. " */",
  1485. format(" /* some comment\n"
  1486. " * a comment that we break\n"
  1487. " * another comment we have to break\n"
  1488. "* a left comment\n"
  1489. " */",
  1490. getLLVMStyleWithColumns(20)));
  1491. EXPECT_EQ("/**\n"
  1492. " * multiline block\n"
  1493. " * comment\n"
  1494. " *\n"
  1495. " */",
  1496. format("/**\n"
  1497. " * multiline block comment\n"
  1498. " *\n"
  1499. " */",
  1500. getLLVMStyleWithColumns(20)));
  1501. EXPECT_EQ("/*\n"
  1502. "\n"
  1503. "\n"
  1504. " */\n",
  1505. format(" /* \n"
  1506. " \n"
  1507. " \n"
  1508. " */\n"));
  1509. EXPECT_EQ("/* a a */",
  1510. format("/* a a */", getLLVMStyleWithColumns(15)));
  1511. EXPECT_EQ("/* a a bc */",
  1512. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1513. EXPECT_EQ("/* aaa aaa\n"
  1514. " * aaaaa */",
  1515. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1516. EXPECT_EQ("/* aaa aaa\n"
  1517. " * aaaaa */",
  1518. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1519. }
  1520. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1521. EXPECT_EQ("#define X \\\n"
  1522. " /* \\\n"
  1523. " Test \\\n"
  1524. " Macro comment \\\n"
  1525. " with a long \\\n"
  1526. " line \\\n"
  1527. " */ \\\n"
  1528. " A + B",
  1529. format("#define X \\\n"
  1530. " /*\n"
  1531. " Test\n"
  1532. " Macro comment with a long line\n"
  1533. " */ \\\n"
  1534. " A + B",
  1535. getLLVMStyleWithColumns(20)));
  1536. EXPECT_EQ("#define X \\\n"
  1537. " /* Macro comment \\\n"
  1538. " with a long \\\n"
  1539. " line */ \\\n"
  1540. " A + B",
  1541. format("#define X \\\n"
  1542. " /* Macro comment with a long\n"
  1543. " line */ \\\n"
  1544. " A + B",
  1545. getLLVMStyleWithColumns(20)));
  1546. EXPECT_EQ("#define X \\\n"
  1547. " /* Macro comment \\\n"
  1548. " * with a long \\\n"
  1549. " * line */ \\\n"
  1550. " A + B",
  1551. format("#define X \\\n"
  1552. " /* Macro comment with a long line */ \\\n"
  1553. " A + B",
  1554. getLLVMStyleWithColumns(20)));
  1555. }
  1556. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1557. EXPECT_EQ(
  1558. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1559. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1560. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1561. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1562. " aaaaaaaaaaaaaaaaaaaa};",
  1563. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1564. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1565. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1566. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1567. " aaaaaaaaaaaaaaaaaaaa };"));
  1568. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1569. " bbbbbbbbbbb, ccccccccccc};");
  1570. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1571. " // comment for bb....\n"
  1572. " bbbbbbbbbbb, ccccccccccc};");
  1573. verifyGoogleFormat(
  1574. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1575. " bbbbbbbbbbb, ccccccccccc};");
  1576. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1577. " // comment for bb....\n"
  1578. " bbbbbbbbbbb, ccccccccccc};");
  1579. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1580. " {d, e, f}, // Group #2\n"
  1581. " {g, h, i}}; // Group #3");
  1582. verifyFormat("S s = {{// Group #1\n"
  1583. " a, b, c},\n"
  1584. " {// Group #2\n"
  1585. " d, e, f},\n"
  1586. " {// Group #3\n"
  1587. " g, h, i}};");
  1588. EXPECT_EQ("S s = {\n"
  1589. " // Some comment\n"
  1590. " a,\n"
  1591. "\n"
  1592. " // Comment after empty line\n"
  1593. " b}",
  1594. format("S s = {\n"
  1595. " // Some comment\n"
  1596. " a,\n"
  1597. " \n"
  1598. " // Comment after empty line\n"
  1599. " b\n"
  1600. "}"));
  1601. EXPECT_EQ("S s = {\n"
  1602. " /* Some comment */\n"
  1603. " a,\n"
  1604. "\n"
  1605. " /* Comment after empty line */\n"
  1606. " b}",
  1607. format("S s = {\n"
  1608. " /* Some comment */\n"
  1609. " a,\n"
  1610. " \n"
  1611. " /* Comment after empty line */\n"
  1612. " b\n"
  1613. "}"));
  1614. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1615. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1616. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1617. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1618. }
  1619. TEST_F(FormatTest, IgnoresIf0Contents) {
  1620. EXPECT_EQ("#if 0\n"
  1621. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1622. "#endif\n"
  1623. "void f() {}",
  1624. format("#if 0\n"
  1625. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1626. "#endif\n"
  1627. "void f( ) { }"));
  1628. EXPECT_EQ("#if false\n"
  1629. "void f( ) { }\n"
  1630. "#endif\n"
  1631. "void g() {}\n",
  1632. format("#if false\n"
  1633. "void f( ) { }\n"
  1634. "#endif\n"
  1635. "void g( ) { }\n"));
  1636. EXPECT_EQ("enum E {\n"
  1637. " One,\n"
  1638. " Two,\n"
  1639. "#if 0\n"
  1640. "Three,\n"
  1641. " Four,\n"
  1642. "#endif\n"
  1643. " Five\n"
  1644. "};",
  1645. format("enum E {\n"
  1646. " One,Two,\n"
  1647. "#if 0\n"
  1648. "Three,\n"
  1649. " Four,\n"
  1650. "#endif\n"
  1651. " Five};"));
  1652. EXPECT_EQ("enum F {\n"
  1653. " One,\n"
  1654. "#if 1\n"
  1655. " Two,\n"
  1656. "#if 0\n"
  1657. "Three,\n"
  1658. " Four,\n"
  1659. "#endif\n"
  1660. " Five\n"
  1661. "#endif\n"
  1662. "};",
  1663. format("enum F {\n"
  1664. "One,\n"
  1665. "#if 1\n"
  1666. "Two,\n"
  1667. "#if 0\n"
  1668. "Three,\n"
  1669. " Four,\n"
  1670. "#endif\n"
  1671. "Five\n"
  1672. "#endif\n"
  1673. "};"));
  1674. EXPECT_EQ("enum G {\n"
  1675. " One,\n"
  1676. "#if 0\n"
  1677. "Two,\n"
  1678. "#else\n"
  1679. " Three,\n"
  1680. "#endif\n"
  1681. " Four\n"
  1682. "};",
  1683. format("enum G {\n"
  1684. "One,\n"
  1685. "#if 0\n"
  1686. "Two,\n"
  1687. "#else\n"
  1688. "Three,\n"
  1689. "#endif\n"
  1690. "Four\n"
  1691. "};"));
  1692. EXPECT_EQ("enum H {\n"
  1693. " One,\n"
  1694. "#if 0\n"
  1695. "#ifdef Q\n"
  1696. "Two,\n"
  1697. "#else\n"
  1698. "Three,\n"
  1699. "#endif\n"
  1700. "#endif\n"
  1701. " Four\n"
  1702. "};",
  1703. format("enum H {\n"
  1704. "One,\n"
  1705. "#if 0\n"
  1706. "#ifdef Q\n"
  1707. "Two,\n"
  1708. "#else\n"
  1709. "Three,\n"
  1710. "#endif\n"
  1711. "#endif\n"
  1712. "Four\n"
  1713. "};"));
  1714. EXPECT_EQ("enum I {\n"
  1715. " One,\n"
  1716. "#if /* test */ 0 || 1\n"
  1717. "Two,\n"
  1718. "Three,\n"
  1719. "#endif\n"
  1720. " Four\n"
  1721. "};",
  1722. format("enum I {\n"
  1723. "One,\n"
  1724. "#if /* test */ 0 || 1\n"
  1725. "Two,\n"
  1726. "Three,\n"
  1727. "#endif\n"
  1728. "Four\n"
  1729. "};"));
  1730. EXPECT_EQ("enum J {\n"
  1731. " One,\n"
  1732. "#if 0\n"
  1733. "#if 0\n"
  1734. "Two,\n"
  1735. "#else\n"
  1736. "Three,\n"
  1737. "#endif\n"
  1738. "Four,\n"
  1739. "#endif\n"
  1740. " Five\n"
  1741. "};",
  1742. format("enum J {\n"
  1743. "One,\n"
  1744. "#if 0\n"
  1745. "#if 0\n"
  1746. "Two,\n"
  1747. "#else\n"
  1748. "Three,\n"
  1749. "#endif\n"
  1750. "Four,\n"
  1751. "#endif\n"
  1752. "Five\n"
  1753. "};"));
  1754. }
  1755. //===----------------------------------------------------------------------===//
  1756. // Tests for classes, namespaces, etc.
  1757. //===----------------------------------------------------------------------===//
  1758. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1759. verifyFormat("class A {};");
  1760. }
  1761. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1762. verifyFormat("class A {\n"
  1763. "public:\n"
  1764. "public: // comment\n"
  1765. "protected:\n"
  1766. "private:\n"
  1767. " void f() {}\n"
  1768. "};");
  1769. verifyGoogleFormat("class A {\n"
  1770. " public:\n"
  1771. " protected:\n"
  1772. " private:\n"
  1773. " void f() {}\n"
  1774. "};");
  1775. verifyFormat("class A {\n"
  1776. "public slots:\n"
  1777. " void f1() {}\n"
  1778. "public Q_SLOTS:\n"
  1779. " void f2() {}\n"
  1780. "protected slots:\n"
  1781. " void f3() {}\n"
  1782. "protected Q_SLOTS:\n"
  1783. " void f4() {}\n"
  1784. "private slots:\n"
  1785. " void f5() {}\n"
  1786. "private Q_SLOTS:\n"
  1787. " void f6() {}\n"
  1788. "signals:\n"
  1789. " void g1();\n"
  1790. "Q_SIGNALS:\n"
  1791. " void g2();\n"
  1792. "};");
  1793. // Don't interpret 'signals' the wrong way.
  1794. verifyFormat("signals.set();");
  1795. verifyFormat("for (Signals signals : f()) {\n}");
  1796. verifyFormat("{\n"
  1797. " signals.set(); // This needs indentation.\n"
  1798. "}");
  1799. }
  1800. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1801. EXPECT_EQ("class A {\n"
  1802. "public:\n"
  1803. " void f();\n"
  1804. "\n"
  1805. "private:\n"
  1806. " void g() {}\n"
  1807. " // test\n"
  1808. "protected:\n"
  1809. " int h;\n"
  1810. "};",
  1811. format("class A {\n"
  1812. "public:\n"
  1813. "void f();\n"
  1814. "private:\n"
  1815. "void g() {}\n"
  1816. "// test\n"
  1817. "protected:\n"
  1818. "int h;\n"
  1819. "};"));
  1820. EXPECT_EQ("class A {\n"
  1821. "protected:\n"
  1822. "public:\n"
  1823. " void f();\n"
  1824. "};",
  1825. format("class A {\n"
  1826. "protected:\n"
  1827. "\n"
  1828. "public:\n"
  1829. "\n"
  1830. " void f();\n"
  1831. "};"));
  1832. // Even ensure proper spacing inside macros.
  1833. EXPECT_EQ("#define B \\\n"
  1834. " class A { \\\n"
  1835. " protected: \\\n"
  1836. " public: \\\n"
  1837. " void f(); \\\n"
  1838. " };",
  1839. format("#define B \\\n"
  1840. " class A { \\\n"
  1841. " protected: \\\n"
  1842. " \\\n"
  1843. " public: \\\n"
  1844. " \\\n"
  1845. " void f(); \\\n"
  1846. " };",
  1847. getGoogleStyle()));
  1848. // But don't remove empty lines after macros ending in access specifiers.
  1849. EXPECT_EQ("#define A private:\n"
  1850. "\n"
  1851. "int i;",
  1852. format("#define A private:\n"
  1853. "\n"
  1854. "int i;"));
  1855. }
  1856. TEST_F(FormatTest, FormatsClasses) {
  1857. verifyFormat("class A : public B {};");
  1858. verifyFormat("class A : public ::B {};");
  1859. verifyFormat(
  1860. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1861. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1862. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1863. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1864. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1865. verifyFormat(
  1866. "class A : public B, public C, public D, public E, public F {};");
  1867. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1868. " public C,\n"
  1869. " public D,\n"
  1870. " public E,\n"
  1871. " public F,\n"
  1872. " public G {};");
  1873. verifyFormat("class\n"
  1874. " ReallyReallyLongClassName {\n"
  1875. " int i;\n"
  1876. "};",
  1877. getLLVMStyleWithColumns(32));
  1878. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1879. " aaaaaaaaaaaaaaaa> {};");
  1880. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1881. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1882. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1883. verifyFormat("template <class R, class C>\n"
  1884. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1885. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1886. verifyFormat("class ::A::B {};");
  1887. }
  1888. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1889. verifyFormat("class A {\n} a, b;");
  1890. verifyFormat("struct A {\n} a, b;");
  1891. verifyFormat("union A {\n} a;");
  1892. }
  1893. TEST_F(FormatTest, FormatsEnum) {
  1894. verifyFormat("enum {\n"
  1895. " Zero,\n"
  1896. " One = 1,\n"
  1897. " Two = One + 1,\n"
  1898. " Three = (One + Two),\n"
  1899. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1900. " Five = (One, Two, Three, Four, 5)\n"
  1901. "};");
  1902. verifyGoogleFormat("enum {\n"
  1903. " Zero,\n"
  1904. " One = 1,\n"
  1905. " Two = One + 1,\n"
  1906. " Three = (One + Two),\n"
  1907. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1908. " Five = (One, Two, Three, Four, 5)\n"
  1909. "};");
  1910. verifyFormat("enum Enum {};");
  1911. verifyFormat("enum {};");
  1912. verifyFormat("enum X E {} d;");
  1913. verifyFormat("enum __attribute__((...)) E {} d;");
  1914. verifyFormat("enum __declspec__((...)) E {} d;");
  1915. verifyFormat("enum {\n"
  1916. " Bar = Foo<int, int>::value\n"
  1917. "};",
  1918. getLLVMStyleWithColumns(30));
  1919. verifyFormat("enum ShortEnum { A, B, C };");
  1920. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1921. EXPECT_EQ("enum KeepEmptyLines {\n"
  1922. " ONE,\n"
  1923. "\n"
  1924. " TWO,\n"
  1925. "\n"
  1926. " THREE\n"
  1927. "}",
  1928. format("enum KeepEmptyLines {\n"
  1929. " ONE,\n"
  1930. "\n"
  1931. " TWO,\n"
  1932. "\n"
  1933. "\n"
  1934. " THREE\n"
  1935. "}"));
  1936. verifyFormat("enum E { // comment\n"
  1937. " ONE,\n"
  1938. " TWO\n"
  1939. "};\n"
  1940. "int i;");
  1941. // Not enums.
  1942. verifyFormat("enum X f() {\n"
  1943. " a();\n"
  1944. " return 42;\n"
  1945. "}");
  1946. verifyFormat("enum X Type::f() {\n"
  1947. " a();\n"
  1948. " return 42;\n"
  1949. "}");
  1950. verifyFormat("enum ::X f() {\n"
  1951. " a();\n"
  1952. " return 42;\n"
  1953. "}");
  1954. verifyFormat("enum ns::X f() {\n"
  1955. " a();\n"
  1956. " return 42;\n"
  1957. "}");
  1958. }
  1959. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1960. verifyFormat("enum Type {\n"
  1961. " One = 0; // These semicolons should be commas.\n"
  1962. " Two = 1;\n"
  1963. "};");
  1964. verifyFormat("namespace n {\n"
  1965. "enum Type {\n"
  1966. " One,\n"
  1967. " Two, // missing };\n"
  1968. " int i;\n"
  1969. "}\n"
  1970. "void g() {}");
  1971. }
  1972. TEST_F(FormatTest, FormatsEnumStruct) {
  1973. verifyFormat("enum struct {\n"
  1974. " Zero,\n"
  1975. " One = 1,\n"
  1976. " Two = One + 1,\n"
  1977. " Three = (One + Two),\n"
  1978. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1979. " Five = (One, Two, Three, Four, 5)\n"
  1980. "};");
  1981. verifyFormat("enum struct Enum {};");
  1982. verifyFormat("enum struct {};");
  1983. verifyFormat("enum struct X E {} d;");
  1984. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1985. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1986. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1987. }
  1988. TEST_F(FormatTest, FormatsEnumClass) {
  1989. verifyFormat("enum class {\n"
  1990. " Zero,\n"
  1991. " One = 1,\n"
  1992. " Two = One + 1,\n"
  1993. " Three = (One + Two),\n"
  1994. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1995. " Five = (One, Two, Three, Four, 5)\n"
  1996. "};");
  1997. verifyFormat("enum class Enum {};");
  1998. verifyFormat("enum class {};");
  1999. verifyFormat("enum class X E {} d;");
  2000. verifyFormat("enum class __attribute__((...)) E {} d;");
  2001. verifyFormat("enum class __declspec__((...)) E {} d;");
  2002. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  2003. }
  2004. TEST_F(FormatTest, FormatsEnumTypes) {
  2005. verifyFormat("enum X : int {\n"
  2006. " A, // Force multiple lines.\n"
  2007. " B\n"
  2008. "};");
  2009. verifyFormat("enum X : int { A, B };");
  2010. verifyFormat("enum X : std::uint32_t { A, B };");
  2011. }
  2012. TEST_F(FormatTest, FormatsNSEnums) {
  2013. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  2014. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  2015. " // Information about someDecentlyLongValue.\n"
  2016. " someDecentlyLongValue,\n"
  2017. " // Information about anotherDecentlyLongValue.\n"
  2018. " anotherDecentlyLongValue,\n"
  2019. " // Information about aThirdDecentlyLongValue.\n"
  2020. " aThirdDecentlyLongValue\n"
  2021. "};");
  2022. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  2023. " a = 1,\n"
  2024. " b = 2,\n"
  2025. " c = 3,\n"
  2026. "};");
  2027. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  2028. " a = 1,\n"
  2029. " b = 2,\n"
  2030. " c = 3,\n"
  2031. "};");
  2032. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2033. " a = 1,\n"
  2034. " b = 2,\n"
  2035. " c = 3,\n"
  2036. "};");
  2037. }
  2038. TEST_F(FormatTest, FormatsBitfields) {
  2039. verifyFormat("struct Bitfields {\n"
  2040. " unsigned sClass : 8;\n"
  2041. " unsigned ValueKind : 2;\n"
  2042. "};");
  2043. verifyFormat("struct A {\n"
  2044. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2045. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2046. "};");
  2047. verifyFormat("struct MyStruct {\n"
  2048. " uchar data;\n"
  2049. " uchar : 8;\n"
  2050. " uchar : 8;\n"
  2051. " uchar other;\n"
  2052. "};");
  2053. }
  2054. TEST_F(FormatTest, FormatsNamespaces) {
  2055. verifyFormat("namespace some_namespace {\n"
  2056. "class A {};\n"
  2057. "void f() { f(); }\n"
  2058. "}");
  2059. verifyFormat("namespace {\n"
  2060. "class A {};\n"
  2061. "void f() { f(); }\n"
  2062. "}");
  2063. verifyFormat("inline namespace X {\n"
  2064. "class A {};\n"
  2065. "void f() { f(); }\n"
  2066. "}");
  2067. verifyFormat("using namespace some_namespace;\n"
  2068. "class A {};\n"
  2069. "void f() { f(); }");
  2070. // This code is more common than we thought; if we
  2071. // layout this correctly the semicolon will go into
  2072. // its own line, which is undesirable.
  2073. verifyFormat("namespace {};");
  2074. verifyFormat("namespace {\n"
  2075. "class A {};\n"
  2076. "};");
  2077. verifyFormat("namespace {\n"
  2078. "int SomeVariable = 0; // comment\n"
  2079. "} // namespace");
  2080. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2081. "#define HEADER_GUARD\n"
  2082. "namespace my_namespace {\n"
  2083. "int i;\n"
  2084. "} // my_namespace\n"
  2085. "#endif // HEADER_GUARD",
  2086. format("#ifndef HEADER_GUARD\n"
  2087. " #define HEADER_GUARD\n"
  2088. " namespace my_namespace {\n"
  2089. "int i;\n"
  2090. "} // my_namespace\n"
  2091. "#endif // HEADER_GUARD"));
  2092. EXPECT_EQ("namespace A::B {\n"
  2093. "class C {};\n"
  2094. "}",
  2095. format("namespace A::B {\n"
  2096. "class C {};\n"
  2097. "}"));
  2098. FormatStyle Style = getLLVMStyle();
  2099. Style.NamespaceIndentation = FormatStyle::NI_All;
  2100. EXPECT_EQ("namespace out {\n"
  2101. " int i;\n"
  2102. " namespace in {\n"
  2103. " int i;\n"
  2104. " } // namespace\n"
  2105. "} // namespace",
  2106. format("namespace out {\n"
  2107. "int i;\n"
  2108. "namespace in {\n"
  2109. "int i;\n"
  2110. "} // namespace\n"
  2111. "} // namespace",
  2112. Style));
  2113. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2114. EXPECT_EQ("namespace out {\n"
  2115. "int i;\n"
  2116. "namespace in {\n"
  2117. " int i;\n"
  2118. "} // namespace\n"
  2119. "} // namespace",
  2120. format("namespace out {\n"
  2121. "int i;\n"
  2122. "namespace in {\n"
  2123. "int i;\n"
  2124. "} // namespace\n"
  2125. "} // namespace",
  2126. Style));
  2127. }
  2128. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2129. TEST_F(FormatTest, FormatsInlineASM) {
  2130. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2131. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2132. verifyFormat(
  2133. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2134. " \"cpuid\\n\\t\"\n"
  2135. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2136. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2137. " : \"a\"(value));");
  2138. EXPECT_EQ(
  2139. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2140. " __asm {\n"
  2141. " mov edx,[that] // vtable in edx\n"
  2142. " mov eax,methodIndex\n"
  2143. " call [edx][eax*4] // stdcall\n"
  2144. " }\n"
  2145. "}",
  2146. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2147. " __asm {\n"
  2148. " mov edx,[that] // vtable in edx\n"
  2149. " mov eax,methodIndex\n"
  2150. " call [edx][eax*4] // stdcall\n"
  2151. " }\n"
  2152. "}"));
  2153. EXPECT_EQ("_asm {\n"
  2154. " xor eax, eax;\n"
  2155. " cpuid;\n"
  2156. "}",
  2157. format("_asm {\n"
  2158. " xor eax, eax;\n"
  2159. " cpuid;\n"
  2160. "}"));
  2161. verifyFormat("void function() {\n"
  2162. " // comment\n"
  2163. " asm(\"\");\n"
  2164. "}");
  2165. EXPECT_EQ("__asm {\n"
  2166. "}\n"
  2167. "int i;",
  2168. format("__asm {\n"
  2169. "}\n"
  2170. "int i;"));
  2171. }
  2172. TEST_F(FormatTest, FormatTryCatch) {
  2173. verifyFormat("try {\n"
  2174. " throw a * b;\n"
  2175. "} catch (int a) {\n"
  2176. " // Do nothing.\n"
  2177. "} catch (...) {\n"
  2178. " exit(42);\n"
  2179. "}");
  2180. // Function-level try statements.
  2181. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2182. " return 5;\n"
  2183. "}");
  2184. verifyFormat("class A {\n"
  2185. " int a;\n"
  2186. " A() try : a(0) {\n"
  2187. " } catch (...) {\n"
  2188. " throw;\n"
  2189. " }\n"
  2190. "};\n");
  2191. // Incomplete try-catch blocks.
  2192. verifyIncompleteFormat("try {} catch (");
  2193. }
  2194. TEST_F(FormatTest, FormatSEHTryCatch) {
  2195. verifyFormat("__try {\n"
  2196. " int a = b * c;\n"
  2197. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2198. " // Do nothing.\n"
  2199. "}");
  2200. verifyFormat("__try {\n"
  2201. " int a = b * c;\n"
  2202. "} __finally {\n"
  2203. " // Do nothing.\n"
  2204. "}");
  2205. verifyFormat("DEBUG({\n"
  2206. " __try {\n"
  2207. " } __finally {\n"
  2208. " }\n"
  2209. "});\n");
  2210. }
  2211. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2212. verifyFormat("try {\n"
  2213. " f();\n"
  2214. "} catch {\n"
  2215. " g();\n"
  2216. "}");
  2217. verifyFormat("try {\n"
  2218. " f();\n"
  2219. "} catch (A a) MACRO(x) {\n"
  2220. " g();\n"
  2221. "} catch (B b) MACRO(x) {\n"
  2222. " g();\n"
  2223. "}");
  2224. }
  2225. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2226. FormatStyle Style = getLLVMStyle();
  2227. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2228. FormatStyle::BS_WebKit}) {
  2229. Style.BreakBeforeBraces = BraceStyle;
  2230. verifyFormat("try {\n"
  2231. " // something\n"
  2232. "} catch (...) {\n"
  2233. " // something\n"
  2234. "}",
  2235. Style);
  2236. }
  2237. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2238. verifyFormat("try {\n"
  2239. " // something\n"
  2240. "}\n"
  2241. "catch (...) {\n"
  2242. " // something\n"
  2243. "}",
  2244. Style);
  2245. verifyFormat("__try {\n"
  2246. " // something\n"
  2247. "}\n"
  2248. "__finally {\n"
  2249. " // something\n"
  2250. "}",
  2251. Style);
  2252. verifyFormat("@try {\n"
  2253. " // something\n"
  2254. "}\n"
  2255. "@finally {\n"
  2256. " // something\n"
  2257. "}",
  2258. Style);
  2259. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2260. verifyFormat("try\n"
  2261. "{\n"
  2262. " // something\n"
  2263. "}\n"
  2264. "catch (...)\n"
  2265. "{\n"
  2266. " // something\n"
  2267. "}",
  2268. Style);
  2269. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2270. verifyFormat("try\n"
  2271. " {\n"
  2272. " // something\n"
  2273. " }\n"
  2274. "catch (...)\n"
  2275. " {\n"
  2276. " // something\n"
  2277. " }",
  2278. Style);
  2279. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2280. Style.BraceWrapping.BeforeCatch = true;
  2281. verifyFormat("try {\n"
  2282. " // something\n"
  2283. "}\n"
  2284. "catch (...) {\n"
  2285. " // something\n"
  2286. "}",
  2287. Style);
  2288. }
  2289. TEST_F(FormatTest, FormatObjCTryCatch) {
  2290. verifyFormat("@try {\n"
  2291. " f();\n"
  2292. "} @catch (NSException e) {\n"
  2293. " @throw;\n"
  2294. "} @finally {\n"
  2295. " exit(42);\n"
  2296. "}");
  2297. verifyFormat("DEBUG({\n"
  2298. " @try {\n"
  2299. " } @finally {\n"
  2300. " }\n"
  2301. "});\n");
  2302. }
  2303. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2304. FormatStyle Style = getLLVMStyle();
  2305. verifyFormat("@autoreleasepool {\n"
  2306. " f();\n"
  2307. "}\n"
  2308. "@autoreleasepool {\n"
  2309. " f();\n"
  2310. "}\n",
  2311. Style);
  2312. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2313. verifyFormat("@autoreleasepool\n"
  2314. "{\n"
  2315. " f();\n"
  2316. "}\n"
  2317. "@autoreleasepool\n"
  2318. "{\n"
  2319. " f();\n"
  2320. "}\n",
  2321. Style);
  2322. }
  2323. TEST_F(FormatTest, StaticInitializers) {
  2324. verifyFormat("static SomeClass SC = {1, 'a'};");
  2325. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2326. " 100000000, "
  2327. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2328. // Here, everything other than the "}" would fit on a line.
  2329. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2330. " 10000000000000000000000000};");
  2331. EXPECT_EQ("S s = {a,\n"
  2332. "\n"
  2333. " b};",
  2334. format("S s = {\n"
  2335. " a,\n"
  2336. "\n"
  2337. " b\n"
  2338. "};"));
  2339. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2340. // line. However, the formatting looks a bit off and this probably doesn't
  2341. // happen often in practice.
  2342. verifyFormat("static int Variable[1] = {\n"
  2343. " {1000000000000000000000000000000000000}};",
  2344. getLLVMStyleWithColumns(40));
  2345. }
  2346. TEST_F(FormatTest, DesignatedInitializers) {
  2347. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2348. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2349. " .bbbbbbbbbb = 2,\n"
  2350. " .cccccccccc = 3,\n"
  2351. " .dddddddddd = 4,\n"
  2352. " .eeeeeeeeee = 5};");
  2353. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2354. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2355. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2356. " .ccccccccccccccccccccccccccc = 3,\n"
  2357. " .ddddddddddddddddddddddddddd = 4,\n"
  2358. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2359. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2360. }
  2361. TEST_F(FormatTest, NestedStaticInitializers) {
  2362. verifyFormat("static A x = {{{}}};\n");
  2363. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2364. " {init1, init2, init3, init4}}};",
  2365. getLLVMStyleWithColumns(50));
  2366. verifyFormat("somes Status::global_reps[3] = {\n"
  2367. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2368. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2369. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2370. getLLVMStyleWithColumns(60));
  2371. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2372. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2373. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2374. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2375. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2376. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2377. "rect.fTop}};");
  2378. verifyFormat(
  2379. "SomeArrayOfSomeType a = {\n"
  2380. " {{1, 2, 3},\n"
  2381. " {1, 2, 3},\n"
  2382. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2383. " 333333333333333333333333333333},\n"
  2384. " {1, 2, 3},\n"
  2385. " {1, 2, 3}}};");
  2386. verifyFormat(
  2387. "SomeArrayOfSomeType a = {\n"
  2388. " {{1, 2, 3}},\n"
  2389. " {{1, 2, 3}},\n"
  2390. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2391. " 333333333333333333333333333333}},\n"
  2392. " {{1, 2, 3}},\n"
  2393. " {{1, 2, 3}}};");
  2394. verifyFormat("struct {\n"
  2395. " unsigned bit;\n"
  2396. " const char *const name;\n"
  2397. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2398. " {kOsWin, \"Windows\"},\n"
  2399. " {kOsLinux, \"Linux\"},\n"
  2400. " {kOsCrOS, \"Chrome OS\"}};");
  2401. verifyFormat("struct {\n"
  2402. " unsigned bit;\n"
  2403. " const char *const name;\n"
  2404. "} kBitsToOs[] = {\n"
  2405. " {kOsMac, \"Mac\"},\n"
  2406. " {kOsWin, \"Windows\"},\n"
  2407. " {kOsLinux, \"Linux\"},\n"
  2408. " {kOsCrOS, \"Chrome OS\"},\n"
  2409. "};");
  2410. }
  2411. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2412. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2413. " \\\n"
  2414. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2415. }
  2416. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2417. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2418. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2419. // Do break defaulted and deleted functions.
  2420. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2421. " default;",
  2422. getLLVMStyleWithColumns(40));
  2423. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2424. " delete;",
  2425. getLLVMStyleWithColumns(40));
  2426. }
  2427. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2428. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2429. getLLVMStyleWithColumns(40));
  2430. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2431. getLLVMStyleWithColumns(40));
  2432. EXPECT_EQ("#define Q \\\n"
  2433. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2434. " \"aaaaaaaa.cpp\"",
  2435. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2436. getLLVMStyleWithColumns(40)));
  2437. }
  2438. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2439. EXPECT_EQ("# 123 \"A string literal\"",
  2440. format(" # 123 \"A string literal\""));
  2441. }
  2442. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2443. EXPECT_EQ("#;", format("#;"));
  2444. verifyFormat("#\n;\n;\n;");
  2445. }
  2446. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2447. EXPECT_EQ("#line 42 \"test\"\n",
  2448. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2449. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2450. getLLVMStyleWithColumns(12)));
  2451. }
  2452. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2453. EXPECT_EQ("#line 42 \"test\"",
  2454. format("# \\\n line \\\n 42 \\\n \"test\""));
  2455. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2456. }
  2457. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2458. verifyFormat("#define A \\x20");
  2459. verifyFormat("#define A \\ x20");
  2460. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2461. verifyFormat("#define A ''");
  2462. verifyFormat("#define A ''qqq");
  2463. verifyFormat("#define A `qqq");
  2464. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2465. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2466. "\\na : b);",
  2467. format("const char * c = STRINGIFY(\n"
  2468. "\\na : b);"));
  2469. verifyFormat("a\r\\");
  2470. verifyFormat("a\v\\");
  2471. verifyFormat("a\f\\");
  2472. }
  2473. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2474. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2475. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2476. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2477. // FIXME: We never break before the macro name.
  2478. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2479. verifyFormat("#define A A\n#define A A");
  2480. verifyFormat("#define A(X) A\n#define A A");
  2481. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2482. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2483. }
  2484. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2485. EXPECT_EQ("// somecomment\n"
  2486. "#include \"a.h\"\n"
  2487. "#define A( \\\n"
  2488. " A, B)\n"
  2489. "#include \"b.h\"\n"
  2490. "// somecomment\n",
  2491. format(" // somecomment\n"
  2492. " #include \"a.h\"\n"
  2493. "#define A(A,\\\n"
  2494. " B)\n"
  2495. " #include \"b.h\"\n"
  2496. " // somecomment\n",
  2497. getLLVMStyleWithColumns(13)));
  2498. }
  2499. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2500. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2501. EXPECT_EQ("#define A \\\n"
  2502. " c; \\\n"
  2503. " e;\n"
  2504. "f;",
  2505. format("#define A c; e;\n"
  2506. "f;",
  2507. getLLVMStyleWithColumns(14)));
  2508. }
  2509. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2510. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2511. EXPECT_EQ("int x,\n"
  2512. "#define A\n"
  2513. " y;",
  2514. format("int x,\n#define A\ny;"));
  2515. }
  2516. TEST_F(FormatTest, HashInMacroDefinition) {
  2517. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2518. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2519. verifyFormat("#define A \\\n"
  2520. " { \\\n"
  2521. " f(#c); \\\n"
  2522. " }",
  2523. getLLVMStyleWithColumns(11));
  2524. verifyFormat("#define A(X) \\\n"
  2525. " void function##X()",
  2526. getLLVMStyleWithColumns(22));
  2527. verifyFormat("#define A(a, b, c) \\\n"
  2528. " void a##b##c()",
  2529. getLLVMStyleWithColumns(22));
  2530. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2531. }
  2532. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2533. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2534. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2535. }
  2536. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2537. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2538. " \\\n"
  2539. " b;",
  2540. getLLVMStyleWithColumns(25)));
  2541. EXPECT_EQ("#define A \\\n"
  2542. " \\\n"
  2543. " a; \\\n"
  2544. " b;",
  2545. format("#define A \\\n"
  2546. " \\\n"
  2547. " a; \\\n"
  2548. " b;",
  2549. getLLVMStyleWithColumns(11)));
  2550. EXPECT_EQ("#define A \\\n"
  2551. " a; \\\n"
  2552. " \\\n"
  2553. " b;",
  2554. format("#define A \\\n"
  2555. " a; \\\n"
  2556. " \\\n"
  2557. " b;",
  2558. getLLVMStyleWithColumns(11)));
  2559. }
  2560. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2561. verifyIncompleteFormat("#define A :");
  2562. verifyFormat("#define SOMECASES \\\n"
  2563. " case 1: \\\n"
  2564. " case 2\n",
  2565. getLLVMStyleWithColumns(20));
  2566. verifyFormat("#define A template <typename T>");
  2567. verifyIncompleteFormat("#define STR(x) #x\n"
  2568. "f(STR(this_is_a_string_literal{));");
  2569. verifyFormat("#pragma omp threadprivate( \\\n"
  2570. " y)), // expected-warning",
  2571. getLLVMStyleWithColumns(28));
  2572. verifyFormat("#d, = };");
  2573. verifyFormat("#if \"a");
  2574. verifyIncompleteFormat("({\n"
  2575. "#define b \\\n"
  2576. " } \\\n"
  2577. " a\n"
  2578. "a",
  2579. getLLVMStyleWithColumns(15));
  2580. verifyFormat("#define A \\\n"
  2581. " { \\\n"
  2582. " {\n"
  2583. "#define B \\\n"
  2584. " } \\\n"
  2585. " }",
  2586. getLLVMStyleWithColumns(15));
  2587. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2588. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2589. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2590. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2591. }
  2592. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2593. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2594. EXPECT_EQ("class A : public QObject {\n"
  2595. " Q_OBJECT\n"
  2596. "\n"
  2597. " A() {}\n"
  2598. "};",
  2599. format("class A : public QObject {\n"
  2600. " Q_OBJECT\n"
  2601. "\n"
  2602. " A() {\n}\n"
  2603. "} ;"));
  2604. EXPECT_EQ("MACRO\n"
  2605. "/*static*/ int i;",
  2606. format("MACRO\n"
  2607. " /*static*/ int i;"));
  2608. EXPECT_EQ("SOME_MACRO\n"
  2609. "namespace {\n"
  2610. "void f();\n"
  2611. "}",
  2612. format("SOME_MACRO\n"
  2613. " namespace {\n"
  2614. "void f( );\n"
  2615. "}"));
  2616. // Only if the identifier contains at least 5 characters.
  2617. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2618. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2619. // Only if everything is upper case.
  2620. EXPECT_EQ("class A : public QObject {\n"
  2621. " Q_Object A() {}\n"
  2622. "};",
  2623. format("class A : public QObject {\n"
  2624. " Q_Object\n"
  2625. " A() {\n}\n"
  2626. "} ;"));
  2627. // Only if the next line can actually start an unwrapped line.
  2628. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2629. format("SOME_WEIRD_LOG_MACRO\n"
  2630. "<< SomeThing;"));
  2631. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2632. "(n, buffers))\n",
  2633. getChromiumStyle(FormatStyle::LK_Cpp));
  2634. }
  2635. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2636. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2637. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2638. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2639. "class X {};\n"
  2640. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2641. "int *createScopDetectionPass() { return 0; }",
  2642. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2643. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2644. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2645. " class X {};\n"
  2646. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2647. " int *createScopDetectionPass() { return 0; }"));
  2648. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2649. // braces, so that inner block is indented one level more.
  2650. EXPECT_EQ("int q() {\n"
  2651. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2652. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2653. " IPC_END_MESSAGE_MAP()\n"
  2654. "}",
  2655. format("int q() {\n"
  2656. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2657. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2658. " IPC_END_MESSAGE_MAP()\n"
  2659. "}"));
  2660. // Same inside macros.
  2661. EXPECT_EQ("#define LIST(L) \\\n"
  2662. " L(A) \\\n"
  2663. " L(B) \\\n"
  2664. " L(C)",
  2665. format("#define LIST(L) \\\n"
  2666. " L(A) \\\n"
  2667. " L(B) \\\n"
  2668. " L(C)",
  2669. getGoogleStyle()));
  2670. // These must not be recognized as macros.
  2671. EXPECT_EQ("int q() {\n"
  2672. " f(x);\n"
  2673. " f(x) {}\n"
  2674. " f(x)->g();\n"
  2675. " f(x)->*g();\n"
  2676. " f(x).g();\n"
  2677. " f(x) = x;\n"
  2678. " f(x) += x;\n"
  2679. " f(x) -= x;\n"
  2680. " f(x) *= x;\n"
  2681. " f(x) /= x;\n"
  2682. " f(x) %= x;\n"
  2683. " f(x) &= x;\n"
  2684. " f(x) |= x;\n"
  2685. " f(x) ^= x;\n"
  2686. " f(x) >>= x;\n"
  2687. " f(x) <<= x;\n"
  2688. " f(x)[y].z();\n"
  2689. " LOG(INFO) << x;\n"
  2690. " ifstream(x) >> x;\n"
  2691. "}\n",
  2692. format("int q() {\n"
  2693. " f(x)\n;\n"
  2694. " f(x)\n {}\n"
  2695. " f(x)\n->g();\n"
  2696. " f(x)\n->*g();\n"
  2697. " f(x)\n.g();\n"
  2698. " f(x)\n = x;\n"
  2699. " f(x)\n += x;\n"
  2700. " f(x)\n -= x;\n"
  2701. " f(x)\n *= x;\n"
  2702. " f(x)\n /= x;\n"
  2703. " f(x)\n %= x;\n"
  2704. " f(x)\n &= x;\n"
  2705. " f(x)\n |= x;\n"
  2706. " f(x)\n ^= x;\n"
  2707. " f(x)\n >>= x;\n"
  2708. " f(x)\n <<= x;\n"
  2709. " f(x)\n[y].z();\n"
  2710. " LOG(INFO)\n << x;\n"
  2711. " ifstream(x)\n >> x;\n"
  2712. "}\n"));
  2713. EXPECT_EQ("int q() {\n"
  2714. " F(x)\n"
  2715. " if (1) {\n"
  2716. " }\n"
  2717. " F(x)\n"
  2718. " while (1) {\n"
  2719. " }\n"
  2720. " F(x)\n"
  2721. " G(x);\n"
  2722. " F(x)\n"
  2723. " try {\n"
  2724. " Q();\n"
  2725. " } catch (...) {\n"
  2726. " }\n"
  2727. "}\n",
  2728. format("int q() {\n"
  2729. "F(x)\n"
  2730. "if (1) {}\n"
  2731. "F(x)\n"
  2732. "while (1) {}\n"
  2733. "F(x)\n"
  2734. "G(x);\n"
  2735. "F(x)\n"
  2736. "try { Q(); } catch (...) {}\n"
  2737. "}\n"));
  2738. EXPECT_EQ("class A {\n"
  2739. " A() : t(0) {}\n"
  2740. " A(int i) noexcept() : {}\n"
  2741. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2742. " try : t(0) {\n"
  2743. " } catch (...) {\n"
  2744. " }\n"
  2745. "};",
  2746. format("class A {\n"
  2747. " A()\n : t(0) {}\n"
  2748. " A(int i)\n noexcept() : {}\n"
  2749. " A(X x)\n"
  2750. " try : t(0) {} catch (...) {}\n"
  2751. "};"));
  2752. EXPECT_EQ("class SomeClass {\n"
  2753. "public:\n"
  2754. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2755. "};",
  2756. format("class SomeClass {\n"
  2757. "public:\n"
  2758. " SomeClass()\n"
  2759. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2760. "};"));
  2761. EXPECT_EQ("class SomeClass {\n"
  2762. "public:\n"
  2763. " SomeClass()\n"
  2764. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2765. "};",
  2766. format("class SomeClass {\n"
  2767. "public:\n"
  2768. " SomeClass()\n"
  2769. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2770. "};",
  2771. getLLVMStyleWithColumns(40)));
  2772. verifyFormat("MACRO(>)");
  2773. }
  2774. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2775. verifyFormat("#define A \\\n"
  2776. " f({ \\\n"
  2777. " g(); \\\n"
  2778. " });",
  2779. getLLVMStyleWithColumns(11));
  2780. }
  2781. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2782. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2783. }
  2784. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2785. verifyFormat("{\n { a #c; }\n}");
  2786. }
  2787. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2788. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2789. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2790. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2791. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2792. }
  2793. TEST_F(FormatTest, EscapedNewlines) {
  2794. EXPECT_EQ(
  2795. "#define A \\\n int i; \\\n int j;",
  2796. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2797. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2798. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2799. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2800. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2801. }
  2802. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2803. EXPECT_EQ(
  2804. "int xxxxxxxxx; /* "
  2805. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2806. "zzzzzz\n"
  2807. "0*/",
  2808. format("int xxxxxxxxx; /* "
  2809. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2810. "0*/"));
  2811. }
  2812. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2813. verifyFormat("#define A \\\n"
  2814. " int v( \\\n"
  2815. " a); \\\n"
  2816. " int i;",
  2817. getLLVMStyleWithColumns(11));
  2818. }
  2819. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2820. EXPECT_EQ(
  2821. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2822. " \\\n"
  2823. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2824. "\n"
  2825. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2826. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2827. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2828. "\\\n"
  2829. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2830. " \n"
  2831. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2832. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2833. }
  2834. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2835. EXPECT_EQ("int\n"
  2836. "#define A\n"
  2837. " a;",
  2838. format("int\n#define A\na;"));
  2839. verifyFormat("functionCallTo(\n"
  2840. " someOtherFunction(\n"
  2841. " withSomeParameters, whichInSequence,\n"
  2842. " areLongerThanALine(andAnotherCall,\n"
  2843. "#define A B\n"
  2844. " withMoreParamters,\n"
  2845. " whichStronglyInfluenceTheLayout),\n"
  2846. " andMoreParameters),\n"
  2847. " trailing);",
  2848. getLLVMStyleWithColumns(69));
  2849. verifyFormat("Foo::Foo()\n"
  2850. "#ifdef BAR\n"
  2851. " : baz(0)\n"
  2852. "#endif\n"
  2853. "{\n"
  2854. "}");
  2855. verifyFormat("void f() {\n"
  2856. " if (true)\n"
  2857. "#ifdef A\n"
  2858. " f(42);\n"
  2859. " x();\n"
  2860. "#else\n"
  2861. " g();\n"
  2862. " x();\n"
  2863. "#endif\n"
  2864. "}");
  2865. verifyFormat("void f(param1, param2,\n"
  2866. " param3,\n"
  2867. "#ifdef A\n"
  2868. " param4(param5,\n"
  2869. "#ifdef A1\n"
  2870. " param6,\n"
  2871. "#ifdef A2\n"
  2872. " param7),\n"
  2873. "#else\n"
  2874. " param8),\n"
  2875. " param9,\n"
  2876. "#endif\n"
  2877. " param10,\n"
  2878. "#endif\n"
  2879. " param11)\n"
  2880. "#else\n"
  2881. " param12)\n"
  2882. "#endif\n"
  2883. "{\n"
  2884. " x();\n"
  2885. "}",
  2886. getLLVMStyleWithColumns(28));
  2887. verifyFormat("#if 1\n"
  2888. "int i;");
  2889. verifyFormat("#if 1\n"
  2890. "#endif\n"
  2891. "#if 1\n"
  2892. "#else\n"
  2893. "#endif\n");
  2894. verifyFormat("DEBUG({\n"
  2895. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2896. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2897. "});\n"
  2898. "#if a\n"
  2899. "#else\n"
  2900. "#endif");
  2901. verifyIncompleteFormat("void f(\n"
  2902. "#if A\n"
  2903. " );\n"
  2904. "#else\n"
  2905. "#endif");
  2906. }
  2907. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2908. verifyFormat("#endif\n"
  2909. "#if B");
  2910. }
  2911. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2912. FormatStyle SingleLine = getLLVMStyle();
  2913. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2914. verifyFormat("#if 0\n"
  2915. "#elif 1\n"
  2916. "#endif\n"
  2917. "void foo() {\n"
  2918. " if (test) foo2();\n"
  2919. "}",
  2920. SingleLine);
  2921. }
  2922. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2923. verifyFormat("functionCall({ int i; });");
  2924. verifyFormat("functionCall({\n"
  2925. " int i;\n"
  2926. " int j;\n"
  2927. "});");
  2928. verifyFormat("functionCall(\n"
  2929. " {\n"
  2930. " int i;\n"
  2931. " int j;\n"
  2932. " },\n"
  2933. " aaaa, bbbb, cccc);");
  2934. verifyFormat("functionA(functionB({\n"
  2935. " int i;\n"
  2936. " int j;\n"
  2937. " }),\n"
  2938. " aaaa, bbbb, cccc);");
  2939. verifyFormat("functionCall(\n"
  2940. " {\n"
  2941. " int i;\n"
  2942. " int j;\n"
  2943. " },\n"
  2944. " aaaa, bbbb, // comment\n"
  2945. " cccc);");
  2946. verifyFormat("functionA(functionB({\n"
  2947. " int i;\n"
  2948. " int j;\n"
  2949. " }),\n"
  2950. " aaaa, bbbb, // comment\n"
  2951. " cccc);");
  2952. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2953. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2954. " int i;\n"
  2955. " int j;\n"
  2956. "});");
  2957. verifyFormat(
  2958. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2959. " {\n"
  2960. " int i; // break\n"
  2961. " },\n"
  2962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2963. " ccccccccccccccccc));");
  2964. verifyFormat("DEBUG({\n"
  2965. " if (a)\n"
  2966. " f();\n"
  2967. "});");
  2968. }
  2969. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2970. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2971. "int i;",
  2972. format(" SOME_MACRO {int i;} int i;"));
  2973. }
  2974. TEST_F(FormatTest, LayoutNestedBlocks) {
  2975. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2976. " struct s {\n"
  2977. " int i;\n"
  2978. " };\n"
  2979. " s kBitsToOs[] = {{10}};\n"
  2980. " for (int i = 0; i < 10; ++i)\n"
  2981. " return;\n"
  2982. "}");
  2983. verifyFormat("call(parameter, {\n"
  2984. " something();\n"
  2985. " // Comment using all columns.\n"
  2986. " somethingelse();\n"
  2987. "});",
  2988. getLLVMStyleWithColumns(40));
  2989. verifyFormat("DEBUG( //\n"
  2990. " { f(); }, a);");
  2991. verifyFormat("DEBUG( //\n"
  2992. " {\n"
  2993. " f(); //\n"
  2994. " },\n"
  2995. " a);");
  2996. EXPECT_EQ("call(parameter, {\n"
  2997. " something();\n"
  2998. " // Comment too\n"
  2999. " // looooooooooong.\n"
  3000. " somethingElse();\n"
  3001. "});",
  3002. format("call(parameter, {\n"
  3003. " something();\n"
  3004. " // Comment too looooooooooong.\n"
  3005. " somethingElse();\n"
  3006. "});",
  3007. getLLVMStyleWithColumns(29)));
  3008. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  3009. EXPECT_EQ("DEBUG({ // comment\n"
  3010. " int i;\n"
  3011. "});",
  3012. format("DEBUG({ // comment\n"
  3013. "int i;\n"
  3014. "});"));
  3015. EXPECT_EQ("DEBUG({\n"
  3016. " int i;\n"
  3017. "\n"
  3018. " // comment\n"
  3019. " int j;\n"
  3020. "});",
  3021. format("DEBUG({\n"
  3022. " int i;\n"
  3023. "\n"
  3024. " // comment\n"
  3025. " int j;\n"
  3026. "});"));
  3027. verifyFormat("DEBUG({\n"
  3028. " if (a)\n"
  3029. " return;\n"
  3030. "});");
  3031. verifyGoogleFormat("DEBUG({\n"
  3032. " if (a) return;\n"
  3033. "});");
  3034. FormatStyle Style = getGoogleStyle();
  3035. Style.ColumnLimit = 45;
  3036. verifyFormat("Debug(aaaaa,\n"
  3037. " {\n"
  3038. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3039. " },\n"
  3040. " a);",
  3041. Style);
  3042. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3043. verifyNoCrash("^{v^{a}}");
  3044. }
  3045. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3046. EXPECT_EQ("#define MACRO() \\\n"
  3047. " Debug(aaa, /* force line break */ \\\n"
  3048. " { \\\n"
  3049. " int i; \\\n"
  3050. " int j; \\\n"
  3051. " })",
  3052. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3053. " { int i; int j; })",
  3054. getGoogleStyle()));
  3055. EXPECT_EQ("#define A \\\n"
  3056. " [] { \\\n"
  3057. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3058. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3059. " }",
  3060. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3061. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3062. getGoogleStyle()));
  3063. }
  3064. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3065. EXPECT_EQ("{}", format("{}"));
  3066. verifyFormat("enum E {};");
  3067. verifyFormat("enum E {}");
  3068. }
  3069. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3070. FormatStyle Style = getLLVMStyle();
  3071. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3072. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3073. verifyFormat("FOO_BEGIN\n"
  3074. " FOO_ENTRY\n"
  3075. "FOO_END", Style);
  3076. verifyFormat("FOO_BEGIN\n"
  3077. " NESTED_FOO_BEGIN\n"
  3078. " NESTED_FOO_ENTRY\n"
  3079. " NESTED_FOO_END\n"
  3080. "FOO_END", Style);
  3081. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3082. " int x;\n"
  3083. " x = 1;\n"
  3084. "FOO_END(Baz)", Style);
  3085. }
  3086. //===----------------------------------------------------------------------===//
  3087. // Line break tests.
  3088. //===----------------------------------------------------------------------===//
  3089. TEST_F(FormatTest, PreventConfusingIndents) {
  3090. verifyFormat(
  3091. "void f() {\n"
  3092. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3093. " parameter, parameter, parameter)),\n"
  3094. " SecondLongCall(parameter));\n"
  3095. "}");
  3096. verifyFormat(
  3097. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3098. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3100. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3101. verifyFormat(
  3102. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3103. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3104. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3105. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3106. verifyFormat(
  3107. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3108. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3109. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3110. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3111. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3112. "#define A Just forcing a new line\n"
  3113. " ddd);");
  3114. }
  3115. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3116. verifyFormat(
  3117. "bool aaaaaaa =\n"
  3118. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3119. " bbbbbbbb();");
  3120. verifyFormat(
  3121. "bool aaaaaaa =\n"
  3122. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3123. " bbbbbbbb();");
  3124. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3125. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3126. " ccccccccc == ddddddddddd;");
  3127. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3129. " ccccccccc == ddddddddddd;");
  3130. verifyFormat(
  3131. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3133. " ccccccccc == ddddddddddd;");
  3134. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3135. " aaaaaa) &&\n"
  3136. " bbbbbb && cccccc;");
  3137. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3138. " aaaaaa) >>\n"
  3139. " bbbbbb;");
  3140. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  3141. " SourceMgr.getSpellingColumnNumber(\n"
  3142. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3143. " 1);");
  3144. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3145. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3146. " cccccc) {\n}");
  3147. verifyFormat("b = a &&\n"
  3148. " // Comment\n"
  3149. " b.c && d;");
  3150. // If the LHS of a comparison is not a binary expression itself, the
  3151. // additional linebreak confuses many people.
  3152. verifyFormat(
  3153. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3154. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3155. "}");
  3156. verifyFormat(
  3157. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3158. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3159. "}");
  3160. verifyFormat(
  3161. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3162. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3163. "}");
  3164. // Even explicit parentheses stress the precedence enough to make the
  3165. // additional break unnecessary.
  3166. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3167. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3168. "}");
  3169. // This cases is borderline, but with the indentation it is still readable.
  3170. verifyFormat(
  3171. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3172. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3173. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3174. "}",
  3175. getLLVMStyleWithColumns(75));
  3176. // If the LHS is a binary expression, we should still use the additional break
  3177. // as otherwise the formatting hides the operator precedence.
  3178. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3179. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3180. " 5) {\n"
  3181. "}");
  3182. FormatStyle OnePerLine = getLLVMStyle();
  3183. OnePerLine.BinPackParameters = false;
  3184. verifyFormat(
  3185. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3186. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3187. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3188. OnePerLine);
  3189. }
  3190. TEST_F(FormatTest, ExpressionIndentation) {
  3191. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3192. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3193. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3194. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3195. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3196. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3198. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3199. " ccccccccccccccccccccccccccccccccccccccccc;");
  3200. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3202. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3203. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3204. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3205. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3206. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3207. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3208. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3210. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3211. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3212. verifyFormat("if () {\n"
  3213. "} else if (aaaaa &&\n"
  3214. " bbbbb > // break\n"
  3215. " ccccc) {\n"
  3216. "}");
  3217. // Presence of a trailing comment used to change indentation of b.
  3218. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3219. " b;\n"
  3220. "return aaaaaaaaaaaaaaaaaaa +\n"
  3221. " b; //",
  3222. getLLVMStyleWithColumns(30));
  3223. }
  3224. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3225. // Not sure what the best system is here. Like this, the LHS can be found
  3226. // immediately above an operator (everything with the same or a higher
  3227. // indent). The RHS is aligned right of the operator and so compasses
  3228. // everything until something with the same indent as the operator is found.
  3229. // FIXME: Is this a good system?
  3230. FormatStyle Style = getLLVMStyle();
  3231. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3232. verifyFormat(
  3233. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3234. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3235. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3236. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3237. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3238. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3239. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3240. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3241. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3242. Style);
  3243. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3244. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3245. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3246. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3247. Style);
  3248. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3249. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3250. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3251. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3252. Style);
  3253. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3254. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3255. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3256. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3257. Style);
  3258. verifyFormat("if () {\n"
  3259. "} else if (aaaaa\n"
  3260. " && bbbbb // break\n"
  3261. " > ccccc) {\n"
  3262. "}",
  3263. Style);
  3264. verifyFormat("return (a)\n"
  3265. " // comment\n"
  3266. " + b;",
  3267. Style);
  3268. verifyFormat(
  3269. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3270. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3271. " + cc;",
  3272. Style);
  3273. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3274. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3275. Style);
  3276. // Forced by comments.
  3277. verifyFormat(
  3278. "unsigned ContentSize =\n"
  3279. " sizeof(int16_t) // DWARF ARange version number\n"
  3280. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3281. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3282. " + sizeof(int8_t); // Segment Size (in bytes)");
  3283. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3284. " == boost::fusion::at_c<1>(iiii).second;",
  3285. Style);
  3286. Style.ColumnLimit = 60;
  3287. verifyFormat("zzzzzzzzzz\n"
  3288. " = bbbbbbbbbbbbbbbbb\n"
  3289. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3290. Style);
  3291. }
  3292. TEST_F(FormatTest, NoOperandAlignment) {
  3293. FormatStyle Style = getLLVMStyle();
  3294. Style.AlignOperands = false;
  3295. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3296. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3297. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3298. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3299. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3300. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3301. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3302. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3303. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3304. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3305. Style);
  3306. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3307. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3308. " + cc;",
  3309. Style);
  3310. verifyFormat("int a = aa\n"
  3311. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3312. " * cccccccccccccccccccccccccccccccccccc;",
  3313. Style);
  3314. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3315. verifyFormat("return (a > b\n"
  3316. " // comment1\n"
  3317. " // comment2\n"
  3318. " || c);",
  3319. Style);
  3320. }
  3321. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3322. FormatStyle Style = getLLVMStyle();
  3323. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3324. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3325. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3326. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3327. Style);
  3328. }
  3329. TEST_F(FormatTest, ConstructorInitializers) {
  3330. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3331. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3332. getLLVMStyleWithColumns(45));
  3333. verifyFormat("Constructor()\n"
  3334. " : Inttializer(FitsOnTheLine) {}",
  3335. getLLVMStyleWithColumns(44));
  3336. verifyFormat("Constructor()\n"
  3337. " : Inttializer(FitsOnTheLine) {}",
  3338. getLLVMStyleWithColumns(43));
  3339. verifyFormat("template <typename T>\n"
  3340. "Constructor() : Initializer(FitsOnTheLine) {}",
  3341. getLLVMStyleWithColumns(45));
  3342. verifyFormat(
  3343. "SomeClass::Constructor()\n"
  3344. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3345. verifyFormat(
  3346. "SomeClass::Constructor()\n"
  3347. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3348. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3349. verifyFormat(
  3350. "SomeClass::Constructor()\n"
  3351. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3352. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3353. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3354. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3355. " : aaaaaaaaaa(aaaaaa) {}");
  3356. verifyFormat("Constructor()\n"
  3357. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3358. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3359. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3360. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3361. verifyFormat("Constructor()\n"
  3362. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3363. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3364. verifyFormat("Constructor(int Parameter = 0)\n"
  3365. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3366. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3367. verifyFormat("Constructor()\n"
  3368. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3369. "}",
  3370. getLLVMStyleWithColumns(60));
  3371. verifyFormat("Constructor()\n"
  3372. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3373. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3374. // Here a line could be saved by splitting the second initializer onto two
  3375. // lines, but that is not desirable.
  3376. verifyFormat("Constructor()\n"
  3377. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3378. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3379. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3380. FormatStyle OnePerLine = getLLVMStyle();
  3381. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3382. OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
  3383. verifyFormat("SomeClass::Constructor()\n"
  3384. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3385. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3386. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3387. OnePerLine);
  3388. verifyFormat("SomeClass::Constructor()\n"
  3389. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3390. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3391. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3392. OnePerLine);
  3393. verifyFormat("MyClass::MyClass(int var)\n"
  3394. " : some_var_(var), // 4 space indent\n"
  3395. " some_other_var_(var + 1) { // lined up\n"
  3396. "}",
  3397. OnePerLine);
  3398. verifyFormat("Constructor()\n"
  3399. " : aaaaa(aaaaaa),\n"
  3400. " aaaaa(aaaaaa),\n"
  3401. " aaaaa(aaaaaa),\n"
  3402. " aaaaa(aaaaaa),\n"
  3403. " aaaaa(aaaaaa) {}",
  3404. OnePerLine);
  3405. verifyFormat("Constructor()\n"
  3406. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3407. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3408. OnePerLine);
  3409. OnePerLine.BinPackParameters = false;
  3410. verifyFormat(
  3411. "Constructor()\n"
  3412. " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3413. " aaaaaaaaaaa().aaa(),\n"
  3414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3415. OnePerLine);
  3416. OnePerLine.ColumnLimit = 60;
  3417. verifyFormat("Constructor()\n"
  3418. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3419. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3420. OnePerLine);
  3421. EXPECT_EQ("Constructor()\n"
  3422. " : // Comment forcing unwanted break.\n"
  3423. " aaaa(aaaa) {}",
  3424. format("Constructor() :\n"
  3425. " // Comment forcing unwanted break.\n"
  3426. " aaaa(aaaa) {}"));
  3427. }
  3428. TEST_F(FormatTest, MemoizationTests) {
  3429. // This breaks if the memoization lookup does not take \c Indent and
  3430. // \c LastSpace into account.
  3431. verifyFormat(
  3432. "extern CFRunLoopTimerRef\n"
  3433. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3434. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3435. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3436. " CFRunLoopTimerContext *context) {}");
  3437. // Deep nesting somewhat works around our memoization.
  3438. verifyFormat(
  3439. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3440. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3441. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3442. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3443. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3444. getLLVMStyleWithColumns(65));
  3445. verifyFormat(
  3446. "aaaaa(\n"
  3447. " aaaaa,\n"
  3448. " aaaaa(\n"
  3449. " aaaaa,\n"
  3450. " aaaaa(\n"
  3451. " aaaaa,\n"
  3452. " aaaaa(\n"
  3453. " aaaaa,\n"
  3454. " aaaaa(\n"
  3455. " aaaaa,\n"
  3456. " aaaaa(\n"
  3457. " aaaaa,\n"
  3458. " aaaaa(\n"
  3459. " aaaaa,\n"
  3460. " aaaaa(\n"
  3461. " aaaaa,\n"
  3462. " aaaaa(\n"
  3463. " aaaaa,\n"
  3464. " aaaaa(\n"
  3465. " aaaaa,\n"
  3466. " aaaaa(\n"
  3467. " aaaaa,\n"
  3468. " aaaaa(\n"
  3469. " aaaaa,\n"
  3470. " aaaaa))))))))))));",
  3471. getLLVMStyleWithColumns(65));
  3472. verifyFormat(
  3473. "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"
  3474. " a),\n"
  3475. " a),\n"
  3476. " a),\n"
  3477. " a),\n"
  3478. " a),\n"
  3479. " a),\n"
  3480. " a),\n"
  3481. " a),\n"
  3482. " a),\n"
  3483. " a),\n"
  3484. " a),\n"
  3485. " a),\n"
  3486. " a),\n"
  3487. " a),\n"
  3488. " a),\n"
  3489. " a),\n"
  3490. " a)",
  3491. getLLVMStyleWithColumns(65));
  3492. // This test takes VERY long when memoization is broken.
  3493. FormatStyle OnePerLine = getLLVMStyle();
  3494. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3495. OnePerLine.BinPackParameters = false;
  3496. std::string input = "Constructor()\n"
  3497. " : aaaa(a,\n";
  3498. for (unsigned i = 0, e = 80; i != e; ++i) {
  3499. input += " a,\n";
  3500. }
  3501. input += " a) {}";
  3502. verifyFormat(input, OnePerLine);
  3503. }
  3504. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3505. verifyFormat(
  3506. "void f() {\n"
  3507. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3508. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3509. " f();\n"
  3510. "}");
  3511. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3512. " Intervals[i - 1].getRange().getLast()) {\n}");
  3513. }
  3514. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3515. // Principially, we break function declarations in a certain order:
  3516. // 1) break amongst arguments.
  3517. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3518. " Cccccccccccccc cccccccccccccc);");
  3519. verifyFormat("template <class TemplateIt>\n"
  3520. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3521. " TemplateIt *stop) {}");
  3522. // 2) break after return type.
  3523. verifyFormat(
  3524. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3525. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3526. getGoogleStyle());
  3527. // 3) break after (.
  3528. verifyFormat(
  3529. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3530. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3531. getGoogleStyle());
  3532. // 4) break before after nested name specifiers.
  3533. verifyFormat(
  3534. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3535. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3536. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3537. getGoogleStyle());
  3538. // However, there are exceptions, if a sufficient amount of lines can be
  3539. // saved.
  3540. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3541. // more adjusting.
  3542. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3543. " Cccccccccccccc cccccccccc,\n"
  3544. " Cccccccccccccc cccccccccc,\n"
  3545. " Cccccccccccccc cccccccccc,\n"
  3546. " Cccccccccccccc cccccccccc);");
  3547. verifyFormat(
  3548. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3549. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3550. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3551. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3552. getGoogleStyle());
  3553. verifyFormat(
  3554. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3555. " Cccccccccccccc cccccccccc,\n"
  3556. " Cccccccccccccc cccccccccc,\n"
  3557. " Cccccccccccccc cccccccccc,\n"
  3558. " Cccccccccccccc cccccccccc,\n"
  3559. " Cccccccccccccc cccccccccc,\n"
  3560. " Cccccccccccccc cccccccccc);");
  3561. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3562. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3563. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3564. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3565. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3566. // Break after multi-line parameters.
  3567. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3568. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3569. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3570. " bbbb bbbb);");
  3571. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3572. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3573. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3574. " int bbbbbbbbbbbbb);");
  3575. // Treat overloaded operators like other functions.
  3576. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3577. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3578. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3579. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3580. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3581. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3582. verifyGoogleFormat(
  3583. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3584. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3585. verifyGoogleFormat(
  3586. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3587. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3588. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3589. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3590. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3591. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3592. verifyGoogleFormat(
  3593. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3594. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3595. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3596. FormatStyle Style = getLLVMStyle();
  3597. Style.PointerAlignment = FormatStyle::PAS_Left;
  3598. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3599. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3600. Style);
  3601. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3602. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3603. Style);
  3604. }
  3605. TEST_F(FormatTest, TrailingReturnType) {
  3606. verifyFormat("auto foo() -> int;\n");
  3607. verifyFormat("struct S {\n"
  3608. " auto bar() const -> int;\n"
  3609. "};");
  3610. verifyFormat("template <size_t Order, typename T>\n"
  3611. "auto load_img(const std::string &filename)\n"
  3612. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3613. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3614. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3615. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3616. verifyFormat("template <typename T>\n"
  3617. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3618. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3619. // Not trailing return types.
  3620. verifyFormat("void f() { auto a = b->c(); }");
  3621. }
  3622. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3623. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3624. // they are not function-like.
  3625. FormatStyle Style = getGoogleStyle();
  3626. Style.ColumnLimit = 47;
  3627. verifyFormat("void someLongFunction(\n"
  3628. " int someLoooooooooooooongParameter) const {\n}",
  3629. getLLVMStyleWithColumns(47));
  3630. verifyFormat("LoooooongReturnType\n"
  3631. "someLoooooooongFunction() const {}",
  3632. getLLVMStyleWithColumns(47));
  3633. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3634. " const {}",
  3635. Style);
  3636. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3637. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3638. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3639. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3640. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3641. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3642. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3643. " aaaaaaaaaaa aaaaa) const override;");
  3644. verifyGoogleFormat(
  3645. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3646. " const override;");
  3647. // Even if the first parameter has to be wrapped.
  3648. verifyFormat("void someLongFunction(\n"
  3649. " int someLongParameter) const {}",
  3650. getLLVMStyleWithColumns(46));
  3651. verifyFormat("void someLongFunction(\n"
  3652. " int someLongParameter) const {}",
  3653. Style);
  3654. verifyFormat("void someLongFunction(\n"
  3655. " int someLongParameter) override {}",
  3656. Style);
  3657. verifyFormat("void someLongFunction(\n"
  3658. " int someLongParameter) OVERRIDE {}",
  3659. Style);
  3660. verifyFormat("void someLongFunction(\n"
  3661. " int someLongParameter) final {}",
  3662. Style);
  3663. verifyFormat("void someLongFunction(\n"
  3664. " int someLongParameter) FINAL {}",
  3665. Style);
  3666. verifyFormat("void someLongFunction(\n"
  3667. " int parameter) const override {}",
  3668. Style);
  3669. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3670. verifyFormat("void someLongFunction(\n"
  3671. " int someLongParameter) const\n"
  3672. "{\n"
  3673. "}",
  3674. Style);
  3675. // Unless these are unknown annotations.
  3676. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3677. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3678. " LONG_AND_UGLY_ANNOTATION;");
  3679. // Breaking before function-like trailing annotations is fine to keep them
  3680. // close to their arguments.
  3681. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3682. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3683. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3684. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3685. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3686. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3687. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3688. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3689. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3690. verifyFormat(
  3691. "void aaaaaaaaaaaaaaaaaa()\n"
  3692. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3693. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3694. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3695. " __attribute__((unused));");
  3696. verifyGoogleFormat(
  3697. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3698. " GUARDED_BY(aaaaaaaaaaaa);");
  3699. verifyGoogleFormat(
  3700. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3701. " GUARDED_BY(aaaaaaaaaaaa);");
  3702. verifyGoogleFormat(
  3703. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3704. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3705. verifyGoogleFormat(
  3706. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3707. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3708. }
  3709. TEST_F(FormatTest, FunctionAnnotations) {
  3710. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3711. "int OldFunction(const string &parameter) {}");
  3712. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3713. "string OldFunction(const string &parameter) {}");
  3714. verifyFormat("template <typename T>\n"
  3715. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3716. "string OldFunction(const string &parameter) {}");
  3717. // Not function annotations.
  3718. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3719. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3720. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3721. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3722. }
  3723. TEST_F(FormatTest, BreaksDesireably) {
  3724. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3725. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3726. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3727. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3728. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3729. "}");
  3730. verifyFormat(
  3731. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3732. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3733. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3734. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3735. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3736. verifyFormat(
  3737. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3738. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3739. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3740. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3741. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3742. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3743. verifyFormat(
  3744. "void f() {\n"
  3745. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3746. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3747. "}");
  3748. verifyFormat(
  3749. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3751. verifyFormat(
  3752. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3754. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3755. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3756. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3757. // Indent consistently independent of call expression and unary operator.
  3758. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3759. " dddddddddddddddddddddddddddddd));");
  3760. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3761. " dddddddddddddddddddddddddddddd));");
  3762. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3763. " dddddddddddddddddddddddddddddd));");
  3764. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3765. // taking into account the StopAt value.
  3766. verifyFormat(
  3767. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3768. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3769. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3770. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3771. verifyFormat("{\n {\n {\n"
  3772. " Annotation.SpaceRequiredBefore =\n"
  3773. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3774. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3775. " }\n }\n}");
  3776. // Break on an outer level if there was a break on an inner level.
  3777. EXPECT_EQ("f(g(h(a, // comment\n"
  3778. " b, c),\n"
  3779. " d, e),\n"
  3780. " x, y);",
  3781. format("f(g(h(a, // comment\n"
  3782. " b, c), d, e), x, y);"));
  3783. // Prefer breaking similar line breaks.
  3784. verifyFormat(
  3785. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3786. " NSTrackingMouseEnteredAndExited |\n"
  3787. " NSTrackingActiveAlways;");
  3788. }
  3789. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3790. FormatStyle NoBinPacking = getGoogleStyle();
  3791. NoBinPacking.BinPackParameters = false;
  3792. NoBinPacking.BinPackArguments = true;
  3793. verifyFormat("void f() {\n"
  3794. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3795. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3796. "}",
  3797. NoBinPacking);
  3798. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3799. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3800. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3801. NoBinPacking);
  3802. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3803. verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3804. " vector<int> bbbbbbbbbbbbbbb);",
  3805. NoBinPacking);
  3806. // FIXME: This behavior difference is probably not wanted. However, currently
  3807. // we cannot distinguish BreakBeforeParameter being set because of the wrapped
  3808. // template arguments from BreakBeforeParameter being set because of the
  3809. // one-per-line formatting.
  3810. verifyFormat(
  3811. "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3812. " aaaaaaaaaa> aaaaaaaaaa);",
  3813. NoBinPacking);
  3814. verifyFormat(
  3815. "void fffffffffff(\n"
  3816. " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
  3817. " aaaaaaaaaa);");
  3818. }
  3819. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3820. FormatStyle NoBinPacking = getGoogleStyle();
  3821. NoBinPacking.BinPackParameters = false;
  3822. NoBinPacking.BinPackArguments = false;
  3823. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3824. " aaaaaaaaaaaaaaaaaaaa,\n"
  3825. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3826. NoBinPacking);
  3827. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3828. " aaaaaaaaaaaaa,\n"
  3829. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3830. NoBinPacking);
  3831. verifyFormat(
  3832. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3833. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3834. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3835. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3836. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3837. NoBinPacking);
  3838. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3839. " .aaaaaaaaaaaaaaaaaa();",
  3840. NoBinPacking);
  3841. verifyFormat("void f() {\n"
  3842. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3843. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3844. "}",
  3845. NoBinPacking);
  3846. verifyFormat(
  3847. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3848. " aaaaaaaaaaaa,\n"
  3849. " aaaaaaaaaaaa);",
  3850. NoBinPacking);
  3851. verifyFormat(
  3852. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3853. " ddddddddddddddddddddddddddddd),\n"
  3854. " test);",
  3855. NoBinPacking);
  3856. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3857. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3858. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  3859. " aaaaaaaaaaaaaaaaaa;",
  3860. NoBinPacking);
  3861. verifyFormat("a(\"a\"\n"
  3862. " \"a\",\n"
  3863. " a);");
  3864. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3865. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3866. " aaaaaaaaa,\n"
  3867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3868. NoBinPacking);
  3869. verifyFormat(
  3870. "void f() {\n"
  3871. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3872. " .aaaaaaa();\n"
  3873. "}",
  3874. NoBinPacking);
  3875. verifyFormat(
  3876. "template <class SomeType, class SomeOtherType>\n"
  3877. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3878. NoBinPacking);
  3879. }
  3880. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3881. FormatStyle Style = getLLVMStyleWithColumns(15);
  3882. Style.ExperimentalAutoDetectBinPacking = true;
  3883. EXPECT_EQ("aaa(aaaa,\n"
  3884. " aaaa,\n"
  3885. " aaaa);\n"
  3886. "aaa(aaaa,\n"
  3887. " aaaa,\n"
  3888. " aaaa);",
  3889. format("aaa(aaaa,\n" // one-per-line
  3890. " aaaa,\n"
  3891. " aaaa );\n"
  3892. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3893. Style));
  3894. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3895. " aaaa);\n"
  3896. "aaa(aaaa, aaaa,\n"
  3897. " aaaa);",
  3898. format("aaa(aaaa, aaaa,\n" // bin-packed
  3899. " aaaa );\n"
  3900. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3901. Style));
  3902. }
  3903. TEST_F(FormatTest, FormatsBuilderPattern) {
  3904. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3905. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3906. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3907. " .StartsWith(\".init\", ORDER_INIT)\n"
  3908. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3909. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3910. " .Default(ORDER_TEXT);\n");
  3911. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3912. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3913. verifyFormat(
  3914. "aaaaaaa->aaaaaaa\n"
  3915. " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3917. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3918. verifyFormat(
  3919. "aaaaaaa->aaaaaaa\n"
  3920. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3921. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3922. verifyFormat(
  3923. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3924. " aaaaaaaaaaaaaa);");
  3925. verifyFormat(
  3926. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3927. " aaaaaa->aaaaaaaaaaaa()\n"
  3928. " ->aaaaaaaaaaaaaaaa(\n"
  3929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3930. " ->aaaaaaaaaaaaaaaaa();");
  3931. verifyGoogleFormat(
  3932. "void f() {\n"
  3933. " someo->Add((new util::filetools::Handler(dir))\n"
  3934. " ->OnEvent1(NewPermanentCallback(\n"
  3935. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3936. " ->OnEvent2(NewPermanentCallback(\n"
  3937. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3938. " ->OnEvent3(NewPermanentCallback(\n"
  3939. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3940. " ->OnEvent5(NewPermanentCallback(\n"
  3941. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3942. " ->OnEvent6(NewPermanentCallback(\n"
  3943. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3944. "}");
  3945. verifyFormat(
  3946. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3947. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3948. " .aaaaaaaaaaaaaaa()\n"
  3949. " .aaaaaaaaaaaaaaa()\n"
  3950. " .aaaaaaaaaaaaaaa()\n"
  3951. " .aaaaaaaaaaaaaaa();");
  3952. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3953. " .aaaaaaaaaaaaaaa()\n"
  3954. " .aaaaaaaaaaaaaaa()\n"
  3955. " .aaaaaaaaaaaaaaa();");
  3956. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3957. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3958. " .aaaaaaaaaaaaaaa();");
  3959. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3960. " ->aaaaaaaaaaaaaae(0)\n"
  3961. " ->aaaaaaaaaaaaaaa();");
  3962. // Don't linewrap after very short segments.
  3963. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3964. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3965. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3966. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3967. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3968. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3969. verifyFormat("aaa()\n"
  3970. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3971. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3972. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3973. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3974. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3975. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3976. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3977. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3978. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3979. // Prefer not to break after empty parentheses.
  3980. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3981. " First->LastNewlineOffset);");
  3982. // Prefer not to create "hanging" indents.
  3983. verifyFormat(
  3984. "return !soooooooooooooome_map\n"
  3985. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3986. " .second;");
  3987. verifyFormat(
  3988. "return aaaaaaaaaaaaaaaa\n"
  3989. " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
  3990. " .aaaa(aaaaaaaaaaaaaa);");
  3991. // No hanging indent here.
  3992. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
  3993. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3994. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
  3995. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3996. verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  3997. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3998. getLLVMStyleWithColumns(60));
  3999. verifyFormat("aaaaaaaaaaaaaaaaaa\n"
  4000. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4001. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4002. getLLVMStyleWithColumns(59));
  4003. verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4004. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4005. " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4006. }
  4007. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  4008. verifyFormat(
  4009. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4010. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  4011. verifyFormat(
  4012. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  4013. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  4014. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4015. " ccccccccccccccccccccccccc) {\n}");
  4016. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4017. " ccccccccccccccccccccccccc) {\n}");
  4018. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4019. " ccccccccccccccccccccccccc) {\n}");
  4020. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4021. " ccccccccccccccccccccccccc) {\n}");
  4022. verifyFormat(
  4023. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  4024. " ccccccccccccccccccccccccc) {\n}");
  4025. verifyFormat(
  4026. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  4027. " ccccccccccccccccccccccccc) {\n}");
  4028. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  4029. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  4030. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  4031. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4032. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  4033. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  4034. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  4035. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4036. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  4037. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  4038. " aaaaaaaaaaaaaaa != aa) {\n}");
  4039. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  4040. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  4041. " aaaaaaaaaaaaaaa != aa) {\n}");
  4042. }
  4043. TEST_F(FormatTest, BreaksAfterAssignments) {
  4044. verifyFormat(
  4045. "unsigned Cost =\n"
  4046. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  4047. " SI->getPointerAddressSpaceee());\n");
  4048. verifyFormat(
  4049. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  4050. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  4051. verifyFormat(
  4052. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  4053. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  4054. verifyFormat("unsigned OriginalStartColumn =\n"
  4055. " SourceMgr.getSpellingColumnNumber(\n"
  4056. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  4057. " 1;");
  4058. }
  4059. TEST_F(FormatTest, AlignsAfterAssignments) {
  4060. verifyFormat(
  4061. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4062. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4063. verifyFormat(
  4064. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4065. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4066. verifyFormat(
  4067. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4068. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4069. verifyFormat(
  4070. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4071. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4072. verifyFormat(
  4073. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4074. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4075. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  4076. }
  4077. TEST_F(FormatTest, AlignsAfterReturn) {
  4078. verifyFormat(
  4079. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4080. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4081. verifyFormat(
  4082. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4083. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4084. verifyFormat(
  4085. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4086. " aaaaaaaaaaaaaaaaaaaaaa();");
  4087. verifyFormat(
  4088. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4089. " aaaaaaaaaaaaaaaaaaaaaa());");
  4090. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4091. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4092. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4093. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4094. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4095. verifyFormat("return\n"
  4096. " // true if code is one of a or b.\n"
  4097. " code == a || code == b;");
  4098. }
  4099. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4100. verifyFormat(
  4101. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4102. " aaaaaaaaa aaaaaaa) {}");
  4103. verifyFormat(
  4104. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4105. " aaaaaaaaaaa aaaaaaaaa);");
  4106. verifyFormat(
  4107. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4108. " aaaaaaaaaaaaaaaaaaaaa));");
  4109. FormatStyle Style = getLLVMStyle();
  4110. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4111. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4112. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4113. Style);
  4114. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4115. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4116. Style);
  4117. verifyFormat("SomeLongVariableName->someFunction(\n"
  4118. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4119. Style);
  4120. verifyFormat(
  4121. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4122. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4123. Style);
  4124. verifyFormat(
  4125. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4126. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4127. Style);
  4128. verifyFormat(
  4129. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4130. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4131. Style);
  4132. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4133. Style.BinPackArguments = false;
  4134. Style.BinPackParameters = false;
  4135. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4136. " aaaaaaaaaaa aaaaaaaa,\n"
  4137. " aaaaaaaaa aaaaaaa,\n"
  4138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4139. Style);
  4140. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4141. " aaaaaaaaaaa aaaaaaaaa,\n"
  4142. " aaaaaaaaaaa aaaaaaaaa,\n"
  4143. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4144. Style);
  4145. verifyFormat("SomeLongVariableName->someFunction(\n"
  4146. " foooooooo(\n"
  4147. " aaaaaaaaaaaaaaa,\n"
  4148. " aaaaaaaaaaaaaaaaaaaaa,\n"
  4149. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4150. Style);
  4151. }
  4152. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4153. FormatStyle Style = getLLVMStyleWithColumns(40);
  4154. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4155. " bbbbbbbbbbbbbbbbbbbbbb);",
  4156. Style);
  4157. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4158. Style.AlignOperands = false;
  4159. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4160. " bbbbbbbbbbbbbbbbbbbbbb);",
  4161. Style);
  4162. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4163. Style.AlignOperands = true;
  4164. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4165. " bbbbbbbbbbbbbbbbbbbbbb);",
  4166. Style);
  4167. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4168. Style.AlignOperands = false;
  4169. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4170. " bbbbbbbbbbbbbbbbbbbbbb);",
  4171. Style);
  4172. }
  4173. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4174. verifyFormat(
  4175. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4176. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4177. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4178. verifyFormat(
  4179. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4180. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4181. verifyFormat(
  4182. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4183. " : aaaaaaaaaaaaa);");
  4184. verifyFormat(
  4185. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4186. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4187. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4188. " aaaaaaaaaaaaa);");
  4189. verifyFormat(
  4190. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4191. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4192. " aaaaaaaaaaaaa);");
  4193. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4194. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4195. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4196. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4198. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4199. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4200. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4202. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4203. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4204. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4205. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4206. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4207. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4209. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4210. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4211. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4212. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4213. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4214. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4215. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4216. " : aaaaaaaaaaaaaaaa;");
  4217. verifyFormat(
  4218. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4219. " ? aaaaaaaaaaaaaaa\n"
  4220. " : aaaaaaaaaaaaaaa;");
  4221. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4222. " aaaaaaaaa\n"
  4223. " ? b\n"
  4224. " : c);");
  4225. verifyFormat("return aaaa == bbbb\n"
  4226. " // comment\n"
  4227. " ? aaaa\n"
  4228. " : bbbb;");
  4229. verifyFormat("unsigned Indent =\n"
  4230. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4231. " ? IndentForLevel[TheLine.Level]\n"
  4232. " : TheLine * 2,\n"
  4233. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4234. getLLVMStyleWithColumns(70));
  4235. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4236. " ? aaaaaaaaaaaaaaa\n"
  4237. " : bbbbbbbbbbbbbbb //\n"
  4238. " ? ccccccccccccccc\n"
  4239. " : ddddddddddddddd;");
  4240. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4241. " ? aaaaaaaaaaaaaaa\n"
  4242. " : (bbbbbbbbbbbbbbb //\n"
  4243. " ? ccccccccccccccc\n"
  4244. " : ddddddddddddddd);");
  4245. verifyFormat(
  4246. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4247. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4248. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4249. " aaaaaaaaaaaaaaaaaaaaa\n"
  4250. " : aaaaaaaaaa;");
  4251. verifyFormat(
  4252. "aaaaaa = aaaaaaaaaaaa\n"
  4253. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4254. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4255. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4256. FormatStyle NoBinPacking = getLLVMStyle();
  4257. NoBinPacking.BinPackArguments = false;
  4258. verifyFormat(
  4259. "void f() {\n"
  4260. " g(aaa,\n"
  4261. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4262. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4263. " ? aaaaaaaaaaaaaaa\n"
  4264. " : aaaaaaaaaaaaaaa);\n"
  4265. "}",
  4266. NoBinPacking);
  4267. verifyFormat(
  4268. "void f() {\n"
  4269. " g(aaa,\n"
  4270. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4271. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4272. " ?: aaaaaaaaaaaaaaa);\n"
  4273. "}",
  4274. NoBinPacking);
  4275. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4276. " // comment.\n"
  4277. " ccccccccccccccccccccccccccccccccccccccc\n"
  4278. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4279. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4280. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4281. verifyFormat("return a != b\n"
  4282. " // comment\n"
  4283. " ? a = b\n"
  4284. " : a = b;");
  4285. verifyFormat("return a != b\n"
  4286. " // comment\n"
  4287. " ? a = a != b\n"
  4288. " // comment\n"
  4289. " ? a = b\n"
  4290. " : a\n"
  4291. " : a;\n");
  4292. verifyFormat("return a != b\n"
  4293. " // comment\n"
  4294. " ? a\n"
  4295. " : a = a != b\n"
  4296. " // comment\n"
  4297. " ? a = b\n"
  4298. " : a;");
  4299. }
  4300. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4301. FormatStyle Style = getLLVMStyle();
  4302. Style.BreakBeforeTernaryOperators = false;
  4303. Style.ColumnLimit = 70;
  4304. verifyFormat(
  4305. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4306. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4307. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4308. Style);
  4309. verifyFormat(
  4310. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4311. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4312. Style);
  4313. verifyFormat(
  4314. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4315. " aaaaaaaaaaaaa);",
  4316. Style);
  4317. verifyFormat(
  4318. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4319. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4320. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4321. " aaaaaaaaaaaaa);",
  4322. Style);
  4323. verifyFormat(
  4324. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4325. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4326. " aaaaaaaaaaaaa);",
  4327. Style);
  4328. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4329. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4330. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4331. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4332. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4333. Style);
  4334. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4335. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4336. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4337. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4338. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4339. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4340. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4341. Style);
  4342. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4343. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4344. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4345. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4346. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4347. Style);
  4348. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4349. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4350. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4351. Style);
  4352. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4353. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4354. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4355. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4356. Style);
  4357. verifyFormat(
  4358. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4359. " aaaaaaaaaaaaaaa :\n"
  4360. " aaaaaaaaaaaaaaa;",
  4361. Style);
  4362. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4363. " aaaaaaaaa ?\n"
  4364. " b :\n"
  4365. " c);",
  4366. Style);
  4367. verifyFormat(
  4368. "unsigned Indent =\n"
  4369. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4370. " IndentForLevel[TheLine.Level] :\n"
  4371. " TheLine * 2,\n"
  4372. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4373. Style);
  4374. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4375. " aaaaaaaaaaaaaaa :\n"
  4376. " bbbbbbbbbbbbbbb ? //\n"
  4377. " ccccccccccccccc :\n"
  4378. " ddddddddddddddd;",
  4379. Style);
  4380. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4381. " aaaaaaaaaaaaaaa :\n"
  4382. " (bbbbbbbbbbbbbbb ? //\n"
  4383. " ccccccccccccccc :\n"
  4384. " ddddddddddddddd);",
  4385. Style);
  4386. }
  4387. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4388. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4389. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4390. verifyFormat("bool a = true, b = false;");
  4391. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4392. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4393. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4394. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4395. verifyFormat(
  4396. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4397. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4398. " d = e && f;");
  4399. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4400. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4401. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4402. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4403. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4404. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4405. FormatStyle Style = getGoogleStyle();
  4406. Style.PointerAlignment = FormatStyle::PAS_Left;
  4407. Style.DerivePointerAlignment = false;
  4408. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4409. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4410. " *b = bbbbbbbbbbbbbbbbbbb;",
  4411. Style);
  4412. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4413. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4414. Style);
  4415. }
  4416. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4417. verifyFormat("arr[foo ? bar : baz];");
  4418. verifyFormat("f()[foo ? bar : baz];");
  4419. verifyFormat("(a + b)[foo ? bar : baz];");
  4420. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4421. }
  4422. TEST_F(FormatTest, AlignsStringLiterals) {
  4423. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4424. " \"short literal\");");
  4425. verifyFormat(
  4426. "looooooooooooooooooooooooongFunction(\n"
  4427. " \"short literal\"\n"
  4428. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4429. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4430. " \" string literals\",\n"
  4431. " and, other, parameters);");
  4432. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4433. " \"5678\";",
  4434. format("fun + \"1243\" /* comment */\n"
  4435. " \"5678\";",
  4436. getLLVMStyleWithColumns(28)));
  4437. EXPECT_EQ(
  4438. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4439. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4440. " \"aaaaaaaaaaaaaaaa\";",
  4441. format("aaaaaa ="
  4442. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4443. "aaaaaaaaaaaaaaaaaaaaa\" "
  4444. "\"aaaaaaaaaaaaaaaa\";"));
  4445. verifyFormat("a = a + \"a\"\n"
  4446. " \"a\"\n"
  4447. " \"a\";");
  4448. verifyFormat("f(\"a\", \"b\"\n"
  4449. " \"c\");");
  4450. verifyFormat(
  4451. "#define LL_FORMAT \"ll\"\n"
  4452. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4453. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4454. verifyFormat("#define A(X) \\\n"
  4455. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4456. " \"ccccc\"",
  4457. getLLVMStyleWithColumns(23));
  4458. verifyFormat("#define A \"def\"\n"
  4459. "f(\"abc\" A \"ghi\"\n"
  4460. " \"jkl\");");
  4461. verifyFormat("f(L\"a\"\n"
  4462. " L\"b\");");
  4463. verifyFormat("#define A(X) \\\n"
  4464. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4465. " L\"ccccc\"",
  4466. getLLVMStyleWithColumns(25));
  4467. verifyFormat("f(@\"a\"\n"
  4468. " @\"b\");");
  4469. verifyFormat("NSString s = @\"a\"\n"
  4470. " @\"b\"\n"
  4471. " @\"c\";");
  4472. verifyFormat("NSString s = @\"a\"\n"
  4473. " \"b\"\n"
  4474. " \"c\";");
  4475. }
  4476. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  4477. FormatStyle Style = getLLVMStyle();
  4478. // No declarations or definitions should be moved to own line.
  4479. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  4480. verifyFormat("class A {\n"
  4481. " int f() { return 1; }\n"
  4482. " int g();\n"
  4483. "};\n"
  4484. "int f() { return 1; }\n"
  4485. "int g();\n",
  4486. Style);
  4487. // All declarations and definitions should have the return type moved to its
  4488. // own
  4489. // line.
  4490. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  4491. verifyFormat("class E {\n"
  4492. " int\n"
  4493. " f() {\n"
  4494. " return 1;\n"
  4495. " }\n"
  4496. " int\n"
  4497. " g();\n"
  4498. "};\n"
  4499. "int\n"
  4500. "f() {\n"
  4501. " return 1;\n"
  4502. "}\n"
  4503. "int\n"
  4504. "g();\n",
  4505. Style);
  4506. // Top-level definitions, and no kinds of declarations should have the
  4507. // return type moved to its own line.
  4508. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  4509. verifyFormat("class B {\n"
  4510. " int f() { return 1; }\n"
  4511. " int g();\n"
  4512. "};\n"
  4513. "int\n"
  4514. "f() {\n"
  4515. " return 1;\n"
  4516. "}\n"
  4517. "int g();\n",
  4518. Style);
  4519. // Top-level definitions and declarations should have the return type moved
  4520. // to its own line.
  4521. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  4522. verifyFormat("class C {\n"
  4523. " int f() { return 1; }\n"
  4524. " int g();\n"
  4525. "};\n"
  4526. "int\n"
  4527. "f() {\n"
  4528. " return 1;\n"
  4529. "}\n"
  4530. "int\n"
  4531. "g();\n",
  4532. Style);
  4533. // All definitions should have the return type moved to its own line, but no
  4534. // kinds of declarations.
  4535. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4536. verifyFormat("class D {\n"
  4537. " int\n"
  4538. " f() {\n"
  4539. " return 1;\n"
  4540. " }\n"
  4541. " int g();\n"
  4542. "};\n"
  4543. "int\n"
  4544. "f() {\n"
  4545. " return 1;\n"
  4546. "}\n"
  4547. "int g();\n",
  4548. Style);
  4549. verifyFormat("const char *\n"
  4550. "f(void) {\n" // Break here.
  4551. " return \"\";\n"
  4552. "}\n"
  4553. "const char *bar(void);\n", // No break here.
  4554. Style);
  4555. verifyFormat("template <class T>\n"
  4556. "T *\n"
  4557. "f(T &c) {\n" // Break here.
  4558. " return NULL;\n"
  4559. "}\n"
  4560. "template <class T> T *f(T &c);\n", // No break here.
  4561. Style);
  4562. verifyFormat("class C {\n"
  4563. " int\n"
  4564. " operator+() {\n"
  4565. " return 1;\n"
  4566. " }\n"
  4567. " int\n"
  4568. " operator()() {\n"
  4569. " return 1;\n"
  4570. " }\n"
  4571. "};\n",
  4572. Style);
  4573. verifyFormat("void\n"
  4574. "A::operator()() {}\n"
  4575. "void\n"
  4576. "A::operator>>() {}\n"
  4577. "void\n"
  4578. "A::operator+() {}\n",
  4579. Style);
  4580. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4581. Style);
  4582. verifyFormat("void *\n"
  4583. "operator new(std::size_t s) {}",
  4584. Style);
  4585. verifyFormat("void *\n"
  4586. "operator delete[](void *ptr) {}",
  4587. Style);
  4588. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4589. verifyFormat("const char *\n"
  4590. "f(void)\n" // Break here.
  4591. "{\n"
  4592. " return \"\";\n"
  4593. "}\n"
  4594. "const char *bar(void);\n", // No break here.
  4595. Style);
  4596. verifyFormat("template <class T>\n"
  4597. "T *\n" // Problem here: no line break
  4598. "f(T &c)\n" // Break here.
  4599. "{\n"
  4600. " return NULL;\n"
  4601. "}\n"
  4602. "template <class T> T *f(T &c);\n", // No break here.
  4603. Style);
  4604. }
  4605. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4606. FormatStyle NoBreak = getLLVMStyle();
  4607. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4608. FormatStyle Break = getLLVMStyle();
  4609. Break.AlwaysBreakBeforeMultilineStrings = true;
  4610. verifyFormat("aaaa = \"bbbb\"\n"
  4611. " \"cccc\";",
  4612. NoBreak);
  4613. verifyFormat("aaaa =\n"
  4614. " \"bbbb\"\n"
  4615. " \"cccc\";",
  4616. Break);
  4617. verifyFormat("aaaa(\"bbbb\"\n"
  4618. " \"cccc\");",
  4619. NoBreak);
  4620. verifyFormat("aaaa(\n"
  4621. " \"bbbb\"\n"
  4622. " \"cccc\");",
  4623. Break);
  4624. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4625. " \"cccc\");",
  4626. NoBreak);
  4627. verifyFormat("aaaa(qqq,\n"
  4628. " \"bbbb\"\n"
  4629. " \"cccc\");",
  4630. Break);
  4631. verifyFormat("aaaa(qqq,\n"
  4632. " L\"bbbb\"\n"
  4633. " L\"cccc\");",
  4634. Break);
  4635. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4636. " \"bbbb\"));",
  4637. Break);
  4638. verifyFormat("string s = someFunction(\n"
  4639. " \"abc\"\n"
  4640. " \"abc\");",
  4641. Break);
  4642. // As we break before unary operators, breaking right after them is bad.
  4643. verifyFormat("string foo = abc ? \"x\"\n"
  4644. " \"blah blah blah blah blah blah\"\n"
  4645. " : \"y\";",
  4646. Break);
  4647. // Don't break if there is no column gain.
  4648. verifyFormat("f(\"aaaa\"\n"
  4649. " \"bbbb\");",
  4650. Break);
  4651. // Treat literals with escaped newlines like multi-line string literals.
  4652. EXPECT_EQ("x = \"a\\\n"
  4653. "b\\\n"
  4654. "c\";",
  4655. format("x = \"a\\\n"
  4656. "b\\\n"
  4657. "c\";",
  4658. NoBreak));
  4659. EXPECT_EQ("xxxx =\n"
  4660. " \"a\\\n"
  4661. "b\\\n"
  4662. "c\";",
  4663. format("xxxx = \"a\\\n"
  4664. "b\\\n"
  4665. "c\";",
  4666. Break));
  4667. // Exempt ObjC strings for now.
  4668. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4669. " @\"bbbb\";",
  4670. format("NSString *const kString = @\"aaaa\"\n"
  4671. "@\"bbbb\";",
  4672. Break));
  4673. Break.ColumnLimit = 0;
  4674. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4675. }
  4676. TEST_F(FormatTest, AlignsPipes) {
  4677. verifyFormat(
  4678. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4679. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4680. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4681. verifyFormat(
  4682. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4683. " << aaaaaaaaaaaaaaaaaaaa;");
  4684. verifyFormat(
  4685. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4686. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4687. verifyFormat(
  4688. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4689. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4690. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4691. verifyFormat(
  4692. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4693. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4694. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4695. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4696. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4697. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4698. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4699. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4700. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4701. verifyFormat(
  4702. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4703. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4704. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4705. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4706. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4707. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4708. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4709. " << \"}\";");
  4710. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4711. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4712. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4713. verifyFormat(
  4714. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4715. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4716. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4717. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4718. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4719. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4720. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4721. verifyFormat(
  4722. "void f() {\n"
  4723. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4724. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4725. "}");
  4726. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4727. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4728. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4729. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4730. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4731. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4732. verifyFormat("LOG_IF(aaa == //\n"
  4733. " bbb)\n"
  4734. " << a << b;");
  4735. // Breaking before the first "<<" is generally not desirable.
  4736. verifyFormat(
  4737. "llvm::errs()\n"
  4738. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4739. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4740. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4741. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4742. getLLVMStyleWithColumns(70));
  4743. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4744. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4745. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4746. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4747. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4748. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4749. getLLVMStyleWithColumns(70));
  4750. // But sometimes, breaking before the first "<<" is desirable.
  4751. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4752. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4753. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4754. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4755. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4756. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4757. " << BEF << IsTemplate << Description << E->getType();");
  4758. verifyFormat(
  4759. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4760. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4761. // Incomplete string literal.
  4762. EXPECT_EQ("llvm::errs() << \"\n"
  4763. " << a;",
  4764. format("llvm::errs() << \"\n<<a;"));
  4765. verifyFormat("void f() {\n"
  4766. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4767. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4768. "}");
  4769. // Handle 'endl'.
  4770. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4771. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4772. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4773. // Handle '\n'.
  4774. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
  4775. " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4776. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
  4777. " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
  4778. verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
  4779. " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
  4780. verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4781. }
  4782. TEST_F(FormatTest, UnderstandsEquals) {
  4783. verifyFormat(
  4784. "aaaaaaaaaaaaaaaaa =\n"
  4785. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4786. verifyFormat(
  4787. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4788. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4789. verifyFormat(
  4790. "if (a) {\n"
  4791. " f();\n"
  4792. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4793. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4794. "}");
  4795. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4796. " 100000000 + 10000000) {\n}");
  4797. }
  4798. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4799. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4800. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4801. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4802. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4803. verifyFormat(
  4804. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4805. " Parameter2);");
  4806. verifyFormat(
  4807. "ShortObject->shortFunction(\n"
  4808. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4809. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4810. verifyFormat("loooooooooooooongFunction(\n"
  4811. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4812. verifyFormat(
  4813. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4814. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4815. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4816. " .WillRepeatedly(Return(SomeValue));");
  4817. verifyFormat("void f() {\n"
  4818. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4819. " .Times(2)\n"
  4820. " .WillRepeatedly(Return(SomeValue));\n"
  4821. "}");
  4822. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4823. " ccccccccccccccccccccccc);");
  4824. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4825. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4826. " .aaaaa(aaaaa),\n"
  4827. " aaaaaaaaaaaaaaaaaaaaa);");
  4828. verifyFormat("void f() {\n"
  4829. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4830. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4831. "}");
  4832. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4833. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4834. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4835. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4836. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4837. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4838. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4839. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4840. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4841. "}");
  4842. // Here, it is not necessary to wrap at "." or "->".
  4843. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4844. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4845. verifyFormat(
  4846. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4847. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4848. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4849. verifyFormat(
  4850. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4852. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4853. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4854. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4855. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4856. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4857. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4858. " .a();");
  4859. FormatStyle NoBinPacking = getLLVMStyle();
  4860. NoBinPacking.BinPackParameters = false;
  4861. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4862. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4863. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4864. " aaaaaaaaaaaaaaaaaaa,\n"
  4865. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4866. NoBinPacking);
  4867. // If there is a subsequent call, change to hanging indentation.
  4868. verifyFormat(
  4869. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4870. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4871. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4872. verifyFormat(
  4873. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4874. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4875. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4876. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4877. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4878. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4879. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4880. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4881. }
  4882. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4883. verifyFormat("template <typename T>\n"
  4884. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4885. verifyFormat("template <typename T>\n"
  4886. "// T should be one of {A, B}.\n"
  4887. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4888. verifyFormat(
  4889. "template <typename T>\n"
  4890. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4891. verifyFormat("template <typename T>\n"
  4892. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4893. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4894. verifyFormat(
  4895. "template <typename T>\n"
  4896. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4897. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4898. verifyFormat(
  4899. "template <typename T>\n"
  4900. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4901. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4902. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4903. verifyFormat("template <typename T>\n"
  4904. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4905. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4906. verifyFormat(
  4907. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4908. " typename T4 = char>\n"
  4909. "void f();");
  4910. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4911. " template <typename> class cccccccccccccccccccccc,\n"
  4912. " typename ddddddddddddd>\n"
  4913. "class C {};");
  4914. verifyFormat(
  4915. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4917. verifyFormat("void f() {\n"
  4918. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4919. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4920. "}");
  4921. verifyFormat("template <typename T> class C {};");
  4922. verifyFormat("template <typename T> void f();");
  4923. verifyFormat("template <typename T> void f() {}");
  4924. verifyFormat(
  4925. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4926. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4927. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4928. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4930. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4931. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4932. getLLVMStyleWithColumns(72));
  4933. EXPECT_EQ("static_cast<A< //\n"
  4934. " B> *>(\n"
  4935. "\n"
  4936. " );",
  4937. format("static_cast<A<//\n"
  4938. " B>*>(\n"
  4939. "\n"
  4940. " );"));
  4941. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4942. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4943. FormatStyle AlwaysBreak = getLLVMStyle();
  4944. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4945. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4946. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4947. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4948. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4949. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4950. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4951. verifyFormat("template <template <typename> class Fooooooo,\n"
  4952. " template <typename> class Baaaaaaar>\n"
  4953. "struct C {};",
  4954. AlwaysBreak);
  4955. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4956. "struct C {};",
  4957. AlwaysBreak);
  4958. }
  4959. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4960. verifyFormat(
  4961. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4963. verifyFormat(
  4964. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4965. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4966. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4967. // FIXME: Should we have the extra indent after the second break?
  4968. verifyFormat(
  4969. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4970. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4971. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4972. verifyFormat(
  4973. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4974. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4975. // Breaking at nested name specifiers is generally not desirable.
  4976. verifyFormat(
  4977. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4978. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4979. verifyFormat(
  4980. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4981. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4982. " aaaaaaaaaaaaaaaaaaaaa);",
  4983. getLLVMStyleWithColumns(74));
  4984. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4985. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4986. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4987. }
  4988. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4989. verifyFormat("A<int> a;");
  4990. verifyFormat("A<A<A<int>>> a;");
  4991. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4992. verifyFormat("bool x = a < 1 || 2 > a;");
  4993. verifyFormat("bool x = 5 < f<int>();");
  4994. verifyFormat("bool x = f<int>() > 5;");
  4995. verifyFormat("bool x = 5 < a<int>::x;");
  4996. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4997. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4998. verifyGoogleFormat("A<A<int>> a;");
  4999. verifyGoogleFormat("A<A<A<int>>> a;");
  5000. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  5001. verifyGoogleFormat("A<A<int> > a;");
  5002. verifyGoogleFormat("A<A<A<int> > > a;");
  5003. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  5004. verifyGoogleFormat("A<::A<int>> a;");
  5005. verifyGoogleFormat("A<::A> a;");
  5006. verifyGoogleFormat("A< ::A> a;");
  5007. verifyGoogleFormat("A< ::A<int> > a;");
  5008. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  5009. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  5010. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  5011. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  5012. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  5013. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  5014. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  5015. verifyFormat("test >> a >> b;");
  5016. verifyFormat("test << a >> b;");
  5017. verifyFormat("f<int>();");
  5018. verifyFormat("template <typename T> void f() {}");
  5019. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  5020. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  5021. "sizeof(char)>::type>;");
  5022. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  5023. // Not template parameters.
  5024. verifyFormat("return a < b && c > d;");
  5025. verifyFormat("void f() {\n"
  5026. " while (a < b && c > d) {\n"
  5027. " }\n"
  5028. "}");
  5029. verifyFormat("template <typename... Types>\n"
  5030. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  5031. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  5033. getLLVMStyleWithColumns(60));
  5034. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  5035. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  5036. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  5037. }
  5038. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  5039. verifyFormat("COMPARE(a, ==, b);");
  5040. }
  5041. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  5042. verifyFormat("int A::*x;");
  5043. verifyFormat("int (S::*func)(void *);");
  5044. verifyFormat("void f() { int (S::*func)(void *); }");
  5045. verifyFormat("typedef bool *(Class::*Member)() const;");
  5046. verifyFormat("void f() {\n"
  5047. " (a->*f)();\n"
  5048. " a->*x;\n"
  5049. " (a.*f)();\n"
  5050. " ((*a).*f)();\n"
  5051. " a.*x;\n"
  5052. "}");
  5053. verifyFormat("void f() {\n"
  5054. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5055. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  5056. "}");
  5057. verifyFormat(
  5058. "(aaaaaaaaaa->*bbbbbbb)(\n"
  5059. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  5060. FormatStyle Style = getLLVMStyle();
  5061. Style.PointerAlignment = FormatStyle::PAS_Left;
  5062. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  5063. }
  5064. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  5065. verifyFormat("int a = -2;");
  5066. verifyFormat("f(-1, -2, -3);");
  5067. verifyFormat("a[-1] = 5;");
  5068. verifyFormat("int a = 5 + -2;");
  5069. verifyFormat("if (i == -1) {\n}");
  5070. verifyFormat("if (i != -1) {\n}");
  5071. verifyFormat("if (i > -1) {\n}");
  5072. verifyFormat("if (i < -1) {\n}");
  5073. verifyFormat("++(a->f());");
  5074. verifyFormat("--(a->f());");
  5075. verifyFormat("(a->f())++;");
  5076. verifyFormat("a[42]++;");
  5077. verifyFormat("if (!(a->f())) {\n}");
  5078. verifyFormat("a-- > b;");
  5079. verifyFormat("b ? -a : c;");
  5080. verifyFormat("n * sizeof char16;");
  5081. verifyFormat("n * alignof char16;", getGoogleStyle());
  5082. verifyFormat("sizeof(char);");
  5083. verifyFormat("alignof(char);", getGoogleStyle());
  5084. verifyFormat("return -1;");
  5085. verifyFormat("switch (a) {\n"
  5086. "case -1:\n"
  5087. " break;\n"
  5088. "}");
  5089. verifyFormat("#define X -1");
  5090. verifyFormat("#define X -kConstant");
  5091. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  5092. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  5093. verifyFormat("int a = /* confusing comment */ -1;");
  5094. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  5095. verifyFormat("int a = i /* confusing comment */++;");
  5096. }
  5097. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  5098. verifyFormat("if (!aaaaaaaaaa( // break\n"
  5099. " aaaaa)) {\n"
  5100. "}");
  5101. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  5102. " aaaaa));");
  5103. verifyFormat("*aaa = aaaaaaa( // break\n"
  5104. " bbbbbb);");
  5105. }
  5106. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  5107. verifyFormat("bool operator<();");
  5108. verifyFormat("bool operator>();");
  5109. verifyFormat("bool operator=();");
  5110. verifyFormat("bool operator==();");
  5111. verifyFormat("bool operator!=();");
  5112. verifyFormat("int operator+();");
  5113. verifyFormat("int operator++();");
  5114. verifyFormat("bool operator,();");
  5115. verifyFormat("bool operator();");
  5116. verifyFormat("bool operator()();");
  5117. verifyFormat("bool operator[]();");
  5118. verifyFormat("operator bool();");
  5119. verifyFormat("operator int();");
  5120. verifyFormat("operator void *();");
  5121. verifyFormat("operator SomeType<int>();");
  5122. verifyFormat("operator SomeType<int, int>();");
  5123. verifyFormat("operator SomeType<SomeType<int>>();");
  5124. verifyFormat("void *operator new(std::size_t size);");
  5125. verifyFormat("void *operator new[](std::size_t size);");
  5126. verifyFormat("void operator delete(void *ptr);");
  5127. verifyFormat("void operator delete[](void *ptr);");
  5128. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  5129. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  5130. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
  5131. " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
  5132. verifyFormat(
  5133. "ostream &operator<<(ostream &OutputStream,\n"
  5134. " SomeReallyLongType WithSomeReallyLongValue);");
  5135. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  5136. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  5137. " return left.group < right.group;\n"
  5138. "}");
  5139. verifyFormat("SomeType &operator=(const SomeType &S);");
  5140. verifyFormat("f.template operator()<int>();");
  5141. verifyGoogleFormat("operator void*();");
  5142. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  5143. verifyGoogleFormat("operator ::A();");
  5144. verifyFormat("using A::operator+;");
  5145. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  5146. "int i;");
  5147. }
  5148. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  5149. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  5150. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  5151. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  5152. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  5153. verifyFormat("Deleted &operator=(const Deleted &) &;");
  5154. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  5155. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  5156. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  5157. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  5158. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  5159. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  5160. FormatStyle AlignLeft = getLLVMStyle();
  5161. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  5162. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  5163. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  5164. AlignLeft);
  5165. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  5166. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  5167. FormatStyle Spaces = getLLVMStyle();
  5168. Spaces.SpacesInCStyleCastParentheses = true;
  5169. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5170. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5171. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5172. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5173. Spaces.SpacesInCStyleCastParentheses = false;
  5174. Spaces.SpacesInParentheses = true;
  5175. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5176. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5177. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5178. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5179. }
  5180. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5181. verifyFormat("void f() {\n"
  5182. " A *a = new A;\n"
  5183. " A *a = new (placement) A;\n"
  5184. " delete a;\n"
  5185. " delete (A *)a;\n"
  5186. "}");
  5187. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5188. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5189. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5190. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5191. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5192. verifyFormat("delete[] h->p;");
  5193. }
  5194. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5195. verifyFormat("int *f(int *a) {}");
  5196. verifyFormat("int main(int argc, char **argv) {}");
  5197. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5198. verifyIndependentOfContext("f(a, *a);");
  5199. verifyFormat("void g() { f(*a); }");
  5200. verifyIndependentOfContext("int a = b * 10;");
  5201. verifyIndependentOfContext("int a = 10 * b;");
  5202. verifyIndependentOfContext("int a = b * c;");
  5203. verifyIndependentOfContext("int a += b * c;");
  5204. verifyIndependentOfContext("int a -= b * c;");
  5205. verifyIndependentOfContext("int a *= b * c;");
  5206. verifyIndependentOfContext("int a /= b * c;");
  5207. verifyIndependentOfContext("int a = *b;");
  5208. verifyIndependentOfContext("int a = *b * c;");
  5209. verifyIndependentOfContext("int a = b * *c;");
  5210. verifyIndependentOfContext("int a = b * (10);");
  5211. verifyIndependentOfContext("S << b * (10);");
  5212. verifyIndependentOfContext("return 10 * b;");
  5213. verifyIndependentOfContext("return *b * *c;");
  5214. verifyIndependentOfContext("return a & ~b;");
  5215. verifyIndependentOfContext("f(b ? *c : *d);");
  5216. verifyIndependentOfContext("int a = b ? *c : *d;");
  5217. verifyIndependentOfContext("*b = a;");
  5218. verifyIndependentOfContext("a * ~b;");
  5219. verifyIndependentOfContext("a * !b;");
  5220. verifyIndependentOfContext("a * +b;");
  5221. verifyIndependentOfContext("a * -b;");
  5222. verifyIndependentOfContext("a * ++b;");
  5223. verifyIndependentOfContext("a * --b;");
  5224. verifyIndependentOfContext("a[4] * b;");
  5225. verifyIndependentOfContext("a[a * a] = 1;");
  5226. verifyIndependentOfContext("f() * b;");
  5227. verifyIndependentOfContext("a * [self dostuff];");
  5228. verifyIndependentOfContext("int x = a * (a + b);");
  5229. verifyIndependentOfContext("(a *)(a + b);");
  5230. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5231. verifyIndependentOfContext("int *pa = (int *)&a;");
  5232. verifyIndependentOfContext("return sizeof(int **);");
  5233. verifyIndependentOfContext("return sizeof(int ******);");
  5234. verifyIndependentOfContext("return (int **&)a;");
  5235. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5236. verifyFormat("void f(Type (*parameter)[10]) {}");
  5237. verifyFormat("void f(Type (&parameter)[10]) {}");
  5238. verifyGoogleFormat("return sizeof(int**);");
  5239. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5240. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5241. verifyFormat("auto a = [](int **&, int ***) {};");
  5242. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5243. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5244. verifyFormat("[](const decltype(*a) &value) {}");
  5245. verifyFormat("decltype(a * b) F();");
  5246. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5247. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5248. verifyIndependentOfContext("int i{a * b};");
  5249. verifyIndependentOfContext("aaa && aaa->f();");
  5250. verifyIndependentOfContext("int x = ~*p;");
  5251. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5252. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5253. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5254. verifyFormat("void f() { f(a, c * d); }");
  5255. verifyFormat("void f() { f(new a(), c * d); }");
  5256. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5257. verifyIndependentOfContext("A<int *> a;");
  5258. verifyIndependentOfContext("A<int **> a;");
  5259. verifyIndependentOfContext("A<int *, int *> a;");
  5260. verifyIndependentOfContext("A<int *[]> a;");
  5261. verifyIndependentOfContext(
  5262. "const char *const p = reinterpret_cast<const char *const>(q);");
  5263. verifyIndependentOfContext("A<int **, int **> a;");
  5264. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5265. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5266. verifyFormat("for (; a && b;) {\n}");
  5267. verifyFormat("bool foo = true && [] { return false; }();");
  5268. verifyFormat(
  5269. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5270. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5271. verifyGoogleFormat("**outparam = 1;");
  5272. verifyGoogleFormat("*outparam = a * b;");
  5273. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5274. verifyGoogleFormat("A<int*> a;");
  5275. verifyGoogleFormat("A<int**> a;");
  5276. verifyGoogleFormat("A<int*, int*> a;");
  5277. verifyGoogleFormat("A<int**, int**> a;");
  5278. verifyGoogleFormat("f(b ? *c : *d);");
  5279. verifyGoogleFormat("int a = b ? *c : *d;");
  5280. verifyGoogleFormat("Type* t = **x;");
  5281. verifyGoogleFormat("Type* t = *++*x;");
  5282. verifyGoogleFormat("*++*x;");
  5283. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5284. verifyGoogleFormat("Type* t = x++ * y;");
  5285. verifyGoogleFormat(
  5286. "const char* const p = reinterpret_cast<const char* const>(q);");
  5287. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5288. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5289. verifyGoogleFormat("template <typename T>\n"
  5290. "void f(int i = 0, SomeType** temps = NULL);");
  5291. FormatStyle Left = getLLVMStyle();
  5292. Left.PointerAlignment = FormatStyle::PAS_Left;
  5293. verifyFormat("x = *a(x) = *a(y);", Left);
  5294. verifyFormat("for (;; * = b) {\n}", Left);
  5295. verifyFormat("return *this += 1;", Left);
  5296. verifyIndependentOfContext("a = *(x + y);");
  5297. verifyIndependentOfContext("a = &(x + y);");
  5298. verifyIndependentOfContext("*(x + y).call();");
  5299. verifyIndependentOfContext("&(x + y)->call();");
  5300. verifyFormat("void f() { &(*I).first; }");
  5301. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5302. verifyFormat(
  5303. "int *MyValues = {\n"
  5304. " *A, // Operator detection might be confused by the '{'\n"
  5305. " *BB // Operator detection might be confused by previous comment\n"
  5306. "};");
  5307. verifyIndependentOfContext("if (int *a = &b)");
  5308. verifyIndependentOfContext("if (int &a = *b)");
  5309. verifyIndependentOfContext("if (a & b[i])");
  5310. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5311. verifyIndependentOfContext("if (*b[i])");
  5312. verifyIndependentOfContext("if (int *a = (&b))");
  5313. verifyIndependentOfContext("while (int *a = &b)");
  5314. verifyIndependentOfContext("size = sizeof *a;");
  5315. verifyIndependentOfContext("if (a && (b = c))");
  5316. verifyFormat("void f() {\n"
  5317. " for (const int &v : Values) {\n"
  5318. " }\n"
  5319. "}");
  5320. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5321. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5322. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5323. verifyFormat("#define A (!a * b)");
  5324. verifyFormat("#define MACRO \\\n"
  5325. " int *i = a * b; \\\n"
  5326. " void f(a *b);",
  5327. getLLVMStyleWithColumns(19));
  5328. verifyIndependentOfContext("A = new SomeType *[Length];");
  5329. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5330. verifyIndependentOfContext("T **t = new T *;");
  5331. verifyIndependentOfContext("T **t = new T *();");
  5332. verifyGoogleFormat("A = new SomeType*[Length]();");
  5333. verifyGoogleFormat("A = new SomeType*[Length];");
  5334. verifyGoogleFormat("T** t = new T*;");
  5335. verifyGoogleFormat("T** t = new T*();");
  5336. FormatStyle PointerLeft = getLLVMStyle();
  5337. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5338. verifyFormat("delete *x;", PointerLeft);
  5339. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5340. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5341. verifyFormat("template <bool a, bool b> "
  5342. "typename t::if<x && y>::type f() {}");
  5343. verifyFormat("template <int *y> f() {}");
  5344. verifyFormat("vector<int *> v;");
  5345. verifyFormat("vector<int *const> v;");
  5346. verifyFormat("vector<int *const **const *> v;");
  5347. verifyFormat("vector<int *volatile> v;");
  5348. verifyFormat("vector<a * b> v;");
  5349. verifyFormat("foo<b && false>();");
  5350. verifyFormat("foo<b & 1>();");
  5351. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5352. verifyFormat(
  5353. "template <class T, class = typename std::enable_if<\n"
  5354. " std::is_integral<T>::value &&\n"
  5355. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5356. "void F();",
  5357. getLLVMStyleWithColumns(76));
  5358. verifyFormat(
  5359. "template <class T,\n"
  5360. " class = typename ::std::enable_if<\n"
  5361. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5362. "void F();",
  5363. getGoogleStyleWithColumns(68));
  5364. verifyIndependentOfContext("MACRO(int *i);");
  5365. verifyIndependentOfContext("MACRO(auto *a);");
  5366. verifyIndependentOfContext("MACRO(const A *a);");
  5367. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5368. // FIXME: Is there a way to make this work?
  5369. // verifyIndependentOfContext("MACRO(A *a);");
  5370. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5371. verifyFormat("return options != nullptr && operator==(*options);");
  5372. EXPECT_EQ("#define OP(x) \\\n"
  5373. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5374. " return s << a.DebugString(); \\\n"
  5375. " }",
  5376. format("#define OP(x) \\\n"
  5377. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5378. " return s << a.DebugString(); \\\n"
  5379. " }",
  5380. getLLVMStyleWithColumns(50)));
  5381. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5382. // foo<x> d > v; doesn't make sense.
  5383. verifyFormat("foo<a<b && c> d> v;");
  5384. FormatStyle PointerMiddle = getLLVMStyle();
  5385. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5386. verifyFormat("delete *x;", PointerMiddle);
  5387. verifyFormat("int * x;", PointerMiddle);
  5388. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5389. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5390. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5391. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5392. verifyFormat("A<int *> a;", PointerMiddle);
  5393. verifyFormat("A<int **> a;", PointerMiddle);
  5394. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5395. verifyFormat("A<int * []> a;", PointerMiddle);
  5396. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5397. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5398. verifyFormat("T ** t = new T *;", PointerMiddle);
  5399. // Member function reference qualifiers aren't binary operators.
  5400. verifyFormat("string // break\n"
  5401. "operator()() & {}");
  5402. verifyFormat("string // break\n"
  5403. "operator()() && {}");
  5404. verifyGoogleFormat("template <typename T>\n"
  5405. "auto x() & -> int {}");
  5406. }
  5407. TEST_F(FormatTest, UnderstandsAttributes) {
  5408. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5409. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5410. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5411. FormatStyle AfterType = getLLVMStyle();
  5412. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  5413. verifyFormat("__attribute__((nodebug)) void\n"
  5414. "foo() {}\n",
  5415. AfterType);
  5416. }
  5417. TEST_F(FormatTest, UnderstandsEllipsis) {
  5418. verifyFormat("int printf(const char *fmt, ...);");
  5419. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5420. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5421. FormatStyle PointersLeft = getLLVMStyle();
  5422. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5423. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5424. }
  5425. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5426. EXPECT_EQ("int *a;\n"
  5427. "int *a;\n"
  5428. "int *a;",
  5429. format("int *a;\n"
  5430. "int* a;\n"
  5431. "int *a;",
  5432. getGoogleStyle()));
  5433. EXPECT_EQ("int* a;\n"
  5434. "int* a;\n"
  5435. "int* a;",
  5436. format("int* a;\n"
  5437. "int* a;\n"
  5438. "int *a;",
  5439. getGoogleStyle()));
  5440. EXPECT_EQ("int *a;\n"
  5441. "int *a;\n"
  5442. "int *a;",
  5443. format("int *a;\n"
  5444. "int * a;\n"
  5445. "int * a;",
  5446. getGoogleStyle()));
  5447. EXPECT_EQ("auto x = [] {\n"
  5448. " int *a;\n"
  5449. " int *a;\n"
  5450. " int *a;\n"
  5451. "};",
  5452. format("auto x=[]{int *a;\n"
  5453. "int * a;\n"
  5454. "int * a;};",
  5455. getGoogleStyle()));
  5456. }
  5457. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5458. verifyFormat("int f(int &&a) {}");
  5459. verifyFormat("int f(int a, char &&b) {}");
  5460. verifyFormat("void f() { int &&a = b; }");
  5461. verifyGoogleFormat("int f(int a, char&& b) {}");
  5462. verifyGoogleFormat("void f() { int&& a = b; }");
  5463. verifyIndependentOfContext("A<int &&> a;");
  5464. verifyIndependentOfContext("A<int &&, int &&> a;");
  5465. verifyGoogleFormat("A<int&&> a;");
  5466. verifyGoogleFormat("A<int&&, int&&> a;");
  5467. // Not rvalue references:
  5468. verifyFormat("template <bool B, bool C> class A {\n"
  5469. " static_assert(B && C, \"Something is wrong\");\n"
  5470. "};");
  5471. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5472. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5473. verifyFormat("#define A(a, b) (a && b)");
  5474. }
  5475. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5476. verifyFormat("void f() {\n"
  5477. " x[aaaaaaaaa -\n"
  5478. " b] = 23;\n"
  5479. "}",
  5480. getLLVMStyleWithColumns(15));
  5481. }
  5482. TEST_F(FormatTest, FormatsCasts) {
  5483. verifyFormat("Type *A = static_cast<Type *>(P);");
  5484. verifyFormat("Type *A = (Type *)P;");
  5485. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5486. verifyFormat("int a = (int)(2.0f);");
  5487. verifyFormat("int a = (int)2.0f;");
  5488. verifyFormat("x[(int32)y];");
  5489. verifyFormat("x = (int32)y;");
  5490. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5491. verifyFormat("int a = (int)*b;");
  5492. verifyFormat("int a = (int)2.0f;");
  5493. verifyFormat("int a = (int)~0;");
  5494. verifyFormat("int a = (int)++a;");
  5495. verifyFormat("int a = (int)sizeof(int);");
  5496. verifyFormat("int a = (int)+2;");
  5497. verifyFormat("my_int a = (my_int)2.0f;");
  5498. verifyFormat("my_int a = (my_int)sizeof(int);");
  5499. verifyFormat("return (my_int)aaa;");
  5500. verifyFormat("#define x ((int)-1)");
  5501. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5502. verifyFormat("#define p(q) ((int *)&q)");
  5503. verifyFormat("fn(a)(b) + 1;");
  5504. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5505. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5506. verifyFormat("my_int a = (my_int)~0;");
  5507. verifyFormat("my_int a = (my_int)++a;");
  5508. verifyFormat("my_int a = (my_int)-2;");
  5509. verifyFormat("my_int a = (my_int)1;");
  5510. verifyFormat("my_int a = (my_int *)1;");
  5511. verifyFormat("my_int a = (const my_int)-1;");
  5512. verifyFormat("my_int a = (const my_int *)-1;");
  5513. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5514. verifyFormat("my_int a = (ns::my_int)-2;");
  5515. verifyFormat("case (my_int)ONE:");
  5516. // FIXME: single value wrapped with paren will be treated as cast.
  5517. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5518. verifyFormat("{ (void)F; }");
  5519. // Don't break after a cast's
  5520. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5521. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5522. " bbbbbbbbbbbbbbbbbbbbbb);");
  5523. // These are not casts.
  5524. verifyFormat("void f(int *) {}");
  5525. verifyFormat("f(foo)->b;");
  5526. verifyFormat("f(foo).b;");
  5527. verifyFormat("f(foo)(b);");
  5528. verifyFormat("f(foo)[b];");
  5529. verifyFormat("[](foo) { return 4; }(bar);");
  5530. verifyFormat("(*funptr)(foo)[4];");
  5531. verifyFormat("funptrs[4](foo)[4];");
  5532. verifyFormat("void f(int *);");
  5533. verifyFormat("void f(int *) = 0;");
  5534. verifyFormat("void f(SmallVector<int>) {}");
  5535. verifyFormat("void f(SmallVector<int>);");
  5536. verifyFormat("void f(SmallVector<int>) = 0;");
  5537. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5538. verifyFormat("int a = sizeof(int) * b;");
  5539. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5540. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5541. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5542. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5543. // These are not casts, but at some point were confused with casts.
  5544. verifyFormat("virtual void foo(int *) override;");
  5545. verifyFormat("virtual void foo(char &) const;");
  5546. verifyFormat("virtual void foo(int *a, char *) const;");
  5547. verifyFormat("int a = sizeof(int *) + b;");
  5548. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5549. verifyFormat("bool b = f(g<int>) && c;");
  5550. verifyFormat("typedef void (*f)(int i) func;");
  5551. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5552. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5553. // FIXME: The indentation here is not ideal.
  5554. verifyFormat(
  5555. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5556. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5557. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5558. }
  5559. TEST_F(FormatTest, FormatsFunctionTypes) {
  5560. verifyFormat("A<bool()> a;");
  5561. verifyFormat("A<SomeType()> a;");
  5562. verifyFormat("A<void (*)(int, std::string)> a;");
  5563. verifyFormat("A<void *(int)>;");
  5564. verifyFormat("void *(*a)(int *, SomeType *);");
  5565. verifyFormat("int (*func)(void *);");
  5566. verifyFormat("void f() { int (*func)(void *); }");
  5567. verifyFormat("template <class CallbackClass>\n"
  5568. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5569. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5570. verifyGoogleFormat("void* (*a)(int);");
  5571. verifyGoogleFormat(
  5572. "template <class CallbackClass>\n"
  5573. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5574. // Other constructs can look somewhat like function types:
  5575. verifyFormat("A<sizeof(*x)> a;");
  5576. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5577. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5578. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5579. verifyFormat("int x = f(&h)();");
  5580. }
  5581. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5582. verifyFormat("A (*foo_)[6];");
  5583. verifyFormat("vector<int> (*foo_)[6];");
  5584. }
  5585. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5586. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5587. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5588. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5589. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5590. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5591. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5592. // Different ways of ()-initializiation.
  5593. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5594. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5595. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5596. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5597. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5598. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5599. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5600. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5601. }
  5602. TEST_F(FormatTest, BreaksLongDeclarations) {
  5603. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5604. " AnotherNameForTheLongType;");
  5605. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5606. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5607. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5608. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5609. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5610. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5611. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5612. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5613. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5614. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5615. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5616. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5617. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5618. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5619. FormatStyle Indented = getLLVMStyle();
  5620. Indented.IndentWrappedFunctionNames = true;
  5621. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5622. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5623. Indented);
  5624. verifyFormat(
  5625. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5626. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5627. Indented);
  5628. verifyFormat(
  5629. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5630. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5631. Indented);
  5632. verifyFormat(
  5633. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5634. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5635. Indented);
  5636. // FIXME: Without the comment, this breaks after "(".
  5637. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5638. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5639. getGoogleStyle());
  5640. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5641. " int LoooooooooooooooooooongParam2) {}");
  5642. verifyFormat(
  5643. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5644. " SourceLocation L, IdentifierIn *II,\n"
  5645. " Type *T) {}");
  5646. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5647. "ReallyReaaallyLongFunctionName(\n"
  5648. " const std::string &SomeParameter,\n"
  5649. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5650. " &ReallyReallyLongParameterName,\n"
  5651. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5652. " &AnotherLongParameterName) {}");
  5653. verifyFormat("template <typename A>\n"
  5654. "SomeLoooooooooooooooooooooongType<\n"
  5655. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5656. "Function() {}");
  5657. verifyGoogleFormat(
  5658. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5659. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5660. verifyGoogleFormat(
  5661. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5662. " SourceLocation L) {}");
  5663. verifyGoogleFormat(
  5664. "some_namespace::LongReturnType\n"
  5665. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5666. " int first_long_parameter, int second_parameter) {}");
  5667. verifyGoogleFormat("template <typename T>\n"
  5668. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5669. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5670. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5671. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5672. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5673. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5674. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5675. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5676. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5677. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5678. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5679. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5680. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5681. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5682. }
  5683. TEST_F(FormatTest, FormatsArrays) {
  5684. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5685. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5686. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  5687. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  5688. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  5689. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  5690. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5691. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5692. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5693. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5694. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5695. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5696. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5697. verifyFormat(
  5698. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5699. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5700. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5701. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5702. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5703. verifyFormat(
  5704. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5705. " .aaaaaaa[0]\n"
  5706. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5707. verifyFormat("a[::b::c];");
  5708. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5709. FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
  5710. verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
  5711. }
  5712. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5713. verifyFormat("(a)->b();");
  5714. verifyFormat("--a;");
  5715. }
  5716. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5717. verifyFormat("#include <string>\n"
  5718. "#include <a/b/c.h>\n"
  5719. "#include \"a/b/string\"\n"
  5720. "#include \"string.h\"\n"
  5721. "#include \"string.h\"\n"
  5722. "#include <a-a>\n"
  5723. "#include < path with space >\n"
  5724. "#include_next <test.h>"
  5725. "#include \"abc.h\" // this is included for ABC\n"
  5726. "#include \"some long include\" // with a comment\n"
  5727. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5728. getLLVMStyleWithColumns(35));
  5729. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5730. EXPECT_EQ("#include <a>", format("#include<a>"));
  5731. verifyFormat("#import <string>");
  5732. verifyFormat("#import <a/b/c.h>");
  5733. verifyFormat("#import \"a/b/string\"");
  5734. verifyFormat("#import \"string.h\"");
  5735. verifyFormat("#import \"string.h\"");
  5736. verifyFormat("#if __has_include(<strstream>)\n"
  5737. "#include <strstream>\n"
  5738. "#endif");
  5739. verifyFormat("#define MY_IMPORT <a/b>");
  5740. // Protocol buffer definition or missing "#".
  5741. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5742. getLLVMStyleWithColumns(30));
  5743. FormatStyle Style = getLLVMStyle();
  5744. Style.AlwaysBreakBeforeMultilineStrings = true;
  5745. Style.ColumnLimit = 0;
  5746. verifyFormat("#import \"abc.h\"", Style);
  5747. // But 'import' might also be a regular C++ namespace.
  5748. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5749. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5750. }
  5751. //===----------------------------------------------------------------------===//
  5752. // Error recovery tests.
  5753. //===----------------------------------------------------------------------===//
  5754. TEST_F(FormatTest, IncompleteParameterLists) {
  5755. FormatStyle NoBinPacking = getLLVMStyle();
  5756. NoBinPacking.BinPackParameters = false;
  5757. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5758. " double *min_x,\n"
  5759. " double *max_x,\n"
  5760. " double *min_y,\n"
  5761. " double *max_y,\n"
  5762. " double *min_z,\n"
  5763. " double *max_z, ) {}",
  5764. NoBinPacking);
  5765. }
  5766. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5767. verifyFormat("void f() { return; }\n42");
  5768. verifyFormat("void f() {\n"
  5769. " if (0)\n"
  5770. " return;\n"
  5771. "}\n"
  5772. "42");
  5773. verifyFormat("void f() { return }\n42");
  5774. verifyFormat("void f() {\n"
  5775. " if (0)\n"
  5776. " return\n"
  5777. "}\n"
  5778. "42");
  5779. }
  5780. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5781. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5782. EXPECT_EQ("void f() {\n"
  5783. " if (a)\n"
  5784. " return\n"
  5785. "}",
  5786. format("void f ( ) { if ( a ) return }"));
  5787. EXPECT_EQ("namespace N {\n"
  5788. "void f()\n"
  5789. "}",
  5790. format("namespace N { void f() }"));
  5791. EXPECT_EQ("namespace N {\n"
  5792. "void f() {}\n"
  5793. "void g()\n"
  5794. "}",
  5795. format("namespace N { void f( ) { } void g( ) }"));
  5796. }
  5797. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5798. verifyFormat("int aaaaaaaa =\n"
  5799. " // Overlylongcomment\n"
  5800. " b;",
  5801. getLLVMStyleWithColumns(20));
  5802. verifyFormat("function(\n"
  5803. " ShortArgument,\n"
  5804. " LoooooooooooongArgument);\n",
  5805. getLLVMStyleWithColumns(20));
  5806. }
  5807. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5808. verifyFormat("public:");
  5809. verifyFormat("class A {\n"
  5810. "public\n"
  5811. " void f() {}\n"
  5812. "};");
  5813. verifyFormat("public\n"
  5814. "int qwerty;");
  5815. verifyFormat("public\n"
  5816. "B {}");
  5817. verifyFormat("public\n"
  5818. "{}");
  5819. verifyFormat("public\n"
  5820. "B { int x; }");
  5821. }
  5822. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5823. verifyFormat("{");
  5824. verifyFormat("#})");
  5825. verifyNoCrash("(/**/[:!] ?[).");
  5826. }
  5827. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5828. verifyFormat("do {\n}");
  5829. verifyFormat("do {\n}\n"
  5830. "f();");
  5831. verifyFormat("do {\n}\n"
  5832. "wheeee(fun);");
  5833. verifyFormat("do {\n"
  5834. " f();\n"
  5835. "}");
  5836. }
  5837. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5838. verifyFormat("if {\n foo;\n foo();\n}");
  5839. verifyFormat("switch {\n foo;\n foo();\n}");
  5840. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5841. verifyFormat("while {\n foo;\n foo();\n}");
  5842. verifyFormat("do {\n foo;\n foo();\n} while;");
  5843. }
  5844. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5845. verifyIncompleteFormat("namespace {\n"
  5846. "class Foo { Foo (\n"
  5847. "};\n"
  5848. "} // comment");
  5849. }
  5850. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5851. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5852. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5853. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5854. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5855. EXPECT_EQ("{\n"
  5856. " {\n"
  5857. " breakme(\n"
  5858. " qwe);\n"
  5859. " }\n",
  5860. format("{\n"
  5861. " {\n"
  5862. " breakme(qwe);\n"
  5863. "}\n",
  5864. getLLVMStyleWithColumns(10)));
  5865. }
  5866. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5867. verifyFormat("int x = {\n"
  5868. " avariable,\n"
  5869. " b(alongervariable)};",
  5870. getLLVMStyleWithColumns(25));
  5871. }
  5872. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5873. verifyFormat("return (a)(b){1, 2, 3};");
  5874. }
  5875. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5876. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5877. verifyFormat("vector<int> x{\n"
  5878. " 1, 2, 3, 4,\n"
  5879. "};");
  5880. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5881. verifyFormat("f({1, 2});");
  5882. verifyFormat("auto v = Foo{-1};");
  5883. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5884. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5885. verifyFormat("new vector<int>{1, 2, 3};");
  5886. verifyFormat("new int[3]{1, 2, 3};");
  5887. verifyFormat("new int{1};");
  5888. verifyFormat("return {arg1, arg2};");
  5889. verifyFormat("return {arg1, SomeType{parameter}};");
  5890. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5891. verifyFormat("new T{arg1, arg2};");
  5892. verifyFormat("f(MyMap[{composite, key}]);");
  5893. verifyFormat("class Class {\n"
  5894. " T member = {arg1, arg2};\n"
  5895. "};");
  5896. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5897. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5898. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5899. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5900. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5901. verifyFormat("auto i = decltype(x){};");
  5902. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5903. verifyFormat("Node n{1, Node{1000}, //\n"
  5904. " 2};");
  5905. verifyFormat("Aaaa aaaaaaa{\n"
  5906. " {\n"
  5907. " aaaa,\n"
  5908. " },\n"
  5909. "};");
  5910. verifyFormat("class C : public D {\n"
  5911. " SomeClass SC{2};\n"
  5912. "};");
  5913. verifyFormat("class C : public A {\n"
  5914. " class D : public B {\n"
  5915. " void f() { int i{2}; }\n"
  5916. " };\n"
  5917. "};");
  5918. verifyFormat("#define A {a, a},");
  5919. // In combination with BinPackArguments = false.
  5920. FormatStyle NoBinPacking = getLLVMStyle();
  5921. NoBinPacking.BinPackArguments = false;
  5922. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5923. " bbbbb,\n"
  5924. " ccccc,\n"
  5925. " ddddd,\n"
  5926. " eeeee,\n"
  5927. " ffffff,\n"
  5928. " ggggg,\n"
  5929. " hhhhhh,\n"
  5930. " iiiiii,\n"
  5931. " jjjjjj,\n"
  5932. " kkkkkk};",
  5933. NoBinPacking);
  5934. verifyFormat("const Aaaaaa aaaaa = {\n"
  5935. " aaaaa,\n"
  5936. " bbbbb,\n"
  5937. " ccccc,\n"
  5938. " ddddd,\n"
  5939. " eeeee,\n"
  5940. " ffffff,\n"
  5941. " ggggg,\n"
  5942. " hhhhhh,\n"
  5943. " iiiiii,\n"
  5944. " jjjjjj,\n"
  5945. " kkkkkk,\n"
  5946. "};",
  5947. NoBinPacking);
  5948. verifyFormat(
  5949. "const Aaaaaa aaaaa = {\n"
  5950. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5951. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5952. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5953. "};",
  5954. NoBinPacking);
  5955. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5956. // this might be utterly useless in real code.
  5957. verifyFormat("Constructor::Constructor()\n"
  5958. " : some_value{ //\n"
  5959. " aaaaaaa, //\n"
  5960. " bbbbbbb} {}");
  5961. // In braced lists, the first comment is always assumed to belong to the
  5962. // first element. Thus, it can be moved to the next or previous line as
  5963. // appropriate.
  5964. EXPECT_EQ("function({// First element:\n"
  5965. " 1,\n"
  5966. " // Second element:\n"
  5967. " 2});",
  5968. format("function({\n"
  5969. " // First element:\n"
  5970. " 1,\n"
  5971. " // Second element:\n"
  5972. " 2});"));
  5973. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5974. " // First element:\n"
  5975. " 1,\n"
  5976. " // Second element:\n"
  5977. " 2};",
  5978. format("std::vector<int> MyNumbers{// First element:\n"
  5979. " 1,\n"
  5980. " // Second element:\n"
  5981. " 2};",
  5982. getLLVMStyleWithColumns(30)));
  5983. // A trailing comma should still lead to an enforced line break.
  5984. EXPECT_EQ("vector<int> SomeVector = {\n"
  5985. " // aaa\n"
  5986. " 1, 2,\n"
  5987. "};",
  5988. format("vector<int> SomeVector = { // aaa\n"
  5989. " 1, 2, };"));
  5990. FormatStyle ExtraSpaces = getLLVMStyle();
  5991. ExtraSpaces.Cpp11BracedListStyle = false;
  5992. ExtraSpaces.ColumnLimit = 75;
  5993. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5994. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5995. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5996. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5997. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5998. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5999. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  6000. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  6001. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  6002. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  6003. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  6004. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  6005. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  6006. verifyFormat("class Class {\n"
  6007. " T member = { arg1, arg2 };\n"
  6008. "};",
  6009. ExtraSpaces);
  6010. verifyFormat(
  6011. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6012. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  6013. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  6014. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  6015. ExtraSpaces);
  6016. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  6017. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  6018. ExtraSpaces);
  6019. verifyFormat(
  6020. "someFunction(OtherParam,\n"
  6021. " BracedList{ // comment 1 (Forcing interesting break)\n"
  6022. " param1, param2,\n"
  6023. " // comment 2\n"
  6024. " param3, param4 });",
  6025. ExtraSpaces);
  6026. verifyFormat(
  6027. "std::this_thread::sleep_for(\n"
  6028. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  6029. ExtraSpaces);
  6030. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  6031. " aaaaaaa,\n"
  6032. " aaaaaaaaaa,\n"
  6033. " aaaaa,\n"
  6034. " aaaaaaaaaaaaaaa,\n"
  6035. " aaa,\n"
  6036. " aaaaaaaaaa,\n"
  6037. " a,\n"
  6038. " aaaaaaaaaaaaaaaaaaaaa,\n"
  6039. " aaaaaaaaaaaa,\n"
  6040. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  6041. " aaaaaaa,\n"
  6042. " a};");
  6043. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  6044. }
  6045. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  6046. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6047. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6048. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6049. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6050. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6051. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6052. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  6053. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6054. " 1, 22, 333, 4444, 55555, //\n"
  6055. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6056. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6057. verifyFormat(
  6058. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6059. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6060. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  6061. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6062. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6063. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6064. " 7777777};");
  6065. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6066. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6067. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6068. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6069. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6070. " // Separating comment.\n"
  6071. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6072. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6073. " // Leading comment\n"
  6074. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6075. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6076. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6077. " 1, 1, 1, 1};",
  6078. getLLVMStyleWithColumns(39));
  6079. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6080. " 1, 1, 1, 1};",
  6081. getLLVMStyleWithColumns(38));
  6082. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  6083. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  6084. getLLVMStyleWithColumns(43));
  6085. verifyFormat(
  6086. "static unsigned SomeValues[10][3] = {\n"
  6087. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  6088. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  6089. verifyFormat("static auto fields = new vector<string>{\n"
  6090. " \"aaaaaaaaaaaaa\",\n"
  6091. " \"aaaaaaaaaaaaa\",\n"
  6092. " \"aaaaaaaaaaaa\",\n"
  6093. " \"aaaaaaaaaaaaaa\",\n"
  6094. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6095. " \"aaaaaaaaaaaa\",\n"
  6096. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6097. "};");
  6098. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  6099. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  6100. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  6101. " 3, cccccccccccccccccccccc};",
  6102. getLLVMStyleWithColumns(60));
  6103. // Trailing commas.
  6104. verifyFormat("vector<int> x = {\n"
  6105. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  6106. "};",
  6107. getLLVMStyleWithColumns(39));
  6108. verifyFormat("vector<int> x = {\n"
  6109. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  6110. "};",
  6111. getLLVMStyleWithColumns(39));
  6112. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6113. " 1, 1, 1, 1,\n"
  6114. " /**/ /**/};",
  6115. getLLVMStyleWithColumns(39));
  6116. // Trailing comment in the first line.
  6117. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  6118. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  6119. " 111111111, 222222222, 3333333333, 444444444, //\n"
  6120. " 11111111, 22222222, 333333333, 44444444};");
  6121. // Trailing comment in the last line.
  6122. verifyFormat("int aaaaa[] = {\n"
  6123. " 1, 2, 3, // comment\n"
  6124. " 4, 5, 6 // comment\n"
  6125. "};");
  6126. // With nested lists, we should either format one item per line or all nested
  6127. // lists one on line.
  6128. // FIXME: For some nested lists, we can do better.
  6129. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  6130. " {aaaaaaaaaaaaaaaaaaa},\n"
  6131. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  6132. " {aaaaaaaaaaaaaaaaa}};",
  6133. getLLVMStyleWithColumns(60));
  6134. verifyFormat(
  6135. "SomeStruct my_struct_array = {\n"
  6136. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  6137. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  6138. " {aaa, aaa},\n"
  6139. " {aaa, aaa},\n"
  6140. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  6141. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6142. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  6143. // No column layout should be used here.
  6144. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  6145. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  6146. verifyNoCrash("a<,");
  6147. // No braced initializer here.
  6148. verifyFormat("void f() {\n"
  6149. " struct Dummy {};\n"
  6150. " f(v);\n"
  6151. "}");
  6152. // Long lists should be formatted in columns even if they are nested.
  6153. verifyFormat(
  6154. "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6155. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6156. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6157. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6158. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6159. " 1, 22, 333, 4444, 55555, 666666, 7777777});");
  6160. }
  6161. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  6162. FormatStyle DoNotMerge = getLLVMStyle();
  6163. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  6164. verifyFormat("void f() { return 42; }");
  6165. verifyFormat("void f() {\n"
  6166. " return 42;\n"
  6167. "}",
  6168. DoNotMerge);
  6169. verifyFormat("void f() {\n"
  6170. " // Comment\n"
  6171. "}");
  6172. verifyFormat("{\n"
  6173. "#error {\n"
  6174. " int a;\n"
  6175. "}");
  6176. verifyFormat("{\n"
  6177. " int a;\n"
  6178. "#error {\n"
  6179. "}");
  6180. verifyFormat("void f() {} // comment");
  6181. verifyFormat("void f() { int a; } // comment");
  6182. verifyFormat("void f() {\n"
  6183. "} // comment",
  6184. DoNotMerge);
  6185. verifyFormat("void f() {\n"
  6186. " int a;\n"
  6187. "} // comment",
  6188. DoNotMerge);
  6189. verifyFormat("void f() {\n"
  6190. "} // comment",
  6191. getLLVMStyleWithColumns(15));
  6192. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6193. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6194. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6195. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6196. verifyFormat("class C {\n"
  6197. " C()\n"
  6198. " : iiiiiiii(nullptr),\n"
  6199. " kkkkkkk(nullptr),\n"
  6200. " mmmmmmm(nullptr),\n"
  6201. " nnnnnnn(nullptr) {}\n"
  6202. "};",
  6203. getGoogleStyle());
  6204. FormatStyle NoColumnLimit = getLLVMStyle();
  6205. NoColumnLimit.ColumnLimit = 0;
  6206. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6207. EXPECT_EQ("class C {\n"
  6208. " A() : b(0) {}\n"
  6209. "};",
  6210. format("class C{A():b(0){}};", NoColumnLimit));
  6211. EXPECT_EQ("A()\n"
  6212. " : b(0) {\n"
  6213. "}",
  6214. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6215. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6216. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6217. FormatStyle::SFS_None;
  6218. EXPECT_EQ("A()\n"
  6219. " : b(0) {\n"
  6220. "}",
  6221. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6222. EXPECT_EQ("A()\n"
  6223. " : b(0) {\n"
  6224. "}",
  6225. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6226. verifyFormat("#define A \\\n"
  6227. " void f() { \\\n"
  6228. " int i; \\\n"
  6229. " }",
  6230. getLLVMStyleWithColumns(20));
  6231. verifyFormat("#define A \\\n"
  6232. " void f() { int i; }",
  6233. getLLVMStyleWithColumns(21));
  6234. verifyFormat("#define A \\\n"
  6235. " void f() { \\\n"
  6236. " int i; \\\n"
  6237. " } \\\n"
  6238. " int j;",
  6239. getLLVMStyleWithColumns(22));
  6240. verifyFormat("#define A \\\n"
  6241. " void f() { int i; } \\\n"
  6242. " int j;",
  6243. getLLVMStyleWithColumns(23));
  6244. }
  6245. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6246. FormatStyle MergeInlineOnly = getLLVMStyle();
  6247. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6248. verifyFormat("class C {\n"
  6249. " int f() { return 42; }\n"
  6250. "};",
  6251. MergeInlineOnly);
  6252. verifyFormat("int f() {\n"
  6253. " return 42;\n"
  6254. "}",
  6255. MergeInlineOnly);
  6256. }
  6257. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6258. // Elaborate type variable declarations.
  6259. verifyFormat("struct foo a = {bar};\nint n;");
  6260. verifyFormat("class foo a = {bar};\nint n;");
  6261. verifyFormat("union foo a = {bar};\nint n;");
  6262. // Elaborate types inside function definitions.
  6263. verifyFormat("struct foo f() {}\nint n;");
  6264. verifyFormat("class foo f() {}\nint n;");
  6265. verifyFormat("union foo f() {}\nint n;");
  6266. // Templates.
  6267. verifyFormat("template <class X> void f() {}\nint n;");
  6268. verifyFormat("template <struct X> void f() {}\nint n;");
  6269. verifyFormat("template <union X> void f() {}\nint n;");
  6270. // Actual definitions...
  6271. verifyFormat("struct {\n} n;");
  6272. verifyFormat(
  6273. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6274. verifyFormat("union Z {\n int n;\n} x;");
  6275. verifyFormat("class MACRO Z {\n} n;");
  6276. verifyFormat("class MACRO(X) Z {\n} n;");
  6277. verifyFormat("class __attribute__(X) Z {\n} n;");
  6278. verifyFormat("class __declspec(X) Z {\n} n;");
  6279. verifyFormat("class A##B##C {\n} n;");
  6280. verifyFormat("class alignas(16) Z {\n} n;");
  6281. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6282. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6283. // Redefinition from nested context:
  6284. verifyFormat("class A::B::C {\n} n;");
  6285. // Template definitions.
  6286. verifyFormat(
  6287. "template <typename F>\n"
  6288. "Matcher(const Matcher<F> &Other,\n"
  6289. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6290. " !is_same<F, T>::value>::type * = 0)\n"
  6291. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6292. // FIXME: This is still incorrectly handled at the formatter side.
  6293. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6294. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6295. // FIXME:
  6296. // This now gets parsed incorrectly as class definition.
  6297. // verifyFormat("class A<int> f() {\n}\nint n;");
  6298. // Elaborate types where incorrectly parsing the structural element would
  6299. // break the indent.
  6300. verifyFormat("if (true)\n"
  6301. " class X x;\n"
  6302. "else\n"
  6303. " f();\n");
  6304. // This is simply incomplete. Formatting is not important, but must not crash.
  6305. verifyFormat("class A:");
  6306. }
  6307. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6308. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6309. format("#error Leave all white!!!!! space* alone!\n"));
  6310. EXPECT_EQ(
  6311. "#warning Leave all white!!!!! space* alone!\n",
  6312. format("#warning Leave all white!!!!! space* alone!\n"));
  6313. EXPECT_EQ("#error 1", format(" # error 1"));
  6314. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6315. }
  6316. TEST_F(FormatTest, FormatHashIfExpressions) {
  6317. verifyFormat("#if AAAA && BBBB");
  6318. verifyFormat("#if (AAAA && BBBB)");
  6319. verifyFormat("#elif (AAAA && BBBB)");
  6320. // FIXME: Come up with a better indentation for #elif.
  6321. verifyFormat(
  6322. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6323. " defined(BBBBBBBB)\n"
  6324. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6325. " defined(BBBBBBBB)\n"
  6326. "#endif",
  6327. getLLVMStyleWithColumns(65));
  6328. }
  6329. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6330. FormatStyle AllowsMergedIf = getGoogleStyle();
  6331. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6332. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6333. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6334. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6335. EXPECT_EQ("if (true) return 42;",
  6336. format("if (true)\nreturn 42;", AllowsMergedIf));
  6337. FormatStyle ShortMergedIf = AllowsMergedIf;
  6338. ShortMergedIf.ColumnLimit = 25;
  6339. verifyFormat("#define A \\\n"
  6340. " if (true) return 42;",
  6341. ShortMergedIf);
  6342. verifyFormat("#define A \\\n"
  6343. " f(); \\\n"
  6344. " if (true)\n"
  6345. "#define B",
  6346. ShortMergedIf);
  6347. verifyFormat("#define A \\\n"
  6348. " f(); \\\n"
  6349. " if (true)\n"
  6350. "g();",
  6351. ShortMergedIf);
  6352. verifyFormat("{\n"
  6353. "#ifdef A\n"
  6354. " // Comment\n"
  6355. " if (true) continue;\n"
  6356. "#endif\n"
  6357. " // Comment\n"
  6358. " if (true) continue;\n"
  6359. "}",
  6360. ShortMergedIf);
  6361. ShortMergedIf.ColumnLimit = 29;
  6362. verifyFormat("#define A \\\n"
  6363. " if (aaaaaaaaaa) return 1; \\\n"
  6364. " return 2;",
  6365. ShortMergedIf);
  6366. ShortMergedIf.ColumnLimit = 28;
  6367. verifyFormat("#define A \\\n"
  6368. " if (aaaaaaaaaa) \\\n"
  6369. " return 1; \\\n"
  6370. " return 2;",
  6371. ShortMergedIf);
  6372. }
  6373. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6374. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6375. " f();\n"
  6376. "}");
  6377. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6378. " f();\n"
  6379. "} /* another comment */ else /* comment #3 */ {\n"
  6380. " g();\n"
  6381. "}");
  6382. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6383. " f();\n"
  6384. "}");
  6385. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6386. " f();\n"
  6387. "}");
  6388. verifyFormat("do /* a comment in a strange place */ {\n"
  6389. " f();\n"
  6390. "} /* another comment */ while (0);");
  6391. }
  6392. TEST_F(FormatTest, BlockComments) {
  6393. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6394. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6395. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6396. EXPECT_EQ("#define A /*123*/ \\\n"
  6397. " b\n"
  6398. "/* */\n"
  6399. "someCall(\n"
  6400. " parameter);",
  6401. format("#define A /*123*/ b\n"
  6402. "/* */\n"
  6403. "someCall(parameter);",
  6404. getLLVMStyleWithColumns(15)));
  6405. EXPECT_EQ("#define A\n"
  6406. "/* */ someCall(\n"
  6407. " parameter);",
  6408. format("#define A\n"
  6409. "/* */someCall(parameter);",
  6410. getLLVMStyleWithColumns(15)));
  6411. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6412. EXPECT_EQ("/*\n"
  6413. "*\n"
  6414. " * aaaaaa\n"
  6415. " * aaaaaa\n"
  6416. "*/",
  6417. format("/*\n"
  6418. "*\n"
  6419. " * aaaaaa aaaaaa\n"
  6420. "*/",
  6421. getLLVMStyleWithColumns(10)));
  6422. EXPECT_EQ("/*\n"
  6423. "**\n"
  6424. "* aaaaaa\n"
  6425. "*aaaaaa\n"
  6426. "*/",
  6427. format("/*\n"
  6428. "**\n"
  6429. "* aaaaaa aaaaaa\n"
  6430. "*/",
  6431. getLLVMStyleWithColumns(10)));
  6432. FormatStyle NoBinPacking = getLLVMStyle();
  6433. NoBinPacking.BinPackParameters = false;
  6434. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6435. " 2, /* comment 2 */\n"
  6436. " 3, /* comment 3 */\n"
  6437. " aaaa,\n"
  6438. " bbbb);",
  6439. format("someFunction (1, /* comment 1 */\n"
  6440. " 2, /* comment 2 */ \n"
  6441. " 3, /* comment 3 */\n"
  6442. "aaaa, bbbb );",
  6443. NoBinPacking));
  6444. verifyFormat(
  6445. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6446. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6447. EXPECT_EQ(
  6448. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6449. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6450. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6451. format(
  6452. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6453. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6454. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6455. EXPECT_EQ(
  6456. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6457. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6458. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6459. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6460. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6461. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6462. verifyFormat("void f(int * /* unused */) {}");
  6463. EXPECT_EQ("/*\n"
  6464. " **\n"
  6465. " */",
  6466. format("/*\n"
  6467. " **\n"
  6468. " */"));
  6469. EXPECT_EQ("/*\n"
  6470. " *q\n"
  6471. " */",
  6472. format("/*\n"
  6473. " *q\n"
  6474. " */"));
  6475. EXPECT_EQ("/*\n"
  6476. " * q\n"
  6477. " */",
  6478. format("/*\n"
  6479. " * q\n"
  6480. " */"));
  6481. EXPECT_EQ("/*\n"
  6482. " **/",
  6483. format("/*\n"
  6484. " **/"));
  6485. EXPECT_EQ("/*\n"
  6486. " ***/",
  6487. format("/*\n"
  6488. " ***/"));
  6489. }
  6490. TEST_F(FormatTest, BlockCommentsInMacros) {
  6491. EXPECT_EQ("#define A \\\n"
  6492. " { \\\n"
  6493. " /* one line */ \\\n"
  6494. " someCall();",
  6495. format("#define A { \\\n"
  6496. " /* one line */ \\\n"
  6497. " someCall();",
  6498. getLLVMStyleWithColumns(20)));
  6499. EXPECT_EQ("#define A \\\n"
  6500. " { \\\n"
  6501. " /* previous */ \\\n"
  6502. " /* one line */ \\\n"
  6503. " someCall();",
  6504. format("#define A { \\\n"
  6505. " /* previous */ \\\n"
  6506. " /* one line */ \\\n"
  6507. " someCall();",
  6508. getLLVMStyleWithColumns(20)));
  6509. }
  6510. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6511. EXPECT_EQ("a = {\n"
  6512. " 1111 /* */\n"
  6513. "};",
  6514. format("a = {1111 /* */\n"
  6515. "};",
  6516. getLLVMStyleWithColumns(15)));
  6517. EXPECT_EQ("a = {\n"
  6518. " 1111 /* */\n"
  6519. "};",
  6520. format("a = {1111 /* */\n"
  6521. "};",
  6522. getLLVMStyleWithColumns(15)));
  6523. // FIXME: The formatting is still wrong here.
  6524. EXPECT_EQ("a = {\n"
  6525. " 1111 /* a\n"
  6526. " */\n"
  6527. "};",
  6528. format("a = {1111 /* a */\n"
  6529. "};",
  6530. getLLVMStyleWithColumns(15)));
  6531. }
  6532. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6533. // FIXME: This is not what we want...
  6534. verifyFormat("{\n"
  6535. "// a"
  6536. "// b");
  6537. }
  6538. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6539. verifyFormat("void f(int *a);");
  6540. verifyFormat("void f() { f(fint * b); }");
  6541. verifyFormat("class A {\n void f(int *a);\n};");
  6542. verifyFormat("class A {\n int *a;\n};");
  6543. verifyFormat("namespace a {\n"
  6544. "namespace b {\n"
  6545. "class A {\n"
  6546. " void f() {}\n"
  6547. " int *a;\n"
  6548. "};\n"
  6549. "}\n"
  6550. "}");
  6551. }
  6552. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6553. verifyFormat("while");
  6554. verifyFormat("operator");
  6555. }
  6556. //===----------------------------------------------------------------------===//
  6557. // Objective-C tests.
  6558. //===----------------------------------------------------------------------===//
  6559. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6560. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6561. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6562. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6563. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6564. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6565. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6566. format("-(NSInteger)Method3:(id)anObject;"));
  6567. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6568. format("-(NSInteger)Method4:(id)anObject;"));
  6569. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6570. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6571. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6572. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6573. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6574. "forAllCells:(BOOL)flag;",
  6575. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6576. "forAllCells:(BOOL)flag;"));
  6577. // Very long objectiveC method declaration.
  6578. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6579. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6580. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6581. " inRange:(NSRange)range\n"
  6582. " outRange:(NSRange)out_range\n"
  6583. " outRange1:(NSRange)out_range1\n"
  6584. " outRange2:(NSRange)out_range2\n"
  6585. " outRange3:(NSRange)out_range3\n"
  6586. " outRange4:(NSRange)out_range4\n"
  6587. " outRange5:(NSRange)out_range5\n"
  6588. " outRange6:(NSRange)out_range6\n"
  6589. " outRange7:(NSRange)out_range7\n"
  6590. " outRange8:(NSRange)out_range8\n"
  6591. " outRange9:(NSRange)out_range9;");
  6592. // When the function name has to be wrapped.
  6593. FormatStyle Style = getLLVMStyle();
  6594. Style.IndentWrappedFunctionNames = false;
  6595. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6596. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6597. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6598. "}",
  6599. Style);
  6600. Style.IndentWrappedFunctionNames = true;
  6601. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6602. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6603. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6604. "}",
  6605. Style);
  6606. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6607. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6608. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6609. // protocol lists (but not for template classes):
  6610. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6611. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6612. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6613. // If there's no return type (very rare in practice!), LLVM and Google style
  6614. // agree.
  6615. verifyFormat("- foo;");
  6616. verifyFormat("- foo:(int)f;");
  6617. verifyGoogleFormat("- foo:(int)foo;");
  6618. }
  6619. TEST_F(FormatTest, FormatObjCInterface) {
  6620. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6621. "@public\n"
  6622. " int field1;\n"
  6623. "@protected\n"
  6624. " int field2;\n"
  6625. "@private\n"
  6626. " int field3;\n"
  6627. "@package\n"
  6628. " int field4;\n"
  6629. "}\n"
  6630. "+ (id)init;\n"
  6631. "@end");
  6632. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6633. " @public\n"
  6634. " int field1;\n"
  6635. " @protected\n"
  6636. " int field2;\n"
  6637. " @private\n"
  6638. " int field3;\n"
  6639. " @package\n"
  6640. " int field4;\n"
  6641. "}\n"
  6642. "+ (id)init;\n"
  6643. "@end");
  6644. verifyFormat("@interface /* wait for it */ Foo\n"
  6645. "+ (id)init;\n"
  6646. "// Look, a comment!\n"
  6647. "- (int)answerWith:(int)i;\n"
  6648. "@end");
  6649. verifyFormat("@interface Foo\n"
  6650. "@end\n"
  6651. "@interface Bar\n"
  6652. "@end");
  6653. verifyFormat("@interface Foo : Bar\n"
  6654. "+ (id)init;\n"
  6655. "@end");
  6656. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6657. "+ (id)init;\n"
  6658. "@end");
  6659. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6660. "+ (id)init;\n"
  6661. "@end");
  6662. verifyFormat("@interface Foo (HackStuff)\n"
  6663. "+ (id)init;\n"
  6664. "@end");
  6665. verifyFormat("@interface Foo ()\n"
  6666. "+ (id)init;\n"
  6667. "@end");
  6668. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6669. "+ (id)init;\n"
  6670. "@end");
  6671. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6672. "+ (id)init;\n"
  6673. "@end");
  6674. verifyFormat("@interface Foo {\n"
  6675. " int _i;\n"
  6676. "}\n"
  6677. "+ (id)init;\n"
  6678. "@end");
  6679. verifyFormat("@interface Foo : Bar {\n"
  6680. " int _i;\n"
  6681. "}\n"
  6682. "+ (id)init;\n"
  6683. "@end");
  6684. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6685. " int _i;\n"
  6686. "}\n"
  6687. "+ (id)init;\n"
  6688. "@end");
  6689. verifyFormat("@interface Foo (HackStuff) {\n"
  6690. " int _i;\n"
  6691. "}\n"
  6692. "+ (id)init;\n"
  6693. "@end");
  6694. verifyFormat("@interface Foo () {\n"
  6695. " int _i;\n"
  6696. "}\n"
  6697. "+ (id)init;\n"
  6698. "@end");
  6699. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6700. " int _i;\n"
  6701. "}\n"
  6702. "+ (id)init;\n"
  6703. "@end");
  6704. FormatStyle OnePerLine = getGoogleStyle();
  6705. OnePerLine.BinPackParameters = false;
  6706. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6707. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6708. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6709. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6710. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6711. "}",
  6712. OnePerLine);
  6713. }
  6714. TEST_F(FormatTest, FormatObjCImplementation) {
  6715. verifyFormat("@implementation Foo : NSObject {\n"
  6716. "@public\n"
  6717. " int field1;\n"
  6718. "@protected\n"
  6719. " int field2;\n"
  6720. "@private\n"
  6721. " int field3;\n"
  6722. "@package\n"
  6723. " int field4;\n"
  6724. "}\n"
  6725. "+ (id)init {\n}\n"
  6726. "@end");
  6727. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6728. " @public\n"
  6729. " int field1;\n"
  6730. " @protected\n"
  6731. " int field2;\n"
  6732. " @private\n"
  6733. " int field3;\n"
  6734. " @package\n"
  6735. " int field4;\n"
  6736. "}\n"
  6737. "+ (id)init {\n}\n"
  6738. "@end");
  6739. verifyFormat("@implementation Foo\n"
  6740. "+ (id)init {\n"
  6741. " if (true)\n"
  6742. " return nil;\n"
  6743. "}\n"
  6744. "// Look, a comment!\n"
  6745. "- (int)answerWith:(int)i {\n"
  6746. " return i;\n"
  6747. "}\n"
  6748. "+ (int)answerWith:(int)i {\n"
  6749. " return i;\n"
  6750. "}\n"
  6751. "@end");
  6752. verifyFormat("@implementation Foo\n"
  6753. "@end\n"
  6754. "@implementation Bar\n"
  6755. "@end");
  6756. EXPECT_EQ("@implementation Foo : Bar\n"
  6757. "+ (id)init {\n}\n"
  6758. "- (void)foo {\n}\n"
  6759. "@end",
  6760. format("@implementation Foo : Bar\n"
  6761. "+(id)init{}\n"
  6762. "-(void)foo{}\n"
  6763. "@end"));
  6764. verifyFormat("@implementation Foo {\n"
  6765. " int _i;\n"
  6766. "}\n"
  6767. "+ (id)init {\n}\n"
  6768. "@end");
  6769. verifyFormat("@implementation Foo : Bar {\n"
  6770. " int _i;\n"
  6771. "}\n"
  6772. "+ (id)init {\n}\n"
  6773. "@end");
  6774. verifyFormat("@implementation Foo (HackStuff)\n"
  6775. "+ (id)init {\n}\n"
  6776. "@end");
  6777. verifyFormat("@implementation ObjcClass\n"
  6778. "- (void)method;\n"
  6779. "{}\n"
  6780. "@end");
  6781. }
  6782. TEST_F(FormatTest, FormatObjCProtocol) {
  6783. verifyFormat("@protocol Foo\n"
  6784. "@property(weak) id delegate;\n"
  6785. "- (NSUInteger)numberOfThings;\n"
  6786. "@end");
  6787. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6788. "- (NSUInteger)numberOfThings;\n"
  6789. "@end");
  6790. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6791. "- (NSUInteger)numberOfThings;\n"
  6792. "@end");
  6793. verifyFormat("@protocol Foo;\n"
  6794. "@protocol Bar;\n");
  6795. verifyFormat("@protocol Foo\n"
  6796. "@end\n"
  6797. "@protocol Bar\n"
  6798. "@end");
  6799. verifyFormat("@protocol myProtocol\n"
  6800. "- (void)mandatoryWithInt:(int)i;\n"
  6801. "@optional\n"
  6802. "- (void)optional;\n"
  6803. "@required\n"
  6804. "- (void)required;\n"
  6805. "@optional\n"
  6806. "@property(assign) int madProp;\n"
  6807. "@end\n");
  6808. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6809. " int *looooooooooooooooooooooooooooongNumber;\n"
  6810. "@property(nonatomic, assign, readonly)\n"
  6811. " NSString *looooooooooooooooooooooooooooongName;");
  6812. verifyFormat("@implementation PR18406\n"
  6813. "}\n"
  6814. "@end");
  6815. }
  6816. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6817. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6818. " rect:(NSRect)theRect\n"
  6819. " interval:(float)theInterval {\n"
  6820. "}");
  6821. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6822. " longKeyword:(NSRect)theRect\n"
  6823. " longerKeyword:(float)theInterval\n"
  6824. " error:(NSError **)theError {\n"
  6825. "}");
  6826. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6827. " longKeyword:(NSRect)theRect\n"
  6828. " evenLongerKeyword:(float)theInterval\n"
  6829. " error:(NSError **)theError {\n"
  6830. "}");
  6831. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6832. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6833. " NS_DESIGNATED_INITIALIZER;",
  6834. getLLVMStyleWithColumns(60));
  6835. // Continuation indent width should win over aligning colons if the function
  6836. // name is long.
  6837. FormatStyle continuationStyle = getGoogleStyle();
  6838. continuationStyle.ColumnLimit = 40;
  6839. continuationStyle.IndentWrappedFunctionNames = true;
  6840. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6841. " dontAlignNamef:(NSRect)theRect {\n"
  6842. "}",
  6843. continuationStyle);
  6844. // Make sure we don't break aligning for short parameter names.
  6845. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6846. " aShortf:(NSRect)theRect {\n"
  6847. "}",
  6848. continuationStyle);
  6849. }
  6850. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6851. verifyFormat("[foo bar:baz];");
  6852. verifyFormat("return [foo bar:baz];");
  6853. verifyFormat("return (a)[foo bar:baz];");
  6854. verifyFormat("f([foo bar:baz]);");
  6855. verifyFormat("f(2, [foo bar:baz]);");
  6856. verifyFormat("f(2, a ? b : c);");
  6857. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6858. // Unary operators.
  6859. verifyFormat("int a = +[foo bar:baz];");
  6860. verifyFormat("int a = -[foo bar:baz];");
  6861. verifyFormat("int a = ![foo bar:baz];");
  6862. verifyFormat("int a = ~[foo bar:baz];");
  6863. verifyFormat("int a = ++[foo bar:baz];");
  6864. verifyFormat("int a = --[foo bar:baz];");
  6865. verifyFormat("int a = sizeof [foo bar:baz];");
  6866. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6867. verifyFormat("int a = &[foo bar:baz];");
  6868. verifyFormat("int a = *[foo bar:baz];");
  6869. // FIXME: Make casts work, without breaking f()[4].
  6870. // verifyFormat("int a = (int)[foo bar:baz];");
  6871. // verifyFormat("return (int)[foo bar:baz];");
  6872. // verifyFormat("(void)[foo bar:baz];");
  6873. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6874. // Binary operators.
  6875. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6876. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6877. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6878. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6879. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6880. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6881. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6882. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6883. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6884. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6885. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6886. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6887. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6888. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6889. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6890. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6891. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6892. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6893. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6894. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6895. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6896. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6897. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6898. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6899. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6900. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6901. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6902. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6903. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6904. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6905. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6906. // Whew!
  6907. verifyFormat("return in[42];");
  6908. verifyFormat("for (auto v : in[1]) {\n}");
  6909. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  6910. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  6911. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  6912. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  6913. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  6914. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6915. "}");
  6916. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6917. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  6918. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  6919. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6920. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6921. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6922. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6923. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6924. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6925. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6926. verifyFormat("arr[[self indexForFoo:a]];");
  6927. verifyFormat("throw [self errorFor:a];");
  6928. verifyFormat("@throw [self errorFor:a];");
  6929. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6930. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6931. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6932. // This tests that the formatter doesn't break after "backing" but before ":",
  6933. // which would be at 80 columns.
  6934. verifyFormat(
  6935. "void f() {\n"
  6936. " if ((self = [super initWithContentRect:contentRect\n"
  6937. " styleMask:styleMask ?: otherMask\n"
  6938. " backing:NSBackingStoreBuffered\n"
  6939. " defer:YES]))");
  6940. verifyFormat(
  6941. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6942. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6943. verifyFormat("[myObj short:arg1 // Force line break\n"
  6944. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6945. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6946. " error:arg4];");
  6947. verifyFormat(
  6948. "void f() {\n"
  6949. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6950. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6951. " pos.width(), pos.height())\n"
  6952. " styleMask:NSBorderlessWindowMask\n"
  6953. " backing:NSBackingStoreBuffered\n"
  6954. " defer:NO]);\n"
  6955. "}");
  6956. verifyFormat(
  6957. "void f() {\n"
  6958. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6959. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6960. " pos.width(), pos.height())\n"
  6961. " syeMask:NSBorderlessWindowMask\n"
  6962. " bking:NSBackingStoreBuffered\n"
  6963. " der:NO]);\n"
  6964. "}",
  6965. getLLVMStyleWithColumns(70));
  6966. verifyFormat(
  6967. "void f() {\n"
  6968. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6969. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6970. " pos.width(), pos.height())\n"
  6971. " styleMask:NSBorderlessWindowMask\n"
  6972. " backing:NSBackingStoreBuffered\n"
  6973. " defer:NO]);\n"
  6974. "}",
  6975. getChromiumStyle(FormatStyle::LK_Cpp));
  6976. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6977. " with:contentsNativeView];");
  6978. verifyFormat(
  6979. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6980. " owner:nillllll];");
  6981. verifyFormat(
  6982. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6983. " forType:kBookmarkButtonDragType];");
  6984. verifyFormat("[defaultCenter addObserver:self\n"
  6985. " selector:@selector(willEnterFullscreen)\n"
  6986. " name:kWillEnterFullscreenNotification\n"
  6987. " object:nil];");
  6988. verifyFormat("[image_rep drawInRect:drawRect\n"
  6989. " fromRect:NSZeroRect\n"
  6990. " operation:NSCompositeCopy\n"
  6991. " fraction:1.0\n"
  6992. " respectFlipped:NO\n"
  6993. " hints:nil];");
  6994. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6995. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6996. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  6997. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6998. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  6999. " aaaaaaaaaaaaaaaaaaaaaa];");
  7000. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  7001. " .aaaaaaaa];", // FIXME: Indentation seems off.
  7002. getLLVMStyleWithColumns(60));
  7003. verifyFormat(
  7004. "scoped_nsobject<NSTextField> message(\n"
  7005. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  7006. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  7007. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  7008. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  7009. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  7010. " aaaa:bbb];");
  7011. verifyFormat("[self param:function( //\n"
  7012. " parameter)]");
  7013. verifyFormat(
  7014. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7015. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7016. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  7017. // FIXME: This violates the column limit.
  7018. verifyFormat(
  7019. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7020. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  7021. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  7022. getLLVMStyleWithColumns(60));
  7023. // Variadic parameters.
  7024. verifyFormat(
  7025. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  7026. verifyFormat(
  7027. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7028. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7029. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  7030. verifyFormat("[self // break\n"
  7031. " a:a\n"
  7032. " aaa:aaa];");
  7033. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  7034. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  7035. }
  7036. TEST_F(FormatTest, ObjCAt) {
  7037. verifyFormat("@autoreleasepool");
  7038. verifyFormat("@catch");
  7039. verifyFormat("@class");
  7040. verifyFormat("@compatibility_alias");
  7041. verifyFormat("@defs");
  7042. verifyFormat("@dynamic");
  7043. verifyFormat("@encode");
  7044. verifyFormat("@end");
  7045. verifyFormat("@finally");
  7046. verifyFormat("@implementation");
  7047. verifyFormat("@import");
  7048. verifyFormat("@interface");
  7049. verifyFormat("@optional");
  7050. verifyFormat("@package");
  7051. verifyFormat("@private");
  7052. verifyFormat("@property");
  7053. verifyFormat("@protected");
  7054. verifyFormat("@protocol");
  7055. verifyFormat("@public");
  7056. verifyFormat("@required");
  7057. verifyFormat("@selector");
  7058. verifyFormat("@synchronized");
  7059. verifyFormat("@synthesize");
  7060. verifyFormat("@throw");
  7061. verifyFormat("@try");
  7062. EXPECT_EQ("@interface", format("@ interface"));
  7063. // The precise formatting of this doesn't matter, nobody writes code like
  7064. // this.
  7065. verifyFormat("@ /*foo*/ interface");
  7066. }
  7067. TEST_F(FormatTest, ObjCSnippets) {
  7068. verifyFormat("@autoreleasepool {\n"
  7069. " foo();\n"
  7070. "}");
  7071. verifyFormat("@class Foo, Bar;");
  7072. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  7073. verifyFormat("@dynamic textColor;");
  7074. verifyFormat("char *buf1 = @encode(int *);");
  7075. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  7076. verifyFormat("char *buf1 = @encode(int **);");
  7077. verifyFormat("Protocol *proto = @protocol(p1);");
  7078. verifyFormat("SEL s = @selector(foo:);");
  7079. verifyFormat("@synchronized(self) {\n"
  7080. " f();\n"
  7081. "}");
  7082. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7083. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7084. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  7085. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  7086. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  7087. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7088. getMozillaStyle());
  7089. verifyFormat("@property BOOL editable;", getMozillaStyle());
  7090. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7091. getWebKitStyle());
  7092. verifyFormat("@property BOOL editable;", getWebKitStyle());
  7093. verifyFormat("@import foo.bar;\n"
  7094. "@import baz;");
  7095. }
  7096. TEST_F(FormatTest, ObjCForIn) {
  7097. verifyFormat("- (void)test {\n"
  7098. " for (NSString *n in arrayOfStrings) {\n"
  7099. " foo(n);\n"
  7100. " }\n"
  7101. "}");
  7102. verifyFormat("- (void)test {\n"
  7103. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  7104. " foo(n);\n"
  7105. " }\n"
  7106. "}");
  7107. }
  7108. TEST_F(FormatTest, ObjCLiterals) {
  7109. verifyFormat("@\"String\"");
  7110. verifyFormat("@1");
  7111. verifyFormat("@+4.8");
  7112. verifyFormat("@-4");
  7113. verifyFormat("@1LL");
  7114. verifyFormat("@.5");
  7115. verifyFormat("@'c'");
  7116. verifyFormat("@true");
  7117. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  7118. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  7119. verifyFormat("NSNumber *favoriteColor = @(Green);");
  7120. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  7121. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  7122. }
  7123. TEST_F(FormatTest, ObjCDictLiterals) {
  7124. verifyFormat("@{");
  7125. verifyFormat("@{}");
  7126. verifyFormat("@{@\"one\" : @1}");
  7127. verifyFormat("return @{@\"one\" : @1;");
  7128. verifyFormat("@{@\"one\" : @1}");
  7129. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  7130. verifyFormat("@{\n"
  7131. " @\"one\" : @{@2 : @1},\n"
  7132. "}");
  7133. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  7134. verifyIncompleteFormat("[self setDict:@{}");
  7135. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  7136. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  7137. verifyFormat(
  7138. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  7139. verifyFormat(
  7140. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  7141. verifyFormat("NSDictionary *d = @{\n"
  7142. " @\"nam\" : NSUserNam(),\n"
  7143. " @\"dte\" : [NSDate date],\n"
  7144. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7145. "};");
  7146. verifyFormat(
  7147. "@{\n"
  7148. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7149. "regularFont,\n"
  7150. "};");
  7151. verifyGoogleFormat(
  7152. "@{\n"
  7153. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7154. "regularFont,\n"
  7155. "};");
  7156. verifyFormat(
  7157. "@{\n"
  7158. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  7159. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  7160. "};");
  7161. // We should try to be robust in case someone forgets the "@".
  7162. verifyFormat("NSDictionary *d = {\n"
  7163. " @\"nam\" : NSUserNam(),\n"
  7164. " @\"dte\" : [NSDate date],\n"
  7165. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7166. "};");
  7167. verifyFormat("NSMutableDictionary *dictionary =\n"
  7168. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  7169. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  7170. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  7171. " cccccccccccccccc : ccccccccccccccc\n"
  7172. " }];");
  7173. // Ensure that casts before the key are kept on the same line as the key.
  7174. verifyFormat(
  7175. "NSDictionary *d = @{\n"
  7176. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  7177. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  7178. "};");
  7179. }
  7180. TEST_F(FormatTest, ObjCArrayLiterals) {
  7181. verifyIncompleteFormat("@[");
  7182. verifyFormat("@[]");
  7183. verifyFormat(
  7184. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  7185. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  7186. verifyFormat("NSArray *array = @[ [foo description] ];");
  7187. verifyFormat(
  7188. "NSArray *some_variable = @[\n"
  7189. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7190. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7191. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7192. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7193. "];");
  7194. verifyFormat(
  7195. "NSArray *some_variable = @[\n"
  7196. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7197. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  7198. "];");
  7199. verifyFormat("NSArray *some_variable = @[\n"
  7200. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7201. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7202. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7203. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7204. "];");
  7205. verifyFormat("NSArray *array = @[\n"
  7206. " @\"a\",\n"
  7207. " @\"a\",\n" // Trailing comma -> one per line.
  7208. "];");
  7209. // We should try to be robust in case someone forgets the "@".
  7210. verifyFormat("NSArray *some_variable = [\n"
  7211. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7212. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7213. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7214. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7215. "];");
  7216. verifyFormat(
  7217. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7218. " index:(NSUInteger)index\n"
  7219. " nonDigitAttributes:\n"
  7220. " (NSDictionary *)noDigitAttributes;");
  7221. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7222. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7223. "]];");
  7224. }
  7225. TEST_F(FormatTest, BreaksStringLiterals) {
  7226. EXPECT_EQ("\"some text \"\n"
  7227. "\"other\";",
  7228. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7229. EXPECT_EQ("\"some text \"\n"
  7230. "\"other\";",
  7231. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7232. EXPECT_EQ(
  7233. "#define A \\\n"
  7234. " \"some \" \\\n"
  7235. " \"text \" \\\n"
  7236. " \"other\";",
  7237. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7238. EXPECT_EQ(
  7239. "#define A \\\n"
  7240. " \"so \" \\\n"
  7241. " \"text \" \\\n"
  7242. " \"other\";",
  7243. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7244. EXPECT_EQ("\"some text\"",
  7245. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7246. EXPECT_EQ("\"some text\"",
  7247. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7248. EXPECT_EQ("\"some \"\n"
  7249. "\"text\"",
  7250. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7251. EXPECT_EQ("\"some \"\n"
  7252. "\"text\"",
  7253. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7254. EXPECT_EQ("\"some\"\n"
  7255. "\" tex\"\n"
  7256. "\"t\"",
  7257. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7258. EXPECT_EQ("\"some\"\n"
  7259. "\" tex\"\n"
  7260. "\" and\"",
  7261. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7262. EXPECT_EQ("\"some\"\n"
  7263. "\"/tex\"\n"
  7264. "\"/and\"",
  7265. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7266. EXPECT_EQ("variable =\n"
  7267. " \"long string \"\n"
  7268. " \"literal\";",
  7269. format("variable = \"long string literal\";",
  7270. getLLVMStyleWithColumns(20)));
  7271. EXPECT_EQ("variable = f(\n"
  7272. " \"long string \"\n"
  7273. " \"literal\",\n"
  7274. " short,\n"
  7275. " loooooooooooooooooooong);",
  7276. format("variable = f(\"long string literal\", short, "
  7277. "loooooooooooooooooooong);",
  7278. getLLVMStyleWithColumns(20)));
  7279. EXPECT_EQ(
  7280. "f(g(\"long string \"\n"
  7281. " \"literal\"),\n"
  7282. " b);",
  7283. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7284. EXPECT_EQ("f(g(\"long string \"\n"
  7285. " \"literal\",\n"
  7286. " a),\n"
  7287. " b);",
  7288. format("f(g(\"long string literal\", a), b);",
  7289. getLLVMStyleWithColumns(20)));
  7290. EXPECT_EQ(
  7291. "f(\"one two\".split(\n"
  7292. " variable));",
  7293. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7294. EXPECT_EQ("f(\"one two three four five six \"\n"
  7295. " \"seven\".split(\n"
  7296. " really_looooong_variable));",
  7297. format("f(\"one two three four five six seven\"."
  7298. "split(really_looooong_variable));",
  7299. getLLVMStyleWithColumns(33)));
  7300. EXPECT_EQ("f(\"some \"\n"
  7301. " \"text\",\n"
  7302. " other);",
  7303. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7304. // Only break as a last resort.
  7305. verifyFormat(
  7306. "aaaaaaaaaaaaaaaaaaaa(\n"
  7307. " aaaaaaaaaaaaaaaaaaaa,\n"
  7308. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7309. EXPECT_EQ("\"splitmea\"\n"
  7310. "\"trandomp\"\n"
  7311. "\"oint\"",
  7312. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7313. EXPECT_EQ("\"split/\"\n"
  7314. "\"pathat/\"\n"
  7315. "\"slashes\"",
  7316. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7317. EXPECT_EQ("\"split/\"\n"
  7318. "\"pathat/\"\n"
  7319. "\"slashes\"",
  7320. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7321. EXPECT_EQ("\"split at \"\n"
  7322. "\"spaces/at/\"\n"
  7323. "\"slashes.at.any$\"\n"
  7324. "\"non-alphanumeric%\"\n"
  7325. "\"1111111111characte\"\n"
  7326. "\"rs\"",
  7327. format("\"split at "
  7328. "spaces/at/"
  7329. "slashes.at."
  7330. "any$non-"
  7331. "alphanumeric%"
  7332. "1111111111characte"
  7333. "rs\"",
  7334. getLLVMStyleWithColumns(20)));
  7335. // Verify that splitting the strings understands
  7336. // Style::AlwaysBreakBeforeMultilineStrings.
  7337. EXPECT_EQ(
  7338. "aaaaaaaaaaaa(\n"
  7339. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7340. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7341. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7342. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7343. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7344. getGoogleStyle()));
  7345. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7346. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7347. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7348. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7349. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7350. getGoogleStyle()));
  7351. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7352. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7353. format("llvm::outs() << "
  7354. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7355. "aaaaaaaaaaaaaaaaaaa\";"));
  7356. EXPECT_EQ("ffff(\n"
  7357. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7358. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7359. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7360. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7361. getGoogleStyle()));
  7362. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7363. AlignLeft.AlignEscapedNewlinesLeft = true;
  7364. EXPECT_EQ("#define A \\\n"
  7365. " \"some \" \\\n"
  7366. " \"text \" \\\n"
  7367. " \"other\";",
  7368. format("#define A \"some text other\";", AlignLeft));
  7369. }
  7370. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7371. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7372. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7373. EXPECT_EQ("int i = a(b());",
  7374. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7375. }
  7376. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7377. EXPECT_EQ(
  7378. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7379. "(\n"
  7380. " \"x\t\");",
  7381. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7382. "aaaaaaa("
  7383. "\"x\t\");"));
  7384. }
  7385. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7386. EXPECT_EQ(
  7387. "u8\"utf8 string \"\n"
  7388. "u8\"literal\";",
  7389. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7390. EXPECT_EQ(
  7391. "u\"utf16 string \"\n"
  7392. "u\"literal\";",
  7393. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7394. EXPECT_EQ(
  7395. "U\"utf32 string \"\n"
  7396. "U\"literal\";",
  7397. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7398. EXPECT_EQ("L\"wide string \"\n"
  7399. "L\"literal\";",
  7400. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7401. EXPECT_EQ("@\"NSString \"\n"
  7402. "@\"literal\";",
  7403. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7404. // This input makes clang-format try to split the incomplete unicode escape
  7405. // sequence, which used to lead to a crasher.
  7406. verifyNoCrash(
  7407. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7408. getLLVMStyleWithColumns(60));
  7409. }
  7410. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7411. FormatStyle Style = getGoogleStyleWithColumns(15);
  7412. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7413. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7414. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7415. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7416. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7417. format("u8R\"x(raw literal)x\";", Style));
  7418. }
  7419. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7420. FormatStyle Style = getLLVMStyleWithColumns(20);
  7421. EXPECT_EQ(
  7422. "_T(\"aaaaaaaaaaaaaa\")\n"
  7423. "_T(\"aaaaaaaaaaaaaa\")\n"
  7424. "_T(\"aaaaaaaaaaaa\")",
  7425. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7426. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7427. " _T(\"aaaaaa\"),\n"
  7428. " z);",
  7429. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7430. // FIXME: Handle embedded spaces in one iteration.
  7431. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7432. // "_T(\"aaaaaaaaaaaaa\")\n"
  7433. // "_T(\"aaaaaaaaaaaaa\")\n"
  7434. // "_T(\"a\")",
  7435. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7436. // getLLVMStyleWithColumns(20)));
  7437. EXPECT_EQ(
  7438. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7439. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7440. EXPECT_EQ("f(\n"
  7441. "#if !TEST\n"
  7442. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7443. "#endif\n"
  7444. " );",
  7445. format("f(\n"
  7446. "#if !TEST\n"
  7447. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7448. "#endif\n"
  7449. ");"));
  7450. EXPECT_EQ("f(\n"
  7451. "\n"
  7452. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7453. format("f(\n"
  7454. "\n"
  7455. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7456. }
  7457. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7458. EXPECT_EQ(
  7459. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7460. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7461. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7462. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7463. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7464. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7465. }
  7466. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7467. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7468. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7469. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7470. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7471. ")x\",\n"
  7472. " a),\n"
  7473. " b);",
  7474. format("fffffffffff(g(R\"x(\n"
  7475. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7476. ")x\", a), b);",
  7477. getGoogleStyleWithColumns(20)));
  7478. EXPECT_EQ("fffffffffff(\n"
  7479. " g(R\"x(qqq\n"
  7480. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7481. ")x\",\n"
  7482. " a),\n"
  7483. " b);",
  7484. format("fffffffffff(g(R\"x(qqq\n"
  7485. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7486. ")x\", a), b);",
  7487. getGoogleStyleWithColumns(20)));
  7488. EXPECT_EQ("fffffffffff(R\"x(\n"
  7489. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7490. ")x\");",
  7491. format("fffffffffff(R\"x(\n"
  7492. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7493. ")x\");",
  7494. getGoogleStyleWithColumns(20)));
  7495. EXPECT_EQ("fffffffffff(R\"x(\n"
  7496. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7497. ")x\" + bbbbbb);",
  7498. format("fffffffffff(R\"x(\n"
  7499. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7500. ")x\" + bbbbbb);",
  7501. getGoogleStyleWithColumns(20)));
  7502. EXPECT_EQ("fffffffffff(\n"
  7503. " R\"x(\n"
  7504. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7505. ")x\" +\n"
  7506. " bbbbbb);",
  7507. format("fffffffffff(\n"
  7508. " R\"x(\n"
  7509. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7510. ")x\" + bbbbbb);",
  7511. getGoogleStyleWithColumns(20)));
  7512. }
  7513. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7514. verifyFormat("string a = \"unterminated;");
  7515. EXPECT_EQ("function(\"unterminated,\n"
  7516. " OtherParameter);",
  7517. format("function( \"unterminated,\n"
  7518. " OtherParameter);"));
  7519. }
  7520. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7521. FormatStyle Style = getLLVMStyle();
  7522. Style.Standard = FormatStyle::LS_Cpp03;
  7523. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7524. format("#define x(_a) printf(\"foo\"_a);", Style));
  7525. }
  7526. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7527. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7528. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7529. " \"ddeeefff\");",
  7530. format("someFunction(\"aaabbbcccdddeeefff\");",
  7531. getLLVMStyleWithColumns(25)));
  7532. EXPECT_EQ("someFunction1234567890(\n"
  7533. " \"aaabbbcccdddeeefff\");",
  7534. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7535. getLLVMStyleWithColumns(26)));
  7536. EXPECT_EQ("someFunction1234567890(\n"
  7537. " \"aaabbbcccdddeeeff\"\n"
  7538. " \"f\");",
  7539. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7540. getLLVMStyleWithColumns(25)));
  7541. EXPECT_EQ("someFunction1234567890(\n"
  7542. " \"aaabbbcccdddeeeff\"\n"
  7543. " \"f\");",
  7544. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7545. getLLVMStyleWithColumns(24)));
  7546. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7547. " \"ddde \"\n"
  7548. " \"efff\");",
  7549. format("someFunction(\"aaabbbcc ddde efff\");",
  7550. getLLVMStyleWithColumns(25)));
  7551. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7552. " \"ddeeefff\");",
  7553. format("someFunction(\"aaabbbccc ddeeefff\");",
  7554. getLLVMStyleWithColumns(25)));
  7555. EXPECT_EQ("someFunction1234567890(\n"
  7556. " \"aaabb \"\n"
  7557. " \"cccdddeeefff\");",
  7558. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7559. getLLVMStyleWithColumns(25)));
  7560. EXPECT_EQ("#define A \\\n"
  7561. " string s = \\\n"
  7562. " \"123456789\" \\\n"
  7563. " \"0\"; \\\n"
  7564. " int i;",
  7565. format("#define A string s = \"1234567890\"; int i;",
  7566. getLLVMStyleWithColumns(20)));
  7567. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7568. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7569. " \"dddeeeff\"\n"
  7570. " \"f\");",
  7571. format("someFunction(\"aaabbbcc dddeeefff\");",
  7572. getLLVMStyleWithColumns(25)));
  7573. }
  7574. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7575. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7576. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7577. EXPECT_EQ("\"test\"\n"
  7578. "\"\\n\"",
  7579. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7580. EXPECT_EQ("\"tes\\\\\"\n"
  7581. "\"n\"",
  7582. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7583. EXPECT_EQ("\"\\\\\\\\\"\n"
  7584. "\"\\n\"",
  7585. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7586. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7587. EXPECT_EQ("\"\\uff01\"\n"
  7588. "\"test\"",
  7589. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7590. EXPECT_EQ("\"\\Uff01ff02\"",
  7591. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7592. EXPECT_EQ("\"\\x000000000001\"\n"
  7593. "\"next\"",
  7594. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7595. EXPECT_EQ("\"\\x000000000001next\"",
  7596. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7597. EXPECT_EQ("\"\\x000000000001\"",
  7598. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7599. EXPECT_EQ("\"test\"\n"
  7600. "\"\\000000\"\n"
  7601. "\"000001\"",
  7602. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7603. EXPECT_EQ("\"test\\000\"\n"
  7604. "\"00000000\"\n"
  7605. "\"1\"",
  7606. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7607. }
  7608. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7609. verifyFormat("void f() {\n"
  7610. " return g() {}\n"
  7611. " void h() {}");
  7612. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7613. "g();\n"
  7614. "}");
  7615. }
  7616. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7617. verifyFormat(
  7618. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7619. }
  7620. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7621. verifyFormat("class X {\n"
  7622. " void f() {\n"
  7623. " }\n"
  7624. "};",
  7625. getLLVMStyleWithColumns(12));
  7626. }
  7627. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7628. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7629. EightIndent.IndentWidth = 8;
  7630. EightIndent.ContinuationIndentWidth = 8;
  7631. verifyFormat("void f() {\n"
  7632. " someFunction();\n"
  7633. " if (true) {\n"
  7634. " f();\n"
  7635. " }\n"
  7636. "}",
  7637. EightIndent);
  7638. verifyFormat("class X {\n"
  7639. " void f() {\n"
  7640. " }\n"
  7641. "};",
  7642. EightIndent);
  7643. verifyFormat("int x[] = {\n"
  7644. " call(),\n"
  7645. " call()};",
  7646. EightIndent);
  7647. }
  7648. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7649. verifyFormat("double\n"
  7650. "f();",
  7651. getLLVMStyleWithColumns(8));
  7652. }
  7653. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7654. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7655. Tab.IndentWidth = 8;
  7656. Tab.UseTab = FormatStyle::UT_Always;
  7657. Tab.AlignEscapedNewlinesLeft = true;
  7658. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7659. " bb)\t\t// w\n"
  7660. "\t;",
  7661. format("if (aaaaaaaa &&// q\n"
  7662. "bb)// w\n"
  7663. ";",
  7664. Tab));
  7665. EXPECT_EQ("if (aaa && bbb) // w\n"
  7666. "\t;",
  7667. format("if(aaa&&bbb)// w\n"
  7668. ";",
  7669. Tab));
  7670. verifyFormat("class X {\n"
  7671. "\tvoid f() {\n"
  7672. "\t\tsomeFunction(parameter1,\n"
  7673. "\t\t\t parameter2);\n"
  7674. "\t}\n"
  7675. "};",
  7676. Tab);
  7677. verifyFormat("#define A \\\n"
  7678. "\tvoid f() { \\\n"
  7679. "\t\tsomeFunction( \\\n"
  7680. "\t\t parameter1, \\\n"
  7681. "\t\t parameter2); \\\n"
  7682. "\t}",
  7683. Tab);
  7684. Tab.TabWidth = 4;
  7685. Tab.IndentWidth = 8;
  7686. verifyFormat("class TabWidth4Indent8 {\n"
  7687. "\t\tvoid f() {\n"
  7688. "\t\t\t\tsomeFunction(parameter1,\n"
  7689. "\t\t\t\t\t\t\t parameter2);\n"
  7690. "\t\t}\n"
  7691. "};",
  7692. Tab);
  7693. Tab.TabWidth = 4;
  7694. Tab.IndentWidth = 4;
  7695. verifyFormat("class TabWidth4Indent4 {\n"
  7696. "\tvoid f() {\n"
  7697. "\t\tsomeFunction(parameter1,\n"
  7698. "\t\t\t\t\t parameter2);\n"
  7699. "\t}\n"
  7700. "};",
  7701. Tab);
  7702. Tab.TabWidth = 8;
  7703. Tab.IndentWidth = 4;
  7704. verifyFormat("class TabWidth8Indent4 {\n"
  7705. " void f() {\n"
  7706. "\tsomeFunction(parameter1,\n"
  7707. "\t\t parameter2);\n"
  7708. " }\n"
  7709. "};",
  7710. Tab);
  7711. Tab.TabWidth = 8;
  7712. Tab.IndentWidth = 8;
  7713. EXPECT_EQ("/*\n"
  7714. "\t a\t\tcomment\n"
  7715. "\t in multiple lines\n"
  7716. " */",
  7717. format(" /*\t \t \n"
  7718. " \t \t a\t\tcomment\t \t\n"
  7719. " \t \t in multiple lines\t\n"
  7720. " \t */",
  7721. Tab));
  7722. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7723. verifyFormat("{\n"
  7724. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7725. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7726. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7727. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7728. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7729. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7730. "};",
  7731. Tab);
  7732. verifyFormat("enum AA {\n"
  7733. "\ta1, // Force multiple lines\n"
  7734. "\ta2,\n"
  7735. "\ta3\n"
  7736. "};",
  7737. Tab);
  7738. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7739. " bb) // w\n"
  7740. "\t;",
  7741. format("if (aaaaaaaa &&// q\n"
  7742. "bb)// w\n"
  7743. ";",
  7744. Tab));
  7745. verifyFormat("class X {\n"
  7746. "\tvoid f() {\n"
  7747. "\t\tsomeFunction(parameter1,\n"
  7748. "\t\t parameter2);\n"
  7749. "\t}\n"
  7750. "};",
  7751. Tab);
  7752. verifyFormat("{\n"
  7753. "\tQ(\n"
  7754. "\t {\n"
  7755. "\t\t int a;\n"
  7756. "\t\t someFunction(aaaaaaaa,\n"
  7757. "\t\t bbbbbbb);\n"
  7758. "\t },\n"
  7759. "\t p);\n"
  7760. "}",
  7761. Tab);
  7762. EXPECT_EQ("{\n"
  7763. "\t/* aaaa\n"
  7764. "\t bbbb */\n"
  7765. "}",
  7766. format("{\n"
  7767. "/* aaaa\n"
  7768. " bbbb */\n"
  7769. "}",
  7770. Tab));
  7771. EXPECT_EQ("{\n"
  7772. "\t/*\n"
  7773. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7774. "\t bbbbbbbbbbbbb\n"
  7775. "\t*/\n"
  7776. "}",
  7777. format("{\n"
  7778. "/*\n"
  7779. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7780. "*/\n"
  7781. "}",
  7782. Tab));
  7783. EXPECT_EQ("{\n"
  7784. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7785. "\t// bbbbbbbbbbbbb\n"
  7786. "}",
  7787. format("{\n"
  7788. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7789. "}",
  7790. Tab));
  7791. EXPECT_EQ("{\n"
  7792. "\t/*\n"
  7793. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7794. "\t bbbbbbbbbbbbb\n"
  7795. "\t*/\n"
  7796. "}",
  7797. format("{\n"
  7798. "\t/*\n"
  7799. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7800. "\t*/\n"
  7801. "}",
  7802. Tab));
  7803. EXPECT_EQ("{\n"
  7804. "\t/*\n"
  7805. "\n"
  7806. "\t*/\n"
  7807. "}",
  7808. format("{\n"
  7809. "\t/*\n"
  7810. "\n"
  7811. "\t*/\n"
  7812. "}",
  7813. Tab));
  7814. EXPECT_EQ("{\n"
  7815. "\t/*\n"
  7816. " asdf\n"
  7817. "\t*/\n"
  7818. "}",
  7819. format("{\n"
  7820. "\t/*\n"
  7821. " asdf\n"
  7822. "\t*/\n"
  7823. "}",
  7824. Tab));
  7825. Tab.UseTab = FormatStyle::UT_Never;
  7826. EXPECT_EQ("/*\n"
  7827. " a\t\tcomment\n"
  7828. " in multiple lines\n"
  7829. " */",
  7830. format(" /*\t \t \n"
  7831. " \t \t a\t\tcomment\t \t\n"
  7832. " \t \t in multiple lines\t\n"
  7833. " \t */",
  7834. Tab));
  7835. EXPECT_EQ("/* some\n"
  7836. " comment */",
  7837. format(" \t \t /* some\n"
  7838. " \t \t comment */",
  7839. Tab));
  7840. EXPECT_EQ("int a; /* some\n"
  7841. " comment */",
  7842. format(" \t \t int a; /* some\n"
  7843. " \t \t comment */",
  7844. Tab));
  7845. EXPECT_EQ("int a; /* some\n"
  7846. "comment */",
  7847. format(" \t \t int\ta; /* some\n"
  7848. " \t \t comment */",
  7849. Tab));
  7850. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7851. " comment */",
  7852. format(" \t \t f(\"\t\t\"); /* some\n"
  7853. " \t \t comment */",
  7854. Tab));
  7855. EXPECT_EQ("{\n"
  7856. " /*\n"
  7857. " * Comment\n"
  7858. " */\n"
  7859. " int i;\n"
  7860. "}",
  7861. format("{\n"
  7862. "\t/*\n"
  7863. "\t * Comment\n"
  7864. "\t */\n"
  7865. "\t int i;\n"
  7866. "}"));
  7867. }
  7868. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7869. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7870. "q\"; /* some\n"
  7871. " comment */",
  7872. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7873. "q\"; /* some\n"
  7874. " comment */",
  7875. getLLVMStyle()));
  7876. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7877. "/* some\n"
  7878. " comment */",
  7879. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7880. " /* some\n"
  7881. " comment */",
  7882. getLLVMStyle()));
  7883. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7884. "qqq\n"
  7885. "/* some\n"
  7886. " comment */",
  7887. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7888. "qqq\n"
  7889. " /* some\n"
  7890. " comment */",
  7891. getLLVMStyle()));
  7892. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7893. "wwww; /* some\n"
  7894. " comment */",
  7895. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7896. "wwww; /* some\n"
  7897. " comment */",
  7898. getLLVMStyle()));
  7899. }
  7900. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7901. FormatStyle NoSpace = getLLVMStyle();
  7902. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7903. verifyFormat("while(true)\n"
  7904. " continue;",
  7905. NoSpace);
  7906. verifyFormat("for(;;)\n"
  7907. " continue;",
  7908. NoSpace);
  7909. verifyFormat("if(true)\n"
  7910. " f();\n"
  7911. "else if(true)\n"
  7912. " f();",
  7913. NoSpace);
  7914. verifyFormat("do {\n"
  7915. " do_something();\n"
  7916. "} while(something());",
  7917. NoSpace);
  7918. verifyFormat("switch(x) {\n"
  7919. "default:\n"
  7920. " break;\n"
  7921. "}",
  7922. NoSpace);
  7923. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7924. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7925. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7926. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7927. verifyFormat("alignas(128) char a[128];", NoSpace);
  7928. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7929. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7930. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7931. verifyFormat("typedef void (*cb)(int);", NoSpace);
  7932. verifyFormat("T A::operator()();", NoSpace);
  7933. verifyFormat("X A::operator++(T);", NoSpace);
  7934. FormatStyle Space = getLLVMStyle();
  7935. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7936. verifyFormat("int f ();", Space);
  7937. verifyFormat("void f (int a, T b) {\n"
  7938. " while (true)\n"
  7939. " continue;\n"
  7940. "}",
  7941. Space);
  7942. verifyFormat("if (true)\n"
  7943. " f ();\n"
  7944. "else if (true)\n"
  7945. " f ();",
  7946. Space);
  7947. verifyFormat("do {\n"
  7948. " do_something ();\n"
  7949. "} while (something ());",
  7950. Space);
  7951. verifyFormat("switch (x) {\n"
  7952. "default:\n"
  7953. " break;\n"
  7954. "}",
  7955. Space);
  7956. verifyFormat("A::A () : a (1) {}", Space);
  7957. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7958. verifyFormat("*(&a + 1);\n"
  7959. "&((&a)[1]);\n"
  7960. "a[(b + c) * d];\n"
  7961. "(((a + 1) * 2) + 3) * 4;",
  7962. Space);
  7963. verifyFormat("#define A(x) x", Space);
  7964. verifyFormat("#define A (x) x", Space);
  7965. verifyFormat("#if defined(x)\n"
  7966. "#endif",
  7967. Space);
  7968. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7969. verifyFormat("size_t x = sizeof (x);", Space);
  7970. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7971. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7972. verifyFormat("alignas (128) char a[128];", Space);
  7973. verifyFormat("size_t x = alignof (MyType);", Space);
  7974. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7975. verifyFormat("int f () throw (Deprecated);", Space);
  7976. verifyFormat("typedef void (*cb) (int);", Space);
  7977. verifyFormat("T A::operator() ();", Space);
  7978. verifyFormat("X A::operator++ (T);", Space);
  7979. }
  7980. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7981. FormatStyle Spaces = getLLVMStyle();
  7982. Spaces.SpacesInParentheses = true;
  7983. verifyFormat("call( x, y, z );", Spaces);
  7984. verifyFormat("call();", Spaces);
  7985. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  7986. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  7987. Spaces);
  7988. verifyFormat("while ( (bool)1 )\n"
  7989. " continue;",
  7990. Spaces);
  7991. verifyFormat("for ( ;; )\n"
  7992. " continue;",
  7993. Spaces);
  7994. verifyFormat("if ( true )\n"
  7995. " f();\n"
  7996. "else if ( true )\n"
  7997. " f();",
  7998. Spaces);
  7999. verifyFormat("do {\n"
  8000. " do_something( (int)i );\n"
  8001. "} while ( something() );",
  8002. Spaces);
  8003. verifyFormat("switch ( x ) {\n"
  8004. "default:\n"
  8005. " break;\n"
  8006. "}",
  8007. Spaces);
  8008. Spaces.SpacesInParentheses = false;
  8009. Spaces.SpacesInCStyleCastParentheses = true;
  8010. verifyFormat("Type *A = ( Type * )P;", Spaces);
  8011. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  8012. verifyFormat("x = ( int32 )y;", Spaces);
  8013. verifyFormat("int a = ( int )(2.0f);", Spaces);
  8014. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  8015. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  8016. verifyFormat("#define x (( int )-1)", Spaces);
  8017. // Run the first set of tests again with:
  8018. Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
  8019. Spaces.SpacesInCStyleCastParentheses = true;
  8020. verifyFormat("call(x, y, z);", Spaces);
  8021. verifyFormat("call( );", Spaces);
  8022. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8023. verifyFormat("while (( bool )1)\n"
  8024. " continue;",
  8025. Spaces);
  8026. verifyFormat("for (;;)\n"
  8027. " continue;",
  8028. Spaces);
  8029. verifyFormat("if (true)\n"
  8030. " f( );\n"
  8031. "else if (true)\n"
  8032. " f( );",
  8033. Spaces);
  8034. verifyFormat("do {\n"
  8035. " do_something(( int )i);\n"
  8036. "} while (something( ));",
  8037. Spaces);
  8038. verifyFormat("switch (x) {\n"
  8039. "default:\n"
  8040. " break;\n"
  8041. "}",
  8042. Spaces);
  8043. // Run the first set of tests again with:
  8044. Spaces.SpaceAfterCStyleCast = true;
  8045. verifyFormat("call(x, y, z);", Spaces);
  8046. verifyFormat("call( );", Spaces);
  8047. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8048. verifyFormat("while (( bool ) 1)\n"
  8049. " continue;",
  8050. Spaces);
  8051. verifyFormat("for (;;)\n"
  8052. " continue;",
  8053. Spaces);
  8054. verifyFormat("if (true)\n"
  8055. " f( );\n"
  8056. "else if (true)\n"
  8057. " f( );",
  8058. Spaces);
  8059. verifyFormat("do {\n"
  8060. " do_something(( int ) i);\n"
  8061. "} while (something( ));",
  8062. Spaces);
  8063. verifyFormat("switch (x) {\n"
  8064. "default:\n"
  8065. " break;\n"
  8066. "}",
  8067. Spaces);
  8068. // Run subset of tests again with:
  8069. Spaces.SpacesInCStyleCastParentheses = false;
  8070. Spaces.SpaceAfterCStyleCast = true;
  8071. verifyFormat("while ((bool) 1)\n"
  8072. " continue;",
  8073. Spaces);
  8074. verifyFormat("do {\n"
  8075. " do_something((int) i);\n"
  8076. "} while (something( ));",
  8077. Spaces);
  8078. }
  8079. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  8080. verifyFormat("int a[5];");
  8081. verifyFormat("a[3] += 42;");
  8082. FormatStyle Spaces = getLLVMStyle();
  8083. Spaces.SpacesInSquareBrackets = true;
  8084. // Lambdas unchanged.
  8085. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  8086. verifyFormat("return [i, args...] {};", Spaces);
  8087. // Not lambdas.
  8088. verifyFormat("int a[ 5 ];", Spaces);
  8089. verifyFormat("a[ 3 ] += 42;", Spaces);
  8090. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  8091. verifyFormat("double &operator[](int i) { return 0; }\n"
  8092. "int i;",
  8093. Spaces);
  8094. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  8095. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  8096. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  8097. }
  8098. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  8099. verifyFormat("int a = 5;");
  8100. verifyFormat("a += 42;");
  8101. verifyFormat("a or_eq 8;");
  8102. FormatStyle Spaces = getLLVMStyle();
  8103. Spaces.SpaceBeforeAssignmentOperators = false;
  8104. verifyFormat("int a= 5;", Spaces);
  8105. verifyFormat("a+= 42;", Spaces);
  8106. verifyFormat("a or_eq 8;", Spaces);
  8107. }
  8108. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  8109. FormatStyle Alignment = getLLVMStyle();
  8110. Alignment.AlignConsecutiveAssignments = false;
  8111. verifyFormat("int a = 5;\n"
  8112. "int oneTwoThree = 123;",
  8113. Alignment);
  8114. verifyFormat("int a = 5;\n"
  8115. "int oneTwoThree = 123;",
  8116. Alignment);
  8117. Alignment.AlignConsecutiveAssignments = true;
  8118. verifyFormat("int a = 5;\n"
  8119. "int oneTwoThree = 123;",
  8120. Alignment);
  8121. verifyFormat("int a = method();\n"
  8122. "int oneTwoThree = 133;",
  8123. Alignment);
  8124. verifyFormat("a &= 5;\n"
  8125. "bcd *= 5;\n"
  8126. "ghtyf += 5;\n"
  8127. "dvfvdb -= 5;\n"
  8128. "a /= 5;\n"
  8129. "vdsvsv %= 5;\n"
  8130. "sfdbddfbdfbb ^= 5;\n"
  8131. "dvsdsv |= 5;\n"
  8132. "int dsvvdvsdvvv = 123;",
  8133. Alignment);
  8134. verifyFormat("int i = 1, j = 10;\n"
  8135. "something = 2000;",
  8136. Alignment);
  8137. verifyFormat("something = 2000;\n"
  8138. "int i = 1, j = 10;\n",
  8139. Alignment);
  8140. verifyFormat("something = 2000;\n"
  8141. "another = 911;\n"
  8142. "int i = 1, j = 10;\n"
  8143. "oneMore = 1;\n"
  8144. "i = 2;",
  8145. Alignment);
  8146. verifyFormat("int a = 5;\n"
  8147. "int one = 1;\n"
  8148. "method();\n"
  8149. "int oneTwoThree = 123;\n"
  8150. "int oneTwo = 12;",
  8151. Alignment);
  8152. verifyFormat("int oneTwoThree = 123;\n"
  8153. "int oneTwo = 12;\n"
  8154. "method();\n",
  8155. Alignment);
  8156. verifyFormat("int oneTwoThree = 123; // comment\n"
  8157. "int oneTwo = 12; // comment",
  8158. Alignment);
  8159. EXPECT_EQ("int a = 5;\n"
  8160. "\n"
  8161. "int oneTwoThree = 123;",
  8162. format("int a = 5;\n"
  8163. "\n"
  8164. "int oneTwoThree= 123;",
  8165. Alignment));
  8166. EXPECT_EQ("int a = 5;\n"
  8167. "int one = 1;\n"
  8168. "\n"
  8169. "int oneTwoThree = 123;",
  8170. format("int a = 5;\n"
  8171. "int one = 1;\n"
  8172. "\n"
  8173. "int oneTwoThree = 123;",
  8174. Alignment));
  8175. EXPECT_EQ("int a = 5;\n"
  8176. "int one = 1;\n"
  8177. "\n"
  8178. "int oneTwoThree = 123;\n"
  8179. "int oneTwo = 12;",
  8180. format("int a = 5;\n"
  8181. "int one = 1;\n"
  8182. "\n"
  8183. "int oneTwoThree = 123;\n"
  8184. "int oneTwo = 12;",
  8185. Alignment));
  8186. Alignment.AlignEscapedNewlinesLeft = true;
  8187. verifyFormat("#define A \\\n"
  8188. " int aaaa = 12; \\\n"
  8189. " int b = 23; \\\n"
  8190. " int ccc = 234; \\\n"
  8191. " int dddddddddd = 2345;",
  8192. Alignment);
  8193. Alignment.AlignEscapedNewlinesLeft = false;
  8194. verifyFormat("#define A "
  8195. " \\\n"
  8196. " int aaaa = 12; "
  8197. " \\\n"
  8198. " int b = 23; "
  8199. " \\\n"
  8200. " int ccc = 234; "
  8201. " \\\n"
  8202. " int dddddddddd = 2345;",
  8203. Alignment);
  8204. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8205. "k = 4, int l = 5,\n"
  8206. " int m = 6) {\n"
  8207. " int j = 10;\n"
  8208. " otherThing = 1;\n"
  8209. "}",
  8210. Alignment);
  8211. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8212. " int i = 1;\n"
  8213. " int j = 2;\n"
  8214. " int big = 10000;\n"
  8215. "}",
  8216. Alignment);
  8217. verifyFormat("class C {\n"
  8218. "public:\n"
  8219. " int i = 1;\n"
  8220. " virtual void f() = 0;\n"
  8221. "};",
  8222. Alignment);
  8223. verifyFormat("int i = 1;\n"
  8224. "if (SomeType t = getSomething()) {\n"
  8225. "}\n"
  8226. "int j = 2;\n"
  8227. "int big = 10000;",
  8228. Alignment);
  8229. verifyFormat("int j = 7;\n"
  8230. "for (int k = 0; k < N; ++k) {\n"
  8231. "}\n"
  8232. "int j = 2;\n"
  8233. "int big = 10000;\n"
  8234. "}",
  8235. Alignment);
  8236. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8237. verifyFormat("int i = 1;\n"
  8238. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8239. " = someLooooooooooooooooongFunction();\n"
  8240. "int j = 2;",
  8241. Alignment);
  8242. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8243. verifyFormat("int i = 1;\n"
  8244. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8245. " someLooooooooooooooooongFunction();\n"
  8246. "int j = 2;",
  8247. Alignment);
  8248. verifyFormat("auto lambda = []() {\n"
  8249. " auto i = 0;\n"
  8250. " return 0;\n"
  8251. "};\n"
  8252. "int i = 0;\n"
  8253. "auto v = type{\n"
  8254. " i = 1, //\n"
  8255. " (i = 2), //\n"
  8256. " i = 3 //\n"
  8257. "};",
  8258. Alignment);
  8259. // FIXME: Should align all three assignments
  8260. verifyFormat(
  8261. "int i = 1;\n"
  8262. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8263. " loooooooooooooooooooooongParameterB);\n"
  8264. "int j = 2;",
  8265. Alignment);
  8266. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  8267. " typename B = very_long_type_name_1,\n"
  8268. " typename T_2 = very_long_type_name_2>\n"
  8269. "auto foo() {}\n",
  8270. Alignment);
  8271. verifyFormat("int a, b = 1;\n"
  8272. "int c = 2;\n"
  8273. "int dd = 3;\n",
  8274. Alignment);
  8275. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8276. "float b[1][] = {{3.f}};\n",
  8277. Alignment);
  8278. }
  8279. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8280. FormatStyle Alignment = getLLVMStyle();
  8281. Alignment.AlignConsecutiveDeclarations = false;
  8282. verifyFormat("float const a = 5;\n"
  8283. "int oneTwoThree = 123;",
  8284. Alignment);
  8285. verifyFormat("int a = 5;\n"
  8286. "float const oneTwoThree = 123;",
  8287. Alignment);
  8288. Alignment.AlignConsecutiveDeclarations = true;
  8289. verifyFormat("float const a = 5;\n"
  8290. "int oneTwoThree = 123;",
  8291. Alignment);
  8292. verifyFormat("int a = method();\n"
  8293. "float const oneTwoThree = 133;",
  8294. Alignment);
  8295. verifyFormat("int i = 1, j = 10;\n"
  8296. "something = 2000;",
  8297. Alignment);
  8298. verifyFormat("something = 2000;\n"
  8299. "int i = 1, j = 10;\n",
  8300. Alignment);
  8301. verifyFormat("float something = 2000;\n"
  8302. "double another = 911;\n"
  8303. "int i = 1, j = 10;\n"
  8304. "const int *oneMore = 1;\n"
  8305. "unsigned i = 2;",
  8306. Alignment);
  8307. verifyFormat("float a = 5;\n"
  8308. "int one = 1;\n"
  8309. "method();\n"
  8310. "const double oneTwoThree = 123;\n"
  8311. "const unsigned int oneTwo = 12;",
  8312. Alignment);
  8313. verifyFormat("int oneTwoThree{0}; // comment\n"
  8314. "unsigned oneTwo; // comment",
  8315. Alignment);
  8316. EXPECT_EQ("float const a = 5;\n"
  8317. "\n"
  8318. "int oneTwoThree = 123;",
  8319. format("float const a = 5;\n"
  8320. "\n"
  8321. "int oneTwoThree= 123;",
  8322. Alignment));
  8323. EXPECT_EQ("float a = 5;\n"
  8324. "int one = 1;\n"
  8325. "\n"
  8326. "unsigned oneTwoThree = 123;",
  8327. format("float a = 5;\n"
  8328. "int one = 1;\n"
  8329. "\n"
  8330. "unsigned oneTwoThree = 123;",
  8331. Alignment));
  8332. EXPECT_EQ("float a = 5;\n"
  8333. "int one = 1;\n"
  8334. "\n"
  8335. "unsigned oneTwoThree = 123;\n"
  8336. "int oneTwo = 12;",
  8337. format("float a = 5;\n"
  8338. "int one = 1;\n"
  8339. "\n"
  8340. "unsigned oneTwoThree = 123;\n"
  8341. "int oneTwo = 12;",
  8342. Alignment));
  8343. Alignment.AlignConsecutiveAssignments = true;
  8344. verifyFormat("float something = 2000;\n"
  8345. "double another = 911;\n"
  8346. "int i = 1, j = 10;\n"
  8347. "const int *oneMore = 1;\n"
  8348. "unsigned i = 2;",
  8349. Alignment);
  8350. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8351. "unsigned oneTwo = 0; // comment",
  8352. Alignment);
  8353. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8354. " int const i = 1;\n"
  8355. " int * j = 2;\n"
  8356. " int big = 10000;\n"
  8357. "\n"
  8358. " unsigned oneTwoThree = 123;\n"
  8359. " int oneTwo = 12;\n"
  8360. " method();\n"
  8361. " float k = 2;\n"
  8362. " int ll = 10000;\n"
  8363. "}",
  8364. format("void SomeFunction(int parameter= 0) {\n"
  8365. " int const i= 1;\n"
  8366. " int *j=2;\n"
  8367. " int big = 10000;\n"
  8368. "\n"
  8369. "unsigned oneTwoThree =123;\n"
  8370. "int oneTwo = 12;\n"
  8371. " method();\n"
  8372. "float k= 2;\n"
  8373. "int ll=10000;\n"
  8374. "}",
  8375. Alignment));
  8376. Alignment.AlignConsecutiveAssignments = false;
  8377. Alignment.AlignEscapedNewlinesLeft = true;
  8378. verifyFormat("#define A \\\n"
  8379. " int aaaa = 12; \\\n"
  8380. " float b = 23; \\\n"
  8381. " const int ccc = 234; \\\n"
  8382. " unsigned dddddddddd = 2345;",
  8383. Alignment);
  8384. Alignment.AlignEscapedNewlinesLeft = false;
  8385. Alignment.ColumnLimit = 30;
  8386. verifyFormat("#define A \\\n"
  8387. " int aaaa = 12; \\\n"
  8388. " float b = 23; \\\n"
  8389. " const int ccc = 234; \\\n"
  8390. " int dddddddddd = 2345;",
  8391. Alignment);
  8392. Alignment.ColumnLimit = 80;
  8393. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8394. "k = 4, int l = 5,\n"
  8395. " int m = 6) {\n"
  8396. " const int j = 10;\n"
  8397. " otherThing = 1;\n"
  8398. "}",
  8399. Alignment);
  8400. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8401. " int const i = 1;\n"
  8402. " int * j = 2;\n"
  8403. " int big = 10000;\n"
  8404. "}",
  8405. Alignment);
  8406. verifyFormat("class C {\n"
  8407. "public:\n"
  8408. " int i = 1;\n"
  8409. " virtual void f() = 0;\n"
  8410. "};",
  8411. Alignment);
  8412. verifyFormat("float i = 1;\n"
  8413. "if (SomeType t = getSomething()) {\n"
  8414. "}\n"
  8415. "const unsigned j = 2;\n"
  8416. "int big = 10000;",
  8417. Alignment);
  8418. verifyFormat("float j = 7;\n"
  8419. "for (int k = 0; k < N; ++k) {\n"
  8420. "}\n"
  8421. "unsigned j = 2;\n"
  8422. "int big = 10000;\n"
  8423. "}",
  8424. Alignment);
  8425. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8426. verifyFormat("float i = 1;\n"
  8427. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8428. " = someLooooooooooooooooongFunction();\n"
  8429. "int j = 2;",
  8430. Alignment);
  8431. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8432. verifyFormat("int i = 1;\n"
  8433. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8434. " someLooooooooooooooooongFunction();\n"
  8435. "int j = 2;",
  8436. Alignment);
  8437. Alignment.AlignConsecutiveAssignments = true;
  8438. verifyFormat("auto lambda = []() {\n"
  8439. " auto ii = 0;\n"
  8440. " float j = 0;\n"
  8441. " return 0;\n"
  8442. "};\n"
  8443. "int i = 0;\n"
  8444. "float i2 = 0;\n"
  8445. "auto v = type{\n"
  8446. " i = 1, //\n"
  8447. " (i = 2), //\n"
  8448. " i = 3 //\n"
  8449. "};",
  8450. Alignment);
  8451. Alignment.AlignConsecutiveAssignments = false;
  8452. // FIXME: Should align all three declarations
  8453. verifyFormat(
  8454. "int i = 1;\n"
  8455. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8456. " loooooooooooooooooooooongParameterB);\n"
  8457. "int j = 2;",
  8458. Alignment);
  8459. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8460. // We expect declarations and assignments to align, as long as it doesn't
  8461. // exceed the column limit, starting a new alignemnt sequence whenever it
  8462. // happens.
  8463. Alignment.AlignConsecutiveAssignments = true;
  8464. Alignment.ColumnLimit = 30;
  8465. verifyFormat("float ii = 1;\n"
  8466. "unsigned j = 2;\n"
  8467. "int someVerylongVariable = 1;\n"
  8468. "AnotherLongType ll = 123456;\n"
  8469. "VeryVeryLongType k = 2;\n"
  8470. "int myvar = 1;",
  8471. Alignment);
  8472. Alignment.ColumnLimit = 80;
  8473. Alignment.AlignConsecutiveAssignments = false;
  8474. verifyFormat(
  8475. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  8476. " typename LongType, typename B>\n"
  8477. "auto foo() {}\n",
  8478. Alignment);
  8479. verifyFormat("float a, b = 1;\n"
  8480. "int c = 2;\n"
  8481. "int dd = 3;\n",
  8482. Alignment);
  8483. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8484. "float b[1][] = {{3.f}};\n",
  8485. Alignment);
  8486. Alignment.AlignConsecutiveAssignments = true;
  8487. verifyFormat("float a, b = 1;\n"
  8488. "int c = 2;\n"
  8489. "int dd = 3;\n",
  8490. Alignment);
  8491. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8492. "float b[1][] = {{3.f}};\n",
  8493. Alignment);
  8494. Alignment.AlignConsecutiveAssignments = false;
  8495. Alignment.ColumnLimit = 30;
  8496. Alignment.BinPackParameters = false;
  8497. verifyFormat("void foo(float a,\n"
  8498. " float b,\n"
  8499. " int c,\n"
  8500. " uint32_t *d) {\n"
  8501. " int * e = 0;\n"
  8502. " float f = 0;\n"
  8503. " double g = 0;\n"
  8504. "}\n"
  8505. "void bar(ino_t a,\n"
  8506. " int b,\n"
  8507. " uint32_t *c,\n"
  8508. " bool d) {}\n",
  8509. Alignment);
  8510. Alignment.BinPackParameters = true;
  8511. Alignment.ColumnLimit = 80;
  8512. }
  8513. TEST_F(FormatTest, LinuxBraceBreaking) {
  8514. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8515. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8516. verifyFormat("namespace a\n"
  8517. "{\n"
  8518. "class A\n"
  8519. "{\n"
  8520. " void f()\n"
  8521. " {\n"
  8522. " if (true) {\n"
  8523. " a();\n"
  8524. " b();\n"
  8525. " } else {\n"
  8526. " a();\n"
  8527. " }\n"
  8528. " }\n"
  8529. " void g() { return; }\n"
  8530. "};\n"
  8531. "struct B {\n"
  8532. " int x;\n"
  8533. "};\n"
  8534. "}\n",
  8535. LinuxBraceStyle);
  8536. verifyFormat("enum X {\n"
  8537. " Y = 0,\n"
  8538. "}\n",
  8539. LinuxBraceStyle);
  8540. verifyFormat("struct S {\n"
  8541. " int Type;\n"
  8542. " union {\n"
  8543. " int x;\n"
  8544. " double y;\n"
  8545. " } Value;\n"
  8546. " class C\n"
  8547. " {\n"
  8548. " MyFavoriteType Value;\n"
  8549. " } Class;\n"
  8550. "}\n",
  8551. LinuxBraceStyle);
  8552. }
  8553. TEST_F(FormatTest, MozillaBraceBreaking) {
  8554. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8555. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8556. verifyFormat("namespace a {\n"
  8557. "class A\n"
  8558. "{\n"
  8559. " void f()\n"
  8560. " {\n"
  8561. " if (true) {\n"
  8562. " a();\n"
  8563. " b();\n"
  8564. " }\n"
  8565. " }\n"
  8566. " void g() { return; }\n"
  8567. "};\n"
  8568. "enum E\n"
  8569. "{\n"
  8570. " A,\n"
  8571. " // foo\n"
  8572. " B,\n"
  8573. " C\n"
  8574. "};\n"
  8575. "struct B\n"
  8576. "{\n"
  8577. " int x;\n"
  8578. "};\n"
  8579. "}\n",
  8580. MozillaBraceStyle);
  8581. verifyFormat("struct S\n"
  8582. "{\n"
  8583. " int Type;\n"
  8584. " union\n"
  8585. " {\n"
  8586. " int x;\n"
  8587. " double y;\n"
  8588. " } Value;\n"
  8589. " class C\n"
  8590. " {\n"
  8591. " MyFavoriteType Value;\n"
  8592. " } Class;\n"
  8593. "}\n",
  8594. MozillaBraceStyle);
  8595. }
  8596. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8597. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8598. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8599. verifyFormat("namespace a {\n"
  8600. "class A {\n"
  8601. " void f()\n"
  8602. " {\n"
  8603. " if (true) {\n"
  8604. " a();\n"
  8605. " b();\n"
  8606. " }\n"
  8607. " }\n"
  8608. " void g() { return; }\n"
  8609. "};\n"
  8610. "struct B {\n"
  8611. " int x;\n"
  8612. "};\n"
  8613. "}\n",
  8614. StroustrupBraceStyle);
  8615. verifyFormat("void foo()\n"
  8616. "{\n"
  8617. " if (a) {\n"
  8618. " a();\n"
  8619. " }\n"
  8620. " else {\n"
  8621. " b();\n"
  8622. " }\n"
  8623. "}\n",
  8624. StroustrupBraceStyle);
  8625. verifyFormat("#ifdef _DEBUG\n"
  8626. "int foo(int i = 0)\n"
  8627. "#else\n"
  8628. "int foo(int i = 5)\n"
  8629. "#endif\n"
  8630. "{\n"
  8631. " return i;\n"
  8632. "}",
  8633. StroustrupBraceStyle);
  8634. verifyFormat("void foo() {}\n"
  8635. "void bar()\n"
  8636. "#ifdef _DEBUG\n"
  8637. "{\n"
  8638. " foo();\n"
  8639. "}\n"
  8640. "#else\n"
  8641. "{\n"
  8642. "}\n"
  8643. "#endif",
  8644. StroustrupBraceStyle);
  8645. verifyFormat("void foobar() { int i = 5; }\n"
  8646. "#ifdef _DEBUG\n"
  8647. "void bar() {}\n"
  8648. "#else\n"
  8649. "void bar() { foobar(); }\n"
  8650. "#endif",
  8651. StroustrupBraceStyle);
  8652. }
  8653. TEST_F(FormatTest, AllmanBraceBreaking) {
  8654. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8655. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8656. verifyFormat("namespace a\n"
  8657. "{\n"
  8658. "class A\n"
  8659. "{\n"
  8660. " void f()\n"
  8661. " {\n"
  8662. " if (true)\n"
  8663. " {\n"
  8664. " a();\n"
  8665. " b();\n"
  8666. " }\n"
  8667. " }\n"
  8668. " void g() { return; }\n"
  8669. "};\n"
  8670. "struct B\n"
  8671. "{\n"
  8672. " int x;\n"
  8673. "};\n"
  8674. "}",
  8675. AllmanBraceStyle);
  8676. verifyFormat("void f()\n"
  8677. "{\n"
  8678. " if (true)\n"
  8679. " {\n"
  8680. " a();\n"
  8681. " }\n"
  8682. " else if (false)\n"
  8683. " {\n"
  8684. " b();\n"
  8685. " }\n"
  8686. " else\n"
  8687. " {\n"
  8688. " c();\n"
  8689. " }\n"
  8690. "}\n",
  8691. AllmanBraceStyle);
  8692. verifyFormat("void f()\n"
  8693. "{\n"
  8694. " for (int i = 0; i < 10; ++i)\n"
  8695. " {\n"
  8696. " a();\n"
  8697. " }\n"
  8698. " while (false)\n"
  8699. " {\n"
  8700. " b();\n"
  8701. " }\n"
  8702. " do\n"
  8703. " {\n"
  8704. " c();\n"
  8705. " } while (false)\n"
  8706. "}\n",
  8707. AllmanBraceStyle);
  8708. verifyFormat("void f(int a)\n"
  8709. "{\n"
  8710. " switch (a)\n"
  8711. " {\n"
  8712. " case 0:\n"
  8713. " break;\n"
  8714. " case 1:\n"
  8715. " {\n"
  8716. " break;\n"
  8717. " }\n"
  8718. " case 2:\n"
  8719. " {\n"
  8720. " }\n"
  8721. " break;\n"
  8722. " default:\n"
  8723. " break;\n"
  8724. " }\n"
  8725. "}\n",
  8726. AllmanBraceStyle);
  8727. verifyFormat("enum X\n"
  8728. "{\n"
  8729. " Y = 0,\n"
  8730. "}\n",
  8731. AllmanBraceStyle);
  8732. verifyFormat("enum X\n"
  8733. "{\n"
  8734. " Y = 0\n"
  8735. "}\n",
  8736. AllmanBraceStyle);
  8737. verifyFormat("@interface BSApplicationController ()\n"
  8738. "{\n"
  8739. "@private\n"
  8740. " id _extraIvar;\n"
  8741. "}\n"
  8742. "@end\n",
  8743. AllmanBraceStyle);
  8744. verifyFormat("#ifdef _DEBUG\n"
  8745. "int foo(int i = 0)\n"
  8746. "#else\n"
  8747. "int foo(int i = 5)\n"
  8748. "#endif\n"
  8749. "{\n"
  8750. " return i;\n"
  8751. "}",
  8752. AllmanBraceStyle);
  8753. verifyFormat("void foo() {}\n"
  8754. "void bar()\n"
  8755. "#ifdef _DEBUG\n"
  8756. "{\n"
  8757. " foo();\n"
  8758. "}\n"
  8759. "#else\n"
  8760. "{\n"
  8761. "}\n"
  8762. "#endif",
  8763. AllmanBraceStyle);
  8764. verifyFormat("void foobar() { int i = 5; }\n"
  8765. "#ifdef _DEBUG\n"
  8766. "void bar() {}\n"
  8767. "#else\n"
  8768. "void bar() { foobar(); }\n"
  8769. "#endif",
  8770. AllmanBraceStyle);
  8771. // This shouldn't affect ObjC blocks..
  8772. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  8773. " // ...\n"
  8774. " int i;\n"
  8775. "}];",
  8776. AllmanBraceStyle);
  8777. verifyFormat("void (^block)(void) = ^{\n"
  8778. " // ...\n"
  8779. " int i;\n"
  8780. "};",
  8781. AllmanBraceStyle);
  8782. // .. or dict literals.
  8783. verifyFormat("void f()\n"
  8784. "{\n"
  8785. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  8786. "}",
  8787. AllmanBraceStyle);
  8788. verifyFormat("int f()\n"
  8789. "{ // comment\n"
  8790. " return 42;\n"
  8791. "}",
  8792. AllmanBraceStyle);
  8793. AllmanBraceStyle.ColumnLimit = 19;
  8794. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  8795. AllmanBraceStyle.ColumnLimit = 18;
  8796. verifyFormat("void f()\n"
  8797. "{\n"
  8798. " int i;\n"
  8799. "}",
  8800. AllmanBraceStyle);
  8801. AllmanBraceStyle.ColumnLimit = 80;
  8802. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  8803. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  8804. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  8805. verifyFormat("void f(bool b)\n"
  8806. "{\n"
  8807. " if (b)\n"
  8808. " {\n"
  8809. " return;\n"
  8810. " }\n"
  8811. "}\n",
  8812. BreakBeforeBraceShortIfs);
  8813. verifyFormat("void f(bool b)\n"
  8814. "{\n"
  8815. " if (b) return;\n"
  8816. "}\n",
  8817. BreakBeforeBraceShortIfs);
  8818. verifyFormat("void f(bool b)\n"
  8819. "{\n"
  8820. " while (b)\n"
  8821. " {\n"
  8822. " return;\n"
  8823. " }\n"
  8824. "}\n",
  8825. BreakBeforeBraceShortIfs);
  8826. }
  8827. TEST_F(FormatTest, GNUBraceBreaking) {
  8828. FormatStyle GNUBraceStyle = getLLVMStyle();
  8829. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  8830. verifyFormat("namespace a\n"
  8831. "{\n"
  8832. "class A\n"
  8833. "{\n"
  8834. " void f()\n"
  8835. " {\n"
  8836. " int a;\n"
  8837. " {\n"
  8838. " int b;\n"
  8839. " }\n"
  8840. " if (true)\n"
  8841. " {\n"
  8842. " a();\n"
  8843. " b();\n"
  8844. " }\n"
  8845. " }\n"
  8846. " void g() { return; }\n"
  8847. "}\n"
  8848. "}",
  8849. GNUBraceStyle);
  8850. verifyFormat("void f()\n"
  8851. "{\n"
  8852. " if (true)\n"
  8853. " {\n"
  8854. " a();\n"
  8855. " }\n"
  8856. " else if (false)\n"
  8857. " {\n"
  8858. " b();\n"
  8859. " }\n"
  8860. " else\n"
  8861. " {\n"
  8862. " c();\n"
  8863. " }\n"
  8864. "}\n",
  8865. GNUBraceStyle);
  8866. verifyFormat("void f()\n"
  8867. "{\n"
  8868. " for (int i = 0; i < 10; ++i)\n"
  8869. " {\n"
  8870. " a();\n"
  8871. " }\n"
  8872. " while (false)\n"
  8873. " {\n"
  8874. " b();\n"
  8875. " }\n"
  8876. " do\n"
  8877. " {\n"
  8878. " c();\n"
  8879. " }\n"
  8880. " while (false);\n"
  8881. "}\n",
  8882. GNUBraceStyle);
  8883. verifyFormat("void f(int a)\n"
  8884. "{\n"
  8885. " switch (a)\n"
  8886. " {\n"
  8887. " case 0:\n"
  8888. " break;\n"
  8889. " case 1:\n"
  8890. " {\n"
  8891. " break;\n"
  8892. " }\n"
  8893. " case 2:\n"
  8894. " {\n"
  8895. " }\n"
  8896. " break;\n"
  8897. " default:\n"
  8898. " break;\n"
  8899. " }\n"
  8900. "}\n",
  8901. GNUBraceStyle);
  8902. verifyFormat("enum X\n"
  8903. "{\n"
  8904. " Y = 0,\n"
  8905. "}\n",
  8906. GNUBraceStyle);
  8907. verifyFormat("@interface BSApplicationController ()\n"
  8908. "{\n"
  8909. "@private\n"
  8910. " id _extraIvar;\n"
  8911. "}\n"
  8912. "@end\n",
  8913. GNUBraceStyle);
  8914. verifyFormat("#ifdef _DEBUG\n"
  8915. "int foo(int i = 0)\n"
  8916. "#else\n"
  8917. "int foo(int i = 5)\n"
  8918. "#endif\n"
  8919. "{\n"
  8920. " return i;\n"
  8921. "}",
  8922. GNUBraceStyle);
  8923. verifyFormat("void foo() {}\n"
  8924. "void bar()\n"
  8925. "#ifdef _DEBUG\n"
  8926. "{\n"
  8927. " foo();\n"
  8928. "}\n"
  8929. "#else\n"
  8930. "{\n"
  8931. "}\n"
  8932. "#endif",
  8933. GNUBraceStyle);
  8934. verifyFormat("void foobar() { int i = 5; }\n"
  8935. "#ifdef _DEBUG\n"
  8936. "void bar() {}\n"
  8937. "#else\n"
  8938. "void bar() { foobar(); }\n"
  8939. "#endif",
  8940. GNUBraceStyle);
  8941. }
  8942. TEST_F(FormatTest, WebKitBraceBreaking) {
  8943. FormatStyle WebKitBraceStyle = getLLVMStyle();
  8944. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  8945. verifyFormat("namespace a {\n"
  8946. "class A {\n"
  8947. " void f()\n"
  8948. " {\n"
  8949. " if (true) {\n"
  8950. " a();\n"
  8951. " b();\n"
  8952. " }\n"
  8953. " }\n"
  8954. " void g() { return; }\n"
  8955. "};\n"
  8956. "enum E {\n"
  8957. " A,\n"
  8958. " // foo\n"
  8959. " B,\n"
  8960. " C\n"
  8961. "};\n"
  8962. "struct B {\n"
  8963. " int x;\n"
  8964. "};\n"
  8965. "}\n",
  8966. WebKitBraceStyle);
  8967. verifyFormat("struct S {\n"
  8968. " int Type;\n"
  8969. " union {\n"
  8970. " int x;\n"
  8971. " double y;\n"
  8972. " } Value;\n"
  8973. " class C {\n"
  8974. " MyFavoriteType Value;\n"
  8975. " } Class;\n"
  8976. "};\n",
  8977. WebKitBraceStyle);
  8978. }
  8979. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  8980. verifyFormat("void f() {\n"
  8981. " try {\n"
  8982. " } catch (const Exception &e) {\n"
  8983. " }\n"
  8984. "}\n",
  8985. getLLVMStyle());
  8986. }
  8987. TEST_F(FormatTest, UnderstandsPragmas) {
  8988. verifyFormat("#pragma omp reduction(| : var)");
  8989. verifyFormat("#pragma omp reduction(+ : var)");
  8990. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  8991. "(including parentheses).",
  8992. format("#pragma mark Any non-hyphenated or hyphenated string "
  8993. "(including parentheses)."));
  8994. }
  8995. TEST_F(FormatTest, UnderstandPragmaOption) {
  8996. verifyFormat("#pragma option -C -A");
  8997. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  8998. }
  8999. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  9000. for (size_t i = 1; i < Styles.size(); ++i) \
  9001. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  9002. << " differs from Style #0"
  9003. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  9004. SmallVector<FormatStyle, 3> Styles;
  9005. Styles.resize(3);
  9006. Styles[0] = getLLVMStyle();
  9007. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  9008. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  9009. EXPECT_ALL_STYLES_EQUAL(Styles);
  9010. Styles[0] = getGoogleStyle();
  9011. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  9012. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  9013. EXPECT_ALL_STYLES_EQUAL(Styles);
  9014. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9015. EXPECT_TRUE(
  9016. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  9017. EXPECT_TRUE(
  9018. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  9019. EXPECT_ALL_STYLES_EQUAL(Styles);
  9020. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  9021. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  9022. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  9023. EXPECT_ALL_STYLES_EQUAL(Styles);
  9024. Styles[0] = getMozillaStyle();
  9025. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  9026. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  9027. EXPECT_ALL_STYLES_EQUAL(Styles);
  9028. Styles[0] = getWebKitStyle();
  9029. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  9030. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  9031. EXPECT_ALL_STYLES_EQUAL(Styles);
  9032. Styles[0] = getGNUStyle();
  9033. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  9034. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  9035. EXPECT_ALL_STYLES_EQUAL(Styles);
  9036. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  9037. }
  9038. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  9039. SmallVector<FormatStyle, 8> Styles;
  9040. Styles.resize(2);
  9041. Styles[0] = getGoogleStyle();
  9042. Styles[1] = getLLVMStyle();
  9043. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9044. EXPECT_ALL_STYLES_EQUAL(Styles);
  9045. Styles.resize(5);
  9046. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9047. Styles[1] = getLLVMStyle();
  9048. Styles[1].Language = FormatStyle::LK_JavaScript;
  9049. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9050. Styles[2] = getLLVMStyle();
  9051. Styles[2].Language = FormatStyle::LK_JavaScript;
  9052. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  9053. "BasedOnStyle: Google",
  9054. &Styles[2])
  9055. .value());
  9056. Styles[3] = getLLVMStyle();
  9057. Styles[3].Language = FormatStyle::LK_JavaScript;
  9058. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  9059. "Language: JavaScript",
  9060. &Styles[3])
  9061. .value());
  9062. Styles[4] = getLLVMStyle();
  9063. Styles[4].Language = FormatStyle::LK_JavaScript;
  9064. EXPECT_EQ(0, parseConfiguration("---\n"
  9065. "BasedOnStyle: LLVM\n"
  9066. "IndentWidth: 123\n"
  9067. "---\n"
  9068. "BasedOnStyle: Google\n"
  9069. "Language: JavaScript",
  9070. &Styles[4])
  9071. .value());
  9072. EXPECT_ALL_STYLES_EQUAL(Styles);
  9073. }
  9074. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  9075. Style.FIELD = false; \
  9076. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  9077. EXPECT_TRUE(Style.FIELD); \
  9078. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  9079. EXPECT_FALSE(Style.FIELD);
  9080. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  9081. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  9082. Style.STRUCT.FIELD = false; \
  9083. EXPECT_EQ(0, \
  9084. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  9085. .value()); \
  9086. EXPECT_TRUE(Style.STRUCT.FIELD); \
  9087. EXPECT_EQ(0, \
  9088. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  9089. .value()); \
  9090. EXPECT_FALSE(Style.STRUCT.FIELD);
  9091. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  9092. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  9093. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  9094. EXPECT_NE(VALUE, Style.FIELD); \
  9095. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  9096. EXPECT_EQ(VALUE, Style.FIELD)
  9097. TEST_F(FormatTest, ParsesConfigurationBools) {
  9098. FormatStyle Style = {};
  9099. Style.Language = FormatStyle::LK_Cpp;
  9100. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  9101. CHECK_PARSE_BOOL(AlignOperands);
  9102. CHECK_PARSE_BOOL(AlignTrailingComments);
  9103. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  9104. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  9105. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  9106. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  9107. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  9108. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  9109. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  9110. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  9111. CHECK_PARSE_BOOL(BinPackArguments);
  9112. CHECK_PARSE_BOOL(BinPackParameters);
  9113. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  9114. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  9115. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  9116. CHECK_PARSE_BOOL(DerivePointerAlignment);
  9117. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  9118. CHECK_PARSE_BOOL(DisableFormat);
  9119. CHECK_PARSE_BOOL(IndentCaseLabels);
  9120. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  9121. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  9122. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  9123. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  9124. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  9125. CHECK_PARSE_BOOL(ReflowComments);
  9126. CHECK_PARSE_BOOL(SortIncludes);
  9127. CHECK_PARSE_BOOL(SpacesInParentheses);
  9128. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  9129. CHECK_PARSE_BOOL(SpacesInAngles);
  9130. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  9131. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  9132. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  9133. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  9134. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  9135. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  9136. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  9137. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  9138. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  9139. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  9140. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  9141. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  9142. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  9143. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  9144. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  9145. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  9146. }
  9147. #undef CHECK_PARSE_BOOL
  9148. TEST_F(FormatTest, ParsesConfiguration) {
  9149. FormatStyle Style = {};
  9150. Style.Language = FormatStyle::LK_Cpp;
  9151. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  9152. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  9153. ConstructorInitializerIndentWidth, 1234u);
  9154. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  9155. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  9156. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  9157. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  9158. PenaltyBreakBeforeFirstCallParameter, 1234u);
  9159. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  9160. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  9161. PenaltyReturnTypeOnItsOwnLine, 1234u);
  9162. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  9163. SpacesBeforeTrailingComments, 1234u);
  9164. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  9165. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  9166. Style.PointerAlignment = FormatStyle::PAS_Middle;
  9167. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  9168. FormatStyle::PAS_Left);
  9169. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  9170. FormatStyle::PAS_Right);
  9171. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  9172. FormatStyle::PAS_Middle);
  9173. // For backward compatibility:
  9174. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  9175. FormatStyle::PAS_Left);
  9176. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  9177. FormatStyle::PAS_Right);
  9178. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  9179. FormatStyle::PAS_Middle);
  9180. Style.Standard = FormatStyle::LS_Auto;
  9181. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  9182. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  9183. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  9184. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  9185. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  9186. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  9187. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  9188. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  9189. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  9190. FormatStyle::BOS_None);
  9191. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  9192. FormatStyle::BOS_All);
  9193. // For backward compatibility:
  9194. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  9195. FormatStyle::BOS_None);
  9196. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  9197. FormatStyle::BOS_All);
  9198. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9199. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  9200. FormatStyle::BAS_Align);
  9201. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  9202. FormatStyle::BAS_DontAlign);
  9203. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  9204. FormatStyle::BAS_AlwaysBreak);
  9205. // For backward compatibility:
  9206. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  9207. FormatStyle::BAS_DontAlign);
  9208. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  9209. FormatStyle::BAS_Align);
  9210. Style.UseTab = FormatStyle::UT_ForIndentation;
  9211. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  9212. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  9213. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  9214. // For backward compatibility:
  9215. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  9216. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  9217. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  9218. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  9219. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9220. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  9221. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  9222. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  9223. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  9224. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  9225. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9226. // For backward compatibility:
  9227. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  9228. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9229. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  9230. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9231. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  9232. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  9233. FormatStyle::SBPO_Never);
  9234. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  9235. FormatStyle::SBPO_Always);
  9236. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  9237. FormatStyle::SBPO_ControlStatements);
  9238. // For backward compatibility:
  9239. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  9240. FormatStyle::SBPO_Never);
  9241. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  9242. FormatStyle::SBPO_ControlStatements);
  9243. Style.ColumnLimit = 123;
  9244. FormatStyle BaseStyle = getLLVMStyle();
  9245. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  9246. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  9247. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  9248. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  9249. FormatStyle::BS_Attach);
  9250. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9251. FormatStyle::BS_Linux);
  9252. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9253. FormatStyle::BS_Mozilla);
  9254. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9255. FormatStyle::BS_Stroustrup);
  9256. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9257. FormatStyle::BS_Allman);
  9258. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9259. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9260. FormatStyle::BS_WebKit);
  9261. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9262. FormatStyle::BS_Custom);
  9263. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  9264. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  9265. FormatStyle::RTBS_None);
  9266. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  9267. FormatStyle::RTBS_All);
  9268. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  9269. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  9270. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  9271. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  9272. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  9273. AlwaysBreakAfterReturnType,
  9274. FormatStyle::RTBS_TopLevelDefinitions);
  9275. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9276. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9277. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9278. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9279. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9280. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9281. AlwaysBreakAfterDefinitionReturnType,
  9282. FormatStyle::DRTBS_TopLevel);
  9283. Style.NamespaceIndentation = FormatStyle::NI_All;
  9284. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9285. FormatStyle::NI_None);
  9286. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9287. FormatStyle::NI_Inner);
  9288. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9289. FormatStyle::NI_All);
  9290. // FIXME: This is required because parsing a configuration simply overwrites
  9291. // the first N elements of the list instead of resetting it.
  9292. Style.ForEachMacros.clear();
  9293. std::vector<std::string> BoostForeach;
  9294. BoostForeach.push_back("BOOST_FOREACH");
  9295. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9296. std::vector<std::string> BoostAndQForeach;
  9297. BoostAndQForeach.push_back("BOOST_FOREACH");
  9298. BoostAndQForeach.push_back("Q_FOREACH");
  9299. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9300. BoostAndQForeach);
  9301. Style.IncludeCategories.clear();
  9302. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9303. {".*", 1}};
  9304. CHECK_PARSE("IncludeCategories:\n"
  9305. " - Regex: abc/.*\n"
  9306. " Priority: 2\n"
  9307. " - Regex: .*\n"
  9308. " Priority: 1",
  9309. IncludeCategories, ExpectedCategories);
  9310. }
  9311. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9312. FormatStyle Style = {};
  9313. Style.Language = FormatStyle::LK_Cpp;
  9314. CHECK_PARSE("Language: Cpp\n"
  9315. "IndentWidth: 12",
  9316. IndentWidth, 12u);
  9317. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9318. "IndentWidth: 34",
  9319. &Style),
  9320. ParseError::Unsuitable);
  9321. EXPECT_EQ(12u, Style.IndentWidth);
  9322. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9323. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9324. Style.Language = FormatStyle::LK_JavaScript;
  9325. CHECK_PARSE("Language: JavaScript\n"
  9326. "IndentWidth: 12",
  9327. IndentWidth, 12u);
  9328. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9329. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9330. "IndentWidth: 34",
  9331. &Style),
  9332. ParseError::Unsuitable);
  9333. EXPECT_EQ(23u, Style.IndentWidth);
  9334. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9335. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9336. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9337. "IndentWidth: 67",
  9338. IndentWidth, 67u);
  9339. CHECK_PARSE("---\n"
  9340. "Language: JavaScript\n"
  9341. "IndentWidth: 12\n"
  9342. "---\n"
  9343. "Language: Cpp\n"
  9344. "IndentWidth: 34\n"
  9345. "...\n",
  9346. IndentWidth, 12u);
  9347. Style.Language = FormatStyle::LK_Cpp;
  9348. CHECK_PARSE("---\n"
  9349. "Language: JavaScript\n"
  9350. "IndentWidth: 12\n"
  9351. "---\n"
  9352. "Language: Cpp\n"
  9353. "IndentWidth: 34\n"
  9354. "...\n",
  9355. IndentWidth, 34u);
  9356. CHECK_PARSE("---\n"
  9357. "IndentWidth: 78\n"
  9358. "---\n"
  9359. "Language: JavaScript\n"
  9360. "IndentWidth: 56\n"
  9361. "...\n",
  9362. IndentWidth, 78u);
  9363. Style.ColumnLimit = 123;
  9364. Style.IndentWidth = 234;
  9365. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9366. Style.TabWidth = 345;
  9367. EXPECT_FALSE(parseConfiguration("---\n"
  9368. "IndentWidth: 456\n"
  9369. "BreakBeforeBraces: Allman\n"
  9370. "---\n"
  9371. "Language: JavaScript\n"
  9372. "IndentWidth: 111\n"
  9373. "TabWidth: 111\n"
  9374. "---\n"
  9375. "Language: Cpp\n"
  9376. "BreakBeforeBraces: Stroustrup\n"
  9377. "TabWidth: 789\n"
  9378. "...\n",
  9379. &Style));
  9380. EXPECT_EQ(123u, Style.ColumnLimit);
  9381. EXPECT_EQ(456u, Style.IndentWidth);
  9382. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9383. EXPECT_EQ(789u, Style.TabWidth);
  9384. EXPECT_EQ(parseConfiguration("---\n"
  9385. "Language: JavaScript\n"
  9386. "IndentWidth: 56\n"
  9387. "---\n"
  9388. "IndentWidth: 78\n"
  9389. "...\n",
  9390. &Style),
  9391. ParseError::Error);
  9392. EXPECT_EQ(parseConfiguration("---\n"
  9393. "Language: JavaScript\n"
  9394. "IndentWidth: 56\n"
  9395. "---\n"
  9396. "Language: JavaScript\n"
  9397. "IndentWidth: 78\n"
  9398. "...\n",
  9399. &Style),
  9400. ParseError::Error);
  9401. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9402. }
  9403. #undef CHECK_PARSE
  9404. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9405. FormatStyle Style = {};
  9406. Style.Language = FormatStyle::LK_JavaScript;
  9407. Style.BreakBeforeTernaryOperators = true;
  9408. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9409. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9410. Style.BreakBeforeTernaryOperators = true;
  9411. EXPECT_EQ(0, parseConfiguration("---\n"
  9412. "BasedOnStyle: Google\n"
  9413. "---\n"
  9414. "Language: JavaScript\n"
  9415. "IndentWidth: 76\n"
  9416. "...\n",
  9417. &Style)
  9418. .value());
  9419. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9420. EXPECT_EQ(76u, Style.IndentWidth);
  9421. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9422. }
  9423. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9424. FormatStyle Style = getLLVMStyle();
  9425. std::string YAML = configurationAsText(Style);
  9426. FormatStyle ParsedStyle = {};
  9427. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9428. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9429. EXPECT_EQ(Style, ParsedStyle);
  9430. }
  9431. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9432. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9433. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9434. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9435. "\"\xef\xee\xf0\xf3...\"",
  9436. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9437. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9438. "\xef\xee\xf0\xf3...\"",
  9439. getLLVMStyleWithColumns(12)));
  9440. }
  9441. TEST_F(FormatTest, HandlesUTF8BOM) {
  9442. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9443. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9444. format("\xef\xbb\xbf#include <iostream>"));
  9445. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9446. format("\xef\xbb\xbf\n#include <iostream>"));
  9447. }
  9448. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9449. #if !defined(_MSC_VER)
  9450. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9451. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9452. getLLVMStyleWithColumns(35));
  9453. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9454. getLLVMStyleWithColumns(31));
  9455. verifyFormat("// Однажды в студёную зимнюю пору...",
  9456. getLLVMStyleWithColumns(36));
  9457. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9458. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9459. getLLVMStyleWithColumns(39));
  9460. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9461. getLLVMStyleWithColumns(35));
  9462. }
  9463. TEST_F(FormatTest, SplitsUTF8Strings) {
  9464. // Non-printable characters' width is currently considered to be the length in
  9465. // bytes in UTF8. The characters can be displayed in very different manner
  9466. // (zero-width, single width with a substitution glyph, expanded to their code
  9467. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9468. EXPECT_EQ("\"aaaaÄ\"\n"
  9469. "\"\xc2\x8d\";",
  9470. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9471. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9472. "\"\xc2\x8d\";",
  9473. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9474. EXPECT_EQ("\"Однажды, в \"\n"
  9475. "\"студёную \"\n"
  9476. "\"зимнюю \"\n"
  9477. "\"пору,\"",
  9478. format("\"Однажды, в студёную зимнюю пору,\"",
  9479. getLLVMStyleWithColumns(13)));
  9480. EXPECT_EQ(
  9481. "\"一 二 三 \"\n"
  9482. "\"四 五六 \"\n"
  9483. "\"七 八 九 \"\n"
  9484. "\"十\"",
  9485. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9486. EXPECT_EQ("\"一\t二 \"\n"
  9487. "\"\t三 \"\n"
  9488. "\"四 五\t六 \"\n"
  9489. "\"\t七 \"\n"
  9490. "\"八九十\tqq\"",
  9491. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9492. getLLVMStyleWithColumns(11)));
  9493. // UTF8 character in an escape sequence.
  9494. EXPECT_EQ("\"aaaaaa\"\n"
  9495. "\"\\\xC2\x8D\"",
  9496. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9497. }
  9498. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9499. EXPECT_EQ("const char *sssss =\n"
  9500. " \"一二三四五六七八\\\n"
  9501. " 九 十\";",
  9502. format("const char *sssss = \"一二三四五六七八\\\n"
  9503. " 九 十\";",
  9504. getLLVMStyleWithColumns(30)));
  9505. }
  9506. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9507. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9508. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9509. EXPECT_EQ("// Я из лесу\n"
  9510. "// вышел; был\n"
  9511. "// сильный\n"
  9512. "// мороз.",
  9513. format("// Я из лесу вышел; был сильный мороз.",
  9514. getLLVMStyleWithColumns(13)));
  9515. EXPECT_EQ("// 一二三\n"
  9516. "// 四五六七\n"
  9517. "// 八 九\n"
  9518. "// 十",
  9519. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9520. }
  9521. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9522. EXPECT_EQ("/* Гляжу,\n"
  9523. " * поднимается\n"
  9524. " * медленно в\n"
  9525. " * гору\n"
  9526. " * Лошадка,\n"
  9527. " * везущая\n"
  9528. " * хворосту\n"
  9529. " * воз. */",
  9530. format("/* Гляжу, поднимается медленно в гору\n"
  9531. " * Лошадка, везущая хворосту воз. */",
  9532. getLLVMStyleWithColumns(13)));
  9533. EXPECT_EQ(
  9534. "/* 一二三\n"
  9535. " * 四五六七\n"
  9536. " * 八 九\n"
  9537. " * 十 */",
  9538. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9539. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9540. " * 𝕓𝕪𝕥𝕖\n"
  9541. " * 𝖀𝕿𝕱-𝟠 */",
  9542. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9543. }
  9544. #endif // _MSC_VER
  9545. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9546. FormatStyle Style = getLLVMStyle();
  9547. Style.ConstructorInitializerIndentWidth = 4;
  9548. verifyFormat(
  9549. "SomeClass::Constructor()\n"
  9550. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9551. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9552. Style);
  9553. Style.ConstructorInitializerIndentWidth = 2;
  9554. verifyFormat(
  9555. "SomeClass::Constructor()\n"
  9556. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9557. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9558. Style);
  9559. Style.ConstructorInitializerIndentWidth = 0;
  9560. verifyFormat(
  9561. "SomeClass::Constructor()\n"
  9562. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9563. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9564. Style);
  9565. }
  9566. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9567. FormatStyle Style = getLLVMStyle();
  9568. Style.BreakConstructorInitializersBeforeComma = true;
  9569. Style.ConstructorInitializerIndentWidth = 4;
  9570. verifyFormat("SomeClass::Constructor()\n"
  9571. " : a(a)\n"
  9572. " , b(b)\n"
  9573. " , c(c) {}",
  9574. Style);
  9575. verifyFormat("SomeClass::Constructor()\n"
  9576. " : a(a) {}",
  9577. Style);
  9578. Style.ColumnLimit = 0;
  9579. verifyFormat("SomeClass::Constructor()\n"
  9580. " : a(a) {}",
  9581. Style);
  9582. verifyFormat("SomeClass::Constructor()\n"
  9583. " : a(a)\n"
  9584. " , b(b)\n"
  9585. " , c(c) {}",
  9586. Style);
  9587. verifyFormat("SomeClass::Constructor()\n"
  9588. " : a(a) {\n"
  9589. " foo();\n"
  9590. " bar();\n"
  9591. "}",
  9592. Style);
  9593. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9594. verifyFormat("SomeClass::Constructor()\n"
  9595. " : a(a)\n"
  9596. " , b(b)\n"
  9597. " , c(c) {\n}",
  9598. Style);
  9599. verifyFormat("SomeClass::Constructor()\n"
  9600. " : a(a) {\n}",
  9601. Style);
  9602. Style.ColumnLimit = 80;
  9603. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9604. Style.ConstructorInitializerIndentWidth = 2;
  9605. verifyFormat("SomeClass::Constructor()\n"
  9606. " : a(a)\n"
  9607. " , b(b)\n"
  9608. " , c(c) {}",
  9609. Style);
  9610. Style.ConstructorInitializerIndentWidth = 0;
  9611. verifyFormat("SomeClass::Constructor()\n"
  9612. ": a(a)\n"
  9613. ", b(b)\n"
  9614. ", c(c) {}",
  9615. Style);
  9616. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9617. Style.ConstructorInitializerIndentWidth = 4;
  9618. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9619. verifyFormat(
  9620. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9621. Style);
  9622. verifyFormat(
  9623. "SomeClass::Constructor()\n"
  9624. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9625. Style);
  9626. Style.ConstructorInitializerIndentWidth = 4;
  9627. Style.ColumnLimit = 60;
  9628. verifyFormat("SomeClass::Constructor()\n"
  9629. " : aaaaaaaa(aaaaaaaa)\n"
  9630. " , aaaaaaaa(aaaaaaaa)\n"
  9631. " , aaaaaaaa(aaaaaaaa) {}",
  9632. Style);
  9633. }
  9634. TEST_F(FormatTest, Destructors) {
  9635. verifyFormat("void F(int &i) { i.~int(); }");
  9636. verifyFormat("void F(int &i) { i->~int(); }");
  9637. }
  9638. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9639. FormatStyle Style = getWebKitStyle();
  9640. // Don't indent in outer namespaces.
  9641. verifyFormat("namespace outer {\n"
  9642. "int i;\n"
  9643. "namespace inner {\n"
  9644. " int i;\n"
  9645. "} // namespace inner\n"
  9646. "} // namespace outer\n"
  9647. "namespace other_outer {\n"
  9648. "int i;\n"
  9649. "}",
  9650. Style);
  9651. // Don't indent case labels.
  9652. verifyFormat("switch (variable) {\n"
  9653. "case 1:\n"
  9654. "case 2:\n"
  9655. " doSomething();\n"
  9656. " break;\n"
  9657. "default:\n"
  9658. " ++variable;\n"
  9659. "}",
  9660. Style);
  9661. // Wrap before binary operators.
  9662. EXPECT_EQ("void f()\n"
  9663. "{\n"
  9664. " if (aaaaaaaaaaaaaaaa\n"
  9665. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9666. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9667. " return;\n"
  9668. "}",
  9669. format("void f() {\n"
  9670. "if (aaaaaaaaaaaaaaaa\n"
  9671. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9672. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9673. "return;\n"
  9674. "}",
  9675. Style));
  9676. // Allow functions on a single line.
  9677. verifyFormat("void f() { return; }", Style);
  9678. // Constructor initializers are formatted one per line with the "," on the
  9679. // new line.
  9680. verifyFormat("Constructor()\n"
  9681. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  9682. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  9683. " aaaaaaaaaaaaaa)\n"
  9684. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  9685. "{\n"
  9686. "}",
  9687. Style);
  9688. verifyFormat("SomeClass::Constructor()\n"
  9689. " : a(a)\n"
  9690. "{\n"
  9691. "}",
  9692. Style);
  9693. EXPECT_EQ("SomeClass::Constructor()\n"
  9694. " : a(a)\n"
  9695. "{\n"
  9696. "}",
  9697. format("SomeClass::Constructor():a(a){}", Style));
  9698. verifyFormat("SomeClass::Constructor()\n"
  9699. " : a(a)\n"
  9700. " , b(b)\n"
  9701. " , c(c)\n"
  9702. "{\n"
  9703. "}",
  9704. Style);
  9705. verifyFormat("SomeClass::Constructor()\n"
  9706. " : a(a)\n"
  9707. "{\n"
  9708. " foo();\n"
  9709. " bar();\n"
  9710. "}",
  9711. Style);
  9712. // Access specifiers should be aligned left.
  9713. verifyFormat("class C {\n"
  9714. "public:\n"
  9715. " int i;\n"
  9716. "};",
  9717. Style);
  9718. // Do not align comments.
  9719. verifyFormat("int a; // Do not\n"
  9720. "double b; // align comments.",
  9721. Style);
  9722. // Do not align operands.
  9723. EXPECT_EQ("ASSERT(aaaa\n"
  9724. " || bbbb);",
  9725. format("ASSERT ( aaaa\n||bbbb);", Style));
  9726. // Accept input's line breaks.
  9727. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  9728. " || bbbbbbbbbbbbbbb) {\n"
  9729. " i++;\n"
  9730. "}",
  9731. format("if (aaaaaaaaaaaaaaa\n"
  9732. "|| bbbbbbbbbbbbbbb) { i++; }",
  9733. Style));
  9734. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  9735. " i++;\n"
  9736. "}",
  9737. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  9738. // Don't automatically break all macro definitions (llvm.org/PR17842).
  9739. verifyFormat("#define aNumber 10", Style);
  9740. // However, generally keep the line breaks that the user authored.
  9741. EXPECT_EQ("#define aNumber \\\n"
  9742. " 10",
  9743. format("#define aNumber \\\n"
  9744. " 10",
  9745. Style));
  9746. // Keep empty and one-element array literals on a single line.
  9747. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  9748. " copyItems:YES];",
  9749. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  9750. "copyItems:YES];",
  9751. Style));
  9752. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  9753. " copyItems:YES];",
  9754. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  9755. " copyItems:YES];",
  9756. Style));
  9757. // FIXME: This does not seem right, there should be more indentation before
  9758. // the array literal's entries. Nested blocks have the same problem.
  9759. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9760. " @\"a\",\n"
  9761. " @\"a\"\n"
  9762. "]\n"
  9763. " copyItems:YES];",
  9764. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9765. " @\"a\",\n"
  9766. " @\"a\"\n"
  9767. " ]\n"
  9768. " copyItems:YES];",
  9769. Style));
  9770. EXPECT_EQ(
  9771. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9772. " copyItems:YES];",
  9773. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9774. " copyItems:YES];",
  9775. Style));
  9776. verifyFormat("[self.a b:c c:d];", Style);
  9777. EXPECT_EQ("[self.a b:c\n"
  9778. " c:d];",
  9779. format("[self.a b:c\n"
  9780. "c:d];",
  9781. Style));
  9782. }
  9783. TEST_F(FormatTest, FormatsLambdas) {
  9784. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  9785. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  9786. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  9787. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  9788. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  9789. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  9790. verifyFormat("void f() {\n"
  9791. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  9792. "}\n");
  9793. verifyFormat("void f() {\n"
  9794. " other(x.begin(), //\n"
  9795. " x.end(), //\n"
  9796. " [&](int, int) { return 1; });\n"
  9797. "}\n");
  9798. verifyFormat("SomeFunction([]() { // A cool function...\n"
  9799. " return 43;\n"
  9800. "});");
  9801. EXPECT_EQ("SomeFunction([]() {\n"
  9802. "#define A a\n"
  9803. " return 43;\n"
  9804. "});",
  9805. format("SomeFunction([](){\n"
  9806. "#define A a\n"
  9807. "return 43;\n"
  9808. "});"));
  9809. verifyFormat("void f() {\n"
  9810. " SomeFunction([](decltype(x), A *a) {});\n"
  9811. "}");
  9812. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  9813. " [](const aaaaaaaaaa &a) { return a; });");
  9814. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  9815. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  9816. "});");
  9817. verifyFormat("Constructor()\n"
  9818. " : Field([] { // comment\n"
  9819. " int i;\n"
  9820. " }) {}");
  9821. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  9822. " return some_parameter.size();\n"
  9823. "};");
  9824. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  9825. " [](const string &s) { return s; };");
  9826. verifyFormat("int i = aaaaaa ? 1 //\n"
  9827. " : [] {\n"
  9828. " return 2; //\n"
  9829. " }();");
  9830. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  9831. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  9832. " return x == 2; // force break\n"
  9833. " });");
  9834. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  9835. " int iiiiiiiiiiii) {\n"
  9836. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  9837. "});",
  9838. getLLVMStyleWithColumns(60));
  9839. verifyFormat("SomeFunction({[&] {\n"
  9840. " // comment\n"
  9841. " },\n"
  9842. " [&] {\n"
  9843. " // comment\n"
  9844. " }});");
  9845. verifyFormat("SomeFunction({[&] {\n"
  9846. " // comment\n"
  9847. "}});");
  9848. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  9849. " [&]() { return true; },\n"
  9850. " aaaaa aaaaaaaaa);");
  9851. // Lambdas with return types.
  9852. verifyFormat("int c = []() -> int { return 2; }();\n");
  9853. verifyFormat("int c = []() -> int * { return 2; }();\n");
  9854. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  9855. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  9856. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  9857. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  9858. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  9859. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  9860. verifyFormat("[a, a]() -> a<1> {};");
  9861. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  9862. " int j) -> int {\n"
  9863. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  9864. "};");
  9865. verifyFormat(
  9866. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  9867. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  9868. " return aaaaaaaaaaaaaaaaa;\n"
  9869. " });",
  9870. getLLVMStyleWithColumns(70));
  9871. // Multiple lambdas in the same parentheses change indentation rules.
  9872. verifyFormat("SomeFunction(\n"
  9873. " []() {\n"
  9874. " int i = 42;\n"
  9875. " return i;\n"
  9876. " },\n"
  9877. " []() {\n"
  9878. " int j = 43;\n"
  9879. " return j;\n"
  9880. " });");
  9881. // More complex introducers.
  9882. verifyFormat("return [i, args...] {};");
  9883. // Not lambdas.
  9884. verifyFormat("constexpr char hello[]{\"hello\"};");
  9885. verifyFormat("double &operator[](int i) { return 0; }\n"
  9886. "int i;");
  9887. verifyFormat("std::unique_ptr<int[]> foo() {}");
  9888. verifyFormat("int i = a[a][a]->f();");
  9889. verifyFormat("int i = (*b)[a]->f();");
  9890. // Other corner cases.
  9891. verifyFormat("void f() {\n"
  9892. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  9893. " );\n"
  9894. "}");
  9895. // Lambdas created through weird macros.
  9896. verifyFormat("void f() {\n"
  9897. " MACRO((const AA &a) { return 1; });\n"
  9898. " MACRO((AA &a) { return 1; });\n"
  9899. "}");
  9900. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  9901. " doo_dah();\n"
  9902. " doo_dah();\n"
  9903. " })) {\n"
  9904. "}");
  9905. verifyFormat("auto lambda = []() {\n"
  9906. " int a = 2\n"
  9907. "#if A\n"
  9908. " + 2\n"
  9909. "#endif\n"
  9910. " ;\n"
  9911. "};");
  9912. }
  9913. TEST_F(FormatTest, FormatsBlocks) {
  9914. FormatStyle ShortBlocks = getLLVMStyle();
  9915. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  9916. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  9917. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  9918. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  9919. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  9920. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  9921. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  9922. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  9923. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  9924. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  9925. verifyFormat("[operation setCompletionBlock:^{\n"
  9926. " [self onOperationDone];\n"
  9927. "}];");
  9928. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  9929. " [self onOperationDone];\n"
  9930. "}]};");
  9931. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  9932. " f();\n"
  9933. "}];");
  9934. verifyFormat("int a = [operation block:^int(int *i) {\n"
  9935. " return 1;\n"
  9936. "}];");
  9937. verifyFormat("[myObject doSomethingWith:arg1\n"
  9938. " aaa:^int(int *a) {\n"
  9939. " return 1;\n"
  9940. " }\n"
  9941. " bbb:f(a * bbbbbbbb)];");
  9942. verifyFormat("[operation setCompletionBlock:^{\n"
  9943. " [self.delegate newDataAvailable];\n"
  9944. "}];",
  9945. getLLVMStyleWithColumns(60));
  9946. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  9947. " NSString *path = [self sessionFilePath];\n"
  9948. " if (path) {\n"
  9949. " // ...\n"
  9950. " }\n"
  9951. "});");
  9952. verifyFormat("[[SessionService sharedService]\n"
  9953. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9954. " if (window) {\n"
  9955. " [self windowDidLoad:window];\n"
  9956. " } else {\n"
  9957. " [self errorLoadingWindow];\n"
  9958. " }\n"
  9959. " }];");
  9960. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9961. " // ...\n"
  9962. "};\n",
  9963. getLLVMStyleWithColumns(40));
  9964. verifyFormat("[[SessionService sharedService]\n"
  9965. " loadWindowWithCompletionBlock: //\n"
  9966. " ^(SessionWindow *window) {\n"
  9967. " if (window) {\n"
  9968. " [self windowDidLoad:window];\n"
  9969. " } else {\n"
  9970. " [self errorLoadingWindow];\n"
  9971. " }\n"
  9972. " }];",
  9973. getLLVMStyleWithColumns(60));
  9974. verifyFormat("[myObject doSomethingWith:arg1\n"
  9975. " firstBlock:^(Foo *a) {\n"
  9976. " // ...\n"
  9977. " int i;\n"
  9978. " }\n"
  9979. " secondBlock:^(Bar *b) {\n"
  9980. " // ...\n"
  9981. " int i;\n"
  9982. " }\n"
  9983. " thirdBlock:^Foo(Bar *b) {\n"
  9984. " // ...\n"
  9985. " int i;\n"
  9986. " }];");
  9987. verifyFormat("[myObject doSomethingWith:arg1\n"
  9988. " firstBlock:-1\n"
  9989. " secondBlock:^(Bar *b) {\n"
  9990. " // ...\n"
  9991. " int i;\n"
  9992. " }];");
  9993. verifyFormat("f(^{\n"
  9994. " @autoreleasepool {\n"
  9995. " if (a) {\n"
  9996. " g();\n"
  9997. " }\n"
  9998. " }\n"
  9999. "});");
  10000. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  10001. FormatStyle FourIndent = getLLVMStyle();
  10002. FourIndent.ObjCBlockIndentWidth = 4;
  10003. verifyFormat("[operation setCompletionBlock:^{\n"
  10004. " [self onOperationDone];\n"
  10005. "}];",
  10006. FourIndent);
  10007. }
  10008. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  10009. FormatStyle ZeroColumn = getLLVMStyle();
  10010. ZeroColumn.ColumnLimit = 0;
  10011. verifyFormat("[[SessionService sharedService] "
  10012. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10013. " if (window) {\n"
  10014. " [self windowDidLoad:window];\n"
  10015. " } else {\n"
  10016. " [self errorLoadingWindow];\n"
  10017. " }\n"
  10018. "}];",
  10019. ZeroColumn);
  10020. EXPECT_EQ("[[SessionService sharedService]\n"
  10021. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10022. " if (window) {\n"
  10023. " [self windowDidLoad:window];\n"
  10024. " } else {\n"
  10025. " [self errorLoadingWindow];\n"
  10026. " }\n"
  10027. " }];",
  10028. format("[[SessionService sharedService]\n"
  10029. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10030. " if (window) {\n"
  10031. " [self windowDidLoad:window];\n"
  10032. " } else {\n"
  10033. " [self errorLoadingWindow];\n"
  10034. " }\n"
  10035. "}];",
  10036. ZeroColumn));
  10037. verifyFormat("[myObject doSomethingWith:arg1\n"
  10038. " firstBlock:^(Foo *a) {\n"
  10039. " // ...\n"
  10040. " int i;\n"
  10041. " }\n"
  10042. " secondBlock:^(Bar *b) {\n"
  10043. " // ...\n"
  10044. " int i;\n"
  10045. " }\n"
  10046. " thirdBlock:^Foo(Bar *b) {\n"
  10047. " // ...\n"
  10048. " int i;\n"
  10049. " }];",
  10050. ZeroColumn);
  10051. verifyFormat("f(^{\n"
  10052. " @autoreleasepool {\n"
  10053. " if (a) {\n"
  10054. " g();\n"
  10055. " }\n"
  10056. " }\n"
  10057. "});",
  10058. ZeroColumn);
  10059. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10060. " // ...\n"
  10061. "};",
  10062. ZeroColumn);
  10063. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  10064. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  10065. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10066. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  10067. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  10068. " int i;\n"
  10069. "};",
  10070. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10071. }
  10072. TEST_F(FormatTest, SupportsCRLF) {
  10073. EXPECT_EQ("int a;\r\n"
  10074. "int b;\r\n"
  10075. "int c;\r\n",
  10076. format("int a;\r\n"
  10077. " int b;\r\n"
  10078. " int c;\r\n",
  10079. getLLVMStyle()));
  10080. EXPECT_EQ("int a;\r\n"
  10081. "int b;\r\n"
  10082. "int c;\r\n",
  10083. format("int a;\r\n"
  10084. " int b;\n"
  10085. " int c;\r\n",
  10086. getLLVMStyle()));
  10087. EXPECT_EQ("int a;\n"
  10088. "int b;\n"
  10089. "int c;\n",
  10090. format("int a;\r\n"
  10091. " int b;\n"
  10092. " int c;\n",
  10093. getLLVMStyle()));
  10094. EXPECT_EQ("\"aaaaaaa \"\r\n"
  10095. "\"bbbbbbb\";\r\n",
  10096. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  10097. EXPECT_EQ("#define A \\\r\n"
  10098. " b; \\\r\n"
  10099. " c; \\\r\n"
  10100. " d;\r\n",
  10101. format("#define A \\\r\n"
  10102. " b; \\\r\n"
  10103. " c; d; \r\n",
  10104. getGoogleStyle()));
  10105. EXPECT_EQ("/*\r\n"
  10106. "multi line block comments\r\n"
  10107. "should not introduce\r\n"
  10108. "an extra carriage return\r\n"
  10109. "*/\r\n",
  10110. format("/*\r\n"
  10111. "multi line block comments\r\n"
  10112. "should not introduce\r\n"
  10113. "an extra carriage return\r\n"
  10114. "*/\r\n"));
  10115. }
  10116. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  10117. verifyFormat("MY_CLASS(C) {\n"
  10118. " int i;\n"
  10119. " int j;\n"
  10120. "};");
  10121. }
  10122. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  10123. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  10124. TwoIndent.ContinuationIndentWidth = 2;
  10125. EXPECT_EQ("int i =\n"
  10126. " longFunction(\n"
  10127. " arg);",
  10128. format("int i = longFunction(arg);", TwoIndent));
  10129. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  10130. SixIndent.ContinuationIndentWidth = 6;
  10131. EXPECT_EQ("int i =\n"
  10132. " longFunction(\n"
  10133. " arg);",
  10134. format("int i = longFunction(arg);", SixIndent));
  10135. }
  10136. TEST_F(FormatTest, SpacesInAngles) {
  10137. FormatStyle Spaces = getLLVMStyle();
  10138. Spaces.SpacesInAngles = true;
  10139. verifyFormat("static_cast< int >(arg);", Spaces);
  10140. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  10141. verifyFormat("f< int, float >();", Spaces);
  10142. verifyFormat("template <> g() {}", Spaces);
  10143. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  10144. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  10145. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  10146. Spaces);
  10147. Spaces.Standard = FormatStyle::LS_Cpp03;
  10148. Spaces.SpacesInAngles = true;
  10149. verifyFormat("A< A< int > >();", Spaces);
  10150. Spaces.SpacesInAngles = false;
  10151. verifyFormat("A<A<int> >();", Spaces);
  10152. Spaces.Standard = FormatStyle::LS_Cpp11;
  10153. Spaces.SpacesInAngles = true;
  10154. verifyFormat("A< A< int > >();", Spaces);
  10155. Spaces.SpacesInAngles = false;
  10156. verifyFormat("A<A<int>>();", Spaces);
  10157. }
  10158. TEST_F(FormatTest, TripleAngleBrackets) {
  10159. verifyFormat("f<<<1, 1>>>();");
  10160. verifyFormat("f<<<1, 1, 1, s>>>();");
  10161. verifyFormat("f<<<a, b, c, d>>>();");
  10162. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  10163. verifyFormat("f<param><<<1, 1>>>();");
  10164. verifyFormat("f<1><<<1, 1>>>();");
  10165. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  10166. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10167. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  10168. }
  10169. TEST_F(FormatTest, MergeLessLessAtEnd) {
  10170. verifyFormat("<<");
  10171. EXPECT_EQ("< < <", format("\\\n<<<"));
  10172. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10173. "aaallvm::outs() <<");
  10174. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10175. "aaaallvm::outs()\n <<");
  10176. }
  10177. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  10178. std::string code = "#if A\n"
  10179. "#if B\n"
  10180. "a.\n"
  10181. "#endif\n"
  10182. " a = 1;\n"
  10183. "#else\n"
  10184. "#endif\n"
  10185. "#if C\n"
  10186. "#else\n"
  10187. "#endif\n";
  10188. EXPECT_EQ(code, format(code));
  10189. }
  10190. TEST_F(FormatTest, HandleConflictMarkers) {
  10191. // Git/SVN conflict markers.
  10192. EXPECT_EQ("int a;\n"
  10193. "void f() {\n"
  10194. " callme(some(parameter1,\n"
  10195. "<<<<<<< text by the vcs\n"
  10196. " parameter2),\n"
  10197. "||||||| text by the vcs\n"
  10198. " parameter2),\n"
  10199. " parameter3,\n"
  10200. "======= text by the vcs\n"
  10201. " parameter2, parameter3),\n"
  10202. ">>>>>>> text by the vcs\n"
  10203. " otherparameter);\n",
  10204. format("int a;\n"
  10205. "void f() {\n"
  10206. " callme(some(parameter1,\n"
  10207. "<<<<<<< text by the vcs\n"
  10208. " parameter2),\n"
  10209. "||||||| text by the vcs\n"
  10210. " parameter2),\n"
  10211. " parameter3,\n"
  10212. "======= text by the vcs\n"
  10213. " parameter2,\n"
  10214. " parameter3),\n"
  10215. ">>>>>>> text by the vcs\n"
  10216. " otherparameter);\n"));
  10217. // Perforce markers.
  10218. EXPECT_EQ("void f() {\n"
  10219. " function(\n"
  10220. ">>>> text by the vcs\n"
  10221. " parameter,\n"
  10222. "==== text by the vcs\n"
  10223. " parameter,\n"
  10224. "==== text by the vcs\n"
  10225. " parameter,\n"
  10226. "<<<< text by the vcs\n"
  10227. " parameter);\n",
  10228. format("void f() {\n"
  10229. " function(\n"
  10230. ">>>> text by the vcs\n"
  10231. " parameter,\n"
  10232. "==== text by the vcs\n"
  10233. " parameter,\n"
  10234. "==== text by the vcs\n"
  10235. " parameter,\n"
  10236. "<<<< text by the vcs\n"
  10237. " parameter);\n"));
  10238. EXPECT_EQ("<<<<<<<\n"
  10239. "|||||||\n"
  10240. "=======\n"
  10241. ">>>>>>>",
  10242. format("<<<<<<<\n"
  10243. "|||||||\n"
  10244. "=======\n"
  10245. ">>>>>>>"));
  10246. EXPECT_EQ("<<<<<<<\n"
  10247. "|||||||\n"
  10248. "int i;\n"
  10249. "=======\n"
  10250. ">>>>>>>",
  10251. format("<<<<<<<\n"
  10252. "|||||||\n"
  10253. "int i;\n"
  10254. "=======\n"
  10255. ">>>>>>>"));
  10256. // FIXME: Handle parsing of macros around conflict markers correctly:
  10257. EXPECT_EQ("#define Macro \\\n"
  10258. "<<<<<<<\n"
  10259. "Something \\\n"
  10260. "|||||||\n"
  10261. "Else \\\n"
  10262. "=======\n"
  10263. "Other \\\n"
  10264. ">>>>>>>\n"
  10265. " End int i;\n",
  10266. format("#define Macro \\\n"
  10267. "<<<<<<<\n"
  10268. " Something \\\n"
  10269. "|||||||\n"
  10270. " Else \\\n"
  10271. "=======\n"
  10272. " Other \\\n"
  10273. ">>>>>>>\n"
  10274. " End\n"
  10275. "int i;\n"));
  10276. }
  10277. TEST_F(FormatTest, DisableRegions) {
  10278. EXPECT_EQ("int i;\n"
  10279. "// clang-format off\n"
  10280. " int j;\n"
  10281. "// clang-format on\n"
  10282. "int k;",
  10283. format(" int i;\n"
  10284. " // clang-format off\n"
  10285. " int j;\n"
  10286. " // clang-format on\n"
  10287. " int k;"));
  10288. EXPECT_EQ("int i;\n"
  10289. "/* clang-format off */\n"
  10290. " int j;\n"
  10291. "/* clang-format on */\n"
  10292. "int k;",
  10293. format(" int i;\n"
  10294. " /* clang-format off */\n"
  10295. " int j;\n"
  10296. " /* clang-format on */\n"
  10297. " int k;"));
  10298. }
  10299. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10300. format("? ) =");
  10301. verifyNoCrash("#define a\\\n /**/}");
  10302. }
  10303. TEST_F(FormatTest, FormatsTableGenCode) {
  10304. FormatStyle Style = getLLVMStyle();
  10305. Style.Language = FormatStyle::LK_TableGen;
  10306. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  10307. }
  10308. } // end namespace
  10309. } // end namespace format
  10310. } // end namespace clang