FormatTest.cpp 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351
  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, ReformatsMovedLines) {
  170. EXPECT_EQ(
  171. "template <typename T> T *getFETokenInfo() const {\n"
  172. " return static_cast<T *>(FETokenInfo);\n"
  173. "}\n"
  174. " int a; // <- Should not be formatted",
  175. format(
  176. "template<typename T>\n"
  177. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  178. " int a; // <- Should not be formatted",
  179. 9, 5, getLLVMStyle()));
  180. }
  181. //===----------------------------------------------------------------------===//
  182. // Tests for control statements.
  183. //===----------------------------------------------------------------------===//
  184. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  185. verifyFormat("if (true)\n f();\ng();");
  186. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  187. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  188. FormatStyle AllowsMergedIf = getLLVMStyle();
  189. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  190. verifyFormat("if (a)\n"
  191. " // comment\n"
  192. " f();",
  193. AllowsMergedIf);
  194. verifyFormat("if (a)\n"
  195. " ;",
  196. AllowsMergedIf);
  197. verifyFormat("if (a)\n"
  198. " if (b) return;",
  199. AllowsMergedIf);
  200. verifyFormat("if (a) // Can't merge this\n"
  201. " f();\n",
  202. AllowsMergedIf);
  203. verifyFormat("if (a) /* still don't merge */\n"
  204. " f();",
  205. AllowsMergedIf);
  206. verifyFormat("if (a) { // Never merge this\n"
  207. " f();\n"
  208. "}",
  209. AllowsMergedIf);
  210. verifyFormat("if (a) { /* Never merge this */\n"
  211. " f();\n"
  212. "}",
  213. AllowsMergedIf);
  214. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  215. EXPECT_EQ("if (a) return; // comment",
  216. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  217. AllowsMergedIf.ColumnLimit = 14;
  218. verifyFormat("if (a) return;", AllowsMergedIf);
  219. verifyFormat("if (aaaaaaaaa)\n"
  220. " return;",
  221. AllowsMergedIf);
  222. AllowsMergedIf.ColumnLimit = 13;
  223. verifyFormat("if (a)\n return;", AllowsMergedIf);
  224. }
  225. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  226. FormatStyle AllowsMergedLoops = getLLVMStyle();
  227. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  228. verifyFormat("while (true) continue;", AllowsMergedLoops);
  229. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  230. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  231. verifyFormat("while (true)\n"
  232. " ;",
  233. AllowsMergedLoops);
  234. verifyFormat("for (;;)\n"
  235. " ;",
  236. AllowsMergedLoops);
  237. verifyFormat("for (;;)\n"
  238. " for (;;) continue;",
  239. AllowsMergedLoops);
  240. verifyFormat("for (;;) // Can't merge this\n"
  241. " continue;",
  242. AllowsMergedLoops);
  243. verifyFormat("for (;;) /* still don't merge */\n"
  244. " continue;",
  245. AllowsMergedLoops);
  246. }
  247. TEST_F(FormatTest, ParseIfElse) {
  248. verifyFormat("if (true)\n"
  249. " if (true)\n"
  250. " if (true)\n"
  251. " f();\n"
  252. " else\n"
  253. " g();\n"
  254. " else\n"
  255. " h();\n"
  256. "else\n"
  257. " i();");
  258. verifyFormat("if (true)\n"
  259. " if (true)\n"
  260. " if (true) {\n"
  261. " if (true)\n"
  262. " f();\n"
  263. " } else {\n"
  264. " g();\n"
  265. " }\n"
  266. " else\n"
  267. " h();\n"
  268. "else {\n"
  269. " i();\n"
  270. "}");
  271. }
  272. TEST_F(FormatTest, ElseIf) {
  273. verifyFormat("if (a) {\n} else if (b) {\n}");
  274. verifyFormat("if (a)\n"
  275. " f();\n"
  276. "else if (b)\n"
  277. " g();\n"
  278. "else\n"
  279. " h();");
  280. }
  281. TEST_F(FormatTest, FormatsForLoop) {
  282. verifyFormat(
  283. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  284. " ++VeryVeryLongLoopVariable)\n"
  285. " ;");
  286. verifyFormat("for (;;)\n"
  287. " f();");
  288. verifyFormat("for (;;) {\n}");
  289. verifyFormat("for (;;) {\n"
  290. " f();\n"
  291. "}");
  292. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  293. verifyFormat(
  294. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  295. " E = UnwrappedLines.end();\n"
  296. " I != E; ++I) {\n}");
  297. verifyFormat(
  298. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  299. " ++IIIII) {\n}");
  300. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  301. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  302. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  303. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  304. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  305. " E = FD->getDeclsInPrototypeScope().end();\n"
  306. " I != E; ++I) {\n}");
  307. // FIXME: Not sure whether we want extra identation in line 3 here:
  308. verifyFormat(
  309. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  310. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  311. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  312. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  313. " ++aaaaaaaaaaa) {\n}");
  314. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  315. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  316. "}");
  317. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  318. " aaaaaaaaaa);\n"
  319. " iter; ++iter) {\n"
  320. "}");
  321. FormatStyle NoBinPacking = getLLVMStyle();
  322. NoBinPacking.BinPackParameters = false;
  323. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  324. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  325. " aaaaaaaaaaaaaaaa,\n"
  326. " aaaaaaaaaaaaaaaa,\n"
  327. " aaaaaaaaaaaaaaaa);\n"
  328. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  329. "}",
  330. NoBinPacking);
  331. verifyFormat(
  332. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  333. " E = UnwrappedLines.end();\n"
  334. " I != E;\n"
  335. " ++I) {\n}",
  336. NoBinPacking);
  337. }
  338. TEST_F(FormatTest, RangeBasedForLoops) {
  339. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  340. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  341. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  342. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  343. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  344. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  345. }
  346. TEST_F(FormatTest, FormatsWhileLoop) {
  347. verifyFormat("while (true) {\n}");
  348. verifyFormat("while (true)\n"
  349. " f();");
  350. verifyFormat("while () {\n}");
  351. verifyFormat("while () {\n"
  352. " f();\n"
  353. "}");
  354. }
  355. TEST_F(FormatTest, FormatsDoWhile) {
  356. verifyFormat("do {\n"
  357. " do_something();\n"
  358. "} while (something());");
  359. verifyFormat("do\n"
  360. " do_something();\n"
  361. "while (something());");
  362. }
  363. TEST_F(FormatTest, FormatsSwitchStatement) {
  364. verifyFormat("switch (x) {\n"
  365. "case 1:\n"
  366. " f();\n"
  367. " break;\n"
  368. "case kFoo:\n"
  369. "case ns::kBar:\n"
  370. "case kBaz:\n"
  371. " break;\n"
  372. "default:\n"
  373. " g();\n"
  374. " break;\n"
  375. "}");
  376. verifyFormat("switch (x) {\n"
  377. "case 1: {\n"
  378. " f();\n"
  379. " break;\n"
  380. "}\n"
  381. "}");
  382. verifyFormat("switch (x) {\n"
  383. "case 1: {\n"
  384. " f();\n"
  385. " {\n"
  386. " g();\n"
  387. " h();\n"
  388. " }\n"
  389. " break;\n"
  390. "}\n"
  391. "}");
  392. verifyFormat("switch (x) {\n"
  393. "case 1: {\n"
  394. " f();\n"
  395. " if (foo) {\n"
  396. " g();\n"
  397. " h();\n"
  398. " }\n"
  399. " break;\n"
  400. "}\n"
  401. "}");
  402. verifyFormat("switch (x) {\n"
  403. "case 1: {\n"
  404. " f();\n"
  405. " g();\n"
  406. "} break;\n"
  407. "}");
  408. verifyFormat("switch (test)\n"
  409. " ;");
  410. verifyFormat("switch (x) {\n"
  411. "default: {\n"
  412. " // Do nothing.\n"
  413. "}\n"
  414. "}");
  415. verifyFormat("switch (x) {\n"
  416. "// comment\n"
  417. "// if 1, do f()\n"
  418. "case 1:\n"
  419. " f();\n"
  420. "}");
  421. verifyFormat("switch (x) {\n"
  422. "case 1:\n"
  423. " // Do amazing stuff\n"
  424. " {\n"
  425. " f();\n"
  426. " g();\n"
  427. " }\n"
  428. " break;\n"
  429. "}");
  430. verifyFormat("#define A \\\n"
  431. " switch (x) { \\\n"
  432. " case a: \\\n"
  433. " foo = b; \\\n"
  434. " }", getLLVMStyleWithColumns(20));
  435. verifyGoogleFormat("switch (x) {\n"
  436. " case 1:\n"
  437. " f();\n"
  438. " break;\n"
  439. " case kFoo:\n"
  440. " case ns::kBar:\n"
  441. " case kBaz:\n"
  442. " break;\n"
  443. " default:\n"
  444. " g();\n"
  445. " break;\n"
  446. "}");
  447. verifyGoogleFormat("switch (x) {\n"
  448. " case 1: {\n"
  449. " f();\n"
  450. " break;\n"
  451. " }\n"
  452. "}");
  453. verifyGoogleFormat("switch (test)\n"
  454. " ;");
  455. }
  456. TEST_F(FormatTest, FormatsLabels) {
  457. verifyFormat("void f() {\n"
  458. " some_code();\n"
  459. "test_label:\n"
  460. " some_other_code();\n"
  461. " {\n"
  462. " some_more_code();\n"
  463. " another_label:\n"
  464. " some_more_code();\n"
  465. " }\n"
  466. "}");
  467. verifyFormat("some_code();\n"
  468. "test_label:\n"
  469. "some_other_code();");
  470. }
  471. //===----------------------------------------------------------------------===//
  472. // Tests for comments.
  473. //===----------------------------------------------------------------------===//
  474. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  475. verifyFormat("//* */");
  476. verifyFormat("// line 1\n"
  477. "// line 2\n"
  478. "void f() {}\n");
  479. verifyFormat("void f() {\n"
  480. " // Doesn't do anything\n"
  481. "}");
  482. verifyFormat("void f(int i, // some comment (probably for i)\n"
  483. " int j, // some comment (probably for j)\n"
  484. " int k); // some comment (probably for k)");
  485. verifyFormat("void f(int i,\n"
  486. " // some comment (probably for j)\n"
  487. " int j,\n"
  488. " // some comment (probably for k)\n"
  489. " int k);");
  490. verifyFormat("int i // This is a fancy variable\n"
  491. " = 5; // with nicely aligned comment.");
  492. verifyFormat("// Leading comment.\n"
  493. "int a; // Trailing comment.");
  494. verifyFormat("int a; // Trailing comment\n"
  495. " // on 2\n"
  496. " // or 3 lines.\n"
  497. "int b;");
  498. verifyFormat("int a; // Trailing comment\n"
  499. "\n"
  500. "// Leading comment.\n"
  501. "int b;");
  502. verifyFormat("int a; // Comment.\n"
  503. " // More details.\n"
  504. "int bbbb; // Another comment.");
  505. verifyFormat(
  506. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  507. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  508. "int cccccccccccccccccccccccccccccc; // comment\n"
  509. "int ddd; // looooooooooooooooooooooooong comment\n"
  510. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  511. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  512. "int ccccccccccccccccccc; // comment");
  513. verifyFormat("#include \"a\" // comment\n"
  514. "#include \"a/b/c\" // comment");
  515. verifyFormat("#include <a> // comment\n"
  516. "#include <a/b/c> // comment");
  517. verifyFormat("enum E {\n"
  518. " // comment\n"
  519. " VAL_A, // comment\n"
  520. " VAL_B\n"
  521. "};");
  522. verifyFormat(
  523. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  524. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  525. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  526. " // Comment inside a statement.\n"
  527. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  528. verifyFormat(
  529. "bool aaaaaaaaaaaaa = // comment\n"
  530. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  531. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  532. verifyFormat("int aaaa; // aaaaa\n"
  533. "int aa; // aaaaaaa",
  534. getLLVMStyleWithColumns(20));
  535. EXPECT_EQ("void f() { // This does something ..\n"
  536. "}\n"
  537. "int a; // This is unrelated",
  538. format("void f() { // This does something ..\n"
  539. " }\n"
  540. "int a; // This is unrelated"));
  541. EXPECT_EQ("void f() { // This does something ..\n"
  542. "} // awesome..\n"
  543. "\n"
  544. "int a; // This is unrelated",
  545. format("void f() { // This does something ..\n"
  546. " } // awesome..\n"
  547. " \n"
  548. "int a; // This is unrelated"));
  549. EXPECT_EQ("int i; // single line trailing comment",
  550. format("int i;\\\n// single line trailing comment"));
  551. verifyGoogleFormat("int a; // Trailing comment.");
  552. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  553. " parameter));");
  554. verifyGoogleFormat("#endif // HEADER_GUARD");
  555. verifyFormat("const char *test[] = {\n"
  556. " // A\n"
  557. " \"aaaa\",\n"
  558. " // B\n"
  559. " \"aaaaa\",\n"
  560. "};");
  561. verifyGoogleFormat(
  562. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  563. " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment");
  564. EXPECT_EQ("D(a, {\n"
  565. " // test\n"
  566. " int a;\n"
  567. "});",
  568. format("D(a, {\n"
  569. "// test\n"
  570. "int a;\n"
  571. "});"));
  572. }
  573. TEST_F(FormatTest, CanFormatCommentsLocally) {
  574. EXPECT_EQ("int a; // comment\n"
  575. "int b; // comment",
  576. format("int a; // comment\n"
  577. "int b; // comment",
  578. 0, 0, getLLVMStyle()));
  579. EXPECT_EQ("int a; // comment\n"
  580. " // line 2\n"
  581. "int b;",
  582. format("int a; // comment\n"
  583. " // line 2\n"
  584. "int b;",
  585. 28, 0, getLLVMStyle()));
  586. EXPECT_EQ("int aaaaaa; // comment\n"
  587. "int b;\n"
  588. "int c; // unrelated comment",
  589. format("int aaaaaa; // comment\n"
  590. "int b;\n"
  591. "int c; // unrelated comment",
  592. 31, 0, getLLVMStyle()));
  593. }
  594. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  595. EXPECT_EQ("// comment", format("// comment "));
  596. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  597. format("int aaaaaaa, bbbbbbb; // comment ",
  598. getLLVMStyleWithColumns(33)));
  599. }
  600. TEST_F(FormatTest, UnderstandsMultiLineComments) {
  601. verifyFormat("f(/*test=*/ true);");
  602. EXPECT_EQ(
  603. "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  604. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  605. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n"
  606. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  607. EXPECT_EQ(
  608. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  609. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  610. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  611. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  612. FormatStyle NoBinPacking = getLLVMStyle();
  613. NoBinPacking.BinPackParameters = false;
  614. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  615. " /* parameter 2 */ aaaaaa,\n"
  616. " /* parameter 3 */ aaaaaa,\n"
  617. " /* parameter 4 */ aaaaaa);",
  618. NoBinPacking);
  619. }
  620. TEST_F(FormatTest, AlignsMultiLineComments) {
  621. EXPECT_EQ("/*\n"
  622. " * Really multi-line\n"
  623. " * comment.\n"
  624. " */\n"
  625. "void f() {}",
  626. format(" /*\n"
  627. " * Really multi-line\n"
  628. " * comment.\n"
  629. " */\n"
  630. " void f() {}"));
  631. EXPECT_EQ("class C {\n"
  632. " /*\n"
  633. " * Another multi-line\n"
  634. " * comment.\n"
  635. " */\n"
  636. " void f() {}\n"
  637. "};",
  638. format("class C {\n"
  639. "/*\n"
  640. " * Another multi-line\n"
  641. " * comment.\n"
  642. " */\n"
  643. "void f() {}\n"
  644. "};"));
  645. EXPECT_EQ("/*\n"
  646. " 1. This is a comment with non-trivial formatting.\n"
  647. " 1.1. We have to indent/outdent all lines equally\n"
  648. " 1.1.1. to keep the formatting.\n"
  649. " */",
  650. format(" /*\n"
  651. " 1. This is a comment with non-trivial formatting.\n"
  652. " 1.1. We have to indent/outdent all lines equally\n"
  653. " 1.1.1. to keep the formatting.\n"
  654. " */"));
  655. EXPECT_EQ("/*\n"
  656. " Don't try to outdent if there's not enough inentation.\n"
  657. " */",
  658. format(" /*\n"
  659. " Don't try to outdent if there's not enough inentation.\n"
  660. " */"));
  661. }
  662. TEST_F(FormatTest, SplitsLongCxxComments) {
  663. EXPECT_EQ("// A comment that\n"
  664. "// doesn't fit on\n"
  665. "// one line",
  666. format("// A comment that doesn't fit on one line",
  667. getLLVMStyleWithColumns(20)));
  668. EXPECT_EQ("// a b c d\n"
  669. "// e f g\n"
  670. "// h i j k",
  671. format("// a b c d e f g h i j k",
  672. getLLVMStyleWithColumns(10)));
  673. EXPECT_EQ("// a b c d\n"
  674. "// e f g\n"
  675. "// h i j k",
  676. format("\\\n// a b c d e f g h i j k",
  677. getLLVMStyleWithColumns(10)));
  678. EXPECT_EQ("if (true) // A comment that\n"
  679. " // doesn't fit on\n"
  680. " // one line",
  681. format("if (true) // A comment that doesn't fit on one line ",
  682. getLLVMStyleWithColumns(30)));
  683. EXPECT_EQ("// Don't_touch_leading_whitespace",
  684. format("// Don't_touch_leading_whitespace",
  685. getLLVMStyleWithColumns(20)));
  686. EXPECT_EQ(
  687. "//Don't add leading\n"
  688. "//whitespace",
  689. format("//Don't add leading whitespace", getLLVMStyleWithColumns(20)));
  690. EXPECT_EQ("// A comment before\n"
  691. "// a macro\n"
  692. "// definition\n"
  693. "#define a b",
  694. format("// A comment before a macro definition\n"
  695. "#define a b",
  696. getLLVMStyleWithColumns(20)));
  697. }
  698. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  699. EXPECT_EQ("namespace {\n}\n// Test\n#define A",
  700. format("namespace {}\n // Test\n#define A"));
  701. EXPECT_EQ("namespace {\n}\n/* Test */\n#define A",
  702. format("namespace {}\n /* Test */\n#define A"));
  703. EXPECT_EQ("namespace {\n}\n/* Test */ #define A",
  704. format("namespace {}\n /* Test */ #define A"));
  705. }
  706. TEST_F(FormatTest, SplitsLongLinesInComments) {
  707. EXPECT_EQ("/* This is a long\n"
  708. " * comment that\n"
  709. " * doesn't\n"
  710. " * fit on one line.\n"
  711. " */",
  712. format("/* "
  713. "This is a long "
  714. "comment that "
  715. "doesn't "
  716. "fit on one line. */",
  717. getLLVMStyleWithColumns(20)));
  718. EXPECT_EQ("/* a b c d\n"
  719. " * e f g\n"
  720. " * h i j k\n"
  721. " */",
  722. format("/* a b c d e f g h i j k */",
  723. getLLVMStyleWithColumns(10)));
  724. EXPECT_EQ("/* a b c d\n"
  725. " * e f g\n"
  726. " * h i j k\n"
  727. " */",
  728. format("\\\n/* a b c d e f g h i j k */",
  729. getLLVMStyleWithColumns(10)));
  730. EXPECT_EQ("/*\n"
  731. "This is a long\n"
  732. "comment that doesn't\n"
  733. "fit on one line.\n"
  734. "*/",
  735. format("/*\n"
  736. "This is a long "
  737. "comment that doesn't "
  738. "fit on one line. \n"
  739. "*/", getLLVMStyleWithColumns(20)));
  740. EXPECT_EQ("/*\n"
  741. " * This is a long\n"
  742. " * comment that\n"
  743. " * doesn't fit on\n"
  744. " * one line.\n"
  745. " */",
  746. format("/* \n"
  747. " * This is a long "
  748. " comment that "
  749. " doesn't fit on "
  750. " one line. \n"
  751. " */", getLLVMStyleWithColumns(20)));
  752. EXPECT_EQ("/*\n"
  753. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  754. " * so_it_should_be_broken\n"
  755. " * wherever_a_space_occurs\n"
  756. " */",
  757. format("/*\n"
  758. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  759. " so_it_should_be_broken "
  760. " wherever_a_space_occurs \n"
  761. " */",
  762. getLLVMStyleWithColumns(20)));
  763. EXPECT_EQ("/*\n"
  764. " * This_comment_can_not_be_broken_into_lines\n"
  765. " */",
  766. format("/*\n"
  767. " * This_comment_can_not_be_broken_into_lines\n"
  768. " */",
  769. getLLVMStyleWithColumns(20)));
  770. EXPECT_EQ("{\n"
  771. " /*\n"
  772. " This is another\n"
  773. " long comment that\n"
  774. " doesn't fit on one\n"
  775. " line 1234567890\n"
  776. " */\n"
  777. "}",
  778. format("{\n"
  779. "/*\n"
  780. "This is another "
  781. " long comment that "
  782. " doesn't fit on one"
  783. " line 1234567890\n"
  784. "*/\n"
  785. "}", getLLVMStyleWithColumns(20)));
  786. EXPECT_EQ("{\n"
  787. " /*\n"
  788. " * This i s\n"
  789. " * another comment\n"
  790. " * t hat doesn' t\n"
  791. " * fit on one l i\n"
  792. " * n e\n"
  793. " */\n"
  794. "}",
  795. format("{\n"
  796. "/*\n"
  797. " * This i s"
  798. " another comment"
  799. " t hat doesn' t"
  800. " fit on one l i"
  801. " n e\n"
  802. " */\n"
  803. "}", getLLVMStyleWithColumns(20)));
  804. EXPECT_EQ("/*\n"
  805. " * This is a long\n"
  806. " * comment that\n"
  807. " * doesn't fit on\n"
  808. " * one line\n"
  809. " */",
  810. format(" /*\n"
  811. " * This is a long comment that doesn't fit on one line\n"
  812. " */", getLLVMStyleWithColumns(20)));
  813. EXPECT_EQ("{\n"
  814. " if (something) /* This is a\n"
  815. "long comment */\n"
  816. " ;\n"
  817. "}",
  818. format("{\n"
  819. " if (something) /* This is a long comment */\n"
  820. " ;\n"
  821. "}",
  822. getLLVMStyleWithColumns(30)));
  823. }
  824. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  825. EXPECT_EQ("#define X \\\n"
  826. " /* \\\n"
  827. " Test \\\n"
  828. " Macro comment \\\n"
  829. " with a long \\\n"
  830. " line \\\n"
  831. " */ \\\n"
  832. " A + B",
  833. format("#define X \\\n"
  834. " /*\n"
  835. " Test\n"
  836. " Macro comment with a long line\n"
  837. " */ \\\n"
  838. " A + B",
  839. getLLVMStyleWithColumns(20)));
  840. EXPECT_EQ("#define X \\\n"
  841. " /* Macro comment \\\n"
  842. " with a long \\\n"
  843. " line */ \\\n"
  844. " A + B",
  845. format("#define X \\\n"
  846. " /* Macro comment with a long\n"
  847. " line */ \\\n"
  848. " A + B",
  849. getLLVMStyleWithColumns(20)));
  850. EXPECT_EQ("#define X \\\n"
  851. " /* Macro comment \\\n"
  852. " * with a long \\\n"
  853. " * line */ \\\n"
  854. " A + B",
  855. format("#define X \\\n"
  856. " /* Macro comment with a long line */ \\\n"
  857. " A + B",
  858. getLLVMStyleWithColumns(20)));
  859. }
  860. TEST_F(FormatTest, CommentsInStaticInitializers) {
  861. EXPECT_EQ(
  862. "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  863. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  864. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  865. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  866. " aaaaaaaaaaaaaaaaaaaa };",
  867. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  868. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  869. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  870. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  871. " aaaaaaaaaaaaaaaaaaaa };"));
  872. verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
  873. " bbbbbbbbbbb, ccccccccccc };");
  874. verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
  875. " // comment for bb....\n"
  876. " bbbbbbbbbbb, ccccccccccc };");
  877. verifyGoogleFormat(
  878. "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
  879. " bbbbbbbbbbb, ccccccccccc };");
  880. verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
  881. " // comment for bb....\n"
  882. " bbbbbbbbbbb, ccccccccccc };");
  883. verifyFormat("S s = { { a, b, c }, // Group #1\n"
  884. " { d, e, f }, // Group #2\n"
  885. " { g, h, i } }; // Group #3");
  886. verifyFormat("S s = { { // Group #1\n"
  887. " a, b, c },\n"
  888. " { // Group #2\n"
  889. " d, e, f },\n"
  890. " { // Group #3\n"
  891. " g, h, i } };");
  892. EXPECT_EQ("S s = {\n"
  893. " // Some comment\n"
  894. " a,\n"
  895. "\n"
  896. " // Comment after empty line\n"
  897. " b\n"
  898. "}",
  899. format("S s = {\n"
  900. " // Some comment\n"
  901. " a,\n"
  902. " \n"
  903. " // Comment after empty line\n"
  904. " b\n"
  905. "}"));
  906. EXPECT_EQ("S s = { a, b };", format("S s = {\n"
  907. " a,\n"
  908. "\n"
  909. " b\n"
  910. "};"));
  911. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  912. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  913. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  914. " 0x00, 0x00, 0x00, 0x00 // comment\n"
  915. "};");
  916. }
  917. //===----------------------------------------------------------------------===//
  918. // Tests for classes, namespaces, etc.
  919. //===----------------------------------------------------------------------===//
  920. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  921. verifyFormat("class A {\n};");
  922. }
  923. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  924. verifyFormat("class A {\n"
  925. "public:\n"
  926. "public: // comment\n"
  927. "protected:\n"
  928. "private:\n"
  929. " void f() {}\n"
  930. "};");
  931. verifyGoogleFormat("class A {\n"
  932. " public:\n"
  933. " protected:\n"
  934. " private:\n"
  935. " void f() {}\n"
  936. "};");
  937. }
  938. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  939. EXPECT_EQ("class A {\n"
  940. "public:\n"
  941. " void f();\n"
  942. "\n"
  943. "private:\n"
  944. " void g() {}\n"
  945. " // test\n"
  946. "protected:\n"
  947. " int h;\n"
  948. "};",
  949. format("class A {\n"
  950. "public:\n"
  951. "void f();\n"
  952. "private:\n"
  953. "void g() {}\n"
  954. "// test\n"
  955. "protected:\n"
  956. "int h;\n"
  957. "};"));
  958. }
  959. TEST_F(FormatTest, FormatsClasses) {
  960. verifyFormat("class A : public B {\n};");
  961. verifyFormat("class A : public ::B {\n};");
  962. verifyFormat(
  963. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  964. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
  965. "};\n");
  966. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  967. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  968. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
  969. "};\n");
  970. verifyFormat(
  971. "class A : public B, public C, public D, public E, public F, public G {\n"
  972. "};");
  973. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  974. " public C,\n"
  975. " public D,\n"
  976. " public E,\n"
  977. " public F,\n"
  978. " public G {\n"
  979. "};");
  980. verifyFormat("class\n"
  981. " ReallyReallyLongClassName {\n};",
  982. getLLVMStyleWithColumns(32));
  983. }
  984. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  985. verifyFormat("class A {\n} a, b;");
  986. verifyFormat("struct A {\n} a, b;");
  987. verifyFormat("union A {\n} a;");
  988. }
  989. TEST_F(FormatTest, FormatsEnum) {
  990. verifyFormat("enum {\n"
  991. " Zero,\n"
  992. " One = 1,\n"
  993. " Two = One + 1,\n"
  994. " Three = (One + Two),\n"
  995. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  996. " Five = (One, Two, Three, Four, 5)\n"
  997. "};");
  998. verifyFormat("enum Enum {\n"
  999. "};");
  1000. verifyFormat("enum {\n"
  1001. "};");
  1002. verifyFormat("enum X E {\n} d;");
  1003. verifyFormat("enum __attribute__((...)) E {\n} d;");
  1004. verifyFormat("enum __declspec__((...)) E {\n} d;");
  1005. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1006. }
  1007. TEST_F(FormatTest, FormatsBitfields) {
  1008. verifyFormat("struct Bitfields {\n"
  1009. " unsigned sClass : 8;\n"
  1010. " unsigned ValueKind : 2;\n"
  1011. "};");
  1012. }
  1013. TEST_F(FormatTest, FormatsNamespaces) {
  1014. verifyFormat("namespace some_namespace {\n"
  1015. "class A {\n};\n"
  1016. "void f() { f(); }\n"
  1017. "}");
  1018. verifyFormat("namespace {\n"
  1019. "class A {\n};\n"
  1020. "void f() { f(); }\n"
  1021. "}");
  1022. verifyFormat("inline namespace X {\n"
  1023. "class A {\n};\n"
  1024. "void f() { f(); }\n"
  1025. "}");
  1026. verifyFormat("using namespace some_namespace;\n"
  1027. "class A {\n};\n"
  1028. "void f() { f(); }");
  1029. // This code is more common than we thought; if we
  1030. // layout this correctly the semicolon will go into
  1031. // its own line, which is undesireable.
  1032. verifyFormat("namespace {\n};");
  1033. verifyFormat("namespace {\n"
  1034. "class A {\n"
  1035. "};\n"
  1036. "};");
  1037. }
  1038. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  1039. TEST_F(FormatTest, FormatsInlineASM) {
  1040. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  1041. verifyFormat(
  1042. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  1043. " \"cpuid\\n\\t\"\n"
  1044. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  1045. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  1046. " : \"a\"(value));");
  1047. }
  1048. TEST_F(FormatTest, FormatTryCatch) {
  1049. // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
  1050. // also not create single-line-blocks.
  1051. verifyFormat("try {\n"
  1052. " throw a * b;\n"
  1053. "}\n"
  1054. "catch (int a) {\n"
  1055. " // Do nothing.\n"
  1056. "}\n"
  1057. "catch (...) {\n"
  1058. " exit(42);\n"
  1059. "}");
  1060. // Function-level try statements.
  1061. verifyFormat("int f() try { return 4; }\n"
  1062. "catch (...) {\n"
  1063. " return 5;\n"
  1064. "}");
  1065. verifyFormat("class A {\n"
  1066. " int a;\n"
  1067. " A() try : a(0) {}\n"
  1068. " catch (...) {\n"
  1069. " throw;\n"
  1070. " }\n"
  1071. "};\n");
  1072. }
  1073. TEST_F(FormatTest, FormatObjCTryCatch) {
  1074. verifyFormat("@try {\n"
  1075. " f();\n"
  1076. "}\n"
  1077. "@catch (NSException e) {\n"
  1078. " @throw;\n"
  1079. "}\n"
  1080. "@finally {\n"
  1081. " exit(42);\n"
  1082. "}");
  1083. }
  1084. TEST_F(FormatTest, StaticInitializers) {
  1085. verifyFormat("static SomeClass SC = { 1, 'a' };");
  1086. // FIXME: Format like enums if the static initializer does not fit on a line.
  1087. verifyFormat(
  1088. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  1089. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  1090. "};");
  1091. verifyFormat(
  1092. "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
  1093. " looooooooooooooooooooooooooooooooooongname,\n"
  1094. " looooooooooooooooooooooooooooooong };");
  1095. // Allow bin-packing in static initializers as this would often lead to
  1096. // terrible results, e.g.:
  1097. verifyGoogleFormat(
  1098. "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
  1099. " looooooooooooooooooooooooooooooooooongname,\n"
  1100. " looooooooooooooooooooooooooooooong };");
  1101. // Here, everything other than the "}" would fit on a line.
  1102. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  1103. " 100000000000000000000000\n"
  1104. "};");
  1105. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  1106. // line. However, the formatting looks a bit off and this probably doesn't
  1107. // happen often in practice.
  1108. verifyFormat("static int Variable[1] = {\n"
  1109. " { 1000000000000000000000000000000000000 }\n"
  1110. "};",
  1111. getLLVMStyleWithColumns(40));
  1112. }
  1113. TEST_F(FormatTest, NestedStaticInitializers) {
  1114. verifyFormat("static A x = { { {} } };\n");
  1115. verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
  1116. " { init1, init2, init3, init4 } } };");
  1117. verifyFormat("somes Status::global_reps[3] = {\n"
  1118. " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
  1119. " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
  1120. " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
  1121. "};");
  1122. verifyGoogleFormat("somes Status::global_reps[3] = {\n"
  1123. " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
  1124. " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
  1125. " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
  1126. "};");
  1127. verifyFormat(
  1128. "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
  1129. " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
  1130. " } };");
  1131. verifyFormat(
  1132. "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
  1133. " { 111111111111111111111111111111,\n"
  1134. " 222222222222222222222222222222,\n"
  1135. " 333333333333333333333333333333 },\n"
  1136. " { 1, 2, 3 }, { 1, 2, 3 } } };");
  1137. verifyFormat(
  1138. "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
  1139. " { { 111111111111111111111111111111,\n"
  1140. " 222222222222222222222222222222,\n"
  1141. " 333333333333333333333333333333 } },\n"
  1142. " { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
  1143. // FIXME: We might at some point want to handle this similar to parameter
  1144. // lists, where we have an option to put each on a single line.
  1145. verifyFormat(
  1146. "struct {\n"
  1147. " unsigned bit;\n"
  1148. " const char *const name;\n"
  1149. "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
  1150. " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
  1151. }
  1152. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  1153. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1154. " \\\n"
  1155. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  1156. }
  1157. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  1158. verifyFormat(
  1159. "virtual void write(ELFWriter *writerrr,\n"
  1160. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  1161. }
  1162. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  1163. EXPECT_EQ("#123 \"A string literal\"",
  1164. format(" # 123 \"A string literal\""));
  1165. EXPECT_EQ("#;", format("#;"));
  1166. verifyFormat("#\n;\n;\n;");
  1167. }
  1168. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  1169. EXPECT_EQ("#line 42 \"test\"\n",
  1170. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  1171. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  1172. getLLVMStyleWithColumns(12)));
  1173. }
  1174. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  1175. EXPECT_EQ("#line 42 \"test\"",
  1176. format("# \\\n line \\\n 42 \\\n \"test\""));
  1177. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  1178. }
  1179. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  1180. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  1181. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  1182. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  1183. // FIXME: We never break before the macro name.
  1184. verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
  1185. verifyFormat("#define A A\n#define A A");
  1186. verifyFormat("#define A(X) A\n#define A A");
  1187. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  1188. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  1189. }
  1190. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  1191. EXPECT_EQ("// somecomment\n"
  1192. "#include \"a.h\"\n"
  1193. "#define A( \\\n"
  1194. " A, B)\n"
  1195. "#include \"b.h\"\n"
  1196. "// somecomment\n",
  1197. format(" // somecomment\n"
  1198. " #include \"a.h\"\n"
  1199. "#define A(A,\\\n"
  1200. " B)\n"
  1201. " #include \"b.h\"\n"
  1202. " // somecomment\n",
  1203. getLLVMStyleWithColumns(13)));
  1204. }
  1205. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  1206. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  1207. EXPECT_EQ("#define A \\\n"
  1208. " c; \\\n"
  1209. " e;\n"
  1210. "f;",
  1211. format("#define A c; e;\n"
  1212. "f;",
  1213. getLLVMStyleWithColumns(14)));
  1214. }
  1215. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  1216. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  1217. EXPECT_EQ("int i;\n"
  1218. "#define A \\\n"
  1219. " int i; \\\n"
  1220. " int j\n"
  1221. "int k;",
  1222. format("int i;\n"
  1223. "#define A \\\n"
  1224. " int i ; \\\n"
  1225. " int j\n"
  1226. "int k;",
  1227. 8, 0, getGoogleStyle())); // 8: position of "#define".
  1228. EXPECT_EQ("int i;\n"
  1229. "#define A \\\n"
  1230. " int i; \\\n"
  1231. " int j\n"
  1232. "int k;",
  1233. format("int i;\n"
  1234. "#define A \\\n"
  1235. " int i ; \\\n"
  1236. " int j\n"
  1237. "int k;",
  1238. 45, 0, getGoogleStyle())); // 45: position of "j".
  1239. }
  1240. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  1241. EXPECT_EQ("int x,\n"
  1242. "#define A\n"
  1243. " y;",
  1244. format("int x,\n#define A\ny;"));
  1245. }
  1246. TEST_F(FormatTest, HashInMacroDefinition) {
  1247. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  1248. verifyFormat("#define A \\\n"
  1249. " { \\\n"
  1250. " f(#c);\\\n"
  1251. " }",
  1252. getLLVMStyleWithColumns(11));
  1253. verifyFormat("#define A(X) \\\n"
  1254. " void function##X()",
  1255. getLLVMStyleWithColumns(22));
  1256. verifyFormat("#define A(a, b, c) \\\n"
  1257. " void a##b##c()",
  1258. getLLVMStyleWithColumns(22));
  1259. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  1260. }
  1261. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  1262. verifyFormat("#define A (1)");
  1263. }
  1264. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  1265. EXPECT_EQ("#define A b;", format("#define A \\\n"
  1266. " \\\n"
  1267. " b;",
  1268. getLLVMStyleWithColumns(25)));
  1269. EXPECT_EQ("#define A \\\n"
  1270. " \\\n"
  1271. " a; \\\n"
  1272. " b;",
  1273. format("#define A \\\n"
  1274. " \\\n"
  1275. " a; \\\n"
  1276. " b;",
  1277. getLLVMStyleWithColumns(11)));
  1278. EXPECT_EQ("#define A \\\n"
  1279. " a; \\\n"
  1280. " \\\n"
  1281. " b;",
  1282. format("#define A \\\n"
  1283. " a; \\\n"
  1284. " \\\n"
  1285. " b;",
  1286. getLLVMStyleWithColumns(11)));
  1287. }
  1288. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  1289. verifyFormat("#define A :");
  1290. // FIXME: Improve formatting of case labels in macros.
  1291. verifyFormat("#define SOMECASES \\\n"
  1292. " case 1: \\\n"
  1293. " case 2\n",
  1294. getLLVMStyleWithColumns(20));
  1295. verifyFormat("#define A template <typename T>");
  1296. verifyFormat("#define STR(x) #x\n"
  1297. "f(STR(this_is_a_string_literal{));");
  1298. }
  1299. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  1300. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1301. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1302. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1303. "class X {\n"
  1304. "};\n"
  1305. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1306. "int *createScopDetectionPass() { return 0; }",
  1307. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1308. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1309. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1310. " class X {};\n"
  1311. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1312. " int *createScopDetectionPass() { return 0; }"));
  1313. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  1314. // braces, so that inner block is indented one level more.
  1315. EXPECT_EQ("int q() {\n"
  1316. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1317. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1318. " IPC_END_MESSAGE_MAP()\n"
  1319. "}",
  1320. format("int q() {\n"
  1321. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1322. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1323. " IPC_END_MESSAGE_MAP()\n"
  1324. "}"));
  1325. EXPECT_EQ("int q() {\n"
  1326. " f(x);\n"
  1327. " f(x) {}\n"
  1328. " f(x)->g();\n"
  1329. " f(x)->*g();\n"
  1330. " f(x).g();\n"
  1331. " f(x) = x;\n"
  1332. " f(x) += x;\n"
  1333. " f(x) -= x;\n"
  1334. " f(x) *= x;\n"
  1335. " f(x) /= x;\n"
  1336. " f(x) %= x;\n"
  1337. " f(x) &= x;\n"
  1338. " f(x) |= x;\n"
  1339. " f(x) ^= x;\n"
  1340. " f(x) >>= x;\n"
  1341. " f(x) <<= x;\n"
  1342. " f(x)[y].z();\n"
  1343. " LOG(INFO) << x;\n"
  1344. " ifstream(x) >> x;\n"
  1345. "}\n",
  1346. format("int q() {\n"
  1347. " f(x)\n;\n"
  1348. " f(x)\n {}\n"
  1349. " f(x)\n->g();\n"
  1350. " f(x)\n->*g();\n"
  1351. " f(x)\n.g();\n"
  1352. " f(x)\n = x;\n"
  1353. " f(x)\n += x;\n"
  1354. " f(x)\n -= x;\n"
  1355. " f(x)\n *= x;\n"
  1356. " f(x)\n /= x;\n"
  1357. " f(x)\n %= x;\n"
  1358. " f(x)\n &= x;\n"
  1359. " f(x)\n |= x;\n"
  1360. " f(x)\n ^= x;\n"
  1361. " f(x)\n >>= x;\n"
  1362. " f(x)\n <<= x;\n"
  1363. " f(x)\n[y].z();\n"
  1364. " LOG(INFO)\n << x;\n"
  1365. " ifstream(x)\n >> x;\n"
  1366. "}\n"));
  1367. EXPECT_EQ("int q() {\n"
  1368. " f(x)\n"
  1369. " if (1) {\n"
  1370. " }\n"
  1371. " f(x)\n"
  1372. " while (1) {\n"
  1373. " }\n"
  1374. " f(x)\n"
  1375. " g(x);\n"
  1376. " f(x)\n"
  1377. " try {\n"
  1378. " q();\n"
  1379. " }\n"
  1380. " catch (...) {\n"
  1381. " }\n"
  1382. "}\n",
  1383. format("int q() {\n"
  1384. "f(x)\n"
  1385. "if (1) {}\n"
  1386. "f(x)\n"
  1387. "while (1) {}\n"
  1388. "f(x)\n"
  1389. "g(x);\n"
  1390. "f(x)\n"
  1391. "try { q(); } catch (...) {}\n"
  1392. "}\n"));
  1393. EXPECT_EQ("class A {\n"
  1394. " A() : t(0) {}\n"
  1395. " A(X x)\n" // FIXME: function-level try blocks are broken.
  1396. " try : t(0) {\n"
  1397. " }\n"
  1398. " catch (...) {\n"
  1399. " }\n"
  1400. "};",
  1401. format("class A {\n"
  1402. " A()\n : t(0) {}\n"
  1403. " A(X x)\n"
  1404. " try : t(0) {} catch (...) {}\n"
  1405. "};"));
  1406. }
  1407. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  1408. verifyFormat("#define A \\\n"
  1409. " f({ \\\n"
  1410. " g(); \\\n"
  1411. " });", getLLVMStyleWithColumns(11));
  1412. }
  1413. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  1414. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  1415. }
  1416. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  1417. verifyFormat("{\n { a #c; }\n}");
  1418. }
  1419. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  1420. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  1421. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  1422. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  1423. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  1424. }
  1425. TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
  1426. EXPECT_EQ(
  1427. "#define A \\\n int i; \\\n int j;",
  1428. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  1429. }
  1430. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  1431. verifyFormat("#define A \\\n"
  1432. " int v( \\\n"
  1433. " a); \\\n"
  1434. " int i;",
  1435. getLLVMStyleWithColumns(11));
  1436. }
  1437. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  1438. EXPECT_EQ(
  1439. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1440. " \\\n"
  1441. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1442. "\n"
  1443. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1444. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  1445. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1446. "\\\n"
  1447. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1448. " \n"
  1449. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1450. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  1451. }
  1452. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  1453. EXPECT_EQ("int\n"
  1454. "#define A\n"
  1455. " a;",
  1456. format("int\n#define A\na;"));
  1457. verifyFormat("functionCallTo(\n"
  1458. " someOtherFunction(\n"
  1459. " withSomeParameters, whichInSequence,\n"
  1460. " areLongerThanALine(andAnotherCall,\n"
  1461. "#define A B\n"
  1462. " withMoreParamters,\n"
  1463. " whichStronglyInfluenceTheLayout),\n"
  1464. " andMoreParameters),\n"
  1465. " trailing);",
  1466. getLLVMStyleWithColumns(69));
  1467. }
  1468. TEST_F(FormatTest, LayoutBlockInsideParens) {
  1469. EXPECT_EQ("functionCall({\n"
  1470. " int i;\n"
  1471. "});",
  1472. format(" functionCall ( {int i;} );"));
  1473. }
  1474. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  1475. EXPECT_EQ("SOME_MACRO { int i; }\n"
  1476. "int i;",
  1477. format(" SOME_MACRO {int i;} int i;"));
  1478. }
  1479. TEST_F(FormatTest, LayoutNestedBlocks) {
  1480. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  1481. " struct s {\n"
  1482. " int i;\n"
  1483. " };\n"
  1484. " s kBitsToOs[] = { { 10 } };\n"
  1485. " for (int i = 0; i < 10; ++i)\n"
  1486. " return;\n"
  1487. "}");
  1488. }
  1489. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  1490. EXPECT_EQ("{}", format("{}"));
  1491. // Negative test for enum.
  1492. verifyFormat("enum E {\n};");
  1493. // Note that when there's a missing ';', we still join...
  1494. verifyFormat("enum E {}");
  1495. }
  1496. //===----------------------------------------------------------------------===//
  1497. // Line break tests.
  1498. //===----------------------------------------------------------------------===//
  1499. TEST_F(FormatTest, FormatsAwesomeMethodCall) {
  1500. verifyFormat(
  1501. "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  1502. " parameter, parameter, parameter)),\n"
  1503. " SecondLongCall(parameter));");
  1504. }
  1505. TEST_F(FormatTest, PreventConfusingIndents) {
  1506. verifyFormat(
  1507. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1508. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1509. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  1510. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  1511. verifyFormat(
  1512. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
  1513. " aaaaaaaaaaaaaaaaaaaaaaaa[\n"
  1514. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n"
  1515. " aaaaaaaaaaaaaaaaaaaaaaaa];");
  1516. verifyFormat(
  1517. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  1518. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  1519. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  1520. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  1521. verifyFormat("int a = bbbb && ccc && fffff(\n"
  1522. "#define A Just forcing a new line\n"
  1523. " ddd);");
  1524. }
  1525. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  1526. verifyFormat(
  1527. "bool aaaaaaa =\n"
  1528. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  1529. " bbbbbbbb();");
  1530. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  1531. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  1532. " ccccccccc == ddddddddddd;");
  1533. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  1534. " aaaaaa) &&\n"
  1535. " bbbbbb && cccccc;");
  1536. verifyFormat("Whitespaces.addUntouchableComment(\n"
  1537. " SourceMgr.getSpellingColumnNumber(\n"
  1538. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  1539. " 1);");
  1540. FormatStyle OnePerLine = getLLVMStyle();
  1541. OnePerLine.BinPackParameters = false;
  1542. verifyFormat(
  1543. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1544. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1545. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  1546. OnePerLine);
  1547. }
  1548. TEST_F(FormatTest, ExpressionIndentation) {
  1549. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1550. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1551. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  1552. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  1553. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  1554. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  1555. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  1556. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  1557. " ccccccccccccccccccccccccccccccccccccccccc;");
  1558. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  1559. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1560. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  1561. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  1562. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1563. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  1564. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  1565. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  1566. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  1567. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  1568. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1569. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  1570. }
  1571. TEST_F(FormatTest, ConstructorInitializers) {
  1572. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  1573. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  1574. getLLVMStyleWithColumns(45));
  1575. verifyFormat("Constructor()\n"
  1576. " : Inttializer(FitsOnTheLine) {}",
  1577. getLLVMStyleWithColumns(44));
  1578. verifyFormat("Constructor()\n"
  1579. " : Inttializer(FitsOnTheLine) {}",
  1580. getLLVMStyleWithColumns(43));
  1581. verifyFormat(
  1582. "SomeClass::Constructor()\n"
  1583. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  1584. verifyFormat(
  1585. "SomeClass::Constructor()\n"
  1586. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  1587. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  1588. verifyFormat(
  1589. "SomeClass::Constructor()\n"
  1590. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  1591. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  1592. verifyFormat("Constructor()\n"
  1593. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  1594. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1595. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  1596. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  1597. verifyFormat("Constructor()\n"
  1598. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1599. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  1600. verifyFormat("Constructor(int Parameter = 0)\n"
  1601. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  1602. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  1603. // Here a line could be saved by splitting the second initializer onto two
  1604. // lines, but that is not desireable.
  1605. verifyFormat("Constructor()\n"
  1606. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  1607. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  1608. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  1609. FormatStyle OnePerLine = getLLVMStyle();
  1610. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  1611. verifyFormat("SomeClass::Constructor()\n"
  1612. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  1613. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  1614. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  1615. OnePerLine);
  1616. verifyFormat("SomeClass::Constructor()\n"
  1617. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  1618. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  1619. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  1620. OnePerLine);
  1621. verifyFormat("MyClass::MyClass(int var)\n"
  1622. " : some_var_(var), // 4 space indent\n"
  1623. " some_other_var_(var + 1) { // lined up\n"
  1624. "}",
  1625. OnePerLine);
  1626. verifyFormat("Constructor()\n"
  1627. " : aaaaa(aaaaaa),\n"
  1628. " aaaaa(aaaaaa),\n"
  1629. " aaaaa(aaaaaa),\n"
  1630. " aaaaa(aaaaaa),\n"
  1631. " aaaaa(aaaaaa) {}",
  1632. OnePerLine);
  1633. verifyFormat("Constructor()\n"
  1634. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  1635. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  1636. OnePerLine);
  1637. }
  1638. TEST_F(FormatTest, MemoizationTests) {
  1639. // This breaks if the memoization lookup does not take \c Indent and
  1640. // \c LastSpace into account.
  1641. verifyFormat(
  1642. "extern CFRunLoopTimerRef\n"
  1643. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  1644. " CFTimeInterval interval, CFOptionFlags flags,\n"
  1645. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  1646. " CFRunLoopTimerContext *context) {}");
  1647. // Deep nesting somewhat works around our memoization.
  1648. verifyFormat(
  1649. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  1650. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  1651. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  1652. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  1653. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  1654. getLLVMStyleWithColumns(65));
  1655. verifyFormat(
  1656. "aaaaa(\n"
  1657. " aaaaa,\n"
  1658. " aaaaa(\n"
  1659. " aaaaa,\n"
  1660. " aaaaa(\n"
  1661. " aaaaa,\n"
  1662. " aaaaa(\n"
  1663. " aaaaa,\n"
  1664. " aaaaa(\n"
  1665. " aaaaa,\n"
  1666. " aaaaa(\n"
  1667. " aaaaa,\n"
  1668. " aaaaa(\n"
  1669. " aaaaa,\n"
  1670. " aaaaa(\n"
  1671. " aaaaa,\n"
  1672. " aaaaa(\n"
  1673. " aaaaa,\n"
  1674. " aaaaa(\n"
  1675. " aaaaa,\n"
  1676. " aaaaa(\n"
  1677. " aaaaa,\n"
  1678. " aaaaa(\n"
  1679. " aaaaa,\n"
  1680. " aaaaa))))))))))));",
  1681. getLLVMStyleWithColumns(65));
  1682. verifyFormat(
  1683. "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"
  1684. " a),\n"
  1685. " a),\n"
  1686. " a),\n"
  1687. " a),\n"
  1688. " a),\n"
  1689. " a),\n"
  1690. " a),\n"
  1691. " a),\n"
  1692. " a),\n"
  1693. " a),\n"
  1694. " a),\n"
  1695. " a),\n"
  1696. " a),\n"
  1697. " a),\n"
  1698. " a),\n"
  1699. " a),\n"
  1700. " a)",
  1701. getLLVMStyleWithColumns(65));
  1702. // This test takes VERY long when memoization is broken.
  1703. FormatStyle OnePerLine = getLLVMStyle();
  1704. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  1705. OnePerLine.BinPackParameters = false;
  1706. std::string input = "Constructor()\n"
  1707. " : aaaa(a,\n";
  1708. for (unsigned i = 0, e = 80; i != e; ++i) {
  1709. input += " a,\n";
  1710. }
  1711. input += " a) {}";
  1712. verifyFormat(input, OnePerLine);
  1713. }
  1714. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  1715. verifyFormat(
  1716. "void f() {\n"
  1717. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  1718. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  1719. " f();\n"
  1720. "}");
  1721. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  1722. " Intervals[i - 1].getRange().getLast()) {\n}");
  1723. }
  1724. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  1725. // Principially, we break function declarations in a certain order:
  1726. // 1) break amongst arguments.
  1727. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  1728. " Cccccccccccccc cccccccccccccc);");
  1729. // 2) break after return type.
  1730. verifyFormat(
  1731. "Aaaaaaaaaaaaaaaaaaaaaaaa\n"
  1732. " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
  1733. // 3) break after (.
  1734. verifyFormat(
  1735. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  1736. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
  1737. // 4) break before after nested name specifiers.
  1738. verifyFormat(
  1739. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1740. " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  1741. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
  1742. // However, there are exceptions, if a sufficient amount of lines can be
  1743. // saved.
  1744. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  1745. // more adjusting.
  1746. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  1747. " Cccccccccccccc cccccccccc,\n"
  1748. " Cccccccccccccc cccccccccc,\n"
  1749. " Cccccccccccccc cccccccccc,\n"
  1750. " Cccccccccccccc cccccccccc);");
  1751. verifyFormat(
  1752. "Aaaaaaaaaaaaaaaaaa\n"
  1753. " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  1754. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  1755. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  1756. verifyFormat(
  1757. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  1758. " Cccccccccccccc cccccccccc,\n"
  1759. " Cccccccccccccc cccccccccc,\n"
  1760. " Cccccccccccccc cccccccccc,\n"
  1761. " Cccccccccccccc cccccccccc,\n"
  1762. " Cccccccccccccc cccccccccc,\n"
  1763. " Cccccccccccccc cccccccccc);");
  1764. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  1765. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  1766. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  1767. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  1768. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  1769. // Break after multi-line parameters.
  1770. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1771. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1772. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1773. " bbbb bbbb);");
  1774. }
  1775. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  1776. verifyFormat("void someLongFunction(\n"
  1777. " int someLongParameter) const {}",
  1778. getLLVMStyleWithColumns(46));
  1779. FormatStyle Style = getGoogleStyle();
  1780. Style.ColumnLimit = 47;
  1781. verifyFormat("void\n"
  1782. "someLongFunction(int someLongParameter) const {\n}",
  1783. getLLVMStyleWithColumns(47));
  1784. verifyFormat("void someLongFunction(\n"
  1785. " int someLongParameter) const {}",
  1786. Style);
  1787. verifyFormat("LoooooongReturnType\n"
  1788. "someLoooooooongFunction() const {}",
  1789. getLLVMStyleWithColumns(47));
  1790. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  1791. " const {}",
  1792. Style);
  1793. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  1794. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  1795. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  1796. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  1797. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  1798. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  1799. verifyFormat(
  1800. "void aaaaaaaaaaaaaaaaaa()\n"
  1801. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  1802. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  1803. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1804. " __attribute__((unused));");
  1805. verifyFormat(
  1806. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1807. " GUARDED_BY(aaaaaaaaaaaa);");
  1808. }
  1809. TEST_F(FormatTest, BreaksDesireably) {
  1810. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  1811. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  1812. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  1813. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1814. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  1815. "}");
  1816. verifyFormat(
  1817. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1818. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  1819. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1820. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1821. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  1822. verifyFormat(
  1823. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1824. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  1825. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1826. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  1827. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1828. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1829. verifyFormat(
  1830. "void f() {\n"
  1831. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  1832. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  1833. "}");
  1834. verifyFormat(
  1835. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1836. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  1837. verifyFormat(
  1838. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1839. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  1840. verifyFormat(
  1841. "aaaaaaaaaaaaaaaaa(\n"
  1842. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1843. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1844. // This test case breaks on an incorrect memoization, i.e. an optimization not
  1845. // taking into account the StopAt value.
  1846. verifyFormat(
  1847. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1848. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1849. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1850. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1851. verifyFormat("{\n {\n {\n"
  1852. " Annotation.SpaceRequiredBefore =\n"
  1853. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  1854. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  1855. " }\n }\n}");
  1856. }
  1857. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  1858. FormatStyle NoBinPacking = getGoogleStyle();
  1859. NoBinPacking.BinPackParameters = false;
  1860. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  1861. " aaaaaaaaaaaaaaaaaaaa,\n"
  1862. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  1863. NoBinPacking);
  1864. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  1865. " aaaaaaaaaaaaa,\n"
  1866. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  1867. NoBinPacking);
  1868. verifyFormat(
  1869. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  1870. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1871. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  1872. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1873. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  1874. NoBinPacking);
  1875. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  1876. " .aaaaaaaaaaaaaaaaaa();",
  1877. NoBinPacking);
  1878. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1879. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
  1880. NoBinPacking);
  1881. verifyFormat(
  1882. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1883. " aaaaaaaaaaaa,\n"
  1884. " aaaaaaaaaaaa);",
  1885. NoBinPacking);
  1886. verifyFormat(
  1887. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  1888. " ddddddddddddddddddddddddddddd),\n"
  1889. " test);",
  1890. NoBinPacking);
  1891. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  1892. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  1893. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  1894. NoBinPacking);
  1895. verifyFormat("a(\"a\"\n"
  1896. " \"a\",\n"
  1897. " a);");
  1898. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  1899. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  1900. " aaaaaaaaa,\n"
  1901. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  1902. NoBinPacking);
  1903. verifyFormat(
  1904. "void f() {\n"
  1905. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  1906. " .aaaaaaa();\n"
  1907. "}",
  1908. NoBinPacking);
  1909. verifyFormat(
  1910. "template <class SomeType, class SomeOtherType>\n"
  1911. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  1912. NoBinPacking);
  1913. }
  1914. TEST_F(FormatTest, FormatsBuilderPattern) {
  1915. verifyFormat(
  1916. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  1917. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  1918. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
  1919. " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
  1920. " .Default(ORDER_TEXT);\n");
  1921. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  1922. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  1923. verifyFormat(
  1924. "aaaaaaa->aaaaaaa\n"
  1925. " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  1926. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  1927. verifyFormat(
  1928. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  1929. " aaaaaaaaaaaaaa);");
  1930. verifyFormat(
  1931. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
  1932. " ->aaaaaaaaaaaaaaaa(\n"
  1933. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  1934. " ->aaaaaaaaaaaaaaaaa();");
  1935. }
  1936. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  1937. verifyFormat(
  1938. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1939. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  1940. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  1941. " ccccccccccccccccccccccccc) {\n}");
  1942. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  1943. " ccccccccccccccccccccccccc) {\n}");
  1944. verifyFormat(
  1945. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  1946. " ccccccccccccccccccccccccc) {\n}");
  1947. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  1948. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  1949. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  1950. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  1951. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  1952. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  1953. " aaaaaaaaaaaaaaa != aa) {\n}");
  1954. }
  1955. TEST_F(FormatTest, BreaksAfterAssignments) {
  1956. verifyFormat(
  1957. "unsigned Cost =\n"
  1958. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  1959. " SI->getPointerAddressSpaceee());\n");
  1960. verifyFormat(
  1961. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  1962. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  1963. verifyFormat(
  1964. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n"
  1965. " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  1966. }
  1967. TEST_F(FormatTest, AlignsAfterAssignments) {
  1968. verifyFormat(
  1969. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1970. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  1971. verifyFormat(
  1972. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1973. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  1974. verifyFormat(
  1975. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1976. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  1977. verifyFormat(
  1978. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1979. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  1980. verifyFormat("double LooooooooooooooooooooooooongResult =\n"
  1981. " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1982. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  1983. }
  1984. TEST_F(FormatTest, AlignsAfterReturn) {
  1985. verifyFormat(
  1986. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1987. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  1988. verifyFormat(
  1989. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  1990. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  1991. verifyFormat(
  1992. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  1993. " aaaaaaaaaaaaaaaaaaaaaa();");
  1994. verifyFormat(
  1995. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  1996. " aaaaaaaaaaaaaaaaaaaaaa());");
  1997. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1998. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1999. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2000. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  2001. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2002. }
  2003. TEST_F(FormatTest, BreaksConditionalExpressions) {
  2004. verifyFormat(
  2005. "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
  2006. " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2007. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2008. verifyFormat(
  2009. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2010. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2011. verifyFormat(
  2012. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  2013. " : aaaaaaaaaaaaa);");
  2014. verifyFormat(
  2015. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2016. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2017. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2018. " aaaaaaaaaaaaa);");
  2019. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2020. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2021. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2022. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2023. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2024. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2025. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2026. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2027. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2028. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2029. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2030. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2031. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2032. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2033. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2034. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  2035. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2036. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2037. " : aaaaaaaaaaaaaaaa;");
  2038. verifyFormat(
  2039. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2040. " ? aaaaaaaaaaaaaaa\n"
  2041. " : aaaaaaaaaaaaaaa;");
  2042. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  2043. " aaaaaaaaa\n"
  2044. " ? b\n"
  2045. " : c);");
  2046. verifyFormat(
  2047. "unsigned Indent =\n"
  2048. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  2049. " ? IndentForLevel[TheLine.Level]\n"
  2050. " : TheLine * 2,\n"
  2051. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  2052. getLLVMStyleWithColumns(70));
  2053. FormatStyle NoBinPacking = getLLVMStyle();
  2054. NoBinPacking.BinPackParameters = false;
  2055. verifyFormat(
  2056. "void f() {\n"
  2057. " g(aaa,\n"
  2058. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  2059. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2060. " ? aaaaaaaaaaaaaaa\n"
  2061. " : aaaaaaaaaaaaaaa);\n"
  2062. "}",
  2063. NoBinPacking);
  2064. }
  2065. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  2066. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  2067. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  2068. verifyFormat("bool a = true, b = false;");
  2069. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2070. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  2071. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  2072. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  2073. verifyFormat(
  2074. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2075. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  2076. " d = e && f;");
  2077. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  2078. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  2079. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  2080. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  2081. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  2082. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  2083. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  2084. // line. Also fix indent for breaking after the type, this looks bad.
  2085. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2086. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  2087. " *b = bbbbbbbbbbbbbbbbbbb;");
  2088. // Not ideal, but pointer-with-type does not allow much here.
  2089. verifyGoogleFormat(
  2090. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  2091. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
  2092. }
  2093. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  2094. verifyFormat("arr[foo ? bar : baz];");
  2095. verifyFormat("f()[foo ? bar : baz];");
  2096. verifyFormat("(a + b)[foo ? bar : baz];");
  2097. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  2098. }
  2099. TEST_F(FormatTest, AlignsStringLiterals) {
  2100. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  2101. " \"short literal\");");
  2102. verifyFormat(
  2103. "looooooooooooooooooooooooongFunction(\n"
  2104. " \"short literal\"\n"
  2105. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  2106. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  2107. " \" string literals\",\n"
  2108. " and, other, parameters);");
  2109. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  2110. " \"5678\";",
  2111. format("fun + \"1243\" /* comment */\n"
  2112. " \"5678\";",
  2113. getLLVMStyleWithColumns(28)));
  2114. EXPECT_EQ(
  2115. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  2116. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  2117. " \"aaaaaaaaaaaaaaaa\";",
  2118. format("aaaaaa ="
  2119. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  2120. "aaaaaaaaaaaaaaaaaaaaa\" "
  2121. "\"aaaaaaaaaaaaaaaa\";"));
  2122. verifyFormat("a = a + \"a\"\n"
  2123. " \"a\"\n"
  2124. " \"a\";");
  2125. verifyFormat("f(\"a\", \"b\"\n"
  2126. " \"c\");");
  2127. verifyFormat(
  2128. "#define LL_FORMAT \"ll\"\n"
  2129. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  2130. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  2131. verifyFormat("#define A(X) \\\n"
  2132. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  2133. " \"ccccc\"",
  2134. getLLVMStyleWithColumns(23));
  2135. verifyFormat("#define A \"def\"\n"
  2136. "f(\"abc\" A \"ghi\"\n"
  2137. " \"jkl\");");
  2138. }
  2139. TEST_F(FormatTest, AlignsPipes) {
  2140. verifyFormat(
  2141. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2142. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2143. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2144. verifyFormat(
  2145. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  2146. " << aaaaaaaaaaaaaaaaaaaa;");
  2147. verifyFormat(
  2148. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2149. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2150. verifyFormat(
  2151. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  2152. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  2153. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  2154. verifyFormat(
  2155. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2156. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2157. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2158. verifyFormat("return out << \"somepacket = {\\n\"\n"
  2159. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  2160. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  2161. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  2162. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  2163. " << \"}\";");
  2164. verifyFormat(
  2165. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  2166. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  2167. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  2168. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  2169. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  2170. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  2171. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  2172. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  2173. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2174. verifyFormat(
  2175. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2176. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  2177. }
  2178. TEST_F(FormatTest, UnderstandsEquals) {
  2179. verifyFormat(
  2180. "aaaaaaaaaaaaaaaaa =\n"
  2181. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2182. verifyFormat(
  2183. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2184. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  2185. verifyFormat(
  2186. "if (a) {\n"
  2187. " f();\n"
  2188. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2189. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2190. "}");
  2191. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2192. " 100000000 + 10000000) {\n}");
  2193. }
  2194. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  2195. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  2196. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  2197. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  2198. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  2199. verifyFormat(
  2200. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  2201. " Parameter2);");
  2202. verifyFormat(
  2203. "ShortObject->shortFunction(\n"
  2204. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  2205. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  2206. verifyFormat("loooooooooooooongFunction(\n"
  2207. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  2208. verifyFormat(
  2209. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  2210. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  2211. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  2212. " .WillRepeatedly(Return(SomeValue));");
  2213. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
  2214. " .insert(ccccccccccccccccccccccc);");
  2215. verifyFormat(
  2216. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2217. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2218. " .aaaaaaaaaaaaaaa(\n"
  2219. " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2220. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2221. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2222. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2223. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2224. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  2225. "}");
  2226. // Here, it is not necessary to wrap at "." or "->".
  2227. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  2228. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  2229. verifyFormat(
  2230. "aaaaaaaaaaa->aaaaaaaaa(\n"
  2231. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2232. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  2233. verifyFormat(
  2234. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2235. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  2236. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  2237. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  2238. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  2239. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  2240. // FIXME: Should we break before .a()?
  2241. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2242. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  2243. FormatStyle NoBinPacking = getLLVMStyle();
  2244. NoBinPacking.BinPackParameters = false;
  2245. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  2246. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  2247. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  2248. " aaaaaaaaaaaaaaaaaaa,\n"
  2249. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2250. NoBinPacking);
  2251. }
  2252. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  2253. verifyFormat("template <typename T>\n"
  2254. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  2255. verifyFormat(
  2256. "template <typename T>\n"
  2257. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  2258. verifyFormat("template <typename T>\n"
  2259. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  2260. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  2261. verifyFormat(
  2262. "template <typename T>\n"
  2263. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  2264. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  2265. verifyFormat(
  2266. "template <typename T>\n"
  2267. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  2268. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  2269. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2270. verifyFormat("template <typename T>\n"
  2271. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2272. " int aaaaaaaaaaaaaaaaaaaaaa);");
  2273. verifyFormat(
  2274. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  2275. " typename T4 = char>\n"
  2276. "void f();");
  2277. verifyFormat(
  2278. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  2279. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2280. verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  2281. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
  2282. }
  2283. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  2284. verifyFormat(
  2285. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  2286. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  2287. verifyFormat(
  2288. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  2289. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2290. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  2291. // FIXME: Should we have the extra indent after the second break?
  2292. verifyFormat(
  2293. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  2294. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  2295. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  2296. // FIXME: Look into whether we should indent 4 from the start or 4 from
  2297. // "bbbbb..." here instead of what we are doing now.
  2298. verifyFormat(
  2299. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  2300. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  2301. // Breaking at nested name specifiers is generally not desirable.
  2302. verifyFormat(
  2303. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2304. " aaaaaaaaaaaaaaaaaaaaaaa);");
  2305. verifyFormat(
  2306. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  2307. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2308. " aaaaaaaaaaaaaaaaaaaaa);",
  2309. getLLVMStyleWithColumns(74));
  2310. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  2311. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2312. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  2313. }
  2314. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  2315. verifyFormat("A<int> a;");
  2316. verifyFormat("A<A<A<int> > > a;");
  2317. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  2318. verifyFormat("bool x = a < 1 || 2 > a;");
  2319. verifyFormat("bool x = 5 < f<int>();");
  2320. verifyFormat("bool x = f<int>() > 5;");
  2321. verifyFormat("bool x = 5 < a<int>::x;");
  2322. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  2323. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  2324. verifyGoogleFormat("A<A<int>> a;");
  2325. verifyGoogleFormat("A<A<A<int>>> a;");
  2326. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  2327. verifyGoogleFormat("A<A<int> > a;");
  2328. verifyGoogleFormat("A<A<A<int> > > a;");
  2329. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  2330. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  2331. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  2332. verifyFormat("test >> a >> b;");
  2333. verifyFormat("test << a >> b;");
  2334. verifyFormat("f<int>();");
  2335. verifyFormat("template <typename T> void f() {}");
  2336. }
  2337. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  2338. verifyFormat("COMPARE(a, ==, b);");
  2339. }
  2340. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  2341. verifyFormat("int A::*x;");
  2342. verifyFormat("int (S::*func)(void *);");
  2343. verifyFormat("typedef bool *(Class::*Member)() const;");
  2344. verifyFormat("void f() {\n"
  2345. " (a->*f)();\n"
  2346. " a->*x;\n"
  2347. " (a.*f)();\n"
  2348. " ((*a).*f)();\n"
  2349. " a.*x;\n"
  2350. "}");
  2351. FormatStyle Style = getLLVMStyle();
  2352. Style.PointerBindsToType = true;
  2353. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  2354. }
  2355. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  2356. verifyFormat("int a = -2;");
  2357. verifyFormat("f(-1, -2, -3);");
  2358. verifyFormat("a[-1] = 5;");
  2359. verifyFormat("int a = 5 + -2;");
  2360. verifyFormat("if (i == -1) {\n}");
  2361. verifyFormat("if (i != -1) {\n}");
  2362. verifyFormat("if (i > -1) {\n}");
  2363. verifyFormat("if (i < -1) {\n}");
  2364. verifyFormat("++(a->f());");
  2365. verifyFormat("--(a->f());");
  2366. verifyFormat("(a->f())++;");
  2367. verifyFormat("a[42]++;");
  2368. verifyFormat("if (!(a->f())) {\n}");
  2369. verifyFormat("a-- > b;");
  2370. verifyFormat("b ? -a : c;");
  2371. verifyFormat("n * sizeof char16;");
  2372. verifyFormat("n * alignof char16;");
  2373. verifyFormat("sizeof(char);");
  2374. verifyFormat("alignof(char);");
  2375. verifyFormat("return -1;");
  2376. verifyFormat("switch (a) {\n"
  2377. "case -1:\n"
  2378. " break;\n"
  2379. "}");
  2380. verifyFormat("#define X -1");
  2381. verifyFormat("#define X -kConstant");
  2382. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
  2383. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
  2384. verifyFormat("int a = /* confusing comment */ -1;");
  2385. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  2386. verifyFormat("int a = i /* confusing comment */++;");
  2387. }
  2388. TEST_F(FormatTest, UndestandsOverloadedOperators) {
  2389. verifyFormat("bool operator<();");
  2390. verifyFormat("bool operator>();");
  2391. verifyFormat("bool operator=();");
  2392. verifyFormat("bool operator==();");
  2393. verifyFormat("bool operator!=();");
  2394. verifyFormat("int operator+();");
  2395. verifyFormat("int operator++();");
  2396. verifyFormat("bool operator();");
  2397. verifyFormat("bool operator()();");
  2398. verifyFormat("bool operator[]();");
  2399. verifyFormat("operator bool();");
  2400. verifyFormat("operator int();");
  2401. verifyFormat("operator void *();");
  2402. verifyFormat("operator SomeType<int>();");
  2403. verifyFormat("operator SomeType<int, int>();");
  2404. verifyFormat("operator SomeType<SomeType<int> >();");
  2405. verifyFormat("void *operator new(std::size_t size);");
  2406. verifyFormat("void *operator new[](std::size_t size);");
  2407. verifyFormat("void operator delete(void *ptr);");
  2408. verifyFormat("void operator delete[](void *ptr);");
  2409. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  2410. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  2411. verifyFormat(
  2412. "ostream &operator<<(ostream &OutputStream,\n"
  2413. " SomeReallyLongType WithSomeReallyLongValue);");
  2414. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  2415. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  2416. " return left.group < right.group;\n"
  2417. "}");
  2418. verifyFormat("SomeType &operator=(const SomeType &S);");
  2419. verifyGoogleFormat("operator void*();");
  2420. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  2421. }
  2422. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  2423. verifyFormat("void f() {\n"
  2424. " A *a = new A;\n"
  2425. " A *a = new (placement) A;\n"
  2426. " delete a;\n"
  2427. " delete (A *)a;\n"
  2428. "}");
  2429. }
  2430. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  2431. verifyFormat("int *f(int *a) {}");
  2432. verifyFormat("int main(int argc, char **argv) {}");
  2433. verifyFormat("Test::Test(int b) : a(b * b) {}");
  2434. verifyIndependentOfContext("f(a, *a);");
  2435. verifyFormat("void g() { f(*a); }");
  2436. verifyIndependentOfContext("int a = b * 10;");
  2437. verifyIndependentOfContext("int a = 10 * b;");
  2438. verifyIndependentOfContext("int a = b * c;");
  2439. verifyIndependentOfContext("int a += b * c;");
  2440. verifyIndependentOfContext("int a -= b * c;");
  2441. verifyIndependentOfContext("int a *= b * c;");
  2442. verifyIndependentOfContext("int a /= b * c;");
  2443. verifyIndependentOfContext("int a = *b;");
  2444. verifyIndependentOfContext("int a = *b * c;");
  2445. verifyIndependentOfContext("int a = b * *c;");
  2446. verifyIndependentOfContext("return 10 * b;");
  2447. verifyIndependentOfContext("return *b * *c;");
  2448. verifyIndependentOfContext("return a & ~b;");
  2449. verifyIndependentOfContext("f(b ? *c : *d);");
  2450. verifyIndependentOfContext("int a = b ? *c : *d;");
  2451. verifyIndependentOfContext("*b = a;");
  2452. verifyIndependentOfContext("a * ~b;");
  2453. verifyIndependentOfContext("a * !b;");
  2454. verifyIndependentOfContext("a * +b;");
  2455. verifyIndependentOfContext("a * -b;");
  2456. verifyIndependentOfContext("a * ++b;");
  2457. verifyIndependentOfContext("a * --b;");
  2458. verifyIndependentOfContext("a[4] * b;");
  2459. verifyIndependentOfContext("a[a * a] = 1;");
  2460. verifyIndependentOfContext("f() * b;");
  2461. verifyIndependentOfContext("a * [self dostuff];");
  2462. verifyIndependentOfContext("int x = a * (a + b);");
  2463. verifyIndependentOfContext("(a *)(a + b);");
  2464. verifyIndependentOfContext("int *pa = (int *)&a;");
  2465. verifyIndependentOfContext("return sizeof(int **);");
  2466. verifyIndependentOfContext("return sizeof(int ******);");
  2467. verifyIndependentOfContext("return (int **&)a;");
  2468. verifyFormat("void f(Type (*parameter)[10]) {}");
  2469. verifyGoogleFormat("return sizeof(int**);");
  2470. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  2471. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  2472. // FIXME: The newline is wrong.
  2473. verifyFormat("auto a = [](int **&, int ***) {}\n;");
  2474. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  2475. verifyIndependentOfContext("A<int *> a;");
  2476. verifyIndependentOfContext("A<int **> a;");
  2477. verifyIndependentOfContext("A<int *, int *> a;");
  2478. verifyIndependentOfContext(
  2479. "const char *const p = reinterpret_cast<const char *const>(q);");
  2480. verifyIndependentOfContext("A<int **, int **> a;");
  2481. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  2482. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  2483. verifyFormat("for (; a && b;) {\n}");
  2484. verifyFormat(
  2485. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2486. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2487. verifyGoogleFormat("int main(int argc, char** argv) {}");
  2488. verifyGoogleFormat("A<int*> a;");
  2489. verifyGoogleFormat("A<int**> a;");
  2490. verifyGoogleFormat("A<int*, int*> a;");
  2491. verifyGoogleFormat("A<int**, int**> a;");
  2492. verifyGoogleFormat("f(b ? *c : *d);");
  2493. verifyGoogleFormat("int a = b ? *c : *d;");
  2494. verifyGoogleFormat("Type* t = **x;");
  2495. verifyGoogleFormat("Type* t = *++*x;");
  2496. verifyGoogleFormat("*++*x;");
  2497. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  2498. verifyGoogleFormat("Type* t = x++ * y;");
  2499. verifyGoogleFormat(
  2500. "const char* const p = reinterpret_cast<const char* const>(q);");
  2501. verifyIndependentOfContext("a = *(x + y);");
  2502. verifyIndependentOfContext("a = &(x + y);");
  2503. verifyIndependentOfContext("*(x + y).call();");
  2504. verifyIndependentOfContext("&(x + y)->call();");
  2505. verifyFormat("void f() { &(*I).first; }");
  2506. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  2507. verifyFormat(
  2508. "int *MyValues = {\n"
  2509. " *A, // Operator detection might be confused by the '{'\n"
  2510. " *BB // Operator detection might be confused by previous comment\n"
  2511. "};");
  2512. verifyIndependentOfContext("if (int *a = &b)");
  2513. verifyIndependentOfContext("if (int &a = *b)");
  2514. verifyIndependentOfContext("if (a & b[i])");
  2515. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  2516. verifyIndependentOfContext("if (*b[i])");
  2517. verifyIndependentOfContext("if (int *a = (&b))");
  2518. verifyIndependentOfContext("while (int *a = &b)");
  2519. verifyFormat("void f() {\n"
  2520. " for (const int &v : Values) {\n"
  2521. " }\n"
  2522. "}");
  2523. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  2524. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  2525. verifyFormat("#define MACRO \\\n"
  2526. " int *i = a * b; \\\n"
  2527. " void f(a *b);",
  2528. getLLVMStyleWithColumns(19));
  2529. verifyIndependentOfContext("A = new SomeType *[Length];");
  2530. verifyIndependentOfContext("A = new SomeType *[Length]();");
  2531. verifyGoogleFormat("A = new SomeType* [Length]();");
  2532. verifyGoogleFormat("A = new SomeType* [Length];");
  2533. FormatStyle PointerLeft = getLLVMStyle();
  2534. PointerLeft.PointerBindsToType = true;
  2535. verifyFormat("delete *x;", PointerLeft);
  2536. }
  2537. TEST_F(FormatTest, UnderstandsEllipsis) {
  2538. verifyFormat("int printf(const char *fmt, ...);");
  2539. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  2540. }
  2541. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  2542. EXPECT_EQ("int *a;\n"
  2543. "int *a;\n"
  2544. "int *a;",
  2545. format("int *a;\n"
  2546. "int* a;\n"
  2547. "int *a;",
  2548. getGoogleStyle()));
  2549. EXPECT_EQ("int* a;\n"
  2550. "int* a;\n"
  2551. "int* a;",
  2552. format("int* a;\n"
  2553. "int* a;\n"
  2554. "int *a;",
  2555. getGoogleStyle()));
  2556. EXPECT_EQ("int *a;\n"
  2557. "int *a;\n"
  2558. "int *a;",
  2559. format("int *a;\n"
  2560. "int * a;\n"
  2561. "int * a;",
  2562. getGoogleStyle()));
  2563. }
  2564. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  2565. verifyFormat("int f(int &&a) {}");
  2566. verifyFormat("int f(int a, char &&b) {}");
  2567. verifyFormat("void f() { int &&a = b; }");
  2568. verifyGoogleFormat("int f(int a, char&& b) {}");
  2569. verifyGoogleFormat("void f() { int&& a = b; }");
  2570. // FIXME: These require somewhat deeper changes in template arguments
  2571. // formatting.
  2572. // verifyIndependentOfContext("A<int &&> a;");
  2573. // verifyIndependentOfContext("A<int &&, int &&> a;");
  2574. // verifyGoogleFormat("A<int&&> a;");
  2575. // verifyGoogleFormat("A<int&&, int&&> a;");
  2576. }
  2577. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  2578. verifyFormat("void f() {\n"
  2579. " x[aaaaaaaaa -\n"
  2580. " b] = 23;\n"
  2581. "}",
  2582. getLLVMStyleWithColumns(15));
  2583. }
  2584. TEST_F(FormatTest, FormatsCasts) {
  2585. verifyFormat("Type *A = static_cast<Type *>(P);");
  2586. verifyFormat("Type *A = (Type *)P;");
  2587. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  2588. verifyFormat("int a = (int)(2.0f);");
  2589. // FIXME: These also need to be identified.
  2590. verifyFormat("int a = (int) 2.0f;");
  2591. verifyFormat("int a = (int) * b;");
  2592. // These are not casts.
  2593. verifyFormat("void f(int *) {}");
  2594. verifyFormat("f(foo)->b;");
  2595. verifyFormat("f(foo).b;");
  2596. verifyFormat("f(foo)(b);");
  2597. verifyFormat("f(foo)[b];");
  2598. verifyFormat("[](foo) { return 4; }(bar)];");
  2599. verifyFormat("(*funptr)(foo)[4];");
  2600. verifyFormat("funptrs[4](foo)[4];");
  2601. verifyFormat("void f(int *);");
  2602. verifyFormat("void f(int *) = 0;");
  2603. verifyFormat("void f(SmallVector<int>) {}");
  2604. verifyFormat("void f(SmallVector<int>);");
  2605. verifyFormat("void f(SmallVector<int>) = 0;");
  2606. verifyFormat("void f(int i = (kValue) * kMask) {}");
  2607. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  2608. verifyFormat("int a = sizeof(int) * b;");
  2609. verifyFormat("int a = alignof(int) * b;");
  2610. // These are not casts, but at some point were confused with casts.
  2611. verifyFormat("virtual void foo(int *) override;");
  2612. verifyFormat("virtual void foo(char &) const;");
  2613. verifyFormat("virtual void foo(int *a, char *) const;");
  2614. verifyFormat("int a = sizeof(int *) + b;");
  2615. verifyFormat("int a = alignof(int *) + b;");
  2616. }
  2617. TEST_F(FormatTest, FormatsFunctionTypes) {
  2618. verifyFormat("A<bool()> a;");
  2619. verifyFormat("A<SomeType()> a;");
  2620. verifyFormat("A<void(*)(int, std::string)> a;");
  2621. verifyFormat("A<void *(int)>;");
  2622. verifyFormat("void *(*a)(int *, SomeType *);");
  2623. // FIXME: Inconsistent.
  2624. verifyFormat("int (*func)(void *);");
  2625. verifyFormat("void f() { int(*func)(void *); }");
  2626. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  2627. verifyGoogleFormat("void* (*a)(int);");
  2628. // Other constructs can look somewhat like function types:
  2629. verifyFormat("A<sizeof(*x)> a;");
  2630. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  2631. }
  2632. TEST_F(FormatTest, BreaksLongDeclarations) {
  2633. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  2634. " AnotherNameForTheLongType;");
  2635. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  2636. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  2637. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  2638. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  2639. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  2640. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  2641. // FIXME: Without the comment, this breaks after "(".
  2642. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  2643. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
  2644. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  2645. " int LoooooooooooooooooooongParam2) {}");
  2646. verifyFormat(
  2647. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  2648. " SourceLocation L, IdentifierIn *II,\n"
  2649. " Type *T) {}");
  2650. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  2651. "ReallyReallyLongFunctionName(\n"
  2652. " const std::string &SomeParameter,\n"
  2653. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  2654. " ReallyReallyLongParameterName,\n"
  2655. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  2656. " AnotherLongParameterName) {}");
  2657. verifyFormat("template <typename A>\n"
  2658. "SomeLoooooooooooooooooooooongType<\n"
  2659. " typename some_namespace::SomeOtherType<A>::Type>\n"
  2660. "Function() {}");
  2661. verifyFormat(
  2662. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  2663. " aaaaaaaaaaaaaaaaaaaaaaa;");
  2664. verifyGoogleFormat(
  2665. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  2666. " SourceLocation L) {}");
  2667. verifyGoogleFormat(
  2668. "some_namespace::LongReturnType\n"
  2669. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  2670. " int first_long_parameter, int second_parameter) {}");
  2671. verifyGoogleFormat("template <typename T>\n"
  2672. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  2673. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  2674. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2675. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  2676. }
  2677. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  2678. verifyFormat("(a)->b();");
  2679. verifyFormat("--a;");
  2680. }
  2681. TEST_F(FormatTest, HandlesIncludeDirectives) {
  2682. verifyFormat("#include <string>\n"
  2683. "#include <a/b/c.h>\n"
  2684. "#include \"a/b/string\"\n"
  2685. "#include \"string.h\"\n"
  2686. "#include \"string.h\"\n"
  2687. "#include <a-a>\n"
  2688. "#include < path with space >\n"
  2689. "#include \"abc.h\" // this is included for ABC\n"
  2690. "#include \"some long include\" // with a comment\n"
  2691. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  2692. getLLVMStyleWithColumns(35));
  2693. verifyFormat("#import <string>");
  2694. verifyFormat("#import <a/b/c.h>");
  2695. verifyFormat("#import \"a/b/string\"");
  2696. verifyFormat("#import \"string.h\"");
  2697. verifyFormat("#import \"string.h\"");
  2698. }
  2699. //===----------------------------------------------------------------------===//
  2700. // Error recovery tests.
  2701. //===----------------------------------------------------------------------===//
  2702. TEST_F(FormatTest, IncompleteParameterLists) {
  2703. FormatStyle NoBinPacking = getLLVMStyle();
  2704. NoBinPacking.BinPackParameters = false;
  2705. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  2706. " double *min_x,\n"
  2707. " double *max_x,\n"
  2708. " double *min_y,\n"
  2709. " double *max_y,\n"
  2710. " double *min_z,\n"
  2711. " double *max_z, ) {}",
  2712. NoBinPacking);
  2713. }
  2714. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  2715. verifyFormat("void f() { return; }\n42");
  2716. verifyFormat("void f() {\n"
  2717. " if (0)\n"
  2718. " return;\n"
  2719. "}\n"
  2720. "42");
  2721. verifyFormat("void f() { return }\n42");
  2722. verifyFormat("void f() {\n"
  2723. " if (0)\n"
  2724. " return\n"
  2725. "}\n"
  2726. "42");
  2727. }
  2728. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  2729. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  2730. EXPECT_EQ("void f() {\n"
  2731. " if (a)\n"
  2732. " return\n"
  2733. "}",
  2734. format("void f ( ) { if ( a ) return }"));
  2735. EXPECT_EQ("namespace N {\n"
  2736. "void f()\n"
  2737. "}",
  2738. format("namespace N { void f() }"));
  2739. EXPECT_EQ("namespace N {\n"
  2740. "void f() {}\n"
  2741. "void g()\n"
  2742. "}",
  2743. format("namespace N { void f( ) { } void g( ) }"));
  2744. }
  2745. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  2746. verifyFormat("int aaaaaaaa =\n"
  2747. " // Overlylongcomment\n"
  2748. " b;",
  2749. getLLVMStyleWithColumns(20));
  2750. verifyFormat("function(\n"
  2751. " ShortArgument,\n"
  2752. " LoooooooooooongArgument);\n",
  2753. getLLVMStyleWithColumns(20));
  2754. }
  2755. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  2756. verifyFormat("public:");
  2757. verifyFormat("class A {\n"
  2758. "public\n"
  2759. " void f() {}\n"
  2760. "};");
  2761. verifyFormat("public\n"
  2762. "int qwerty;");
  2763. verifyFormat("public\n"
  2764. "B {}");
  2765. verifyFormat("public\n"
  2766. "{}");
  2767. verifyFormat("public\n"
  2768. "B { int x; }");
  2769. }
  2770. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  2771. verifyFormat("{");
  2772. verifyFormat("#})");
  2773. }
  2774. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  2775. verifyFormat("do {\n}");
  2776. verifyFormat("do {\n}\n"
  2777. "f();");
  2778. verifyFormat("do {\n}\n"
  2779. "wheeee(fun);");
  2780. verifyFormat("do {\n"
  2781. " f();\n"
  2782. "}");
  2783. }
  2784. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  2785. verifyFormat("if {\n foo;\n foo();\n}");
  2786. verifyFormat("switch {\n foo;\n foo();\n}");
  2787. verifyFormat("for {\n foo;\n foo();\n}");
  2788. verifyFormat("while {\n foo;\n foo();\n}");
  2789. verifyFormat("do {\n foo;\n foo();\n} while;");
  2790. }
  2791. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  2792. verifyFormat("namespace {\n"
  2793. "class Foo { Foo ( }; } // comment");
  2794. }
  2795. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  2796. EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
  2797. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  2798. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  2799. EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
  2800. EXPECT_EQ("{\n"
  2801. " {\n"
  2802. " breakme(\n"
  2803. " qwe);\n"
  2804. "}\n",
  2805. format("{\n"
  2806. " {\n"
  2807. " breakme(qwe);\n"
  2808. "}\n",
  2809. getLLVMStyleWithColumns(10)));
  2810. }
  2811. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  2812. verifyFormat("int x = {\n"
  2813. " avariable,\n"
  2814. " b(alongervariable)\n"
  2815. "};",
  2816. getLLVMStyleWithColumns(25));
  2817. }
  2818. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  2819. verifyFormat("return (a)(b) { 1, 2, 3 };");
  2820. }
  2821. TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
  2822. // FIXME: This is bad, find a better and more generic solution.
  2823. verifyFormat(
  2824. "Aaa({\n"
  2825. " int i;\n"
  2826. "},\n"
  2827. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2828. " ccccccccccccccccc));");
  2829. }
  2830. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  2831. verifyFormat("void f() { return 42; }");
  2832. verifyFormat("void f() {\n"
  2833. " // Comment\n"
  2834. "}");
  2835. verifyFormat("{\n"
  2836. "#error {\n"
  2837. " int a;\n"
  2838. "}");
  2839. verifyFormat("{\n"
  2840. " int a;\n"
  2841. "#error {\n"
  2842. "}");
  2843. verifyFormat("void f() {} // comment");
  2844. verifyFormat("void f() { int a; } // comment");
  2845. verifyFormat("void f() {\n"
  2846. "} // comment",
  2847. getLLVMStyleWithColumns(15));
  2848. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  2849. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  2850. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  2851. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  2852. }
  2853. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  2854. // Elaborate type variable declarations.
  2855. verifyFormat("struct foo a = { bar };\nint n;");
  2856. verifyFormat("class foo a = { bar };\nint n;");
  2857. verifyFormat("union foo a = { bar };\nint n;");
  2858. // Elaborate types inside function definitions.
  2859. verifyFormat("struct foo f() {}\nint n;");
  2860. verifyFormat("class foo f() {}\nint n;");
  2861. verifyFormat("union foo f() {}\nint n;");
  2862. // Templates.
  2863. verifyFormat("template <class X> void f() {}\nint n;");
  2864. verifyFormat("template <struct X> void f() {}\nint n;");
  2865. verifyFormat("template <union X> void f() {}\nint n;");
  2866. // Actual definitions...
  2867. verifyFormat("struct {\n} n;");
  2868. verifyFormat(
  2869. "template <template <class T, class Y>, class Z> class X {\n} n;");
  2870. verifyFormat("union Z {\n int n;\n} x;");
  2871. verifyFormat("class MACRO Z {\n} n;");
  2872. verifyFormat("class MACRO(X) Z {\n} n;");
  2873. verifyFormat("class __attribute__(X) Z {\n} n;");
  2874. verifyFormat("class __declspec(X) Z {\n} n;");
  2875. verifyFormat("class A##B##C {\n} n;");
  2876. // Redefinition from nested context:
  2877. verifyFormat("class A::B::C {\n} n;");
  2878. // Template definitions.
  2879. verifyFormat(
  2880. "template <typename F>\n"
  2881. "Matcher(const Matcher<F> &Other,\n"
  2882. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  2883. " !is_same<F, T>::value>::type * = 0)\n"
  2884. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  2885. // FIXME: This is still incorrectly handled at the formatter side.
  2886. verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
  2887. // FIXME:
  2888. // This now gets parsed incorrectly as class definition.
  2889. // verifyFormat("class A<int> f() {\n}\nint n;");
  2890. // Elaborate types where incorrectly parsing the structural element would
  2891. // break the indent.
  2892. verifyFormat("if (true)\n"
  2893. " class X x;\n"
  2894. "else\n"
  2895. " f();\n");
  2896. // This is simply incomplete. Formatting is not important, but must not crash.
  2897. verifyFormat("class A:");
  2898. }
  2899. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  2900. verifyFormat("#error Leave all white!!!!! space* alone!\n");
  2901. verifyFormat("#warning Leave all white!!!!! space* alone!\n");
  2902. EXPECT_EQ("#error 1", format(" # error 1"));
  2903. EXPECT_EQ("#warning 1", format(" # warning 1"));
  2904. }
  2905. TEST_F(FormatTest, FormatHashIfExpressions) {
  2906. // FIXME: Come up with a better indentation for #elif.
  2907. verifyFormat(
  2908. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  2909. " defined(BBBBBBBB)\n"
  2910. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  2911. " defined(BBBBBBBB)\n"
  2912. "#endif",
  2913. getLLVMStyleWithColumns(65));
  2914. }
  2915. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  2916. FormatStyle AllowsMergedIf = getGoogleStyle();
  2917. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  2918. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  2919. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  2920. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  2921. EXPECT_EQ("if (true) return 42;",
  2922. format("if (true)\nreturn 42;", AllowsMergedIf));
  2923. FormatStyle ShortMergedIf = AllowsMergedIf;
  2924. ShortMergedIf.ColumnLimit = 25;
  2925. verifyFormat("#define A \\\n"
  2926. " if (true) return 42;",
  2927. ShortMergedIf);
  2928. verifyFormat("#define A \\\n"
  2929. " f(); \\\n"
  2930. " if (true)\n"
  2931. "#define B",
  2932. ShortMergedIf);
  2933. verifyFormat("#define A \\\n"
  2934. " f(); \\\n"
  2935. " if (true)\n"
  2936. "g();",
  2937. ShortMergedIf);
  2938. verifyFormat("{\n"
  2939. "#ifdef A\n"
  2940. " // Comment\n"
  2941. " if (true) continue;\n"
  2942. "#endif\n"
  2943. " // Comment\n"
  2944. " if (true) continue;",
  2945. ShortMergedIf);
  2946. }
  2947. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  2948. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  2949. " f();\n"
  2950. "}");
  2951. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  2952. " f();\n"
  2953. "} /* another comment */ else /* comment #3 */ {\n"
  2954. " g();\n"
  2955. "}");
  2956. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  2957. " f();\n"
  2958. "}");
  2959. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  2960. " f();\n"
  2961. "}");
  2962. verifyFormat("do /* a comment in a strange place */ {\n"
  2963. " f();\n"
  2964. "} /* another comment */ while (0);");
  2965. }
  2966. TEST_F(FormatTest, BlockComments) {
  2967. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  2968. format("/* *//* */ /* */\n/* *//* */ /* */"));
  2969. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  2970. EXPECT_EQ("#define A /*123*/\\\n"
  2971. " b\n"
  2972. "/* */\n"
  2973. "someCall(\n"
  2974. " parameter);",
  2975. format("#define A /*123*/ b\n"
  2976. "/* */\n"
  2977. "someCall(parameter);",
  2978. getLLVMStyleWithColumns(15)));
  2979. EXPECT_EQ("#define A\n"
  2980. "/* */ someCall(\n"
  2981. " parameter);",
  2982. format("#define A\n"
  2983. "/* */someCall(parameter);",
  2984. getLLVMStyleWithColumns(15)));
  2985. FormatStyle NoBinPacking = getLLVMStyle();
  2986. NoBinPacking.BinPackParameters = false;
  2987. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  2988. " 2, /* comment 2 */\n"
  2989. " 3, /* comment 3 */\n"
  2990. " aaaa,\n"
  2991. " bbbb);",
  2992. format("someFunction (1, /* comment 1 */\n"
  2993. " 2, /* comment 2 */ \n"
  2994. " 3, /* comment 3 */\n"
  2995. "aaaa, bbbb );",
  2996. NoBinPacking));
  2997. verifyFormat(
  2998. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2999. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3000. EXPECT_EQ(
  3001. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  3002. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3004. format(
  3005. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  3006. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  3008. EXPECT_EQ(
  3009. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  3010. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  3011. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  3012. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  3013. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  3014. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  3015. }
  3016. TEST_F(FormatTest, BlockCommentsInMacros) {
  3017. EXPECT_EQ("#define A \\\n"
  3018. " { \\\n"
  3019. " /* one line */ \\\n"
  3020. " someCall();",
  3021. format("#define A { \\\n"
  3022. " /* one line */ \\\n"
  3023. " someCall();",
  3024. getLLVMStyleWithColumns(20)));
  3025. EXPECT_EQ("#define A \\\n"
  3026. " { \\\n"
  3027. " /* previous */ \\\n"
  3028. " /* one line */ \\\n"
  3029. " someCall();",
  3030. format("#define A { \\\n"
  3031. " /* previous */ \\\n"
  3032. " /* one line */ \\\n"
  3033. " someCall();",
  3034. getLLVMStyleWithColumns(20)));
  3035. }
  3036. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  3037. // FIXME: This is not what we want...
  3038. verifyFormat("{\n"
  3039. "// a"
  3040. "// b");
  3041. }
  3042. TEST_F(FormatTest, FormatStarDependingOnContext) {
  3043. verifyFormat("void f(int *a);");
  3044. verifyFormat("void f() { f(fint * b); }");
  3045. verifyFormat("class A {\n void f(int *a);\n};");
  3046. verifyFormat("class A {\n int *a;\n};");
  3047. verifyFormat("namespace a {\n"
  3048. "namespace b {\n"
  3049. "class A {\n"
  3050. " void f() {}\n"
  3051. " int *a;\n"
  3052. "};\n"
  3053. "}\n"
  3054. "}");
  3055. }
  3056. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  3057. verifyFormat("while");
  3058. verifyFormat("operator");
  3059. }
  3060. //===----------------------------------------------------------------------===//
  3061. // Objective-C tests.
  3062. //===----------------------------------------------------------------------===//
  3063. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  3064. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  3065. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  3066. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  3067. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  3068. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  3069. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  3070. format("-(NSInteger)Method3:(id)anObject;"));
  3071. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  3072. format("-(NSInteger)Method4:(id)anObject;"));
  3073. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  3074. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  3075. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  3076. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  3077. EXPECT_EQ(
  3078. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  3079. format(
  3080. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  3081. // Very long objectiveC method declaration.
  3082. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  3083. " inRange:(NSRange)range\n"
  3084. " outRange:(NSRange)out_range\n"
  3085. " outRange1:(NSRange)out_range1\n"
  3086. " outRange2:(NSRange)out_range2\n"
  3087. " outRange3:(NSRange)out_range3\n"
  3088. " outRange4:(NSRange)out_range4\n"
  3089. " outRange5:(NSRange)out_range5\n"
  3090. " outRange6:(NSRange)out_range6\n"
  3091. " outRange7:(NSRange)out_range7\n"
  3092. " outRange8:(NSRange)out_range8\n"
  3093. " outRange9:(NSRange)out_range9;");
  3094. verifyFormat("- (int)sum:(vector<int>)numbers;");
  3095. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  3096. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  3097. // protocol lists (but not for template classes):
  3098. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  3099. verifyFormat("- (int(*)())foo:(int(*)())f;");
  3100. verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
  3101. // If there's no return type (very rare in practice!), LLVM and Google style
  3102. // agree.
  3103. verifyFormat("- foo;");
  3104. verifyFormat("- foo:(int)f;");
  3105. verifyGoogleFormat("- foo:(int)foo;");
  3106. }
  3107. TEST_F(FormatTest, FormatObjCBlocks) {
  3108. verifyFormat("int (^Block)(int, int);");
  3109. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
  3110. }
  3111. TEST_F(FormatTest, FormatObjCInterface) {
  3112. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  3113. "@public\n"
  3114. " int field1;\n"
  3115. "@protected\n"
  3116. " int field2;\n"
  3117. "@private\n"
  3118. " int field3;\n"
  3119. "@package\n"
  3120. " int field4;\n"
  3121. "}\n"
  3122. "+ (id)init;\n"
  3123. "@end");
  3124. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  3125. " @public\n"
  3126. " int field1;\n"
  3127. " @protected\n"
  3128. " int field2;\n"
  3129. " @private\n"
  3130. " int field3;\n"
  3131. " @package\n"
  3132. " int field4;\n"
  3133. "}\n"
  3134. "+ (id)init;\n"
  3135. "@end");
  3136. verifyFormat("@interface /* wait for it */ Foo\n"
  3137. "+ (id)init;\n"
  3138. "// Look, a comment!\n"
  3139. "- (int)answerWith:(int)i;\n"
  3140. "@end");
  3141. verifyFormat("@interface Foo\n"
  3142. "@end\n"
  3143. "@interface Bar\n"
  3144. "@end");
  3145. verifyFormat("@interface Foo : Bar\n"
  3146. "+ (id)init;\n"
  3147. "@end");
  3148. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  3149. "+ (id)init;\n"
  3150. "@end");
  3151. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  3152. "+ (id)init;\n"
  3153. "@end");
  3154. verifyFormat("@interface Foo (HackStuff)\n"
  3155. "+ (id)init;\n"
  3156. "@end");
  3157. verifyFormat("@interface Foo ()\n"
  3158. "+ (id)init;\n"
  3159. "@end");
  3160. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  3161. "+ (id)init;\n"
  3162. "@end");
  3163. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  3164. "+ (id)init;\n"
  3165. "@end");
  3166. verifyFormat("@interface Foo {\n"
  3167. " int _i;\n"
  3168. "}\n"
  3169. "+ (id)init;\n"
  3170. "@end");
  3171. verifyFormat("@interface Foo : Bar {\n"
  3172. " int _i;\n"
  3173. "}\n"
  3174. "+ (id)init;\n"
  3175. "@end");
  3176. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  3177. " int _i;\n"
  3178. "}\n"
  3179. "+ (id)init;\n"
  3180. "@end");
  3181. verifyFormat("@interface Foo (HackStuff) {\n"
  3182. " int _i;\n"
  3183. "}\n"
  3184. "+ (id)init;\n"
  3185. "@end");
  3186. verifyFormat("@interface Foo () {\n"
  3187. " int _i;\n"
  3188. "}\n"
  3189. "+ (id)init;\n"
  3190. "@end");
  3191. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  3192. " int _i;\n"
  3193. "}\n"
  3194. "+ (id)init;\n"
  3195. "@end");
  3196. }
  3197. TEST_F(FormatTest, FormatObjCImplementation) {
  3198. verifyFormat("@implementation Foo : NSObject {\n"
  3199. "@public\n"
  3200. " int field1;\n"
  3201. "@protected\n"
  3202. " int field2;\n"
  3203. "@private\n"
  3204. " int field3;\n"
  3205. "@package\n"
  3206. " int field4;\n"
  3207. "}\n"
  3208. "+ (id)init {\n}\n"
  3209. "@end");
  3210. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  3211. " @public\n"
  3212. " int field1;\n"
  3213. " @protected\n"
  3214. " int field2;\n"
  3215. " @private\n"
  3216. " int field3;\n"
  3217. " @package\n"
  3218. " int field4;\n"
  3219. "}\n"
  3220. "+ (id)init {\n}\n"
  3221. "@end");
  3222. verifyFormat("@implementation Foo\n"
  3223. "+ (id)init {\n"
  3224. " if (true)\n"
  3225. " return nil;\n"
  3226. "}\n"
  3227. "// Look, a comment!\n"
  3228. "- (int)answerWith:(int)i {\n"
  3229. " return i;\n"
  3230. "}\n"
  3231. "+ (int)answerWith:(int)i {\n"
  3232. " return i;\n"
  3233. "}\n"
  3234. "@end");
  3235. verifyFormat("@implementation Foo\n"
  3236. "@end\n"
  3237. "@implementation Bar\n"
  3238. "@end");
  3239. verifyFormat("@implementation Foo : Bar\n"
  3240. "+ (id)init {\n}\n"
  3241. "- (void)foo {\n}\n"
  3242. "@end");
  3243. verifyFormat("@implementation Foo {\n"
  3244. " int _i;\n"
  3245. "}\n"
  3246. "+ (id)init {\n}\n"
  3247. "@end");
  3248. verifyFormat("@implementation Foo : Bar {\n"
  3249. " int _i;\n"
  3250. "}\n"
  3251. "+ (id)init {\n}\n"
  3252. "@end");
  3253. verifyFormat("@implementation Foo (HackStuff)\n"
  3254. "+ (id)init {\n}\n"
  3255. "@end");
  3256. }
  3257. TEST_F(FormatTest, FormatObjCProtocol) {
  3258. verifyFormat("@protocol Foo\n"
  3259. "@property(weak) id delegate;\n"
  3260. "- (NSUInteger)numberOfThings;\n"
  3261. "@end");
  3262. verifyFormat("@protocol MyProtocol <NSObject>\n"
  3263. "- (NSUInteger)numberOfThings;\n"
  3264. "@end");
  3265. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  3266. "- (NSUInteger)numberOfThings;\n"
  3267. "@end");
  3268. verifyFormat("@protocol Foo;\n"
  3269. "@protocol Bar;\n");
  3270. verifyFormat("@protocol Foo\n"
  3271. "@end\n"
  3272. "@protocol Bar\n"
  3273. "@end");
  3274. verifyFormat("@protocol myProtocol\n"
  3275. "- (void)mandatoryWithInt:(int)i;\n"
  3276. "@optional\n"
  3277. "- (void)optional;\n"
  3278. "@required\n"
  3279. "- (void)required;\n"
  3280. "@optional\n"
  3281. "@property(assign) int madProp;\n"
  3282. "@end\n");
  3283. }
  3284. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  3285. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  3286. " rect:(NSRect)theRect\n"
  3287. " interval:(float)theInterval {\n"
  3288. "}");
  3289. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  3290. " longKeyword:(NSRect)theRect\n"
  3291. " evenLongerKeyword:(float)theInterval\n"
  3292. " error:(NSError **)theError {\n"
  3293. "}");
  3294. }
  3295. TEST_F(FormatTest, FormatObjCMethodExpr) {
  3296. verifyFormat("[foo bar:baz];");
  3297. verifyFormat("return [foo bar:baz];");
  3298. verifyFormat("f([foo bar:baz]);");
  3299. verifyFormat("f(2, [foo bar:baz]);");
  3300. verifyFormat("f(2, a ? b : c);");
  3301. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  3302. // Unary operators.
  3303. verifyFormat("int a = +[foo bar:baz];");
  3304. verifyFormat("int a = -[foo bar:baz];");
  3305. verifyFormat("int a = ![foo bar:baz];");
  3306. verifyFormat("int a = ~[foo bar:baz];");
  3307. verifyFormat("int a = ++[foo bar:baz];");
  3308. verifyFormat("int a = --[foo bar:baz];");
  3309. verifyFormat("int a = sizeof [foo bar:baz];");
  3310. verifyFormat("int a = alignof [foo bar:baz];");
  3311. verifyFormat("int a = &[foo bar:baz];");
  3312. verifyFormat("int a = *[foo bar:baz];");
  3313. // FIXME: Make casts work, without breaking f()[4].
  3314. //verifyFormat("int a = (int)[foo bar:baz];");
  3315. //verifyFormat("return (int)[foo bar:baz];");
  3316. //verifyFormat("(void)[foo bar:baz];");
  3317. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  3318. // Binary operators.
  3319. verifyFormat("[foo bar:baz], [foo bar:baz];");
  3320. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  3321. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  3322. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  3323. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  3324. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  3325. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  3326. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  3327. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  3328. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  3329. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  3330. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  3331. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  3332. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  3333. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  3334. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  3335. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  3336. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  3337. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  3338. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  3339. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  3340. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  3341. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  3342. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  3343. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  3344. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  3345. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  3346. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  3347. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  3348. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  3349. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  3350. // Whew!
  3351. verifyFormat("return in[42];");
  3352. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  3353. "}");
  3354. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  3355. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  3356. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  3357. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  3358. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  3359. verifyFormat("[button setAction:@selector(zoomOut:)];");
  3360. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  3361. verifyFormat("arr[[self indexForFoo:a]];");
  3362. verifyFormat("throw [self errorFor:a];");
  3363. verifyFormat("@throw [self errorFor:a];");
  3364. // This tests that the formatter doesn't break after "backing" but before ":",
  3365. // which would be at 80 columns.
  3366. verifyFormat(
  3367. "void f() {\n"
  3368. " if ((self = [super initWithContentRect:contentRect\n"
  3369. " styleMask:styleMask\n"
  3370. " backing:NSBackingStoreBuffered\n"
  3371. " defer:YES]))");
  3372. verifyFormat(
  3373. "[foo checkThatBreakingAfterColonWorksOk:\n"
  3374. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  3375. verifyFormat("[myObj short:arg1 // Force line break\n"
  3376. " longKeyword:arg2\n"
  3377. " evenLongerKeyword:arg3\n"
  3378. " error:arg4];");
  3379. verifyFormat(
  3380. "void f() {\n"
  3381. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  3382. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  3383. " pos.width(), pos.height())\n"
  3384. " styleMask:NSBorderlessWindowMask\n"
  3385. " backing:NSBackingStoreBuffered\n"
  3386. " defer:NO]);\n"
  3387. "}");
  3388. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  3389. " with:contentsNativeView];");
  3390. verifyFormat(
  3391. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  3392. " owner:nillllll];");
  3393. verifyFormat(
  3394. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  3395. " forType:kBookmarkButtonDragType];");
  3396. verifyFormat("[defaultCenter addObserver:self\n"
  3397. " selector:@selector(willEnterFullscreen)\n"
  3398. " name:kWillEnterFullscreenNotification\n"
  3399. " object:nil];");
  3400. verifyFormat("[image_rep drawInRect:drawRect\n"
  3401. " fromRect:NSZeroRect\n"
  3402. " operation:NSCompositeCopy\n"
  3403. " fraction:1.0\n"
  3404. " respectFlipped:NO\n"
  3405. " hints:nil];");
  3406. verifyFormat(
  3407. "scoped_nsobject<NSTextField> message(\n"
  3408. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  3409. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  3410. }
  3411. TEST_F(FormatTest, ObjCAt) {
  3412. verifyFormat("@autoreleasepool");
  3413. verifyFormat("@catch");
  3414. verifyFormat("@class");
  3415. verifyFormat("@compatibility_alias");
  3416. verifyFormat("@defs");
  3417. verifyFormat("@dynamic");
  3418. verifyFormat("@encode");
  3419. verifyFormat("@end");
  3420. verifyFormat("@finally");
  3421. verifyFormat("@implementation");
  3422. verifyFormat("@import");
  3423. verifyFormat("@interface");
  3424. verifyFormat("@optional");
  3425. verifyFormat("@package");
  3426. verifyFormat("@private");
  3427. verifyFormat("@property");
  3428. verifyFormat("@protected");
  3429. verifyFormat("@protocol");
  3430. verifyFormat("@public");
  3431. verifyFormat("@required");
  3432. verifyFormat("@selector");
  3433. verifyFormat("@synchronized");
  3434. verifyFormat("@synthesize");
  3435. verifyFormat("@throw");
  3436. verifyFormat("@try");
  3437. EXPECT_EQ("@interface", format("@ interface"));
  3438. // The precise formatting of this doesn't matter, nobody writes code like
  3439. // this.
  3440. verifyFormat("@ /*foo*/ interface");
  3441. }
  3442. TEST_F(FormatTest, ObjCSnippets) {
  3443. verifyFormat("@autoreleasepool {\n"
  3444. " foo();\n"
  3445. "}");
  3446. verifyFormat("@class Foo, Bar;");
  3447. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  3448. verifyFormat("@dynamic textColor;");
  3449. verifyFormat("char *buf1 = @encode(int *);");
  3450. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  3451. verifyFormat("char *buf1 = @encode(int **);");
  3452. verifyFormat("Protocol *proto = @protocol(p1);");
  3453. verifyFormat("SEL s = @selector(foo:);");
  3454. verifyFormat("@synchronized(self) {\n"
  3455. " f();\n"
  3456. "}");
  3457. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  3458. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  3459. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  3460. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  3461. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  3462. }
  3463. TEST_F(FormatTest, ObjCLiterals) {
  3464. verifyFormat("@\"String\"");
  3465. verifyFormat("@1");
  3466. verifyFormat("@+4.8");
  3467. verifyFormat("@-4");
  3468. verifyFormat("@1LL");
  3469. verifyFormat("@.5");
  3470. verifyFormat("@'c'");
  3471. verifyFormat("@true");
  3472. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  3473. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  3474. verifyFormat("NSNumber *favoriteColor = @(Green);");
  3475. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  3476. verifyFormat("@[");
  3477. verifyFormat("@[]");
  3478. verifyFormat(
  3479. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  3480. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  3481. verifyFormat("@{");
  3482. verifyFormat("@{}");
  3483. verifyFormat("@{ @\"one\" : @1 }");
  3484. verifyFormat("return @{ @\"one\" : @1 };");
  3485. verifyFormat("@{ @\"one\" : @1, }");
  3486. // FIXME: Breaking in cases where we think there's a structural error
  3487. // showed that we're incorrectly parsing this code. We need to fix the
  3488. // parsing here.
  3489. verifyFormat("@{ @\"one\" : @\n"
  3490. "{ @2 : @1 }\n"
  3491. "}");
  3492. verifyFormat("@{ @\"one\" : @\n"
  3493. "{ @2 : @1 }\n"
  3494. ",\n"
  3495. "}");
  3496. verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
  3497. verifyFormat("[self setDict:@{}");
  3498. verifyFormat("[self setDict:@{ @1 : @2 }");
  3499. verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
  3500. verifyFormat(
  3501. "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
  3502. verifyFormat(
  3503. "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
  3504. // FIXME: Nested and multi-line array and dictionary literals need more work.
  3505. verifyFormat(
  3506. "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
  3507. " @\"processInfo\" : [NSProcessInfo processInfo] };");
  3508. }
  3509. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  3510. EXPECT_EQ("{\n"
  3511. "{\n"
  3512. "a;\n"
  3513. "b;\n"
  3514. "}\n"
  3515. "}",
  3516. format("{\n"
  3517. "{\n"
  3518. "a;\n"
  3519. " b;\n"
  3520. "}\n"
  3521. "}",
  3522. 13, 2, getLLVMStyle()));
  3523. EXPECT_EQ("{\n"
  3524. "{\n"
  3525. " a;\n"
  3526. "b;\n"
  3527. "}\n"
  3528. "}",
  3529. format("{\n"
  3530. "{\n"
  3531. " a;\n"
  3532. "b;\n"
  3533. "}\n"
  3534. "}",
  3535. 9, 2, getLLVMStyle()));
  3536. EXPECT_EQ("{\n"
  3537. "{\n"
  3538. "public:\n"
  3539. " b;\n"
  3540. "}\n"
  3541. "}",
  3542. format("{\n"
  3543. "{\n"
  3544. "public:\n"
  3545. " b;\n"
  3546. "}\n"
  3547. "}",
  3548. 17, 2, getLLVMStyle()));
  3549. EXPECT_EQ("{\n"
  3550. "{\n"
  3551. "a;\n"
  3552. "}\n"
  3553. "{\n"
  3554. " b; //\n"
  3555. "}\n"
  3556. "}",
  3557. format("{\n"
  3558. "{\n"
  3559. "a;\n"
  3560. "}\n"
  3561. "{\n"
  3562. " b; //\n"
  3563. "}\n"
  3564. "}",
  3565. 22, 2, getLLVMStyle()));
  3566. EXPECT_EQ(" {\n"
  3567. " a; //\n"
  3568. " }",
  3569. format(" {\n"
  3570. "a; //\n"
  3571. " }",
  3572. 4, 2, getLLVMStyle()));
  3573. EXPECT_EQ("void f() {}\n"
  3574. "void g() {}",
  3575. format("void f() {}\n"
  3576. "void g() {}",
  3577. 13, 0, getLLVMStyle()));
  3578. EXPECT_EQ("int a; // comment\n"
  3579. " // line 2\n"
  3580. "int b;",
  3581. format("int a; // comment\n"
  3582. " // line 2\n"
  3583. " int b;",
  3584. 35, 0, getLLVMStyle()));
  3585. }
  3586. TEST_F(FormatTest, BreakStringLiterals) {
  3587. EXPECT_EQ("\"some text \"\n"
  3588. "\"other\";",
  3589. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  3590. EXPECT_EQ("\"some text \"\n"
  3591. "\"other\";",
  3592. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  3593. EXPECT_EQ(
  3594. "#define A \\\n"
  3595. " \"some \" \\\n"
  3596. " \"text \" \\\n"
  3597. " \"other\";",
  3598. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  3599. EXPECT_EQ(
  3600. "#define A \\\n"
  3601. " \"so \" \\\n"
  3602. " \"text \" \\\n"
  3603. " \"other\";",
  3604. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  3605. EXPECT_EQ("\"some text\"",
  3606. format("\"some text\"", getLLVMStyleWithColumns(1)));
  3607. EXPECT_EQ("\"some text\"",
  3608. format("\"some text\"", getLLVMStyleWithColumns(11)));
  3609. EXPECT_EQ("\"some \"\n"
  3610. "\"text\"",
  3611. format("\"some text\"", getLLVMStyleWithColumns(10)));
  3612. EXPECT_EQ("\"some \"\n"
  3613. "\"text\"",
  3614. format("\"some text\"", getLLVMStyleWithColumns(7)));
  3615. EXPECT_EQ("\"some\"\n"
  3616. "\" tex\"\n"
  3617. "\"t\"",
  3618. format("\"some text\"", getLLVMStyleWithColumns(6)));
  3619. EXPECT_EQ("\"some\"\n"
  3620. "\" tex\"\n"
  3621. "\" and\"",
  3622. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  3623. EXPECT_EQ("\"some\"\n"
  3624. "\"/tex\"\n"
  3625. "\"/and\"",
  3626. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  3627. EXPECT_EQ("variable =\n"
  3628. " \"long string \"\n"
  3629. " \"literal\";",
  3630. format("variable = \"long string literal\";",
  3631. getLLVMStyleWithColumns(20)));
  3632. EXPECT_EQ("variable = f(\n"
  3633. " \"long string \"\n"
  3634. " \"literal\",\n"
  3635. " short,\n"
  3636. " loooooooooooooooooooong);",
  3637. format("variable = f(\"long string literal\", short, "
  3638. "loooooooooooooooooooong);",
  3639. getLLVMStyleWithColumns(20)));
  3640. EXPECT_EQ(
  3641. "f(\"one two\".split(\n"
  3642. " variable));",
  3643. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  3644. EXPECT_EQ("f(\"one two three four five six \"\n"
  3645. " \"seven\".split(\n"
  3646. " really_looooong_variable));",
  3647. format("f(\"one two three four five six seven\"."
  3648. "split(really_looooong_variable));",
  3649. getLLVMStyleWithColumns(33)));
  3650. EXPECT_EQ("f(\"some \"\n"
  3651. " \"text\",\n"
  3652. " other);",
  3653. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  3654. // Only break as a last resort.
  3655. verifyFormat(
  3656. "aaaaaaaaaaaaaaaaaaaa(\n"
  3657. " aaaaaaaaaaaaaaaaaaaa,\n"
  3658. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  3659. EXPECT_EQ(
  3660. "\"splitmea\"\n"
  3661. "\"trandomp\"\n"
  3662. "\"oint\"",
  3663. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  3664. EXPECT_EQ(
  3665. "\"split/\"\n"
  3666. "\"pathat/\"\n"
  3667. "\"slashes\"",
  3668. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  3669. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  3670. AlignLeft.AlignEscapedNewlinesLeft = true;
  3671. EXPECT_EQ(
  3672. "#define A \\\n"
  3673. " \"some \" \\\n"
  3674. " \"text \" \\\n"
  3675. " \"other\";",
  3676. format("#define A \"some text other\";", AlignLeft));
  3677. }
  3678. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  3679. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  3680. " \"ddeeefff\");",
  3681. format("someFunction(\"aaabbbcccdddeeefff\");",
  3682. getLLVMStyleWithColumns(25)));
  3683. EXPECT_EQ("someFunction1234567890(\n"
  3684. " \"aaabbbcccdddeeefff\");",
  3685. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  3686. getLLVMStyleWithColumns(26)));
  3687. EXPECT_EQ("someFunction1234567890(\n"
  3688. " \"aaabbbcccdddeeeff\"\n"
  3689. " \"f\");",
  3690. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  3691. getLLVMStyleWithColumns(25)));
  3692. EXPECT_EQ("someFunction1234567890(\n"
  3693. " \"aaabbbcccdddeeeff\"\n"
  3694. " \"f\");",
  3695. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  3696. getLLVMStyleWithColumns(24)));
  3697. EXPECT_EQ("someFunction(\n"
  3698. " \"aaabbbcc \"\n"
  3699. " \"dddeeefff\");",
  3700. format("someFunction(\"aaabbbcc dddeeefff\");",
  3701. getLLVMStyleWithColumns(25)));
  3702. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  3703. " \"ddeeefff\");",
  3704. format("someFunction(\"aaabbbccc ddeeefff\");",
  3705. getLLVMStyleWithColumns(25)));
  3706. EXPECT_EQ("someFunction1234567890(\n"
  3707. " \"aaabb \"\n"
  3708. " \"cccdddeeefff\");",
  3709. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  3710. getLLVMStyleWithColumns(25)));
  3711. EXPECT_EQ("#define A \\\n"
  3712. " string s = \\\n"
  3713. " \"123456789\" \\\n"
  3714. " \"0\"; \\\n"
  3715. " int i;",
  3716. format("#define A string s = \"1234567890\"; int i;",
  3717. getLLVMStyleWithColumns(20)));
  3718. }
  3719. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  3720. EXPECT_EQ("\"\\a\"",
  3721. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  3722. EXPECT_EQ("\"\\\"",
  3723. format("\"\\\"", getLLVMStyleWithColumns(2)));
  3724. EXPECT_EQ("\"test\"\n"
  3725. "\"\\n\"",
  3726. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  3727. EXPECT_EQ("\"tes\\\\\"\n"
  3728. "\"n\"",
  3729. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  3730. EXPECT_EQ("\"\\\\\\\\\"\n"
  3731. "\"\\n\"",
  3732. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  3733. EXPECT_EQ("\"\\uff01\"",
  3734. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  3735. EXPECT_EQ("\"\\uff01\"\n"
  3736. "\"test\"",
  3737. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  3738. EXPECT_EQ("\"\\Uff01ff02\"",
  3739. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  3740. EXPECT_EQ("\"\\x000000000001\"\n"
  3741. "\"next\"",
  3742. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  3743. EXPECT_EQ("\"\\x000000000001next\"",
  3744. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  3745. EXPECT_EQ("\"\\x000000000001\"",
  3746. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  3747. EXPECT_EQ("\"test\"\n"
  3748. "\"\\000000\"\n"
  3749. "\"000001\"",
  3750. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  3751. EXPECT_EQ("\"test\\000\"\n"
  3752. "\"00000000\"\n"
  3753. "\"1\"",
  3754. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  3755. EXPECT_EQ("R\"(\\x\\x00)\"\n",
  3756. format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7)));
  3757. }
  3758. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  3759. verifyFormat("void f() {\n"
  3760. " return g() {}\n"
  3761. " void h() {}");
  3762. verifyFormat("if (foo)\n"
  3763. " return { forgot_closing_brace();\n"
  3764. "test();");
  3765. verifyFormat("int a[] = { void forgot_closing_brace()\n"
  3766. "{\n"
  3767. " f();\n"
  3768. " g();\n"
  3769. "}");
  3770. }
  3771. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  3772. verifyFormat("class X {\n"
  3773. " void f() {\n"
  3774. " }\n"
  3775. "};",
  3776. getLLVMStyleWithColumns(12));
  3777. }
  3778. TEST_F(FormatTest, ConfigurableIndentWidth) {
  3779. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  3780. EightIndent.IndentWidth = 8;
  3781. verifyFormat("void f() {\n"
  3782. " someFunction();\n"
  3783. " if (true) {\n"
  3784. " f();\n"
  3785. " }\n"
  3786. "}",
  3787. EightIndent);
  3788. verifyFormat("class X {\n"
  3789. " void f() {\n"
  3790. " }\n"
  3791. "};",
  3792. EightIndent);
  3793. verifyFormat("int x[] = {\n"
  3794. " call(),\n"
  3795. " call(),\n"
  3796. "};",
  3797. EightIndent);
  3798. }
  3799. TEST_F(FormatTest, ConfigurableUseOfTab) {
  3800. FormatStyle Tab = getLLVMStyleWithColumns(42);
  3801. Tab.IndentWidth = 8;
  3802. Tab.UseTab = true;
  3803. Tab.AlignEscapedNewlinesLeft = true;
  3804. verifyFormat("class X {\n"
  3805. "\tvoid f() {\n"
  3806. "\t\tsomeFunction(parameter1,\n"
  3807. "\t\t\t parameter2);\n"
  3808. "\t}\n"
  3809. "};",
  3810. Tab);
  3811. verifyFormat("#define A \\\n"
  3812. "\tvoid f() { \\\n"
  3813. "\t\tsomeFunction( \\\n"
  3814. "\t\t parameter1, \\\n"
  3815. "\t\t parameter2); \\\n"
  3816. "\t}",
  3817. Tab);
  3818. }
  3819. TEST_F(FormatTest, LinuxBraceBreaking) {
  3820. FormatStyle BreakBeforeBrace = getLLVMStyle();
  3821. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
  3822. verifyFormat("namespace a\n"
  3823. "{\n"
  3824. "class A\n"
  3825. "{\n"
  3826. " void f()\n"
  3827. " {\n"
  3828. " if (true) {\n"
  3829. " a();\n"
  3830. " b();\n"
  3831. " }\n"
  3832. " }\n"
  3833. " void g()\n"
  3834. " {\n"
  3835. " return;\n"
  3836. " }\n"
  3837. "}\n"
  3838. "}",
  3839. BreakBeforeBrace);
  3840. }
  3841. TEST_F(FormatTest, StroustrupBraceBreaking) {
  3842. FormatStyle BreakBeforeBrace = getLLVMStyle();
  3843. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  3844. verifyFormat("namespace a {\n"
  3845. "class A {\n"
  3846. " void f()\n"
  3847. " {\n"
  3848. " if (true) {\n"
  3849. " a();\n"
  3850. " b();\n"
  3851. " }\n"
  3852. " }\n"
  3853. " void g()\n"
  3854. " {\n"
  3855. " return;\n"
  3856. " }\n"
  3857. "}\n"
  3858. "}",
  3859. BreakBeforeBrace);
  3860. }
  3861. bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
  3862. for (size_t i = 1; i < Styles.size(); ++i)
  3863. if (!(Styles[0] == Styles[i]))
  3864. return false;
  3865. return true;
  3866. }
  3867. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  3868. FormatStyle Styles[3];
  3869. Styles[0] = getLLVMStyle();
  3870. EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1]));
  3871. EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2]));
  3872. EXPECT_TRUE(allStylesEqual(Styles));
  3873. Styles[0] = getGoogleStyle();
  3874. EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1]));
  3875. EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2]));
  3876. EXPECT_TRUE(allStylesEqual(Styles));
  3877. Styles[0] = getChromiumStyle();
  3878. EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1]));
  3879. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2]));
  3880. EXPECT_TRUE(allStylesEqual(Styles));
  3881. Styles[0] = getMozillaStyle();
  3882. EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1]));
  3883. EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2]));
  3884. EXPECT_TRUE(allStylesEqual(Styles));
  3885. EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0]));
  3886. }
  3887. TEST_F(FormatTest, ParsesConfiguration) {
  3888. FormatStyle Style = {};
  3889. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  3890. EXPECT_NE(VALUE, Style.FIELD); \
  3891. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  3892. EXPECT_EQ(VALUE, Style.FIELD)
  3893. #define CHECK_PARSE_BOOL(FIELD) \
  3894. Style.FIELD = false; \
  3895. EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
  3896. EXPECT_TRUE(Style.FIELD); \
  3897. EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
  3898. EXPECT_FALSE(Style.FIELD);
  3899. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  3900. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  3901. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  3902. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  3903. CHECK_PARSE_BOOL(BinPackParameters);
  3904. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  3905. CHECK_PARSE_BOOL(DerivePointerBinding);
  3906. CHECK_PARSE_BOOL(IndentCaseLabels);
  3907. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  3908. CHECK_PARSE_BOOL(PointerBindsToType);
  3909. CHECK_PARSE_BOOL(UseTab);
  3910. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  3911. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  3912. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  3913. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  3914. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  3915. PenaltyReturnTypeOnItsOwnLine, 1234u);
  3916. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  3917. SpacesBeforeTrailingComments, 1234u);
  3918. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  3919. Style.Standard = FormatStyle::LS_Auto;
  3920. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  3921. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  3922. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  3923. Style.ColumnLimit = 123;
  3924. FormatStyle BaseStyle = getLLVMStyle();
  3925. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  3926. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  3927. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  3928. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  3929. FormatStyle::BS_Attach);
  3930. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  3931. FormatStyle::BS_Linux);
  3932. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  3933. FormatStyle::BS_Stroustrup);
  3934. #undef CHECK_PARSE
  3935. #undef CHECK_PARSE_BOOL
  3936. }
  3937. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  3938. FormatStyle Style = getLLVMStyle();
  3939. std::string YAML = configurationAsText(Style);
  3940. FormatStyle ParsedStyle = {};
  3941. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  3942. EXPECT_EQ(Style, ParsedStyle);
  3943. }
  3944. } // end namespace tooling
  3945. } // end namespace clang