FormatTestComments.cpp 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085
  1. //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Format/Format.h"
  10. #include "../Tooling/ReplacementTest.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. using clang::tooling::ReplacementTest;
  18. namespace clang {
  19. namespace format {
  20. namespace {
  21. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  22. class FormatTestComments : public ::testing::Test {
  23. protected:
  24. enum StatusCheck {
  25. SC_ExpectComplete,
  26. SC_ExpectIncomplete,
  27. SC_DoNotCheck
  28. };
  29. std::string format(llvm::StringRef Code,
  30. const FormatStyle &Style = getLLVMStyle(),
  31. StatusCheck CheckComplete = SC_ExpectComplete) {
  32. DEBUG(llvm::errs() << "---\n");
  33. DEBUG(llvm::errs() << Code << "\n\n");
  34. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  35. FormattingAttemptStatus Status;
  36. tooling::Replacements Replaces =
  37. reformat(Style, Code, Ranges, "<stdin>", &Status);
  38. if (CheckComplete != SC_DoNotCheck) {
  39. bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
  40. EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
  41. << Code << "\n\n";
  42. }
  43. ReplacementCount = Replaces.size();
  44. auto Result = applyAllReplacements(Code, Replaces);
  45. EXPECT_TRUE(static_cast<bool>(Result));
  46. DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  47. return *Result;
  48. }
  49. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getLLVMStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) {
  55. FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto);
  56. Style.ColumnLimit = ColumnLimit;
  57. return Style;
  58. }
  59. void verifyFormat(llvm::StringRef Code,
  60. const FormatStyle &Style = getLLVMStyle()) {
  61. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  62. }
  63. void verifyGoogleFormat(llvm::StringRef Code) {
  64. verifyFormat(Code, getGoogleStyle());
  65. }
  66. /// \brief Verify that clang-format does not crash on the given input.
  67. void verifyNoCrash(llvm::StringRef Code,
  68. const FormatStyle &Style = getLLVMStyle()) {
  69. format(Code, Style, SC_DoNotCheck);
  70. }
  71. int ReplacementCount;
  72. };
  73. //===----------------------------------------------------------------------===//
  74. // Tests for comments.
  75. //===----------------------------------------------------------------------===//
  76. TEST_F(FormatTestComments, UnderstandsSingleLineComments) {
  77. verifyFormat("//* */");
  78. verifyFormat("// line 1\n"
  79. "// line 2\n"
  80. "void f() {}\n");
  81. verifyFormat("void f() {\n"
  82. " // Doesn't do anything\n"
  83. "}");
  84. verifyFormat("SomeObject\n"
  85. " // Calling someFunction on SomeObject\n"
  86. " .someFunction();");
  87. verifyFormat("auto result = SomeObject\n"
  88. " // Calling someFunction on SomeObject\n"
  89. " .someFunction();");
  90. verifyFormat("void f(int i, // some comment (probably for i)\n"
  91. " int j, // some comment (probably for j)\n"
  92. " int k); // some comment (probably for k)");
  93. verifyFormat("void f(int i,\n"
  94. " // some comment (probably for j)\n"
  95. " int j,\n"
  96. " // some comment (probably for k)\n"
  97. " int k);");
  98. verifyFormat("int i // This is a fancy variable\n"
  99. " = 5; // with nicely aligned comment.");
  100. verifyFormat("// Leading comment.\n"
  101. "int a; // Trailing comment.");
  102. verifyFormat("int a; // Trailing comment\n"
  103. " // on 2\n"
  104. " // or 3 lines.\n"
  105. "int b;");
  106. verifyFormat("int a; // Trailing comment\n"
  107. "\n"
  108. "// Leading comment.\n"
  109. "int b;");
  110. verifyFormat("int a; // Comment.\n"
  111. " // More details.\n"
  112. "int bbbb; // Another comment.");
  113. verifyFormat(
  114. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  115. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  116. "int cccccccccccccccccccccccccccccc; // comment\n"
  117. "int ddd; // looooooooooooooooooooooooong comment\n"
  118. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  119. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  120. "int ccccccccccccccccccc; // comment");
  121. verifyFormat("#include \"a\" // comment\n"
  122. "#include \"a/b/c\" // comment");
  123. verifyFormat("#include <a> // comment\n"
  124. "#include <a/b/c> // comment");
  125. EXPECT_EQ("#include \"a\" // comment\n"
  126. "#include \"a/b/c\" // comment",
  127. format("#include \\\n"
  128. " \"a\" // comment\n"
  129. "#include \"a/b/c\" // comment"));
  130. verifyFormat("enum E {\n"
  131. " // comment\n"
  132. " VAL_A, // comment\n"
  133. " VAL_B\n"
  134. "};");
  135. EXPECT_EQ("enum A {\n"
  136. " // line a\n"
  137. " a,\n"
  138. " b, // line b\n"
  139. "\n"
  140. " // line c\n"
  141. " c\n"
  142. "};",
  143. format("enum A {\n"
  144. " // line a\n"
  145. " a,\n"
  146. " b, // line b\n"
  147. "\n"
  148. " // line c\n"
  149. " c\n"
  150. "};",
  151. getLLVMStyleWithColumns(20)));
  152. EXPECT_EQ("enum A {\n"
  153. " a, // line 1\n"
  154. " // line 2\n"
  155. "};",
  156. format("enum A {\n"
  157. " a, // line 1\n"
  158. " // line 2\n"
  159. "};",
  160. getLLVMStyleWithColumns(20)));
  161. EXPECT_EQ("enum A {\n"
  162. " a, // line 1\n"
  163. " // line 2\n"
  164. "};",
  165. format("enum A {\n"
  166. " a, // line 1\n"
  167. " // line 2\n"
  168. "};",
  169. getLLVMStyleWithColumns(20)));
  170. EXPECT_EQ("enum A {\n"
  171. " a, // line 1\n"
  172. " // line 2\n"
  173. " b\n"
  174. "};",
  175. format("enum A {\n"
  176. " a, // line 1\n"
  177. " // line 2\n"
  178. " b\n"
  179. "};",
  180. getLLVMStyleWithColumns(20)));
  181. EXPECT_EQ("enum A {\n"
  182. " a, // line 1\n"
  183. " // line 2\n"
  184. " b\n"
  185. "};",
  186. format("enum A {\n"
  187. " a, // line 1\n"
  188. " // line 2\n"
  189. " b\n"
  190. "};",
  191. getLLVMStyleWithColumns(20)));
  192. verifyFormat(
  193. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  194. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  195. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  196. " // Comment inside a statement.\n"
  197. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  198. verifyFormat("SomeFunction(a,\n"
  199. " // comment\n"
  200. " b + x);");
  201. verifyFormat("SomeFunction(a, a,\n"
  202. " // comment\n"
  203. " b + x);");
  204. verifyFormat(
  205. "bool aaaaaaaaaaaaa = // comment\n"
  206. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  207. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  208. verifyFormat("int aaaa; // aaaaa\n"
  209. "int aa; // aaaaaaa",
  210. getLLVMStyleWithColumns(20));
  211. EXPECT_EQ("void f() { // This does something ..\n"
  212. "}\n"
  213. "int a; // This is unrelated",
  214. format("void f() { // This does something ..\n"
  215. " }\n"
  216. "int a; // This is unrelated"));
  217. EXPECT_EQ("class C {\n"
  218. " void f() { // This does something ..\n"
  219. " } // awesome..\n"
  220. "\n"
  221. " int a; // This is unrelated\n"
  222. "};",
  223. format("class C{void f() { // This does something ..\n"
  224. " } // awesome..\n"
  225. " \n"
  226. "int a; // This is unrelated\n"
  227. "};"));
  228. EXPECT_EQ("int i; // single line trailing comment",
  229. format("int i;\\\n// single line trailing comment"));
  230. verifyGoogleFormat("int a; // Trailing comment.");
  231. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  232. " parameter));");
  233. verifyGoogleFormat("#endif // HEADER_GUARD");
  234. verifyFormat("const char *test[] = {\n"
  235. " // A\n"
  236. " \"aaaa\",\n"
  237. " // B\n"
  238. " \"aaaaa\"};");
  239. verifyGoogleFormat(
  240. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  241. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  242. EXPECT_EQ("D(a, {\n"
  243. " // test\n"
  244. " int a;\n"
  245. "});",
  246. format("D(a, {\n"
  247. "// test\n"
  248. "int a;\n"
  249. "});"));
  250. EXPECT_EQ("lineWith(); // comment\n"
  251. "// at start\n"
  252. "otherLine();",
  253. format("lineWith(); // comment\n"
  254. "// at start\n"
  255. "otherLine();"));
  256. EXPECT_EQ("lineWith(); // comment\n"
  257. "/*\n"
  258. " * at start */\n"
  259. "otherLine();",
  260. format("lineWith(); // comment\n"
  261. "/*\n"
  262. " * at start */\n"
  263. "otherLine();"));
  264. EXPECT_EQ("lineWith(); // comment\n"
  265. " // at start\n"
  266. "otherLine();",
  267. format("lineWith(); // comment\n"
  268. " // at start\n"
  269. "otherLine();"));
  270. EXPECT_EQ("lineWith(); // comment\n"
  271. "// at start\n"
  272. "otherLine(); // comment",
  273. format("lineWith(); // comment\n"
  274. "// at start\n"
  275. "otherLine(); // comment"));
  276. EXPECT_EQ("lineWith();\n"
  277. "// at start\n"
  278. "otherLine(); // comment",
  279. format("lineWith();\n"
  280. " // at start\n"
  281. "otherLine(); // comment"));
  282. EXPECT_EQ("// first\n"
  283. "// at start\n"
  284. "otherLine(); // comment",
  285. format("// first\n"
  286. " // at start\n"
  287. "otherLine(); // comment"));
  288. EXPECT_EQ("f();\n"
  289. "// first\n"
  290. "// at start\n"
  291. "otherLine(); // comment",
  292. format("f();\n"
  293. "// first\n"
  294. " // at start\n"
  295. "otherLine(); // comment"));
  296. verifyFormat("f(); // comment\n"
  297. "// first\n"
  298. "// at start\n"
  299. "otherLine();");
  300. EXPECT_EQ("f(); // comment\n"
  301. "// first\n"
  302. "// at start\n"
  303. "otherLine();",
  304. format("f(); // comment\n"
  305. "// first\n"
  306. " // at start\n"
  307. "otherLine();"));
  308. EXPECT_EQ("f(); // comment\n"
  309. " // first\n"
  310. "// at start\n"
  311. "otherLine();",
  312. format("f(); // comment\n"
  313. " // first\n"
  314. "// at start\n"
  315. "otherLine();"));
  316. EXPECT_EQ("void f() {\n"
  317. " lineWith(); // comment\n"
  318. " // at start\n"
  319. "}",
  320. format("void f() {\n"
  321. " lineWith(); // comment\n"
  322. " // at start\n"
  323. "}"));
  324. EXPECT_EQ("int xy; // a\n"
  325. "int z; // b",
  326. format("int xy; // a\n"
  327. "int z; //b"));
  328. EXPECT_EQ("int xy; // a\n"
  329. "int z; // bb",
  330. format("int xy; // a\n"
  331. "int z; //bb",
  332. getLLVMStyleWithColumns(12)));
  333. verifyFormat("#define A \\\n"
  334. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  335. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  336. getLLVMStyleWithColumns(60));
  337. verifyFormat(
  338. "#define A \\\n"
  339. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  340. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  341. getLLVMStyleWithColumns(61));
  342. verifyFormat("if ( // This is some comment\n"
  343. " x + 3) {\n"
  344. "}");
  345. EXPECT_EQ("if ( // This is some comment\n"
  346. " // spanning two lines\n"
  347. " x + 3) {\n"
  348. "}",
  349. format("if( // This is some comment\n"
  350. " // spanning two lines\n"
  351. " x + 3) {\n"
  352. "}"));
  353. verifyNoCrash("/\\\n/");
  354. verifyNoCrash("/\\\n* */");
  355. // The 0-character somehow makes the lexer return a proper comment.
  356. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  357. }
  358. TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  359. EXPECT_EQ("SomeFunction(a,\n"
  360. " b, // comment\n"
  361. " c);",
  362. format("SomeFunction(a,\n"
  363. " b, // comment\n"
  364. " c);"));
  365. EXPECT_EQ("SomeFunction(a, b,\n"
  366. " // comment\n"
  367. " c);",
  368. format("SomeFunction(a,\n"
  369. " b,\n"
  370. " // comment\n"
  371. " c);"));
  372. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  373. " c);",
  374. format("SomeFunction(a, b, // comment (unclear relation)\n"
  375. " c);"));
  376. EXPECT_EQ("SomeFunction(a, // comment\n"
  377. " b,\n"
  378. " c); // comment",
  379. format("SomeFunction(a, // comment\n"
  380. " b,\n"
  381. " c); // comment"));
  382. EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
  383. " aaaa), //\n"
  384. " aaaa, bbbbb);",
  385. format("aaaaaaaaaa(aaaa(aaaa,\n"
  386. "aaaa), //\n"
  387. "aaaa, bbbbb);"));
  388. }
  389. TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) {
  390. EXPECT_EQ("// comment", format("// comment "));
  391. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  392. format("int aaaaaaa, bbbbbbb; // comment ",
  393. getLLVMStyleWithColumns(33)));
  394. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  395. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  396. }
  397. TEST_F(FormatTestComments, UnderstandsBlockComments) {
  398. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  399. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
  400. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  401. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  402. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  403. "/* Trailing comment for aa... */\n"
  404. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  405. EXPECT_EQ(
  406. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  407. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  408. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  409. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  410. EXPECT_EQ(
  411. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  412. " aaaaaaaaaaaaaaaaaa,\n"
  413. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  414. "}",
  415. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  416. " aaaaaaaaaaaaaaaaaa ,\n"
  417. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  418. "}"));
  419. verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
  420. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  421. FormatStyle NoBinPacking = getLLVMStyle();
  422. NoBinPacking.BinPackParameters = false;
  423. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  424. " /* parameter 2 */ aaaaaa,\n"
  425. " /* parameter 3 */ aaaaaa,\n"
  426. " /* parameter 4 */ aaaaaa);",
  427. NoBinPacking);
  428. // Aligning block comments in macros.
  429. verifyGoogleFormat("#define A \\\n"
  430. " int i; /*a*/ \\\n"
  431. " int jjj; /*b*/");
  432. }
  433. TEST_F(FormatTestComments, AlignsBlockComments) {
  434. EXPECT_EQ("/*\n"
  435. " * Really multi-line\n"
  436. " * comment.\n"
  437. " */\n"
  438. "void f() {}",
  439. format(" /*\n"
  440. " * Really multi-line\n"
  441. " * comment.\n"
  442. " */\n"
  443. " void f() {}"));
  444. EXPECT_EQ("class C {\n"
  445. " /*\n"
  446. " * Another multi-line\n"
  447. " * comment.\n"
  448. " */\n"
  449. " void f() {}\n"
  450. "};",
  451. format("class C {\n"
  452. "/*\n"
  453. " * Another multi-line\n"
  454. " * comment.\n"
  455. " */\n"
  456. "void f() {}\n"
  457. "};"));
  458. EXPECT_EQ("/*\n"
  459. " 1. This is a comment with non-trivial formatting.\n"
  460. " 1.1. We have to indent/outdent all lines equally\n"
  461. " 1.1.1. to keep the formatting.\n"
  462. " */",
  463. format(" /*\n"
  464. " 1. This is a comment with non-trivial formatting.\n"
  465. " 1.1. We have to indent/outdent all lines equally\n"
  466. " 1.1.1. to keep the formatting.\n"
  467. " */"));
  468. EXPECT_EQ("/*\n"
  469. "Don't try to outdent if there's not enough indentation.\n"
  470. "*/",
  471. format(" /*\n"
  472. " Don't try to outdent if there's not enough indentation.\n"
  473. " */"));
  474. EXPECT_EQ("int i; /* Comment with empty...\n"
  475. " *\n"
  476. " * line. */",
  477. format("int i; /* Comment with empty...\n"
  478. " *\n"
  479. " * line. */"));
  480. EXPECT_EQ("int foobar = 0; /* comment */\n"
  481. "int bar = 0; /* multiline\n"
  482. " comment 1 */\n"
  483. "int baz = 0; /* multiline\n"
  484. " comment 2 */\n"
  485. "int bzz = 0; /* multiline\n"
  486. " comment 3 */",
  487. format("int foobar = 0; /* comment */\n"
  488. "int bar = 0; /* multiline\n"
  489. " comment 1 */\n"
  490. "int baz = 0; /* multiline\n"
  491. " comment 2 */\n"
  492. "int bzz = 0; /* multiline\n"
  493. " comment 3 */"));
  494. EXPECT_EQ("int foobar = 0; /* comment */\n"
  495. "int bar = 0; /* multiline\n"
  496. " comment */\n"
  497. "int baz = 0; /* multiline\n"
  498. "comment */",
  499. format("int foobar = 0; /* comment */\n"
  500. "int bar = 0; /* multiline\n"
  501. "comment */\n"
  502. "int baz = 0; /* multiline\n"
  503. "comment */"));
  504. }
  505. TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) {
  506. FormatStyle Style = getLLVMStyleWithColumns(20);
  507. Style.ReflowComments = false;
  508. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  509. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  510. }
  511. TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) {
  512. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  513. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  514. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  515. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  516. EXPECT_EQ(
  517. "void ffffffffffff(\n"
  518. " int aaaaaaaa, int bbbbbbbb,\n"
  519. " int cccccccccccc) { /*\n"
  520. " aaaaaaaaaa\n"
  521. " aaaaaaaaaaaaa\n"
  522. " bbbbbbbbbbbbbb\n"
  523. " bbbbbbbbbb\n"
  524. " */\n"
  525. "}",
  526. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  527. "{ /*\n"
  528. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  529. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  530. " */\n"
  531. "}",
  532. getLLVMStyleWithColumns(40)));
  533. }
  534. TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) {
  535. EXPECT_EQ("void ffffffffff(\n"
  536. " int aaaaa /* test */);",
  537. format("void ffffffffff(int aaaaa /* test */);",
  538. getLLVMStyleWithColumns(35)));
  539. }
  540. TEST_F(FormatTestComments, SplitsLongCxxComments) {
  541. EXPECT_EQ("// A comment that\n"
  542. "// doesn't fit on\n"
  543. "// one line",
  544. format("// A comment that doesn't fit on one line",
  545. getLLVMStyleWithColumns(20)));
  546. EXPECT_EQ("/// A comment that\n"
  547. "/// doesn't fit on\n"
  548. "/// one line",
  549. format("/// A comment that doesn't fit on one line",
  550. getLLVMStyleWithColumns(20)));
  551. EXPECT_EQ("//! A comment that\n"
  552. "//! doesn't fit on\n"
  553. "//! one line",
  554. format("//! A comment that doesn't fit on one line",
  555. getLLVMStyleWithColumns(20)));
  556. EXPECT_EQ("// a b c d\n"
  557. "// e f g\n"
  558. "// h i j k",
  559. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  560. EXPECT_EQ(
  561. "// a b c d\n"
  562. "// e f g\n"
  563. "// h i j k",
  564. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  565. EXPECT_EQ("if (true) // A comment that\n"
  566. " // doesn't fit on\n"
  567. " // one line",
  568. format("if (true) // A comment that doesn't fit on one line ",
  569. getLLVMStyleWithColumns(30)));
  570. EXPECT_EQ("// Don't_touch_leading_whitespace",
  571. format("// Don't_touch_leading_whitespace",
  572. getLLVMStyleWithColumns(20)));
  573. EXPECT_EQ("// Add leading\n"
  574. "// whitespace",
  575. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  576. EXPECT_EQ("/// Add leading\n"
  577. "/// whitespace",
  578. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  579. EXPECT_EQ("//! Add leading\n"
  580. "//! whitespace",
  581. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  582. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  583. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  584. "// limit",
  585. format("//Even if it makes the line exceed the column limit",
  586. getLLVMStyleWithColumns(51)));
  587. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  588. EXPECT_EQ("/// line 1\n"
  589. "// add leading whitespace",
  590. format("/// line 1\n"
  591. "//add leading whitespace",
  592. getLLVMStyleWithColumns(30)));
  593. EXPECT_EQ("/// line 1\n"
  594. "/// line 2\n"
  595. "//! line 3\n"
  596. "//! line 4\n"
  597. "//! line 5\n"
  598. "// line 6\n"
  599. "// line 7",
  600. format("///line 1\n"
  601. "///line 2\n"
  602. "//! line 3\n"
  603. "//!line 4\n"
  604. "//!line 5\n"
  605. "// line 6\n"
  606. "//line 7", getLLVMStyleWithColumns(20)));
  607. EXPECT_EQ("// aa bb cc dd",
  608. format("// aa bb cc dd ",
  609. getLLVMStyleWithColumns(15)));
  610. EXPECT_EQ("// A comment before\n"
  611. "// a macro\n"
  612. "// definition\n"
  613. "#define a b",
  614. format("// A comment before a macro definition\n"
  615. "#define a b",
  616. getLLVMStyleWithColumns(20)));
  617. EXPECT_EQ("void ffffff(\n"
  618. " int aaaaaaaaa, // wwww\n"
  619. " int bbbbbbbbbb, // xxxxxxx\n"
  620. " // yyyyyyyyyy\n"
  621. " int c, int d, int e) {}",
  622. format("void ffffff(\n"
  623. " int aaaaaaaaa, // wwww\n"
  624. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  625. " int c, int d, int e) {}",
  626. getLLVMStyleWithColumns(40)));
  627. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  628. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  629. getLLVMStyleWithColumns(20)));
  630. EXPECT_EQ(
  631. "#define XXX // a b c d\n"
  632. " // e f g h",
  633. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  634. EXPECT_EQ(
  635. "#define XXX // q w e r\n"
  636. " // t y u i",
  637. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  638. EXPECT_EQ("{\n"
  639. " //\n"
  640. " //\\\n"
  641. " // long 1 2 3 4 5\n"
  642. "}",
  643. format("{\n"
  644. " //\n"
  645. " //\\\n"
  646. " // long 1 2 3 4 5\n"
  647. "}",
  648. getLLVMStyleWithColumns(20)));
  649. EXPECT_EQ("{\n"
  650. " //\n"
  651. " //\\\n"
  652. " // long 1 2 3 4 5\n"
  653. " // 6\n"
  654. "}",
  655. format("{\n"
  656. " //\n"
  657. " //\\\n"
  658. " // long 1 2 3 4 5 6\n"
  659. "}",
  660. getLLVMStyleWithColumns(20)));
  661. }
  662. TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) {
  663. EXPECT_EQ("// A comment\n"
  664. "// that doesn't\n"
  665. "// fit on one\n"
  666. "// line",
  667. format("// A comment that doesn't fit on one line",
  668. getLLVMStyleWithColumns(20)));
  669. EXPECT_EQ("/// A comment\n"
  670. "/// that doesn't\n"
  671. "/// fit on one\n"
  672. "/// line",
  673. format("/// A comment that doesn't fit on one line",
  674. getLLVMStyleWithColumns(20)));
  675. }
  676. TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) {
  677. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  678. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  679. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  680. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  681. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  682. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  683. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  684. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  685. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  686. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  687. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  688. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  689. getLLVMStyleWithColumns(50)));
  690. // FIXME: One day we might want to implement adjustment of leading whitespace
  691. // of the consecutive lines in this kind of comment:
  692. EXPECT_EQ("double\n"
  693. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  694. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  695. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  696. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  697. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  698. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  699. getLLVMStyleWithColumns(49)));
  700. }
  701. TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) {
  702. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  703. Pragmas.CommentPragmas = "^ IWYU pragma:";
  704. EXPECT_EQ(
  705. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  706. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  707. EXPECT_EQ(
  708. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  709. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  710. }
  711. TEST_F(FormatTestComments, PriorityOfCommentBreaking) {
  712. EXPECT_EQ("if (xxx ==\n"
  713. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  714. " zzz)\n"
  715. " q();",
  716. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  717. " zzz) q();",
  718. getLLVMStyleWithColumns(40)));
  719. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  720. " yyy && // aaaaaa bbbbbbbb cccc\n"
  721. " zzz)\n"
  722. " q();",
  723. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  724. " zzz) q();",
  725. getLLVMStyleWithColumns(40)));
  726. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  727. " yyy || // aaaaaa bbbbbbbb cccc\n"
  728. " zzz)\n"
  729. " q();",
  730. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  731. " zzz) q();",
  732. getLLVMStyleWithColumns(40)));
  733. EXPECT_EQ("fffffffff(\n"
  734. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  735. " zzz);",
  736. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  737. " zzz);",
  738. getLLVMStyleWithColumns(40)));
  739. }
  740. TEST_F(FormatTestComments, MultiLineCommentsInDefines) {
  741. EXPECT_EQ("#define A(x) /* \\\n"
  742. " a comment \\\n"
  743. " inside */ \\\n"
  744. " f();",
  745. format("#define A(x) /* \\\n"
  746. " a comment \\\n"
  747. " inside */ \\\n"
  748. " f();",
  749. getLLVMStyleWithColumns(17)));
  750. EXPECT_EQ("#define A( \\\n"
  751. " x) /* \\\n"
  752. " a comment \\\n"
  753. " inside */ \\\n"
  754. " f();",
  755. format("#define A( \\\n"
  756. " x) /* \\\n"
  757. " a comment \\\n"
  758. " inside */ \\\n"
  759. " f();",
  760. getLLVMStyleWithColumns(17)));
  761. }
  762. TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
  763. EXPECT_EQ("namespace {}\n// Test\n#define A",
  764. format("namespace {}\n // Test\n#define A"));
  765. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  766. format("namespace {}\n /* Test */\n#define A"));
  767. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  768. format("namespace {}\n /* Test */ #define A"));
  769. }
  770. TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
  771. // Keep the current level if the comment was originally not aligned with
  772. // the preprocessor directive.
  773. EXPECT_EQ("void f() {\n"
  774. " int i;\n"
  775. " /* comment */\n"
  776. "#ifdef A\n"
  777. " int j;\n"
  778. "}",
  779. format("void f() {\n"
  780. " int i;\n"
  781. " /* comment */\n"
  782. "#ifdef A\n"
  783. " int j;\n"
  784. "}"));
  785. EXPECT_EQ("void f() {\n"
  786. " int i;\n"
  787. " /* comment */\n"
  788. "\n"
  789. "#ifdef A\n"
  790. " int j;\n"
  791. "}",
  792. format("void f() {\n"
  793. " int i;\n"
  794. " /* comment */\n"
  795. "\n"
  796. "#ifdef A\n"
  797. " int j;\n"
  798. "}"));
  799. EXPECT_EQ("int f(int i) {\n"
  800. " if (true) {\n"
  801. " ++i;\n"
  802. " }\n"
  803. " // comment\n"
  804. "#ifdef A\n"
  805. " int j;\n"
  806. "#endif\n"
  807. "}",
  808. format("int f(int i) {\n"
  809. " if (true) {\n"
  810. " ++i;\n"
  811. " }\n"
  812. " // comment\n"
  813. "#ifdef A\n"
  814. "int j;\n"
  815. "#endif\n"
  816. "}"));
  817. EXPECT_EQ("int f(int i) {\n"
  818. " if (true) {\n"
  819. " i++;\n"
  820. " } else {\n"
  821. " // comment in else\n"
  822. "#ifdef A\n"
  823. " j++;\n"
  824. "#endif\n"
  825. " }\n"
  826. "}",
  827. format("int f(int i) {\n"
  828. " if (true) {\n"
  829. " i++;\n"
  830. " } else {\n"
  831. " // comment in else\n"
  832. "#ifdef A\n"
  833. " j++;\n"
  834. "#endif\n"
  835. " }\n"
  836. "}"));
  837. EXPECT_EQ("int f(int i) {\n"
  838. " if (true) {\n"
  839. " i++;\n"
  840. " } else {\n"
  841. " /* comment in else */\n"
  842. "#ifdef A\n"
  843. " j++;\n"
  844. "#endif\n"
  845. " }\n"
  846. "}",
  847. format("int f(int i) {\n"
  848. " if (true) {\n"
  849. " i++;\n"
  850. " } else {\n"
  851. " /* comment in else */\n"
  852. "#ifdef A\n"
  853. " j++;\n"
  854. "#endif\n"
  855. " }\n"
  856. "}"));
  857. // Keep the current level if there is an empty line between the comment and
  858. // the preprocessor directive.
  859. EXPECT_EQ("void f() {\n"
  860. " int i;\n"
  861. " /* comment */\n"
  862. "\n"
  863. "#ifdef A\n"
  864. " int j;\n"
  865. "}",
  866. format("void f() {\n"
  867. " int i;\n"
  868. "/* comment */\n"
  869. "\n"
  870. "#ifdef A\n"
  871. " int j;\n"
  872. "}"));
  873. EXPECT_EQ("void f() {\n"
  874. " int i;\n"
  875. " return i;\n"
  876. "}\n"
  877. "// comment\n"
  878. "\n"
  879. "#ifdef A\n"
  880. "int i;\n"
  881. "#endif // A",
  882. format("void f() {\n"
  883. " int i;\n"
  884. " return i;\n"
  885. "}\n"
  886. "// comment\n"
  887. "\n"
  888. "#ifdef A\n"
  889. "int i;\n"
  890. "#endif // A"));
  891. EXPECT_EQ("int f(int i) {\n"
  892. " if (true) {\n"
  893. " ++i;\n"
  894. " }\n"
  895. " // comment\n"
  896. "\n"
  897. "#ifdef A\n"
  898. " int j;\n"
  899. "#endif\n"
  900. "}",
  901. format("int f(int i) {\n"
  902. " if (true) {\n"
  903. " ++i;\n"
  904. " }\n"
  905. " // comment\n"
  906. "\n"
  907. "#ifdef A\n"
  908. " int j;\n"
  909. "#endif\n"
  910. "}"));
  911. EXPECT_EQ("int f(int i) {\n"
  912. " if (true) {\n"
  913. " i++;\n"
  914. " } else {\n"
  915. " // comment in else\n"
  916. "\n"
  917. "#ifdef A\n"
  918. " j++;\n"
  919. "#endif\n"
  920. " }\n"
  921. "}",
  922. format("int f(int i) {\n"
  923. " if (true) {\n"
  924. " i++;\n"
  925. " } else {\n"
  926. "// comment in else\n"
  927. "\n"
  928. "#ifdef A\n"
  929. " j++;\n"
  930. "#endif\n"
  931. " }\n"
  932. "}"));
  933. EXPECT_EQ("int f(int i) {\n"
  934. " if (true) {\n"
  935. " i++;\n"
  936. " } else {\n"
  937. " /* comment in else */\n"
  938. "\n"
  939. "#ifdef A\n"
  940. " j++;\n"
  941. "#endif\n"
  942. " }\n"
  943. "}",
  944. format("int f(int i) {\n"
  945. " if (true) {\n"
  946. " i++;\n"
  947. " } else {\n"
  948. "/* comment in else */\n"
  949. "\n"
  950. "#ifdef A\n"
  951. " j++;\n"
  952. "#endif\n"
  953. " }\n"
  954. "}"));
  955. // Align with the preprocessor directive if the comment was originally aligned
  956. // with the preprocessor directive and there is no newline between the comment
  957. // and the preprocessor directive.
  958. EXPECT_EQ("void f() {\n"
  959. " int i;\n"
  960. "/* comment */\n"
  961. "#ifdef A\n"
  962. " int j;\n"
  963. "}",
  964. format("void f() {\n"
  965. " int i;\n"
  966. "/* comment */\n"
  967. "#ifdef A\n"
  968. " int j;\n"
  969. "}"));
  970. EXPECT_EQ("int f(int i) {\n"
  971. " if (true) {\n"
  972. " ++i;\n"
  973. " }\n"
  974. "// comment\n"
  975. "#ifdef A\n"
  976. " int j;\n"
  977. "#endif\n"
  978. "}",
  979. format("int f(int i) {\n"
  980. " if (true) {\n"
  981. " ++i;\n"
  982. " }\n"
  983. "// comment\n"
  984. "#ifdef A\n"
  985. " int j;\n"
  986. "#endif\n"
  987. "}"));
  988. EXPECT_EQ("int f(int i) {\n"
  989. " if (true) {\n"
  990. " i++;\n"
  991. " } else {\n"
  992. "// comment in else\n"
  993. "#ifdef A\n"
  994. " j++;\n"
  995. "#endif\n"
  996. " }\n"
  997. "}",
  998. format("int f(int i) {\n"
  999. " if (true) {\n"
  1000. " i++;\n"
  1001. " } else {\n"
  1002. " // comment in else\n"
  1003. " #ifdef A\n"
  1004. " j++;\n"
  1005. "#endif\n"
  1006. " }\n"
  1007. "}"));
  1008. EXPECT_EQ("int f(int i) {\n"
  1009. " if (true) {\n"
  1010. " i++;\n"
  1011. " } else {\n"
  1012. "/* comment in else */\n"
  1013. "#ifdef A\n"
  1014. " j++;\n"
  1015. "#endif\n"
  1016. " }\n"
  1017. "}",
  1018. format("int f(int i) {\n"
  1019. " if (true) {\n"
  1020. " i++;\n"
  1021. " } else {\n"
  1022. " /* comment in else */\n"
  1023. " #ifdef A\n"
  1024. " j++;\n"
  1025. "#endif\n"
  1026. " }\n"
  1027. "}"));
  1028. }
  1029. TEST_F(FormatTestComments, SplitsLongLinesInComments) {
  1030. // FIXME: Do we need to fix up the " */" at the end?
  1031. // It doesn't look like any of our current logic triggers this.
  1032. EXPECT_EQ("/* This is a long\n"
  1033. " * comment that\n"
  1034. " * doesn't fit on\n"
  1035. " * one line. */",
  1036. format("/* "
  1037. "This is a long "
  1038. "comment that "
  1039. "doesn't "
  1040. "fit on one line. */",
  1041. getLLVMStyleWithColumns(20)));
  1042. EXPECT_EQ(
  1043. "/* a b c d\n"
  1044. " * e f g\n"
  1045. " * h i j k\n"
  1046. " */",
  1047. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1048. EXPECT_EQ(
  1049. "/* a b c d\n"
  1050. " * e f g\n"
  1051. " * h i j k\n"
  1052. " */",
  1053. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1054. EXPECT_EQ("/*\n"
  1055. "This is a long\n"
  1056. "comment that doesn't\n"
  1057. "fit on one line.\n"
  1058. "*/",
  1059. format("/*\n"
  1060. "This is a long "
  1061. "comment that doesn't "
  1062. "fit on one line. \n"
  1063. "*/",
  1064. getLLVMStyleWithColumns(20)));
  1065. EXPECT_EQ("/*\n"
  1066. " * This is a long\n"
  1067. " * comment that\n"
  1068. " * doesn't fit on\n"
  1069. " * one line.\n"
  1070. " */",
  1071. format("/* \n"
  1072. " * This is a long "
  1073. " comment that "
  1074. " doesn't fit on "
  1075. " one line. \n"
  1076. " */",
  1077. getLLVMStyleWithColumns(20)));
  1078. EXPECT_EQ("/*\n"
  1079. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1080. " * so_it_should_be_broken\n"
  1081. " * wherever_a_space_occurs\n"
  1082. " */",
  1083. format("/*\n"
  1084. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1085. " so_it_should_be_broken "
  1086. " wherever_a_space_occurs \n"
  1087. " */",
  1088. getLLVMStyleWithColumns(20)));
  1089. EXPECT_EQ("/*\n"
  1090. " * This_comment_can_not_be_broken_into_lines\n"
  1091. " */",
  1092. format("/*\n"
  1093. " * This_comment_can_not_be_broken_into_lines\n"
  1094. " */",
  1095. getLLVMStyleWithColumns(20)));
  1096. EXPECT_EQ("{\n"
  1097. " /*\n"
  1098. " This is another\n"
  1099. " long comment that\n"
  1100. " doesn't fit on one\n"
  1101. " line 1234567890\n"
  1102. " */\n"
  1103. "}",
  1104. format("{\n"
  1105. "/*\n"
  1106. "This is another "
  1107. " long comment that "
  1108. " doesn't fit on one"
  1109. " line 1234567890\n"
  1110. "*/\n"
  1111. "}",
  1112. getLLVMStyleWithColumns(20)));
  1113. EXPECT_EQ("{\n"
  1114. " /*\n"
  1115. " * This i s\n"
  1116. " * another comment\n"
  1117. " * t hat doesn' t\n"
  1118. " * fit on one l i\n"
  1119. " * n e\n"
  1120. " */\n"
  1121. "}",
  1122. format("{\n"
  1123. "/*\n"
  1124. " * This i s"
  1125. " another comment"
  1126. " t hat doesn' t"
  1127. " fit on one l i"
  1128. " n e\n"
  1129. " */\n"
  1130. "}",
  1131. getLLVMStyleWithColumns(20)));
  1132. EXPECT_EQ("/*\n"
  1133. " * This is a long\n"
  1134. " * comment that\n"
  1135. " * doesn't fit on\n"
  1136. " * one line\n"
  1137. " */",
  1138. format(" /*\n"
  1139. " * This is a long comment that doesn't fit on one line\n"
  1140. " */",
  1141. getLLVMStyleWithColumns(20)));
  1142. EXPECT_EQ("{\n"
  1143. " if (something) /* This is a\n"
  1144. " long\n"
  1145. " comment */\n"
  1146. " ;\n"
  1147. "}",
  1148. format("{\n"
  1149. " if (something) /* This is a long comment */\n"
  1150. " ;\n"
  1151. "}",
  1152. getLLVMStyleWithColumns(30)));
  1153. EXPECT_EQ("/* A comment before\n"
  1154. " * a macro\n"
  1155. " * definition */\n"
  1156. "#define a b",
  1157. format("/* A comment before a macro definition */\n"
  1158. "#define a b",
  1159. getLLVMStyleWithColumns(20)));
  1160. EXPECT_EQ("/* some comment\n"
  1161. " * a comment that\n"
  1162. " * we break another\n"
  1163. " * comment we have\n"
  1164. " * to break a left\n"
  1165. " * comment\n"
  1166. " */",
  1167. format(" /* some comment\n"
  1168. " * a comment that we break\n"
  1169. " * another comment we have to break\n"
  1170. "* a left comment\n"
  1171. " */",
  1172. getLLVMStyleWithColumns(20)));
  1173. EXPECT_EQ("/**\n"
  1174. " * multiline block\n"
  1175. " * comment\n"
  1176. " *\n"
  1177. " */",
  1178. format("/**\n"
  1179. " * multiline block comment\n"
  1180. " *\n"
  1181. " */",
  1182. getLLVMStyleWithColumns(20)));
  1183. EXPECT_EQ("/*\n"
  1184. "\n"
  1185. "\n"
  1186. " */\n",
  1187. format(" /* \n"
  1188. " \n"
  1189. " \n"
  1190. " */\n"));
  1191. EXPECT_EQ("/* a a */",
  1192. format("/* a a */", getLLVMStyleWithColumns(15)));
  1193. EXPECT_EQ("/* a a bc */",
  1194. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1195. EXPECT_EQ("/* aaa aaa\n"
  1196. " * aaaaa */",
  1197. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1198. EXPECT_EQ("/* aaa aaa\n"
  1199. " * aaaaa */",
  1200. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1201. }
  1202. TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
  1203. EXPECT_EQ("#define X \\\n"
  1204. " /* \\\n"
  1205. " Test \\\n"
  1206. " Macro comment \\\n"
  1207. " with a long \\\n"
  1208. " line \\\n"
  1209. " */ \\\n"
  1210. " A + B",
  1211. format("#define X \\\n"
  1212. " /*\n"
  1213. " Test\n"
  1214. " Macro comment with a long line\n"
  1215. " */ \\\n"
  1216. " A + B",
  1217. getLLVMStyleWithColumns(20)));
  1218. EXPECT_EQ("#define X \\\n"
  1219. " /* Macro comment \\\n"
  1220. " with a long \\\n"
  1221. " line */ \\\n"
  1222. " A + B",
  1223. format("#define X \\\n"
  1224. " /* Macro comment with a long\n"
  1225. " line */ \\\n"
  1226. " A + B",
  1227. getLLVMStyleWithColumns(20)));
  1228. EXPECT_EQ("#define X \\\n"
  1229. " /* Macro comment \\\n"
  1230. " * with a long \\\n"
  1231. " * line */ \\\n"
  1232. " A + B",
  1233. format("#define X \\\n"
  1234. " /* Macro comment with a long line */ \\\n"
  1235. " A + B",
  1236. getLLVMStyleWithColumns(20)));
  1237. }
  1238. TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
  1239. verifyFormat("#ifdef A // line about A\n"
  1240. "// section comment\n"
  1241. "#endif",
  1242. getLLVMStyleWithColumns(80));
  1243. verifyFormat("#ifdef A // line 1 about A\n"
  1244. " // line 2 about A\n"
  1245. "// section comment\n"
  1246. "#endif",
  1247. getLLVMStyleWithColumns(80));
  1248. EXPECT_EQ("#ifdef A // line 1 about A\n"
  1249. " // line 2 about A\n"
  1250. "// section comment\n"
  1251. "#endif",
  1252. format("#ifdef A // line 1 about A\n"
  1253. " // line 2 about A\n"
  1254. "// section comment\n"
  1255. "#endif",
  1256. getLLVMStyleWithColumns(80)));
  1257. verifyFormat("int f() {\n"
  1258. " int i;\n"
  1259. "#ifdef A // comment about A\n"
  1260. " // section comment 1\n"
  1261. " // section comment 2\n"
  1262. " i = 2;\n"
  1263. "#else // comment about #else\n"
  1264. " // section comment 3\n"
  1265. " i = 4;\n"
  1266. "#endif\n"
  1267. "}", getLLVMStyleWithColumns(80));
  1268. }
  1269. TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
  1270. verifyFormat("#if A\n"
  1271. "#else // A\n"
  1272. "int iiii;\n"
  1273. "#endif // B",
  1274. getLLVMStyleWithColumns(20));
  1275. verifyFormat("#if A\n"
  1276. "#else // A\n"
  1277. "int iiii; // CC\n"
  1278. "#endif // B",
  1279. getLLVMStyleWithColumns(20));
  1280. EXPECT_EQ("#if A\n"
  1281. "#else // A1\n"
  1282. " // A2\n"
  1283. "int ii;\n"
  1284. "#endif // B",
  1285. format("#if A\n"
  1286. "#else // A1\n"
  1287. " // A2\n"
  1288. "int ii;\n"
  1289. "#endif // B",
  1290. getLLVMStyleWithColumns(20)));
  1291. }
  1292. TEST_F(FormatTestComments, CommentsInStaticInitializers) {
  1293. EXPECT_EQ(
  1294. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1295. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1296. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1297. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1298. " aaaaaaaaaaaaaaaaaaaa};",
  1299. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1300. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1301. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1302. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1303. " aaaaaaaaaaaaaaaaaaaa };"));
  1304. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1305. " bbbbbbbbbbb, ccccccccccc};");
  1306. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1307. " // comment for bb....\n"
  1308. " bbbbbbbbbbb, ccccccccccc};");
  1309. verifyGoogleFormat(
  1310. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1311. " bbbbbbbbbbb, ccccccccccc};");
  1312. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1313. " // comment for bb....\n"
  1314. " bbbbbbbbbbb, ccccccccccc};");
  1315. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1316. " {d, e, f}, // Group #2\n"
  1317. " {g, h, i}}; // Group #3");
  1318. verifyFormat("S s = {{// Group #1\n"
  1319. " a, b, c},\n"
  1320. " {// Group #2\n"
  1321. " d, e, f},\n"
  1322. " {// Group #3\n"
  1323. " g, h, i}};");
  1324. EXPECT_EQ("S s = {\n"
  1325. " // Some comment\n"
  1326. " a,\n"
  1327. "\n"
  1328. " // Comment after empty line\n"
  1329. " b}",
  1330. format("S s = {\n"
  1331. " // Some comment\n"
  1332. " a,\n"
  1333. " \n"
  1334. " // Comment after empty line\n"
  1335. " b\n"
  1336. "}"));
  1337. EXPECT_EQ("S s = {\n"
  1338. " /* Some comment */\n"
  1339. " a,\n"
  1340. "\n"
  1341. " /* Comment after empty line */\n"
  1342. " b}",
  1343. format("S s = {\n"
  1344. " /* Some comment */\n"
  1345. " a,\n"
  1346. " \n"
  1347. " /* Comment after empty line */\n"
  1348. " b\n"
  1349. "}"));
  1350. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1351. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1352. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1353. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1354. }
  1355. TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
  1356. EXPECT_EQ("if (true) { // comment about branch\n"
  1357. " // comment about f\n"
  1358. " f();\n"
  1359. "}",
  1360. format("if (true) { // comment about branch\n"
  1361. " // comment about f\n"
  1362. " f();\n"
  1363. "}",
  1364. getLLVMStyleWithColumns(80)));
  1365. EXPECT_EQ("if (1) { // if line 1\n"
  1366. " // if line 2\n"
  1367. " // if line 3\n"
  1368. " // f line 1\n"
  1369. " // f line 2\n"
  1370. " f();\n"
  1371. "} else { // else line 1\n"
  1372. " // else line 2\n"
  1373. " // else line 3\n"
  1374. " // g line 1\n"
  1375. " g();\n"
  1376. "}",
  1377. format("if (1) { // if line 1\n"
  1378. " // if line 2\n"
  1379. " // if line 3\n"
  1380. " // f line 1\n"
  1381. " // f line 2\n"
  1382. " f();\n"
  1383. "} else { // else line 1\n"
  1384. " // else line 2\n"
  1385. " // else line 3\n"
  1386. " // g line 1\n"
  1387. " g();\n"
  1388. "}"));
  1389. EXPECT_EQ("do { // line 1\n"
  1390. " // line 2\n"
  1391. " // line 3\n"
  1392. " f();\n"
  1393. "} while (true);",
  1394. format("do { // line 1\n"
  1395. " // line 2\n"
  1396. " // line 3\n"
  1397. " f();\n"
  1398. "} while (true);",
  1399. getLLVMStyleWithColumns(80)));
  1400. EXPECT_EQ("while (a < b) { // line 1\n"
  1401. " // line 2\n"
  1402. " // line 3\n"
  1403. " f();\n"
  1404. "}",
  1405. format("while (a < b) {// line 1\n"
  1406. " // line 2\n"
  1407. " // line 3\n"
  1408. " f();\n"
  1409. "}",
  1410. getLLVMStyleWithColumns(80)));
  1411. }
  1412. TEST_F(FormatTestComments, ReflowsComments) {
  1413. // Break a long line and reflow with the full next line.
  1414. EXPECT_EQ("// long long long\n"
  1415. "// long long",
  1416. format("// long long long long\n"
  1417. "// long",
  1418. getLLVMStyleWithColumns(20)));
  1419. // Keep the trailing newline while reflowing.
  1420. EXPECT_EQ("// long long long\n"
  1421. "// long long\n",
  1422. format("// long long long long\n"
  1423. "// long\n",
  1424. getLLVMStyleWithColumns(20)));
  1425. // Break a long line and reflow with a part of the next line.
  1426. EXPECT_EQ("// long long long\n"
  1427. "// long long\n"
  1428. "// long_long",
  1429. format("// long long long long\n"
  1430. "// long long_long",
  1431. getLLVMStyleWithColumns(20)));
  1432. // Break but do not reflow if the first word from the next line is too long.
  1433. EXPECT_EQ("// long long long\n"
  1434. "// long\n"
  1435. "// long_long_long\n",
  1436. format("// long long long long\n"
  1437. "// long_long_long\n",
  1438. getLLVMStyleWithColumns(20)));
  1439. // Don't break or reflow short lines.
  1440. verifyFormat("// long\n"
  1441. "// long long long lo\n"
  1442. "// long long long lo\n"
  1443. "// long",
  1444. getLLVMStyleWithColumns(20));
  1445. // Keep prefixes and decorations while reflowing.
  1446. EXPECT_EQ("/// long long long\n"
  1447. "/// long long\n",
  1448. format("/// long long long long\n"
  1449. "/// long\n",
  1450. getLLVMStyleWithColumns(20)));
  1451. EXPECT_EQ("//! long long long\n"
  1452. "//! long long\n",
  1453. format("//! long long long long\n"
  1454. "//! long\n",
  1455. getLLVMStyleWithColumns(20)));
  1456. EXPECT_EQ("/* long long long\n"
  1457. " * long long */",
  1458. format("/* long long long long\n"
  1459. " * long */",
  1460. getLLVMStyleWithColumns(20)));
  1461. EXPECT_EQ("///< long long long\n"
  1462. "///< long long\n",
  1463. format("///< long long long long\n"
  1464. "///< long\n",
  1465. getLLVMStyleWithColumns(20)));
  1466. EXPECT_EQ("//!< long long long\n"
  1467. "//!< long long\n",
  1468. format("//!< long long long long\n"
  1469. "//!< long\n",
  1470. getLLVMStyleWithColumns(20)));
  1471. // Don't bring leading whitespace up while reflowing.
  1472. EXPECT_EQ("/* long long long\n"
  1473. " * long long long\n"
  1474. " */",
  1475. format("/* long long long long\n"
  1476. " * long long\n"
  1477. " */",
  1478. getLLVMStyleWithColumns(20)));
  1479. // Reflow the last line of a block comment with its trailing '*/'.
  1480. EXPECT_EQ("/* long long long\n"
  1481. " long long */",
  1482. format("/* long long long long\n"
  1483. " long */",
  1484. getLLVMStyleWithColumns(20)));
  1485. // Reflow two short lines; keep the postfix of the last one.
  1486. EXPECT_EQ("/* long long long\n"
  1487. " * long long long */",
  1488. format("/* long long long long\n"
  1489. " * long\n"
  1490. " * long */",
  1491. getLLVMStyleWithColumns(20)));
  1492. // Put the postfix of the last short reflow line on a newline if it doesn't
  1493. // fit.
  1494. EXPECT_EQ("/* long long long\n"
  1495. " * long long longg\n"
  1496. " */",
  1497. format("/* long long long long\n"
  1498. " * long\n"
  1499. " * longg */",
  1500. getLLVMStyleWithColumns(20)));
  1501. // Reflow lines with leading whitespace.
  1502. EXPECT_EQ("{\n"
  1503. " /*\n"
  1504. " * long long long\n"
  1505. " * long long long\n"
  1506. " * long long long\n"
  1507. " */\n"
  1508. "}",
  1509. format("{\n"
  1510. "/*\n"
  1511. " * long long long long\n"
  1512. " * long\n"
  1513. " * long long long long\n"
  1514. " */\n"
  1515. "}",
  1516. getLLVMStyleWithColumns(20)));
  1517. // Break single line block comments that are first in the line with ' *'
  1518. // decoration.
  1519. EXPECT_EQ("/* long long long\n"
  1520. " * long */",
  1521. format("/* long long long long */", getLLVMStyleWithColumns(20)));
  1522. // Break single line block comment that are not first in the line with ' '
  1523. // decoration.
  1524. EXPECT_EQ("int i; /* long long\n"
  1525. " long */",
  1526. format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
  1527. // Reflow a line that goes just over the column limit.
  1528. EXPECT_EQ("// long long long\n"
  1529. "// lon long",
  1530. format("// long long long lon\n"
  1531. "// long",
  1532. getLLVMStyleWithColumns(20)));
  1533. // Stop reflowing if the next line has a different indentation than the
  1534. // previous line.
  1535. EXPECT_EQ("// long long long\n"
  1536. "// long\n"
  1537. "// long long\n"
  1538. "// long",
  1539. format("// long long long long\n"
  1540. "// long long\n"
  1541. "// long",
  1542. getLLVMStyleWithColumns(20)));
  1543. // Reflow into the last part of a really long line that has been broken into
  1544. // multiple lines.
  1545. EXPECT_EQ("// long long long\n"
  1546. "// long long long\n"
  1547. "// long long long\n",
  1548. format("// long long long long long long long long\n"
  1549. "// long\n",
  1550. getLLVMStyleWithColumns(20)));
  1551. // Break the first line, then reflow the beginning of the second and third
  1552. // line up.
  1553. EXPECT_EQ("// long long long\n"
  1554. "// lon1 lon2 lon2\n"
  1555. "// lon2 lon3 lon3",
  1556. format("// long long long lon1\n"
  1557. "// lon2 lon2 lon2\n"
  1558. "// lon3 lon3",
  1559. getLLVMStyleWithColumns(20)));
  1560. // Reflow the beginning of the second line, then break the rest.
  1561. EXPECT_EQ("// long long long\n"
  1562. "// lon1 lon2 lon2\n"
  1563. "// lon2 lon2 lon2\n"
  1564. "// lon3",
  1565. format("// long long long lon1\n"
  1566. "// lon2 lon2 lon2 lon2 lon2 lon3",
  1567. getLLVMStyleWithColumns(20)));
  1568. // Shrink the first line, then reflow the second line up.
  1569. EXPECT_EQ("// long long long", format("// long long\n"
  1570. "// long",
  1571. getLLVMStyleWithColumns(20)));
  1572. // Don't shrink leading whitespace.
  1573. EXPECT_EQ("int i; /// a",
  1574. format("int i; /// a", getLLVMStyleWithColumns(20)));
  1575. // Shrink trailing whitespace if there is no postfix and reflow.
  1576. EXPECT_EQ("// long long long\n"
  1577. "// long long",
  1578. format("// long long long long \n"
  1579. "// long",
  1580. getLLVMStyleWithColumns(20)));
  1581. // Shrink trailing whitespace to a single one if there is postfix.
  1582. EXPECT_EQ("/* long long long */",
  1583. format("/* long long long */", getLLVMStyleWithColumns(20)));
  1584. // Break a block comment postfix if exceeding the line limit.
  1585. EXPECT_EQ("/* long\n"
  1586. " */",
  1587. format("/* long */", getLLVMStyleWithColumns(20)));
  1588. // Reflow indented comments.
  1589. EXPECT_EQ("{\n"
  1590. " // long long long\n"
  1591. " // long long\n"
  1592. " int i; /* long lon\n"
  1593. " g long\n"
  1594. " */\n"
  1595. "}",
  1596. format("{\n"
  1597. " // long long long long\n"
  1598. " // long\n"
  1599. " int i; /* long lon g\n"
  1600. " long */\n"
  1601. "}",
  1602. getLLVMStyleWithColumns(20)));
  1603. // Don't realign trailing comments after reflow has happened.
  1604. EXPECT_EQ("// long long long\n"
  1605. "// long long\n"
  1606. "long i; // long",
  1607. format("// long long long long\n"
  1608. "// long\n"
  1609. "long i; // long",
  1610. getLLVMStyleWithColumns(20)));
  1611. EXPECT_EQ("// long long long\n"
  1612. "// longng long long\n"
  1613. "// long lo",
  1614. format("// long long long longng\n"
  1615. "// long long long\n"
  1616. "// lo",
  1617. getLLVMStyleWithColumns(20)));
  1618. // Reflow lines after a broken line.
  1619. EXPECT_EQ("int a; // Trailing\n"
  1620. " // comment on\n"
  1621. " // 2 or 3\n"
  1622. " // lines.\n",
  1623. format("int a; // Trailing comment\n"
  1624. " // on 2\n"
  1625. " // or 3\n"
  1626. " // lines.\n",
  1627. getLLVMStyleWithColumns(20)));
  1628. EXPECT_EQ("/// This long line\n"
  1629. "/// gets reflown.\n",
  1630. format("/// This long line gets\n"
  1631. "/// reflown.\n",
  1632. getLLVMStyleWithColumns(20)));
  1633. EXPECT_EQ("//! This long line\n"
  1634. "//! gets reflown.\n",
  1635. format(" //! This long line gets\n"
  1636. " //! reflown.\n",
  1637. getLLVMStyleWithColumns(20)));
  1638. EXPECT_EQ("/* This long line\n"
  1639. " * gets reflown.\n"
  1640. " */\n",
  1641. format("/* This long line gets\n"
  1642. " * reflown.\n"
  1643. " */\n",
  1644. getLLVMStyleWithColumns(20)));
  1645. // Reflow after indentation makes a line too long.
  1646. EXPECT_EQ("{\n"
  1647. " // long long long\n"
  1648. " // lo long\n"
  1649. "}\n",
  1650. format("{\n"
  1651. "// long long long lo\n"
  1652. "// long\n"
  1653. "}\n",
  1654. getLLVMStyleWithColumns(20)));
  1655. // Break and reflow multiple lines.
  1656. EXPECT_EQ("/*\n"
  1657. " * Reflow the end of\n"
  1658. " * line by 11 22 33\n"
  1659. " * 4.\n"
  1660. " */\n",
  1661. format("/*\n"
  1662. " * Reflow the end of line\n"
  1663. " * by\n"
  1664. " * 11\n"
  1665. " * 22\n"
  1666. " * 33\n"
  1667. " * 4.\n"
  1668. " */\n",
  1669. getLLVMStyleWithColumns(20)));
  1670. EXPECT_EQ("/// First line gets\n"
  1671. "/// broken. Second\n"
  1672. "/// line gets\n"
  1673. "/// reflown and\n"
  1674. "/// broken. Third\n"
  1675. "/// gets reflown.\n",
  1676. format("/// First line gets broken.\n"
  1677. "/// Second line gets reflown and broken.\n"
  1678. "/// Third gets reflown.\n",
  1679. getLLVMStyleWithColumns(20)));
  1680. EXPECT_EQ("int i; // first long\n"
  1681. " // long snd\n"
  1682. " // long.\n",
  1683. format("int i; // first long long\n"
  1684. " // snd long.\n",
  1685. getLLVMStyleWithColumns(20)));
  1686. EXPECT_EQ("{\n"
  1687. " // first long line\n"
  1688. " // line second\n"
  1689. " // long line line\n"
  1690. " // third long line\n"
  1691. " // line\n"
  1692. "}\n",
  1693. format("{\n"
  1694. " // first long line line\n"
  1695. " // second long line line\n"
  1696. " // third long line line\n"
  1697. "}\n",
  1698. getLLVMStyleWithColumns(20)));
  1699. EXPECT_EQ("int i; /* first line\n"
  1700. " * second\n"
  1701. " * line third\n"
  1702. " * line\n"
  1703. " */",
  1704. format("int i; /* first line\n"
  1705. " * second line\n"
  1706. " * third line\n"
  1707. " */",
  1708. getLLVMStyleWithColumns(20)));
  1709. // Reflow the last two lines of a section that starts with a line having
  1710. // different indentation.
  1711. EXPECT_EQ(
  1712. "// long\n"
  1713. "// long long long\n"
  1714. "// long long",
  1715. format("// long\n"
  1716. "// long long long long\n"
  1717. "// long",
  1718. getLLVMStyleWithColumns(20)));
  1719. // Keep the block comment endling '*/' while reflowing.
  1720. EXPECT_EQ("/* Long long long\n"
  1721. " * line short */\n",
  1722. format("/* Long long long line\n"
  1723. " * short */\n",
  1724. getLLVMStyleWithColumns(20)));
  1725. // Don't reflow between separate blocks of comments.
  1726. EXPECT_EQ("/* First comment\n"
  1727. " * block will */\n"
  1728. "/* Snd\n"
  1729. " */\n",
  1730. format("/* First comment block\n"
  1731. " * will */\n"
  1732. "/* Snd\n"
  1733. " */\n",
  1734. getLLVMStyleWithColumns(20)));
  1735. // Don't reflow across blank comment lines.
  1736. EXPECT_EQ("int i; // This long\n"
  1737. " // line gets\n"
  1738. " // broken.\n"
  1739. " //\n"
  1740. " // keep.\n",
  1741. format("int i; // This long line gets broken.\n"
  1742. " // \n"
  1743. " // keep.\n",
  1744. getLLVMStyleWithColumns(20)));
  1745. EXPECT_EQ("{\n"
  1746. " /// long long long\n"
  1747. " /// long long\n"
  1748. " ///\n"
  1749. " /// long\n"
  1750. "}",
  1751. format("{\n"
  1752. " /// long long long long\n"
  1753. " /// long\n"
  1754. " ///\n"
  1755. " /// long\n"
  1756. "}",
  1757. getLLVMStyleWithColumns(20)));
  1758. EXPECT_EQ("//! long long long\n"
  1759. "//! long\n"
  1760. "\n"
  1761. "//! long",
  1762. format("//! long long long long\n"
  1763. "\n"
  1764. "//! long",
  1765. getLLVMStyleWithColumns(20)));
  1766. EXPECT_EQ("/* long long long\n"
  1767. " long\n"
  1768. "\n"
  1769. " long */",
  1770. format("/* long long long long\n"
  1771. "\n"
  1772. " long */",
  1773. getLLVMStyleWithColumns(20)));
  1774. EXPECT_EQ("/* long long long\n"
  1775. " * long\n"
  1776. " *\n"
  1777. " * long */",
  1778. format("/* long long long long\n"
  1779. " *\n"
  1780. " * long */",
  1781. getLLVMStyleWithColumns(20)));
  1782. // Don't reflow lines having content that is a single character.
  1783. EXPECT_EQ("// long long long\n"
  1784. "// long\n"
  1785. "// l",
  1786. format("// long long long long\n"
  1787. "// l",
  1788. getLLVMStyleWithColumns(20)));
  1789. // Don't reflow lines starting with two punctuation characters.
  1790. EXPECT_EQ("// long long long\n"
  1791. "// long\n"
  1792. "// ... --- ...",
  1793. format(
  1794. "// long long long long\n"
  1795. "// ... --- ...",
  1796. getLLVMStyleWithColumns(20)));
  1797. // Don't reflow lines starting with '@'.
  1798. EXPECT_EQ("// long long long\n"
  1799. "// long\n"
  1800. "// @param arg",
  1801. format("// long long long long\n"
  1802. "// @param arg",
  1803. getLLVMStyleWithColumns(20)));
  1804. // Don't reflow lines starting with 'TODO'.
  1805. EXPECT_EQ("// long long long\n"
  1806. "// long\n"
  1807. "// TODO: long",
  1808. format("// long long long long\n"
  1809. "// TODO: long",
  1810. getLLVMStyleWithColumns(20)));
  1811. // Don't reflow lines starting with 'FIXME'.
  1812. EXPECT_EQ("// long long long\n"
  1813. "// long\n"
  1814. "// FIXME: long",
  1815. format("// long long long long\n"
  1816. "// FIXME: long",
  1817. getLLVMStyleWithColumns(20)));
  1818. // Don't reflow lines starting with 'XXX'.
  1819. EXPECT_EQ("// long long long\n"
  1820. "// long\n"
  1821. "// XXX: long",
  1822. format("// long long long long\n"
  1823. "// XXX: long",
  1824. getLLVMStyleWithColumns(20)));
  1825. // Don't reflow comment pragmas.
  1826. EXPECT_EQ("// long long long\n"
  1827. "// long\n"
  1828. "// IWYU pragma:",
  1829. format("// long long long long\n"
  1830. "// IWYU pragma:",
  1831. getLLVMStyleWithColumns(20)));
  1832. EXPECT_EQ("/* long long long\n"
  1833. " * long\n"
  1834. " * IWYU pragma:\n"
  1835. " */",
  1836. format("/* long long long long\n"
  1837. " * IWYU pragma:\n"
  1838. " */",
  1839. getLLVMStyleWithColumns(20)));
  1840. // Reflow lines that have a non-punctuation character among their first 2
  1841. // characters.
  1842. EXPECT_EQ("// long long long\n"
  1843. "// long 'long'",
  1844. format(
  1845. "// long long long long\n"
  1846. "// 'long'",
  1847. getLLVMStyleWithColumns(20)));
  1848. // Don't reflow between separate blocks of comments.
  1849. EXPECT_EQ("/* First comment\n"
  1850. " * block will */\n"
  1851. "/* Snd\n"
  1852. " */\n",
  1853. format("/* First comment block\n"
  1854. " * will */\n"
  1855. "/* Snd\n"
  1856. " */\n",
  1857. getLLVMStyleWithColumns(20)));
  1858. // Don't reflow lines having different indentation.
  1859. EXPECT_EQ("// long long long\n"
  1860. "// long\n"
  1861. "// long",
  1862. format("// long long long long\n"
  1863. "// long",
  1864. getLLVMStyleWithColumns(20)));
  1865. // Don't reflow separate bullets in list
  1866. EXPECT_EQ("// - long long long\n"
  1867. "// long\n"
  1868. "// - long",
  1869. format("// - long long long long\n"
  1870. "// - long",
  1871. getLLVMStyleWithColumns(20)));
  1872. EXPECT_EQ("// * long long long\n"
  1873. "// long\n"
  1874. "// * long",
  1875. format("// * long long long long\n"
  1876. "// * long",
  1877. getLLVMStyleWithColumns(20)));
  1878. EXPECT_EQ("// + long long long\n"
  1879. "// long\n"
  1880. "// + long",
  1881. format("// + long long long long\n"
  1882. "// + long",
  1883. getLLVMStyleWithColumns(20)));
  1884. EXPECT_EQ("// 1. long long long\n"
  1885. "// long\n"
  1886. "// 2. long",
  1887. format("// 1. long long long long\n"
  1888. "// 2. long",
  1889. getLLVMStyleWithColumns(20)));
  1890. EXPECT_EQ("// -# long long long\n"
  1891. "// long\n"
  1892. "// -# long",
  1893. format("// -# long long long long\n"
  1894. "// -# long",
  1895. getLLVMStyleWithColumns(20)));
  1896. EXPECT_EQ("// - long long long\n"
  1897. "// long long long\n"
  1898. "// - long",
  1899. format("// - long long long long\n"
  1900. "// long long\n"
  1901. "// - long",
  1902. getLLVMStyleWithColumns(20)));
  1903. EXPECT_EQ("// - long long long\n"
  1904. "// long long long\n"
  1905. "// long\n"
  1906. "// - long",
  1907. format("// - long long long long\n"
  1908. "// long long long\n"
  1909. "// - long",
  1910. getLLVMStyleWithColumns(20)));
  1911. // Large number (>2 digits) are not list items
  1912. EXPECT_EQ("// long long long\n"
  1913. "// long 1024. long.",
  1914. format("// long long long long\n"
  1915. "// 1024. long.",
  1916. getLLVMStyleWithColumns(20)));
  1917. // Do not break before number, to avoid introducing a non-reflowable doxygen
  1918. // list item.
  1919. EXPECT_EQ("// long long\n"
  1920. "// long 10. long.",
  1921. format("// long long long 10.\n"
  1922. "// long.",
  1923. getLLVMStyleWithColumns(20)));
  1924. // Don't break or reflow after implicit string literals.
  1925. verifyFormat("#include <t> // l l l\n"
  1926. " // l",
  1927. getLLVMStyleWithColumns(20));
  1928. // Don't break or reflow comments on import lines.
  1929. EXPECT_EQ("#include \"t\" /* l l l\n"
  1930. " * l */",
  1931. format("#include \"t\" /* l l l\n"
  1932. " * l */",
  1933. getLLVMStyleWithColumns(20)));
  1934. // Don't reflow between different trailing comment sections.
  1935. EXPECT_EQ("int i; // long long\n"
  1936. " // long\n"
  1937. "int j; // long long\n"
  1938. " // long\n",
  1939. format("int i; // long long long\n"
  1940. "int j; // long long long\n",
  1941. getLLVMStyleWithColumns(20)));
  1942. // Don't reflow if the first word on the next line is longer than the
  1943. // available space at current line.
  1944. EXPECT_EQ("int i; // trigger\n"
  1945. " // reflow\n"
  1946. " // longsec\n",
  1947. format("int i; // trigger reflow\n"
  1948. " // longsec\n",
  1949. getLLVMStyleWithColumns(20)));
  1950. // Simple case that correctly handles reflow in parameter lists.
  1951. EXPECT_EQ("a = f(/* looooooooong\n"
  1952. " * long long\n"
  1953. " */\n"
  1954. " a);",
  1955. format("a = f(/* looooooooong long\n* long\n*/ a);",
  1956. getLLVMStyleWithColumns(22)));
  1957. // Tricky case that has fewer lines if we reflow the comment, ending up with
  1958. // fewer lines.
  1959. EXPECT_EQ("a = f(/* loooooong\n"
  1960. " * long long\n"
  1961. " */\n"
  1962. " a);",
  1963. format("a = f(/* loooooong long\n* long\n*/ a);",
  1964. getLLVMStyleWithColumns(22)));
  1965. // Keep empty comment lines.
  1966. EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
  1967. EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
  1968. EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
  1969. EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
  1970. EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
  1971. }
  1972. TEST_F(FormatTestComments, ReflowsCommentsPrecise) {
  1973. // FIXME: This assumes we do not continue compressing whitespace once we are
  1974. // in reflow mode. Consider compressing whitespace.
  1975. // Test that we stop reflowing precisely at the column limit.
  1976. // After reflowing, "// reflows into foo" does not fit the column limit,
  1977. // so we compress the whitespace.
  1978. EXPECT_EQ("// some text that\n"
  1979. "// reflows into foo\n",
  1980. format("// some text that reflows\n"
  1981. "// into foo\n",
  1982. getLLVMStyleWithColumns(20)));
  1983. // Given one more column, "// reflows into foo" does fit the limit, so we
  1984. // do not compress the whitespace.
  1985. EXPECT_EQ("// some text that\n"
  1986. "// reflows into foo\n",
  1987. format("// some text that reflows\n"
  1988. "// into foo\n",
  1989. getLLVMStyleWithColumns(21)));
  1990. // Make sure that we correctly account for the space added in the reflow case
  1991. // when making the reflowing decision.
  1992. // First, when the next line ends precisely one column over the limit, do not
  1993. // reflow.
  1994. EXPECT_EQ("// some text that\n"
  1995. "// reflows\n"
  1996. "// into1234567\n",
  1997. format("// some text that reflows\n"
  1998. "// into1234567\n",
  1999. getLLVMStyleWithColumns(21)));
  2000. // Secondly, when the next line ends later, but the first word in that line
  2001. // is precisely one column over the limit, do not reflow.
  2002. EXPECT_EQ("// some text that\n"
  2003. "// reflows\n"
  2004. "// into1234567 f\n",
  2005. format("// some text that reflows\n"
  2006. "// into1234567 f\n",
  2007. getLLVMStyleWithColumns(21)));
  2008. }
  2009. TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) {
  2010. // Baseline.
  2011. EXPECT_EQ("// some text\n"
  2012. "// that re flows\n",
  2013. format("// some text that\n"
  2014. "// re flows\n",
  2015. getLLVMStyleWithColumns(16)));
  2016. EXPECT_EQ("// some text\n"
  2017. "// that re flows\n",
  2018. format("// some text that\n"
  2019. "// re flows\n",
  2020. getLLVMStyleWithColumns(16)));
  2021. EXPECT_EQ("/* some text\n"
  2022. " * that re flows\n"
  2023. " */\n",
  2024. format("/* some text that\n"
  2025. "* re flows\n"
  2026. "*/\n",
  2027. getLLVMStyleWithColumns(16)));
  2028. // FIXME: We do not reflow if the indent of two subsequent lines differs;
  2029. // given that this is different behavior from block comments, do we want
  2030. // to keep this?
  2031. EXPECT_EQ("// some text\n"
  2032. "// that\n"
  2033. "// re flows\n",
  2034. format("// some text that\n"
  2035. "// re flows\n",
  2036. getLLVMStyleWithColumns(16)));
  2037. // Space within parts of a line that fit.
  2038. // FIXME: Use the earliest possible split while reflowing to compress the
  2039. // whitespace within the line.
  2040. EXPECT_EQ("// some text that\n"
  2041. "// does re flow\n"
  2042. "// more here\n",
  2043. format("// some text that does\n"
  2044. "// re flow more here\n",
  2045. getLLVMStyleWithColumns(21)));
  2046. }
  2047. TEST_F(FormatTestComments, IgnoresIf0Contents) {
  2048. EXPECT_EQ("#if 0\n"
  2049. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  2050. "#endif\n"
  2051. "void f() {}",
  2052. format("#if 0\n"
  2053. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  2054. "#endif\n"
  2055. "void f( ) { }"));
  2056. EXPECT_EQ("#if false\n"
  2057. "void f( ) { }\n"
  2058. "#endif\n"
  2059. "void g() {}\n",
  2060. format("#if false\n"
  2061. "void f( ) { }\n"
  2062. "#endif\n"
  2063. "void g( ) { }\n"));
  2064. EXPECT_EQ("enum E {\n"
  2065. " One,\n"
  2066. " Two,\n"
  2067. "#if 0\n"
  2068. "Three,\n"
  2069. " Four,\n"
  2070. "#endif\n"
  2071. " Five\n"
  2072. "};",
  2073. format("enum E {\n"
  2074. " One,Two,\n"
  2075. "#if 0\n"
  2076. "Three,\n"
  2077. " Four,\n"
  2078. "#endif\n"
  2079. " Five};"));
  2080. EXPECT_EQ("enum F {\n"
  2081. " One,\n"
  2082. "#if 1\n"
  2083. " Two,\n"
  2084. "#if 0\n"
  2085. "Three,\n"
  2086. " Four,\n"
  2087. "#endif\n"
  2088. " Five\n"
  2089. "#endif\n"
  2090. "};",
  2091. format("enum F {\n"
  2092. "One,\n"
  2093. "#if 1\n"
  2094. "Two,\n"
  2095. "#if 0\n"
  2096. "Three,\n"
  2097. " Four,\n"
  2098. "#endif\n"
  2099. "Five\n"
  2100. "#endif\n"
  2101. "};"));
  2102. EXPECT_EQ("enum G {\n"
  2103. " One,\n"
  2104. "#if 0\n"
  2105. "Two,\n"
  2106. "#else\n"
  2107. " Three,\n"
  2108. "#endif\n"
  2109. " Four\n"
  2110. "};",
  2111. format("enum G {\n"
  2112. "One,\n"
  2113. "#if 0\n"
  2114. "Two,\n"
  2115. "#else\n"
  2116. "Three,\n"
  2117. "#endif\n"
  2118. "Four\n"
  2119. "};"));
  2120. EXPECT_EQ("enum H {\n"
  2121. " One,\n"
  2122. "#if 0\n"
  2123. "#ifdef Q\n"
  2124. "Two,\n"
  2125. "#else\n"
  2126. "Three,\n"
  2127. "#endif\n"
  2128. "#endif\n"
  2129. " Four\n"
  2130. "};",
  2131. format("enum H {\n"
  2132. "One,\n"
  2133. "#if 0\n"
  2134. "#ifdef Q\n"
  2135. "Two,\n"
  2136. "#else\n"
  2137. "Three,\n"
  2138. "#endif\n"
  2139. "#endif\n"
  2140. "Four\n"
  2141. "};"));
  2142. EXPECT_EQ("enum I {\n"
  2143. " One,\n"
  2144. "#if /* test */ 0 || 1\n"
  2145. "Two,\n"
  2146. "Three,\n"
  2147. "#endif\n"
  2148. " Four\n"
  2149. "};",
  2150. format("enum I {\n"
  2151. "One,\n"
  2152. "#if /* test */ 0 || 1\n"
  2153. "Two,\n"
  2154. "Three,\n"
  2155. "#endif\n"
  2156. "Four\n"
  2157. "};"));
  2158. EXPECT_EQ("enum J {\n"
  2159. " One,\n"
  2160. "#if 0\n"
  2161. "#if 0\n"
  2162. "Two,\n"
  2163. "#else\n"
  2164. "Three,\n"
  2165. "#endif\n"
  2166. "Four,\n"
  2167. "#endif\n"
  2168. " Five\n"
  2169. "};",
  2170. format("enum J {\n"
  2171. "One,\n"
  2172. "#if 0\n"
  2173. "#if 0\n"
  2174. "Two,\n"
  2175. "#else\n"
  2176. "Three,\n"
  2177. "#endif\n"
  2178. "Four,\n"
  2179. "#endif\n"
  2180. "Five\n"
  2181. "};"));
  2182. // Ignore stuff in SWIG-blocks.
  2183. EXPECT_EQ("#ifdef SWIG\n"
  2184. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  2185. "#endif\n"
  2186. "void f() {}",
  2187. format("#ifdef SWIG\n"
  2188. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  2189. "#endif\n"
  2190. "void f( ) { }"));
  2191. EXPECT_EQ("#ifndef SWIG\n"
  2192. "void f() {}\n"
  2193. "#endif",
  2194. format("#ifndef SWIG\n"
  2195. "void f( ) { }\n"
  2196. "#endif"));
  2197. }
  2198. TEST_F(FormatTestComments, DontCrashOnBlockComments) {
  2199. EXPECT_EQ(
  2200. "int xxxxxxxxx; /* "
  2201. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2202. "zzzzzz\n"
  2203. "0*/",
  2204. format("int xxxxxxxxx; /* "
  2205. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2206. "0*/"));
  2207. }
  2208. TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
  2209. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  2210. " f();\n"
  2211. "}");
  2212. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  2213. " f();\n"
  2214. "} /* another comment */ else /* comment #3 */ {\n"
  2215. " g();\n"
  2216. "}");
  2217. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  2218. " f();\n"
  2219. "}");
  2220. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  2221. " f();\n"
  2222. "}");
  2223. verifyFormat("do /* a comment in a strange place */ {\n"
  2224. " f();\n"
  2225. "} /* another comment */ while (0);");
  2226. }
  2227. TEST_F(FormatTestComments, BlockComments) {
  2228. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  2229. format("/* *//* */ /* */\n/* *//* */ /* */"));
  2230. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  2231. EXPECT_EQ("#define A /*123*/ \\\n"
  2232. " b\n"
  2233. "/* */\n"
  2234. "someCall(\n"
  2235. " parameter);",
  2236. format("#define A /*123*/ b\n"
  2237. "/* */\n"
  2238. "someCall(parameter);",
  2239. getLLVMStyleWithColumns(15)));
  2240. EXPECT_EQ("#define A\n"
  2241. "/* */ someCall(\n"
  2242. " parameter);",
  2243. format("#define A\n"
  2244. "/* */someCall(parameter);",
  2245. getLLVMStyleWithColumns(15)));
  2246. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  2247. EXPECT_EQ("/*\n"
  2248. " *\n"
  2249. " * aaaaaa\n"
  2250. " * aaaaaa\n"
  2251. " */",
  2252. format("/*\n"
  2253. "*\n"
  2254. " * aaaaaa aaaaaa\n"
  2255. "*/",
  2256. getLLVMStyleWithColumns(10)));
  2257. EXPECT_EQ("/*\n"
  2258. "**\n"
  2259. "* aaaaaa\n"
  2260. "*aaaaaa\n"
  2261. "*/",
  2262. format("/*\n"
  2263. "**\n"
  2264. "* aaaaaa aaaaaa\n"
  2265. "*/",
  2266. getLLVMStyleWithColumns(10)));
  2267. EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2268. " /* line 1\n"
  2269. " bbbbbbbbbbbb */\n"
  2270. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  2271. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2272. " /* line 1\n"
  2273. " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  2274. getLLVMStyleWithColumns(50)));
  2275. FormatStyle NoBinPacking = getLLVMStyle();
  2276. NoBinPacking.BinPackParameters = false;
  2277. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  2278. " 2, /* comment 2 */\n"
  2279. " 3, /* comment 3 */\n"
  2280. " aaaa,\n"
  2281. " bbbb);",
  2282. format("someFunction (1, /* comment 1 */\n"
  2283. " 2, /* comment 2 */ \n"
  2284. " 3, /* comment 3 */\n"
  2285. "aaaa, bbbb );",
  2286. NoBinPacking));
  2287. verifyFormat(
  2288. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2289. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2290. EXPECT_EQ(
  2291. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  2292. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2293. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  2294. format(
  2295. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  2296. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2297. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  2298. EXPECT_EQ(
  2299. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  2300. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  2301. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  2302. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  2303. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  2304. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  2305. verifyFormat("void f(int * /* unused */) {}");
  2306. EXPECT_EQ("/*\n"
  2307. " **\n"
  2308. " */",
  2309. format("/*\n"
  2310. " **\n"
  2311. " */"));
  2312. EXPECT_EQ("/*\n"
  2313. " *q\n"
  2314. " */",
  2315. format("/*\n"
  2316. " *q\n"
  2317. " */"));
  2318. EXPECT_EQ("/*\n"
  2319. " * q\n"
  2320. " */",
  2321. format("/*\n"
  2322. " * q\n"
  2323. " */"));
  2324. EXPECT_EQ("/*\n"
  2325. " **/",
  2326. format("/*\n"
  2327. " **/"));
  2328. EXPECT_EQ("/*\n"
  2329. " ***/",
  2330. format("/*\n"
  2331. " ***/"));
  2332. }
  2333. TEST_F(FormatTestComments, BlockCommentsInMacros) {
  2334. EXPECT_EQ("#define A \\\n"
  2335. " { \\\n"
  2336. " /* one line */ \\\n"
  2337. " someCall();",
  2338. format("#define A { \\\n"
  2339. " /* one line */ \\\n"
  2340. " someCall();",
  2341. getLLVMStyleWithColumns(20)));
  2342. EXPECT_EQ("#define A \\\n"
  2343. " { \\\n"
  2344. " /* previous */ \\\n"
  2345. " /* one line */ \\\n"
  2346. " someCall();",
  2347. format("#define A { \\\n"
  2348. " /* previous */ \\\n"
  2349. " /* one line */ \\\n"
  2350. " someCall();",
  2351. getLLVMStyleWithColumns(20)));
  2352. }
  2353. TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
  2354. EXPECT_EQ("a = {\n"
  2355. " 1111 /* */\n"
  2356. "};",
  2357. format("a = {1111 /* */\n"
  2358. "};",
  2359. getLLVMStyleWithColumns(15)));
  2360. EXPECT_EQ("a = {\n"
  2361. " 1111 /* */\n"
  2362. "};",
  2363. format("a = {1111 /* */\n"
  2364. "};",
  2365. getLLVMStyleWithColumns(15)));
  2366. EXPECT_EQ("a = {\n"
  2367. " 1111 /* a\n"
  2368. " */\n"
  2369. "};",
  2370. format("a = {1111 /* a */\n"
  2371. "};",
  2372. getLLVMStyleWithColumns(15)));
  2373. }
  2374. TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
  2375. EXPECT_EQ("a = f(/* long\n"
  2376. " long */\n"
  2377. " a);",
  2378. format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
  2379. EXPECT_EQ("a = f(\n"
  2380. " /* long\n"
  2381. " long */\n"
  2382. " a);",
  2383. format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
  2384. EXPECT_EQ("a = f(/* long\n"
  2385. " long\n"
  2386. " */\n"
  2387. " a);",
  2388. format("a = f(/* long\n"
  2389. " long\n"
  2390. " */a);",
  2391. getLLVMStyleWithColumns(16)));
  2392. EXPECT_EQ("a = f(/* long\n"
  2393. " long\n"
  2394. " */\n"
  2395. " a);",
  2396. format("a = f(/* long\n"
  2397. " long\n"
  2398. " */ a);",
  2399. getLLVMStyleWithColumns(16)));
  2400. EXPECT_EQ("a = f(/* long\n"
  2401. " long\n"
  2402. " */\n"
  2403. " (1 + 1));",
  2404. format("a = f(/* long\n"
  2405. " long\n"
  2406. " */ (1 + 1));",
  2407. getLLVMStyleWithColumns(16)));
  2408. EXPECT_EQ(
  2409. "a = f(a,\n"
  2410. " /* long\n"
  2411. " long */\n"
  2412. " b);",
  2413. format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
  2414. EXPECT_EQ(
  2415. "a = f(\n"
  2416. " a,\n"
  2417. " /* long\n"
  2418. " long */\n"
  2419. " b);",
  2420. format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
  2421. EXPECT_EQ("a = f(a,\n"
  2422. " /* long\n"
  2423. " long */\n"
  2424. " (1 + 1));",
  2425. format("a = f(a, /* long long */ (1 + 1));",
  2426. getLLVMStyleWithColumns(16)));
  2427. EXPECT_EQ("a = f(\n"
  2428. " a,\n"
  2429. " /* long\n"
  2430. " long */\n"
  2431. " (1 + 1));",
  2432. format("a = f(a, /* long long */ (1 + 1));",
  2433. getLLVMStyleWithColumns(15)));
  2434. }
  2435. TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  2436. verifyFormat("{\n"
  2437. " // a\n"
  2438. " // b");
  2439. }
  2440. TEST_F(FormatTestComments, AlignTrailingComments) {
  2441. EXPECT_EQ("#define MACRO(V) \\\n"
  2442. " V(Rt2) /* one more char */ \\\n"
  2443. " V(Rs) /* than here */ \\\n"
  2444. "/* comment 3 */\n",
  2445. format("#define MACRO(V)\\\n"
  2446. "V(Rt2) /* one more char */ \\\n"
  2447. "V(Rs) /* than here */ \\\n"
  2448. "/* comment 3 */\n",
  2449. getLLVMStyleWithColumns(40)));
  2450. EXPECT_EQ("int i = f(abc, // line 1\n"
  2451. " d, // line 2\n"
  2452. " // line 3\n"
  2453. " b);",
  2454. format("int i = f(abc, // line 1\n"
  2455. " d, // line 2\n"
  2456. " // line 3\n"
  2457. " b);",
  2458. getLLVMStyleWithColumns(40)));
  2459. // Align newly broken trailing comments.
  2460. EXPECT_EQ("int ab; // line\n"
  2461. "int a; // long\n"
  2462. " // long\n",
  2463. format("int ab; // line\n"
  2464. "int a; // long long\n",
  2465. getLLVMStyleWithColumns(15)));
  2466. EXPECT_EQ("int ab; // line\n"
  2467. "int a; // long\n"
  2468. " // long\n"
  2469. " // long",
  2470. format("int ab; // line\n"
  2471. "int a; // long long\n"
  2472. " // long",
  2473. getLLVMStyleWithColumns(15)));
  2474. EXPECT_EQ("int ab; // line\n"
  2475. "int a; // long\n"
  2476. " // long\n"
  2477. "pt c; // long",
  2478. format("int ab; // line\n"
  2479. "int a; // long long\n"
  2480. "pt c; // long",
  2481. getLLVMStyleWithColumns(15)));
  2482. EXPECT_EQ("int ab; // line\n"
  2483. "int a; // long\n"
  2484. " // long\n"
  2485. "\n"
  2486. "// long",
  2487. format("int ab; // line\n"
  2488. "int a; // long long\n"
  2489. "\n"
  2490. "// long",
  2491. getLLVMStyleWithColumns(15)));
  2492. // Don't align newly broken trailing comments if that would put them over the
  2493. // column limit.
  2494. EXPECT_EQ("int i, j; // line 1\n"
  2495. "int k; // line longg\n"
  2496. " // long",
  2497. format("int i, j; // line 1\n"
  2498. "int k; // line longg long",
  2499. getLLVMStyleWithColumns(20)));
  2500. // Always align if ColumnLimit = 0
  2501. EXPECT_EQ("int i, j; // line 1\n"
  2502. "int k; // line longg long",
  2503. format("int i, j; // line 1\n"
  2504. "int k; // line longg long",
  2505. getLLVMStyleWithColumns(0)));
  2506. // Align comment line sections aligned with the next token with the next
  2507. // token.
  2508. EXPECT_EQ("class A {\n"
  2509. "public: // public comment\n"
  2510. " // comment about a\n"
  2511. " int a;\n"
  2512. "};",
  2513. format("class A {\n"
  2514. "public: // public comment\n"
  2515. " // comment about a\n"
  2516. " int a;\n"
  2517. "};",
  2518. getLLVMStyleWithColumns(40)));
  2519. EXPECT_EQ("class A {\n"
  2520. "public: // public comment 1\n"
  2521. " // public comment 2\n"
  2522. " // comment 1 about a\n"
  2523. " // comment 2 about a\n"
  2524. " int a;\n"
  2525. "};",
  2526. format("class A {\n"
  2527. "public: // public comment 1\n"
  2528. " // public comment 2\n"
  2529. " // comment 1 about a\n"
  2530. " // comment 2 about a\n"
  2531. " int a;\n"
  2532. "};",
  2533. getLLVMStyleWithColumns(40)));
  2534. EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
  2535. " // comment line 2 on f\n"
  2536. " // comment line 1 before return\n"
  2537. " // comment line 2 before return\n"
  2538. " return n; // comment line 1 on return\n"
  2539. " // comment line 2 on return\n"
  2540. " // comment line 1 after return\n"
  2541. "}",
  2542. format("int f(int n) { // comment line 1 on f\n"
  2543. " // comment line 2 on f\n"
  2544. " // comment line 1 before return\n"
  2545. " // comment line 2 before return\n"
  2546. " return n; // comment line 1 on return\n"
  2547. " // comment line 2 on return\n"
  2548. " // comment line 1 after return\n"
  2549. "}",
  2550. getLLVMStyleWithColumns(40)));
  2551. EXPECT_EQ("int f(int n) {\n"
  2552. " switch (n) { // comment line 1 on switch\n"
  2553. " // comment line 2 on switch\n"
  2554. " // comment line 1 before case 1\n"
  2555. " // comment line 2 before case 1\n"
  2556. " case 1: // comment line 1 on case 1\n"
  2557. " // comment line 2 on case 1\n"
  2558. " // comment line 1 before return 1\n"
  2559. " // comment line 2 before return 1\n"
  2560. " return 1; // comment line 1 on return 1\n"
  2561. " // comment line 2 on return 1\n"
  2562. " // comment line 1 before default\n"
  2563. " // comment line 2 before default\n"
  2564. " default: // comment line 1 on default\n"
  2565. " // comment line 2 on default\n"
  2566. " // comment line 1 before return 2\n"
  2567. " return 2 * f(n - 1); // comment line 1 on return 2\n"
  2568. " // comment line 2 on return 2\n"
  2569. " // comment line 1 after return\n"
  2570. " // comment line 2 after return\n"
  2571. " }\n"
  2572. "}",
  2573. format("int f(int n) {\n"
  2574. " switch (n) { // comment line 1 on switch\n"
  2575. " // comment line 2 on switch\n"
  2576. " // comment line 1 before case 1\n"
  2577. " // comment line 2 before case 1\n"
  2578. " case 1: // comment line 1 on case 1\n"
  2579. " // comment line 2 on case 1\n"
  2580. " // comment line 1 before return 1\n"
  2581. " // comment line 2 before return 1\n"
  2582. " return 1; // comment line 1 on return 1\n"
  2583. " // comment line 2 on return 1\n"
  2584. " // comment line 1 before default\n"
  2585. " // comment line 2 before default\n"
  2586. " default: // comment line 1 on default\n"
  2587. " // comment line 2 on default\n"
  2588. " // comment line 1 before return 2\n"
  2589. " return 2 * f(n - 1); // comment line 1 on return 2\n"
  2590. " // comment line 2 on return 2\n"
  2591. " // comment line 1 after return\n"
  2592. " // comment line 2 after return\n"
  2593. " }\n"
  2594. "}",
  2595. getLLVMStyleWithColumns(80)));
  2596. // If all the lines in a sequence of line comments are aligned with the next
  2597. // token, the first line belongs to the previous token and the other lines
  2598. // belong to the next token.
  2599. EXPECT_EQ("int a; // line about a\n"
  2600. "long b;",
  2601. format("int a; // line about a\n"
  2602. " long b;",
  2603. getLLVMStyleWithColumns(80)));
  2604. EXPECT_EQ("int a; // line about a\n"
  2605. "// line about b\n"
  2606. "long b;",
  2607. format("int a; // line about a\n"
  2608. " // line about b\n"
  2609. " long b;",
  2610. getLLVMStyleWithColumns(80)));
  2611. EXPECT_EQ("int a; // line about a\n"
  2612. "// line 1 about b\n"
  2613. "// line 2 about b\n"
  2614. "long b;",
  2615. format("int a; // line about a\n"
  2616. " // line 1 about b\n"
  2617. " // line 2 about b\n"
  2618. " long b;",
  2619. getLLVMStyleWithColumns(80)));
  2620. }
  2621. TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
  2622. EXPECT_EQ("/*\n"
  2623. " */",
  2624. format("/*\n"
  2625. "*/", getLLVMStyle()));
  2626. EXPECT_EQ("/*\n"
  2627. " */",
  2628. format("/*\n"
  2629. " */", getLLVMStyle()));
  2630. EXPECT_EQ("/*\n"
  2631. " */",
  2632. format("/*\n"
  2633. " */", getLLVMStyle()));
  2634. // Align a single line.
  2635. EXPECT_EQ("/*\n"
  2636. " * line */",
  2637. format("/*\n"
  2638. "* line */",
  2639. getLLVMStyle()));
  2640. EXPECT_EQ("/*\n"
  2641. " * line */",
  2642. format("/*\n"
  2643. " * line */",
  2644. getLLVMStyle()));
  2645. EXPECT_EQ("/*\n"
  2646. " * line */",
  2647. format("/*\n"
  2648. " * line */",
  2649. getLLVMStyle()));
  2650. EXPECT_EQ("/*\n"
  2651. " * line */",
  2652. format("/*\n"
  2653. " * line */",
  2654. getLLVMStyle()));
  2655. EXPECT_EQ("/**\n"
  2656. " * line */",
  2657. format("/**\n"
  2658. "* line */",
  2659. getLLVMStyle()));
  2660. EXPECT_EQ("/**\n"
  2661. " * line */",
  2662. format("/**\n"
  2663. " * line */",
  2664. getLLVMStyle()));
  2665. EXPECT_EQ("/**\n"
  2666. " * line */",
  2667. format("/**\n"
  2668. " * line */",
  2669. getLLVMStyle()));
  2670. EXPECT_EQ("/**\n"
  2671. " * line */",
  2672. format("/**\n"
  2673. " * line */",
  2674. getLLVMStyle()));
  2675. EXPECT_EQ("/**\n"
  2676. " * line */",
  2677. format("/**\n"
  2678. " * line */",
  2679. getLLVMStyle()));
  2680. // Align the end '*/' after a line.
  2681. EXPECT_EQ("/*\n"
  2682. " * line\n"
  2683. " */",
  2684. format("/*\n"
  2685. "* line\n"
  2686. "*/", getLLVMStyle()));
  2687. EXPECT_EQ("/*\n"
  2688. " * line\n"
  2689. " */",
  2690. format("/*\n"
  2691. " * line\n"
  2692. " */", getLLVMStyle()));
  2693. EXPECT_EQ("/*\n"
  2694. " * line\n"
  2695. " */",
  2696. format("/*\n"
  2697. " * line\n"
  2698. " */", getLLVMStyle()));
  2699. // Align two lines.
  2700. EXPECT_EQ("/* line 1\n"
  2701. " * line 2 */",
  2702. format("/* line 1\n"
  2703. " * line 2 */",
  2704. getLLVMStyle()));
  2705. EXPECT_EQ("/* line 1\n"
  2706. " * line 2 */",
  2707. format("/* line 1\n"
  2708. "* line 2 */",
  2709. getLLVMStyle()));
  2710. EXPECT_EQ("/* line 1\n"
  2711. " * line 2 */",
  2712. format("/* line 1\n"
  2713. " * line 2 */",
  2714. getLLVMStyle()));
  2715. EXPECT_EQ("/* line 1\n"
  2716. " * line 2 */",
  2717. format("/* line 1\n"
  2718. " * line 2 */",
  2719. getLLVMStyle()));
  2720. EXPECT_EQ("/* line 1\n"
  2721. " * line 2 */",
  2722. format("/* line 1\n"
  2723. " * line 2 */",
  2724. getLLVMStyle()));
  2725. EXPECT_EQ("int i; /* line 1\n"
  2726. " * line 2 */",
  2727. format("int i; /* line 1\n"
  2728. "* line 2 */",
  2729. getLLVMStyle()));
  2730. EXPECT_EQ("int i; /* line 1\n"
  2731. " * line 2 */",
  2732. format("int i; /* line 1\n"
  2733. " * line 2 */",
  2734. getLLVMStyle()));
  2735. EXPECT_EQ("int i; /* line 1\n"
  2736. " * line 2 */",
  2737. format("int i; /* line 1\n"
  2738. " * line 2 */",
  2739. getLLVMStyle()));
  2740. // Align several lines.
  2741. EXPECT_EQ("/* line 1\n"
  2742. " * line 2\n"
  2743. " * line 3 */",
  2744. format("/* line 1\n"
  2745. " * line 2\n"
  2746. "* line 3 */",
  2747. getLLVMStyle()));
  2748. EXPECT_EQ("/* line 1\n"
  2749. " * line 2\n"
  2750. " * line 3 */",
  2751. format("/* line 1\n"
  2752. " * line 2\n"
  2753. "* line 3 */",
  2754. getLLVMStyle()));
  2755. EXPECT_EQ("/*\n"
  2756. "** line 1\n"
  2757. "** line 2\n"
  2758. "*/",
  2759. format("/*\n"
  2760. "** line 1\n"
  2761. " ** line 2\n"
  2762. "*/",
  2763. getLLVMStyle()));
  2764. // Align with different indent after the decorations.
  2765. EXPECT_EQ("/*\n"
  2766. " * line 1\n"
  2767. " * line 2\n"
  2768. " * line 3\n"
  2769. " * line 4\n"
  2770. " */",
  2771. format("/*\n"
  2772. "* line 1\n"
  2773. " * line 2\n"
  2774. " * line 3\n"
  2775. "* line 4\n"
  2776. "*/", getLLVMStyle()));
  2777. // Align empty or blank lines.
  2778. EXPECT_EQ("/**\n"
  2779. " *\n"
  2780. " *\n"
  2781. " *\n"
  2782. " */",
  2783. format("/**\n"
  2784. "* \n"
  2785. " * \n"
  2786. " *\n"
  2787. "*/", getLLVMStyle()));
  2788. // Align while breaking and reflowing.
  2789. EXPECT_EQ("/*\n"
  2790. " * long long long\n"
  2791. " * long long\n"
  2792. " *\n"
  2793. " * long */",
  2794. format("/*\n"
  2795. " * long long long long\n"
  2796. " * long\n"
  2797. " *\n"
  2798. "* long */",
  2799. getLLVMStyleWithColumns(20)));
  2800. }
  2801. TEST_F(FormatTestComments, NoCrash_Bug34236) {
  2802. // This is a test case from a crasher reported in:
  2803. // https://bugs.llvm.org/show_bug.cgi?id=34236
  2804. // Temporarily disable formatting for readability.
  2805. // clang-format off
  2806. EXPECT_EQ(
  2807. "/* */ /*\n"
  2808. " * a\n"
  2809. " * b c d*/",
  2810. format(
  2811. "/* */ /*\n"
  2812. " * a b\n"
  2813. " * c d*/",
  2814. getLLVMStyleWithColumns(80)));
  2815. // clang-format on
  2816. }
  2817. TEST_F(FormatTestComments, NonTrailingBlockComments) {
  2818. verifyFormat("const /** comment comment */ A = B;",
  2819. getLLVMStyleWithColumns(40));
  2820. verifyFormat("const /** comment comment comment */ A =\n"
  2821. " B;",
  2822. getLLVMStyleWithColumns(40));
  2823. EXPECT_EQ("const /** comment comment comment\n"
  2824. " comment */\n"
  2825. " A = B;",
  2826. format("const /** comment comment comment comment */\n"
  2827. " A = B;",
  2828. getLLVMStyleWithColumns(40)));
  2829. }
  2830. TEST_F(FormatTestComments, PythonStyleComments) {
  2831. // Keeps a space after '#'.
  2832. EXPECT_EQ("# comment\n"
  2833. "key: value",
  2834. format("#comment\n"
  2835. "key:value",
  2836. getTextProtoStyleWithColumns(20)));
  2837. EXPECT_EQ("# comment\n"
  2838. "key: value",
  2839. format("# comment\n"
  2840. "key:value",
  2841. getTextProtoStyleWithColumns(20)));
  2842. // Breaks long comment.
  2843. EXPECT_EQ("# comment comment\n"
  2844. "# comment\n"
  2845. "key: value",
  2846. format("# comment comment comment\n"
  2847. "key:value",
  2848. getTextProtoStyleWithColumns(20)));
  2849. // Indents comments.
  2850. EXPECT_EQ("data {\n"
  2851. " # comment comment\n"
  2852. " # comment\n"
  2853. " key: value\n"
  2854. "}",
  2855. format("data {\n"
  2856. "# comment comment comment\n"
  2857. "key: value}",
  2858. getTextProtoStyleWithColumns(20)));
  2859. EXPECT_EQ("data {\n"
  2860. " # comment comment\n"
  2861. " # comment\n"
  2862. " key: value\n"
  2863. "}",
  2864. format("data {# comment comment comment\n"
  2865. "key: value}",
  2866. getTextProtoStyleWithColumns(20)));
  2867. // Reflows long comments.
  2868. EXPECT_EQ("# comment comment\n"
  2869. "# comment comment\n"
  2870. "key: value",
  2871. format("# comment comment comment\n"
  2872. "# comment\n"
  2873. "key:value",
  2874. getTextProtoStyleWithColumns(20)));
  2875. // Breaks trailing comments.
  2876. EXPECT_EQ("k: val # comment\n"
  2877. " # comment\n"
  2878. "a: 1",
  2879. format("k:val#comment comment\n"
  2880. "a:1",
  2881. getTextProtoStyleWithColumns(20)));
  2882. EXPECT_EQ("id {\n"
  2883. " k: val # comment\n"
  2884. " # comment\n"
  2885. " # line line\n"
  2886. " a: 1\n"
  2887. "}",
  2888. format("id {k:val#comment comment\n"
  2889. "# line line\n"
  2890. "a:1}",
  2891. getTextProtoStyleWithColumns(20)));
  2892. // Aligns trailing comments.
  2893. EXPECT_EQ("k: val # commen1\n"
  2894. " # commen2\n"
  2895. " # commen3\n"
  2896. "# commen4\n"
  2897. "a: 1 # commen5\n"
  2898. " # commen6\n"
  2899. " # commen7",
  2900. format("k:val#commen1 commen2\n"
  2901. " # commen3\n"
  2902. "# commen4\n"
  2903. "a:1#commen5 commen6\n"
  2904. " #commen7",
  2905. getTextProtoStyleWithColumns(20)));
  2906. }
  2907. } // end namespace
  2908. } // end namespace format
  2909. } // end namespace clang