FormatTest.cpp 202 KB

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