FormatTest.cpp 185 KB

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