FormatTest.cpp 205 KB

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