FormatTestComments.cpp 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  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. EXPECT_EQ("/* This is a long\n"
  1031. " * comment that\n"
  1032. " * doesn't\n"
  1033. " * fit on one line.\n"
  1034. " */",
  1035. format("/* "
  1036. "This is a long "
  1037. "comment that "
  1038. "doesn't "
  1039. "fit on one line. */",
  1040. getLLVMStyleWithColumns(20)));
  1041. EXPECT_EQ(
  1042. "/* a b c d\n"
  1043. " * e f g\n"
  1044. " * h i j k\n"
  1045. " */",
  1046. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1047. EXPECT_EQ(
  1048. "/* a b c d\n"
  1049. " * e f g\n"
  1050. " * h i j k\n"
  1051. " */",
  1052. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1053. EXPECT_EQ("/*\n"
  1054. "This is a long\n"
  1055. "comment that doesn't\n"
  1056. "fit on one line.\n"
  1057. "*/",
  1058. format("/*\n"
  1059. "This is a long "
  1060. "comment that doesn't "
  1061. "fit on one line. \n"
  1062. "*/",
  1063. getLLVMStyleWithColumns(20)));
  1064. EXPECT_EQ("/*\n"
  1065. " * This is a long\n"
  1066. " * comment that\n"
  1067. " * doesn't fit on\n"
  1068. " * one line.\n"
  1069. " */",
  1070. format("/* \n"
  1071. " * This is a long "
  1072. " comment that "
  1073. " doesn't fit on "
  1074. " one line. \n"
  1075. " */",
  1076. getLLVMStyleWithColumns(20)));
  1077. EXPECT_EQ("/*\n"
  1078. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1079. " * so_it_should_be_broken\n"
  1080. " * wherever_a_space_occurs\n"
  1081. " */",
  1082. format("/*\n"
  1083. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1084. " so_it_should_be_broken "
  1085. " wherever_a_space_occurs \n"
  1086. " */",
  1087. getLLVMStyleWithColumns(20)));
  1088. EXPECT_EQ("/*\n"
  1089. " * This_comment_can_not_be_broken_into_lines\n"
  1090. " */",
  1091. format("/*\n"
  1092. " * This_comment_can_not_be_broken_into_lines\n"
  1093. " */",
  1094. getLLVMStyleWithColumns(20)));
  1095. EXPECT_EQ("{\n"
  1096. " /*\n"
  1097. " This is another\n"
  1098. " long comment that\n"
  1099. " doesn't fit on one\n"
  1100. " line 1234567890\n"
  1101. " */\n"
  1102. "}",
  1103. format("{\n"
  1104. "/*\n"
  1105. "This is another "
  1106. " long comment that "
  1107. " doesn't fit on one"
  1108. " line 1234567890\n"
  1109. "*/\n"
  1110. "}",
  1111. getLLVMStyleWithColumns(20)));
  1112. EXPECT_EQ("{\n"
  1113. " /*\n"
  1114. " * This i s\n"
  1115. " * another comment\n"
  1116. " * t hat doesn' t\n"
  1117. " * fit on one l i\n"
  1118. " * n e\n"
  1119. " */\n"
  1120. "}",
  1121. format("{\n"
  1122. "/*\n"
  1123. " * This i s"
  1124. " another comment"
  1125. " t hat doesn' t"
  1126. " fit on one l i"
  1127. " n e\n"
  1128. " */\n"
  1129. "}",
  1130. getLLVMStyleWithColumns(20)));
  1131. EXPECT_EQ("/*\n"
  1132. " * This is a long\n"
  1133. " * comment that\n"
  1134. " * doesn't fit on\n"
  1135. " * one line\n"
  1136. " */",
  1137. format(" /*\n"
  1138. " * This is a long comment that doesn't fit on one line\n"
  1139. " */",
  1140. getLLVMStyleWithColumns(20)));
  1141. EXPECT_EQ("{\n"
  1142. " if (something) /* This is a\n"
  1143. " long\n"
  1144. " comment */\n"
  1145. " ;\n"
  1146. "}",
  1147. format("{\n"
  1148. " if (something) /* This is a long comment */\n"
  1149. " ;\n"
  1150. "}",
  1151. getLLVMStyleWithColumns(30)));
  1152. EXPECT_EQ("/* A comment before\n"
  1153. " * a macro\n"
  1154. " * definition */\n"
  1155. "#define a b",
  1156. format("/* A comment before a macro definition */\n"
  1157. "#define a b",
  1158. getLLVMStyleWithColumns(20)));
  1159. EXPECT_EQ("/* some comment\n"
  1160. " * a comment that\n"
  1161. " * we break another\n"
  1162. " * comment we have\n"
  1163. " * to break a left\n"
  1164. " * comment\n"
  1165. " */",
  1166. format(" /* some comment\n"
  1167. " * a comment that we break\n"
  1168. " * another comment we have to break\n"
  1169. "* a left comment\n"
  1170. " */",
  1171. getLLVMStyleWithColumns(20)));
  1172. EXPECT_EQ("/**\n"
  1173. " * multiline block\n"
  1174. " * comment\n"
  1175. " *\n"
  1176. " */",
  1177. format("/**\n"
  1178. " * multiline block comment\n"
  1179. " *\n"
  1180. " */",
  1181. getLLVMStyleWithColumns(20)));
  1182. EXPECT_EQ("/*\n"
  1183. "\n"
  1184. "\n"
  1185. " */\n",
  1186. format(" /* \n"
  1187. " \n"
  1188. " \n"
  1189. " */\n"));
  1190. EXPECT_EQ("/* a a */",
  1191. format("/* a a */", getLLVMStyleWithColumns(15)));
  1192. EXPECT_EQ("/* a a bc */",
  1193. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1194. EXPECT_EQ("/* aaa aaa\n"
  1195. " * aaaaa */",
  1196. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1197. EXPECT_EQ("/* aaa aaa\n"
  1198. " * aaaaa */",
  1199. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1200. }
  1201. TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
  1202. EXPECT_EQ("#define X \\\n"
  1203. " /* \\\n"
  1204. " Test \\\n"
  1205. " Macro comment \\\n"
  1206. " with a long \\\n"
  1207. " line \\\n"
  1208. " */ \\\n"
  1209. " A + B",
  1210. format("#define X \\\n"
  1211. " /*\n"
  1212. " Test\n"
  1213. " Macro comment with a long line\n"
  1214. " */ \\\n"
  1215. " A + B",
  1216. getLLVMStyleWithColumns(20)));
  1217. EXPECT_EQ("#define X \\\n"
  1218. " /* Macro comment \\\n"
  1219. " with a long \\\n"
  1220. " line */ \\\n"
  1221. " A + B",
  1222. format("#define X \\\n"
  1223. " /* Macro comment with a long\n"
  1224. " line */ \\\n"
  1225. " A + B",
  1226. getLLVMStyleWithColumns(20)));
  1227. EXPECT_EQ("#define X \\\n"
  1228. " /* Macro comment \\\n"
  1229. " * with a long \\\n"
  1230. " * line */ \\\n"
  1231. " A + B",
  1232. format("#define X \\\n"
  1233. " /* Macro comment with a long line */ \\\n"
  1234. " A + B",
  1235. getLLVMStyleWithColumns(20)));
  1236. }
  1237. TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
  1238. verifyFormat("#ifdef A // line about A\n"
  1239. "// section comment\n"
  1240. "#endif",
  1241. getLLVMStyleWithColumns(80));
  1242. verifyFormat("#ifdef A // line 1 about A\n"
  1243. " // line 2 about A\n"
  1244. "// section comment\n"
  1245. "#endif",
  1246. getLLVMStyleWithColumns(80));
  1247. EXPECT_EQ("#ifdef A // line 1 about A\n"
  1248. " // line 2 about A\n"
  1249. "// section comment\n"
  1250. "#endif",
  1251. format("#ifdef A // line 1 about A\n"
  1252. " // line 2 about A\n"
  1253. "// section comment\n"
  1254. "#endif",
  1255. getLLVMStyleWithColumns(80)));
  1256. verifyFormat("int f() {\n"
  1257. " int i;\n"
  1258. "#ifdef A // comment about A\n"
  1259. " // section comment 1\n"
  1260. " // section comment 2\n"
  1261. " i = 2;\n"
  1262. "#else // comment about #else\n"
  1263. " // section comment 3\n"
  1264. " i = 4;\n"
  1265. "#endif\n"
  1266. "}", getLLVMStyleWithColumns(80));
  1267. }
  1268. TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
  1269. verifyFormat("#if A\n"
  1270. "#else // A\n"
  1271. "int iiii;\n"
  1272. "#endif // B",
  1273. getLLVMStyleWithColumns(20));
  1274. verifyFormat("#if A\n"
  1275. "#else // A\n"
  1276. "int iiii; // CC\n"
  1277. "#endif // B",
  1278. getLLVMStyleWithColumns(20));
  1279. EXPECT_EQ("#if A\n"
  1280. "#else // A1\n"
  1281. " // A2\n"
  1282. "int ii;\n"
  1283. "#endif // B",
  1284. format("#if A\n"
  1285. "#else // A1\n"
  1286. " // A2\n"
  1287. "int ii;\n"
  1288. "#endif // B",
  1289. getLLVMStyleWithColumns(20)));
  1290. }
  1291. TEST_F(FormatTestComments, CommentsInStaticInitializers) {
  1292. EXPECT_EQ(
  1293. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1294. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1295. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1296. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1297. " aaaaaaaaaaaaaaaaaaaa};",
  1298. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1299. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1300. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1301. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1302. " aaaaaaaaaaaaaaaaaaaa };"));
  1303. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1304. " bbbbbbbbbbb, ccccccccccc};");
  1305. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1306. " // comment for bb....\n"
  1307. " bbbbbbbbbbb, ccccccccccc};");
  1308. verifyGoogleFormat(
  1309. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1310. " bbbbbbbbbbb, ccccccccccc};");
  1311. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1312. " // comment for bb....\n"
  1313. " bbbbbbbbbbb, ccccccccccc};");
  1314. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1315. " {d, e, f}, // Group #2\n"
  1316. " {g, h, i}}; // Group #3");
  1317. verifyFormat("S s = {{// Group #1\n"
  1318. " a, b, c},\n"
  1319. " {// Group #2\n"
  1320. " d, e, f},\n"
  1321. " {// Group #3\n"
  1322. " g, h, i}};");
  1323. EXPECT_EQ("S s = {\n"
  1324. " // Some comment\n"
  1325. " a,\n"
  1326. "\n"
  1327. " // Comment after empty line\n"
  1328. " b}",
  1329. format("S s = {\n"
  1330. " // Some comment\n"
  1331. " a,\n"
  1332. " \n"
  1333. " // Comment after empty line\n"
  1334. " b\n"
  1335. "}"));
  1336. EXPECT_EQ("S s = {\n"
  1337. " /* Some comment */\n"
  1338. " a,\n"
  1339. "\n"
  1340. " /* Comment after empty line */\n"
  1341. " b}",
  1342. format("S s = {\n"
  1343. " /* Some comment */\n"
  1344. " a,\n"
  1345. " \n"
  1346. " /* Comment after empty line */\n"
  1347. " b\n"
  1348. "}"));
  1349. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1350. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1351. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1352. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1353. }
  1354. TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
  1355. EXPECT_EQ("if (true) { // comment about branch\n"
  1356. " // comment about f\n"
  1357. " f();\n"
  1358. "}",
  1359. format("if (true) { // comment about branch\n"
  1360. " // comment about f\n"
  1361. " f();\n"
  1362. "}",
  1363. getLLVMStyleWithColumns(80)));
  1364. EXPECT_EQ("if (1) { // if line 1\n"
  1365. " // if line 2\n"
  1366. " // if line 3\n"
  1367. " // f line 1\n"
  1368. " // f line 2\n"
  1369. " f();\n"
  1370. "} else { // else line 1\n"
  1371. " // else line 2\n"
  1372. " // else line 3\n"
  1373. " // g line 1\n"
  1374. " g();\n"
  1375. "}",
  1376. format("if (1) { // if line 1\n"
  1377. " // if line 2\n"
  1378. " // if line 3\n"
  1379. " // f line 1\n"
  1380. " // f line 2\n"
  1381. " f();\n"
  1382. "} else { // else line 1\n"
  1383. " // else line 2\n"
  1384. " // else line 3\n"
  1385. " // g line 1\n"
  1386. " g();\n"
  1387. "}"));
  1388. EXPECT_EQ("do { // line 1\n"
  1389. " // line 2\n"
  1390. " // line 3\n"
  1391. " f();\n"
  1392. "} while (true);",
  1393. format("do { // line 1\n"
  1394. " // line 2\n"
  1395. " // line 3\n"
  1396. " f();\n"
  1397. "} while (true);",
  1398. getLLVMStyleWithColumns(80)));
  1399. EXPECT_EQ("while (a < b) { // line 1\n"
  1400. " // line 2\n"
  1401. " // line 3\n"
  1402. " f();\n"
  1403. "}",
  1404. format("while (a < b) {// line 1\n"
  1405. " // line 2\n"
  1406. " // line 3\n"
  1407. " f();\n"
  1408. "}",
  1409. getLLVMStyleWithColumns(80)));
  1410. }
  1411. TEST_F(FormatTestComments, ReflowsComments) {
  1412. // Break a long line and reflow with the full next line.
  1413. EXPECT_EQ("// long long long\n"
  1414. "// long long",
  1415. format("// long long long long\n"
  1416. "// long",
  1417. getLLVMStyleWithColumns(20)));
  1418. // Keep the trailing newline while reflowing.
  1419. EXPECT_EQ("// long long long\n"
  1420. "// long long\n",
  1421. format("// long long long long\n"
  1422. "// long\n",
  1423. getLLVMStyleWithColumns(20)));
  1424. // Break a long line and reflow with a part of the next line.
  1425. EXPECT_EQ("// long long long\n"
  1426. "// long long\n"
  1427. "// long_long",
  1428. format("// long long long long\n"
  1429. "// long long_long",
  1430. getLLVMStyleWithColumns(20)));
  1431. // Break but do not reflow if the first word from the next line is too long.
  1432. EXPECT_EQ("// long long long\n"
  1433. "// long\n"
  1434. "// long_long_long\n",
  1435. format("// long long long long\n"
  1436. "// long_long_long\n",
  1437. getLLVMStyleWithColumns(20)));
  1438. // Don't break or reflow short lines.
  1439. verifyFormat("// long\n"
  1440. "// long long long lo\n"
  1441. "// long long long lo\n"
  1442. "// long",
  1443. getLLVMStyleWithColumns(20));
  1444. // Keep prefixes and decorations while reflowing.
  1445. EXPECT_EQ("/// long long long\n"
  1446. "/// long long\n",
  1447. format("/// long long long long\n"
  1448. "/// long\n",
  1449. getLLVMStyleWithColumns(20)));
  1450. EXPECT_EQ("//! long long long\n"
  1451. "//! long long\n",
  1452. format("//! long long long long\n"
  1453. "//! long\n",
  1454. getLLVMStyleWithColumns(20)));
  1455. EXPECT_EQ("/* long long long\n"
  1456. " * long long */",
  1457. format("/* long long long long\n"
  1458. " * long */",
  1459. getLLVMStyleWithColumns(20)));
  1460. EXPECT_EQ("///< long long long\n"
  1461. "///< long long\n",
  1462. format("///< long long long long\n"
  1463. "///< long\n",
  1464. getLLVMStyleWithColumns(20)));
  1465. EXPECT_EQ("//!< long long long\n"
  1466. "//!< long long\n",
  1467. format("//!< long long long long\n"
  1468. "//!< long\n",
  1469. getLLVMStyleWithColumns(20)));
  1470. // Don't bring leading whitespace up while reflowing.
  1471. EXPECT_EQ("/* long long long\n"
  1472. " * long long long\n"
  1473. " */",
  1474. format("/* long long long long\n"
  1475. " * long long\n"
  1476. " */",
  1477. getLLVMStyleWithColumns(20)));
  1478. // Reflow the last line of a block comment with its trailing '*/'.
  1479. EXPECT_EQ("/* long long long\n"
  1480. " long long */",
  1481. format("/* long long long long\n"
  1482. " long */",
  1483. getLLVMStyleWithColumns(20)));
  1484. // Reflow two short lines; keep the postfix of the last one.
  1485. EXPECT_EQ("/* long long long\n"
  1486. " * long long long */",
  1487. format("/* long long long long\n"
  1488. " * long\n"
  1489. " * long */",
  1490. getLLVMStyleWithColumns(20)));
  1491. // Put the postfix of the last short reflow line on a newline if it doesn't
  1492. // fit.
  1493. EXPECT_EQ("/* long long long\n"
  1494. " * long long longg\n"
  1495. " */",
  1496. format("/* long long long long\n"
  1497. " * long\n"
  1498. " * longg */",
  1499. getLLVMStyleWithColumns(20)));
  1500. // Reflow lines with leading whitespace.
  1501. EXPECT_EQ("{\n"
  1502. " /*\n"
  1503. " * long long long\n"
  1504. " * long long long\n"
  1505. " * long long long\n"
  1506. " */\n"
  1507. "}",
  1508. format("{\n"
  1509. "/*\n"
  1510. " * long long long long\n"
  1511. " * long\n"
  1512. " * long long long long\n"
  1513. " */\n"
  1514. "}",
  1515. getLLVMStyleWithColumns(20)));
  1516. // Break single line block comments that are first in the line with ' *'
  1517. // decoration.
  1518. EXPECT_EQ("/* long long long\n"
  1519. " * long */",
  1520. format("/* long long long long */", getLLVMStyleWithColumns(20)));
  1521. // Break single line block comment that are not first in the line with ' '
  1522. // decoration.
  1523. EXPECT_EQ("int i; /* long long\n"
  1524. " long */",
  1525. format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
  1526. // Reflow a line that goes just over the column limit.
  1527. EXPECT_EQ("// long long long\n"
  1528. "// lon long",
  1529. format("// long long long lon\n"
  1530. "// long",
  1531. getLLVMStyleWithColumns(20)));
  1532. // Stop reflowing if the next line has a different indentation than the
  1533. // previous line.
  1534. EXPECT_EQ("// long long long\n"
  1535. "// long\n"
  1536. "// long long\n"
  1537. "// long",
  1538. format("// long long long long\n"
  1539. "// long long\n"
  1540. "// long",
  1541. getLLVMStyleWithColumns(20)));
  1542. // Reflow into the last part of a really long line that has been broken into
  1543. // multiple lines.
  1544. EXPECT_EQ("// long long long\n"
  1545. "// long long long\n"
  1546. "// long long long\n",
  1547. format("// long long long long long long long long\n"
  1548. "// long\n",
  1549. getLLVMStyleWithColumns(20)));
  1550. // Break the first line, then reflow the beginning of the second and third
  1551. // line up.
  1552. EXPECT_EQ("// long long long\n"
  1553. "// lon1 lon2 lon2\n"
  1554. "// lon2 lon3 lon3",
  1555. format("// long long long lon1\n"
  1556. "// lon2 lon2 lon2\n"
  1557. "// lon3 lon3",
  1558. getLLVMStyleWithColumns(20)));
  1559. // Reflow the beginning of the second line, then break the rest.
  1560. EXPECT_EQ("// long long long\n"
  1561. "// lon1 lon2 lon2\n"
  1562. "// lon2 lon2 lon2\n"
  1563. "// lon3",
  1564. format("// long long long lon1\n"
  1565. "// lon2 lon2 lon2 lon2 lon2 lon3",
  1566. getLLVMStyleWithColumns(20)));
  1567. // Shrink the first line, then reflow the second line up.
  1568. EXPECT_EQ("// long long long", format("// long long\n"
  1569. "// long",
  1570. getLLVMStyleWithColumns(20)));
  1571. // Don't shrink leading whitespace.
  1572. EXPECT_EQ("int i; /// a",
  1573. format("int i; /// a", getLLVMStyleWithColumns(20)));
  1574. // Shrink trailing whitespace if there is no postfix and reflow.
  1575. EXPECT_EQ("// long long long\n"
  1576. "// long long",
  1577. format("// long long long long \n"
  1578. "// long",
  1579. getLLVMStyleWithColumns(20)));
  1580. // Shrink trailing whitespace to a single one if there is postfix.
  1581. EXPECT_EQ("/* long long long */",
  1582. format("/* long long long */", getLLVMStyleWithColumns(20)));
  1583. // Break a block comment postfix if exceeding the line limit.
  1584. EXPECT_EQ("/* long\n"
  1585. " */",
  1586. format("/* long */", getLLVMStyleWithColumns(20)));
  1587. // Reflow indented comments.
  1588. EXPECT_EQ("{\n"
  1589. " // long long long\n"
  1590. " // long long\n"
  1591. " int i; /* long lon\n"
  1592. " g long\n"
  1593. " */\n"
  1594. "}",
  1595. format("{\n"
  1596. " // long long long long\n"
  1597. " // long\n"
  1598. " int i; /* long lon g\n"
  1599. " long */\n"
  1600. "}",
  1601. getLLVMStyleWithColumns(20)));
  1602. // Don't realign trailing comments after reflow has happened.
  1603. EXPECT_EQ("// long long long\n"
  1604. "// long long\n"
  1605. "long i; // long",
  1606. format("// long long long long\n"
  1607. "// long\n"
  1608. "long i; // long",
  1609. getLLVMStyleWithColumns(20)));
  1610. EXPECT_EQ("// long long long\n"
  1611. "// longng long long\n"
  1612. "// long lo",
  1613. format("// long long long longng\n"
  1614. "// long long long\n"
  1615. "// lo",
  1616. getLLVMStyleWithColumns(20)));
  1617. // Reflow lines after a broken line.
  1618. EXPECT_EQ("int a; // Trailing\n"
  1619. " // comment on\n"
  1620. " // 2 or 3\n"
  1621. " // lines.\n",
  1622. format("int a; // Trailing comment\n"
  1623. " // on 2\n"
  1624. " // or 3\n"
  1625. " // lines.\n",
  1626. getLLVMStyleWithColumns(20)));
  1627. EXPECT_EQ("/// This long line\n"
  1628. "/// gets reflown.\n",
  1629. format("/// This long line gets\n"
  1630. "/// reflown.\n",
  1631. getLLVMStyleWithColumns(20)));
  1632. EXPECT_EQ("//! This long line\n"
  1633. "//! gets reflown.\n",
  1634. format(" //! This long line gets\n"
  1635. " //! reflown.\n",
  1636. getLLVMStyleWithColumns(20)));
  1637. EXPECT_EQ("/* This long line\n"
  1638. " * gets reflown.\n"
  1639. " */\n",
  1640. format("/* This long line gets\n"
  1641. " * reflown.\n"
  1642. " */\n",
  1643. getLLVMStyleWithColumns(20)));
  1644. // Reflow after indentation makes a line too long.
  1645. EXPECT_EQ("{\n"
  1646. " // long long long\n"
  1647. " // lo long\n"
  1648. "}\n",
  1649. format("{\n"
  1650. "// long long long lo\n"
  1651. "// long\n"
  1652. "}\n",
  1653. getLLVMStyleWithColumns(20)));
  1654. // Break and reflow multiple lines.
  1655. EXPECT_EQ("/*\n"
  1656. " * Reflow the end of\n"
  1657. " * line by 11 22 33\n"
  1658. " * 4.\n"
  1659. " */\n",
  1660. format("/*\n"
  1661. " * Reflow the end of line\n"
  1662. " * by\n"
  1663. " * 11\n"
  1664. " * 22\n"
  1665. " * 33\n"
  1666. " * 4.\n"
  1667. " */\n",
  1668. getLLVMStyleWithColumns(20)));
  1669. EXPECT_EQ("/// First line gets\n"
  1670. "/// broken. Second\n"
  1671. "/// line gets\n"
  1672. "/// reflown and\n"
  1673. "/// broken. Third\n"
  1674. "/// gets reflown.\n",
  1675. format("/// First line gets broken.\n"
  1676. "/// Second line gets reflown and broken.\n"
  1677. "/// Third gets reflown.\n",
  1678. getLLVMStyleWithColumns(20)));
  1679. EXPECT_EQ("int i; // first long\n"
  1680. " // long snd\n"
  1681. " // long.\n",
  1682. format("int i; // first long long\n"
  1683. " // snd long.\n",
  1684. getLLVMStyleWithColumns(20)));
  1685. EXPECT_EQ("{\n"
  1686. " // first long line\n"
  1687. " // line second\n"
  1688. " // long line line\n"
  1689. " // third long line\n"
  1690. " // line\n"
  1691. "}\n",
  1692. format("{\n"
  1693. " // first long line line\n"
  1694. " // second long line line\n"
  1695. " // third long line line\n"
  1696. "}\n",
  1697. getLLVMStyleWithColumns(20)));
  1698. EXPECT_EQ("int i; /* first line\n"
  1699. " * second\n"
  1700. " * line third\n"
  1701. " * line\n"
  1702. " */",
  1703. format("int i; /* first line\n"
  1704. " * second line\n"
  1705. " * third line\n"
  1706. " */",
  1707. getLLVMStyleWithColumns(20)));
  1708. // Reflow the last two lines of a section that starts with a line having
  1709. // different indentation.
  1710. EXPECT_EQ(
  1711. "// long\n"
  1712. "// long long long\n"
  1713. "// long long",
  1714. format("// long\n"
  1715. "// long long long long\n"
  1716. "// long",
  1717. getLLVMStyleWithColumns(20)));
  1718. // Keep the block comment endling '*/' while reflowing.
  1719. EXPECT_EQ("/* Long long long\n"
  1720. " * line short */\n",
  1721. format("/* Long long long line\n"
  1722. " * short */\n",
  1723. getLLVMStyleWithColumns(20)));
  1724. // Don't reflow between separate blocks of comments.
  1725. EXPECT_EQ("/* First comment\n"
  1726. " * block will */\n"
  1727. "/* Snd\n"
  1728. " */\n",
  1729. format("/* First comment block\n"
  1730. " * will */\n"
  1731. "/* Snd\n"
  1732. " */\n",
  1733. getLLVMStyleWithColumns(20)));
  1734. // Don't reflow across blank comment lines.
  1735. EXPECT_EQ("int i; // This long\n"
  1736. " // line gets\n"
  1737. " // broken.\n"
  1738. " //\n"
  1739. " // keep.\n",
  1740. format("int i; // This long line gets broken.\n"
  1741. " // \n"
  1742. " // keep.\n",
  1743. getLLVMStyleWithColumns(20)));
  1744. EXPECT_EQ("{\n"
  1745. " /// long long long\n"
  1746. " /// long long\n"
  1747. " ///\n"
  1748. " /// long\n"
  1749. "}",
  1750. format("{\n"
  1751. " /// long long long long\n"
  1752. " /// long\n"
  1753. " ///\n"
  1754. " /// long\n"
  1755. "}",
  1756. getLLVMStyleWithColumns(20)));
  1757. EXPECT_EQ("//! long long long\n"
  1758. "//! long\n"
  1759. "\n"
  1760. "//! long",
  1761. format("//! long long long long\n"
  1762. "\n"
  1763. "//! long",
  1764. getLLVMStyleWithColumns(20)));
  1765. EXPECT_EQ("/* long long long\n"
  1766. " long\n"
  1767. "\n"
  1768. " long */",
  1769. format("/* long long long long\n"
  1770. "\n"
  1771. " long */",
  1772. getLLVMStyleWithColumns(20)));
  1773. EXPECT_EQ("/* long long long\n"
  1774. " * long\n"
  1775. " *\n"
  1776. " * long */",
  1777. format("/* long long long long\n"
  1778. " *\n"
  1779. " * long */",
  1780. getLLVMStyleWithColumns(20)));
  1781. // Don't reflow lines having content that is a single character.
  1782. EXPECT_EQ("// long long long\n"
  1783. "// long\n"
  1784. "// l",
  1785. format("// long long long long\n"
  1786. "// l",
  1787. getLLVMStyleWithColumns(20)));
  1788. // Don't reflow lines starting with two punctuation characters.
  1789. EXPECT_EQ("// long long long\n"
  1790. "// long\n"
  1791. "// ... --- ...",
  1792. format(
  1793. "// long long long long\n"
  1794. "// ... --- ...",
  1795. getLLVMStyleWithColumns(20)));
  1796. // Don't reflow lines starting with '@'.
  1797. EXPECT_EQ("// long long long\n"
  1798. "// long\n"
  1799. "// @param arg",
  1800. format("// long long long long\n"
  1801. "// @param arg",
  1802. getLLVMStyleWithColumns(20)));
  1803. // Don't reflow lines starting with 'TODO'.
  1804. EXPECT_EQ("// long long long\n"
  1805. "// long\n"
  1806. "// TODO: long",
  1807. format("// long long long long\n"
  1808. "// TODO: long",
  1809. getLLVMStyleWithColumns(20)));
  1810. // Don't reflow lines starting with 'FIXME'.
  1811. EXPECT_EQ("// long long long\n"
  1812. "// long\n"
  1813. "// FIXME: long",
  1814. format("// long long long long\n"
  1815. "// FIXME: long",
  1816. getLLVMStyleWithColumns(20)));
  1817. // Don't reflow lines starting with 'XXX'.
  1818. EXPECT_EQ("// long long long\n"
  1819. "// long\n"
  1820. "// XXX: long",
  1821. format("// long long long long\n"
  1822. "// XXX: long",
  1823. getLLVMStyleWithColumns(20)));
  1824. // Don't reflow comment pragmas.
  1825. EXPECT_EQ("// long long long\n"
  1826. "// long\n"
  1827. "// IWYU pragma:",
  1828. format("// long long long long\n"
  1829. "// IWYU pragma:",
  1830. getLLVMStyleWithColumns(20)));
  1831. EXPECT_EQ("/* long long long\n"
  1832. " * long\n"
  1833. " * IWYU pragma:\n"
  1834. " */",
  1835. format("/* long long long long\n"
  1836. " * IWYU pragma:\n"
  1837. " */",
  1838. getLLVMStyleWithColumns(20)));
  1839. // Reflow lines that have a non-punctuation character among their first 2
  1840. // characters.
  1841. EXPECT_EQ("// long long long\n"
  1842. "// long 'long'",
  1843. format(
  1844. "// long long long long\n"
  1845. "// 'long'",
  1846. getLLVMStyleWithColumns(20)));
  1847. // Don't reflow between separate blocks of comments.
  1848. EXPECT_EQ("/* First comment\n"
  1849. " * block will */\n"
  1850. "/* Snd\n"
  1851. " */\n",
  1852. format("/* First comment block\n"
  1853. " * will */\n"
  1854. "/* Snd\n"
  1855. " */\n",
  1856. getLLVMStyleWithColumns(20)));
  1857. // Don't reflow lines having different indentation.
  1858. EXPECT_EQ("// long long long\n"
  1859. "// long\n"
  1860. "// long",
  1861. format("// long long long long\n"
  1862. "// long",
  1863. getLLVMStyleWithColumns(20)));
  1864. // Don't reflow separate bullets in list
  1865. EXPECT_EQ("// - long long long\n"
  1866. "// long\n"
  1867. "// - long",
  1868. format("// - long long long long\n"
  1869. "// - long",
  1870. getLLVMStyleWithColumns(20)));
  1871. EXPECT_EQ("// * long long long\n"
  1872. "// long\n"
  1873. "// * long",
  1874. format("// * long long long long\n"
  1875. "// * long",
  1876. getLLVMStyleWithColumns(20)));
  1877. EXPECT_EQ("// + long long long\n"
  1878. "// long\n"
  1879. "// + long",
  1880. format("// + long long long long\n"
  1881. "// + long",
  1882. getLLVMStyleWithColumns(20)));
  1883. EXPECT_EQ("// 1. long long long\n"
  1884. "// long\n"
  1885. "// 2. long",
  1886. format("// 1. long long long long\n"
  1887. "// 2. long",
  1888. getLLVMStyleWithColumns(20)));
  1889. EXPECT_EQ("// -# long long long\n"
  1890. "// long\n"
  1891. "// -# long",
  1892. format("// -# long long long long\n"
  1893. "// -# long",
  1894. getLLVMStyleWithColumns(20)));
  1895. EXPECT_EQ("// - long long long\n"
  1896. "// long long long\n"
  1897. "// - long",
  1898. format("// - long long long long\n"
  1899. "// long long\n"
  1900. "// - long",
  1901. getLLVMStyleWithColumns(20)));
  1902. EXPECT_EQ("// - long long long\n"
  1903. "// long long long\n"
  1904. "// long\n"
  1905. "// - long",
  1906. format("// - long long long long\n"
  1907. "// long long long\n"
  1908. "// - long",
  1909. getLLVMStyleWithColumns(20)));
  1910. // Large number (>2 digits) are not list items
  1911. EXPECT_EQ("// long long long\n"
  1912. "// long 1024. long.",
  1913. format("// long long long long\n"
  1914. "// 1024. long.",
  1915. getLLVMStyleWithColumns(20)));
  1916. // Do not break before number, to avoid introducing a non-reflowable doxygen
  1917. // list item.
  1918. EXPECT_EQ("// long long\n"
  1919. "// long 10. long.",
  1920. format("// long long long 10.\n"
  1921. "// long.",
  1922. getLLVMStyleWithColumns(20)));
  1923. // Don't break or reflow after implicit string literals.
  1924. verifyFormat("#include <t> // l l l\n"
  1925. " // l",
  1926. getLLVMStyleWithColumns(20));
  1927. // Don't break or reflow comments on import lines.
  1928. EXPECT_EQ("#include \"t\" /* l l l\n"
  1929. " * l */",
  1930. format("#include \"t\" /* l l l\n"
  1931. " * l */",
  1932. getLLVMStyleWithColumns(20)));
  1933. // Don't reflow between different trailing comment sections.
  1934. EXPECT_EQ("int i; // long long\n"
  1935. " // long\n"
  1936. "int j; // long long\n"
  1937. " // long\n",
  1938. format("int i; // long long long\n"
  1939. "int j; // long long long\n",
  1940. getLLVMStyleWithColumns(20)));
  1941. // Don't reflow if the first word on the next line is longer than the
  1942. // available space at current line.
  1943. EXPECT_EQ("int i; // trigger\n"
  1944. " // reflow\n"
  1945. " // longsec\n",
  1946. format("int i; // trigger reflow\n"
  1947. " // longsec\n",
  1948. getLLVMStyleWithColumns(20)));
  1949. // Simple case that correctly handles reflow in parameter lists.
  1950. EXPECT_EQ("a = f(/* looooooooong\n"
  1951. " * long long\n"
  1952. " */\n"
  1953. " a);",
  1954. format("a = f(/* looooooooong long\n* long\n*/ a);",
  1955. getLLVMStyleWithColumns(22)));
  1956. // Tricky case that has fewer lines if we reflow the comment, ending up with
  1957. // fewer lines.
  1958. EXPECT_EQ("a = f(/* loooooong\n"
  1959. " * long long\n"
  1960. " */\n"
  1961. " a);",
  1962. format("a = f(/* loooooong long\n* long\n*/ a);",
  1963. getLLVMStyleWithColumns(22)));
  1964. // Keep empty comment lines.
  1965. EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
  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. }
  1971. TEST_F(FormatTestComments, IgnoresIf0Contents) {
  1972. EXPECT_EQ("#if 0\n"
  1973. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1974. "#endif\n"
  1975. "void f() {}",
  1976. format("#if 0\n"
  1977. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1978. "#endif\n"
  1979. "void f( ) { }"));
  1980. EXPECT_EQ("#if false\n"
  1981. "void f( ) { }\n"
  1982. "#endif\n"
  1983. "void g() {}\n",
  1984. format("#if false\n"
  1985. "void f( ) { }\n"
  1986. "#endif\n"
  1987. "void g( ) { }\n"));
  1988. EXPECT_EQ("enum E {\n"
  1989. " One,\n"
  1990. " Two,\n"
  1991. "#if 0\n"
  1992. "Three,\n"
  1993. " Four,\n"
  1994. "#endif\n"
  1995. " Five\n"
  1996. "};",
  1997. format("enum E {\n"
  1998. " One,Two,\n"
  1999. "#if 0\n"
  2000. "Three,\n"
  2001. " Four,\n"
  2002. "#endif\n"
  2003. " Five};"));
  2004. EXPECT_EQ("enum F {\n"
  2005. " One,\n"
  2006. "#if 1\n"
  2007. " Two,\n"
  2008. "#if 0\n"
  2009. "Three,\n"
  2010. " Four,\n"
  2011. "#endif\n"
  2012. " Five\n"
  2013. "#endif\n"
  2014. "};",
  2015. format("enum F {\n"
  2016. "One,\n"
  2017. "#if 1\n"
  2018. "Two,\n"
  2019. "#if 0\n"
  2020. "Three,\n"
  2021. " Four,\n"
  2022. "#endif\n"
  2023. "Five\n"
  2024. "#endif\n"
  2025. "};"));
  2026. EXPECT_EQ("enum G {\n"
  2027. " One,\n"
  2028. "#if 0\n"
  2029. "Two,\n"
  2030. "#else\n"
  2031. " Three,\n"
  2032. "#endif\n"
  2033. " Four\n"
  2034. "};",
  2035. format("enum G {\n"
  2036. "One,\n"
  2037. "#if 0\n"
  2038. "Two,\n"
  2039. "#else\n"
  2040. "Three,\n"
  2041. "#endif\n"
  2042. "Four\n"
  2043. "};"));
  2044. EXPECT_EQ("enum H {\n"
  2045. " One,\n"
  2046. "#if 0\n"
  2047. "#ifdef Q\n"
  2048. "Two,\n"
  2049. "#else\n"
  2050. "Three,\n"
  2051. "#endif\n"
  2052. "#endif\n"
  2053. " Four\n"
  2054. "};",
  2055. format("enum H {\n"
  2056. "One,\n"
  2057. "#if 0\n"
  2058. "#ifdef Q\n"
  2059. "Two,\n"
  2060. "#else\n"
  2061. "Three,\n"
  2062. "#endif\n"
  2063. "#endif\n"
  2064. "Four\n"
  2065. "};"));
  2066. EXPECT_EQ("enum I {\n"
  2067. " One,\n"
  2068. "#if /* test */ 0 || 1\n"
  2069. "Two,\n"
  2070. "Three,\n"
  2071. "#endif\n"
  2072. " Four\n"
  2073. "};",
  2074. format("enum I {\n"
  2075. "One,\n"
  2076. "#if /* test */ 0 || 1\n"
  2077. "Two,\n"
  2078. "Three,\n"
  2079. "#endif\n"
  2080. "Four\n"
  2081. "};"));
  2082. EXPECT_EQ("enum J {\n"
  2083. " One,\n"
  2084. "#if 0\n"
  2085. "#if 0\n"
  2086. "Two,\n"
  2087. "#else\n"
  2088. "Three,\n"
  2089. "#endif\n"
  2090. "Four,\n"
  2091. "#endif\n"
  2092. " Five\n"
  2093. "};",
  2094. format("enum J {\n"
  2095. "One,\n"
  2096. "#if 0\n"
  2097. "#if 0\n"
  2098. "Two,\n"
  2099. "#else\n"
  2100. "Three,\n"
  2101. "#endif\n"
  2102. "Four,\n"
  2103. "#endif\n"
  2104. "Five\n"
  2105. "};"));
  2106. // Ignore stuff in SWIG-blocks.
  2107. EXPECT_EQ("#ifdef SWIG\n"
  2108. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  2109. "#endif\n"
  2110. "void f() {}",
  2111. format("#ifdef SWIG\n"
  2112. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  2113. "#endif\n"
  2114. "void f( ) { }"));
  2115. EXPECT_EQ("#ifndef SWIG\n"
  2116. "void f() {}\n"
  2117. "#endif",
  2118. format("#ifndef SWIG\n"
  2119. "void f( ) { }\n"
  2120. "#endif"));
  2121. }
  2122. TEST_F(FormatTestComments, DontCrashOnBlockComments) {
  2123. EXPECT_EQ(
  2124. "int xxxxxxxxx; /* "
  2125. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2126. "zzzzzz\n"
  2127. "0*/",
  2128. format("int xxxxxxxxx; /* "
  2129. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2130. "0*/"));
  2131. }
  2132. TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
  2133. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  2134. " f();\n"
  2135. "}");
  2136. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  2137. " f();\n"
  2138. "} /* another comment */ else /* comment #3 */ {\n"
  2139. " g();\n"
  2140. "}");
  2141. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  2142. " f();\n"
  2143. "}");
  2144. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  2145. " f();\n"
  2146. "}");
  2147. verifyFormat("do /* a comment in a strange place */ {\n"
  2148. " f();\n"
  2149. "} /* another comment */ while (0);");
  2150. }
  2151. TEST_F(FormatTestComments, BlockComments) {
  2152. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  2153. format("/* *//* */ /* */\n/* *//* */ /* */"));
  2154. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  2155. EXPECT_EQ("#define A /*123*/ \\\n"
  2156. " b\n"
  2157. "/* */\n"
  2158. "someCall(\n"
  2159. " parameter);",
  2160. format("#define A /*123*/ b\n"
  2161. "/* */\n"
  2162. "someCall(parameter);",
  2163. getLLVMStyleWithColumns(15)));
  2164. EXPECT_EQ("#define A\n"
  2165. "/* */ someCall(\n"
  2166. " parameter);",
  2167. format("#define A\n"
  2168. "/* */someCall(parameter);",
  2169. getLLVMStyleWithColumns(15)));
  2170. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  2171. EXPECT_EQ("/*\n"
  2172. " *\n"
  2173. " * aaaaaa\n"
  2174. " * aaaaaa\n"
  2175. " */",
  2176. format("/*\n"
  2177. "*\n"
  2178. " * aaaaaa aaaaaa\n"
  2179. "*/",
  2180. getLLVMStyleWithColumns(10)));
  2181. EXPECT_EQ("/*\n"
  2182. "**\n"
  2183. "* aaaaaa\n"
  2184. "*aaaaaa\n"
  2185. "*/",
  2186. format("/*\n"
  2187. "**\n"
  2188. "* aaaaaa aaaaaa\n"
  2189. "*/",
  2190. getLLVMStyleWithColumns(10)));
  2191. EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2192. " /* line 1\n"
  2193. " bbbbbbbbbbbb */\n"
  2194. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  2195. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2196. " /* line 1\n"
  2197. " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  2198. getLLVMStyleWithColumns(50)));
  2199. FormatStyle NoBinPacking = getLLVMStyle();
  2200. NoBinPacking.BinPackParameters = false;
  2201. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  2202. " 2, /* comment 2 */\n"
  2203. " 3, /* comment 3 */\n"
  2204. " aaaa,\n"
  2205. " bbbb);",
  2206. format("someFunction (1, /* comment 1 */\n"
  2207. " 2, /* comment 2 */ \n"
  2208. " 3, /* comment 3 */\n"
  2209. "aaaa, bbbb );",
  2210. NoBinPacking));
  2211. verifyFormat(
  2212. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2214. EXPECT_EQ(
  2215. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  2216. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2217. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  2218. format(
  2219. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  2220. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2221. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  2222. EXPECT_EQ(
  2223. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  2224. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  2225. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  2226. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  2227. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  2228. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  2229. verifyFormat("void f(int * /* unused */) {}");
  2230. EXPECT_EQ("/*\n"
  2231. " **\n"
  2232. " */",
  2233. format("/*\n"
  2234. " **\n"
  2235. " */"));
  2236. EXPECT_EQ("/*\n"
  2237. " *q\n"
  2238. " */",
  2239. format("/*\n"
  2240. " *q\n"
  2241. " */"));
  2242. EXPECT_EQ("/*\n"
  2243. " * q\n"
  2244. " */",
  2245. format("/*\n"
  2246. " * q\n"
  2247. " */"));
  2248. EXPECT_EQ("/*\n"
  2249. " **/",
  2250. format("/*\n"
  2251. " **/"));
  2252. EXPECT_EQ("/*\n"
  2253. " ***/",
  2254. format("/*\n"
  2255. " ***/"));
  2256. }
  2257. TEST_F(FormatTestComments, BlockCommentsInMacros) {
  2258. EXPECT_EQ("#define A \\\n"
  2259. " { \\\n"
  2260. " /* one line */ \\\n"
  2261. " someCall();",
  2262. format("#define A { \\\n"
  2263. " /* one line */ \\\n"
  2264. " someCall();",
  2265. getLLVMStyleWithColumns(20)));
  2266. EXPECT_EQ("#define A \\\n"
  2267. " { \\\n"
  2268. " /* previous */ \\\n"
  2269. " /* one line */ \\\n"
  2270. " someCall();",
  2271. format("#define A { \\\n"
  2272. " /* previous */ \\\n"
  2273. " /* one line */ \\\n"
  2274. " someCall();",
  2275. getLLVMStyleWithColumns(20)));
  2276. }
  2277. TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
  2278. EXPECT_EQ("a = {\n"
  2279. " 1111 /* */\n"
  2280. "};",
  2281. format("a = {1111 /* */\n"
  2282. "};",
  2283. getLLVMStyleWithColumns(15)));
  2284. EXPECT_EQ("a = {\n"
  2285. " 1111 /* */\n"
  2286. "};",
  2287. format("a = {1111 /* */\n"
  2288. "};",
  2289. getLLVMStyleWithColumns(15)));
  2290. EXPECT_EQ("a = {\n"
  2291. " 1111 /* a\n"
  2292. " */\n"
  2293. "};",
  2294. format("a = {1111 /* a */\n"
  2295. "};",
  2296. getLLVMStyleWithColumns(15)));
  2297. }
  2298. TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
  2299. EXPECT_EQ("a = f(/* long\n"
  2300. " long */\n"
  2301. " a);",
  2302. format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
  2303. EXPECT_EQ("a = f(\n"
  2304. " /* long\n"
  2305. " long */\n"
  2306. " a);",
  2307. format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
  2308. EXPECT_EQ("a = f(/* long\n"
  2309. " long\n"
  2310. " */\n"
  2311. " a);",
  2312. format("a = f(/* long\n"
  2313. " long\n"
  2314. " */a);",
  2315. getLLVMStyleWithColumns(16)));
  2316. EXPECT_EQ("a = f(/* long\n"
  2317. " long\n"
  2318. " */\n"
  2319. " a);",
  2320. format("a = f(/* long\n"
  2321. " long\n"
  2322. " */ a);",
  2323. getLLVMStyleWithColumns(16)));
  2324. EXPECT_EQ("a = f(/* long\n"
  2325. " long\n"
  2326. " */\n"
  2327. " (1 + 1));",
  2328. format("a = f(/* long\n"
  2329. " long\n"
  2330. " */ (1 + 1));",
  2331. getLLVMStyleWithColumns(16)));
  2332. EXPECT_EQ(
  2333. "a = f(a,\n"
  2334. " /* long\n"
  2335. " long */\n"
  2336. " b);",
  2337. format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
  2338. EXPECT_EQ(
  2339. "a = f(\n"
  2340. " a,\n"
  2341. " /* long\n"
  2342. " long */\n"
  2343. " b);",
  2344. format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
  2345. EXPECT_EQ("a = f(a,\n"
  2346. " /* long\n"
  2347. " long */\n"
  2348. " (1 + 1));",
  2349. format("a = f(a, /* long long */ (1 + 1));",
  2350. getLLVMStyleWithColumns(16)));
  2351. EXPECT_EQ("a = f(\n"
  2352. " a,\n"
  2353. " /* long\n"
  2354. " long */\n"
  2355. " (1 + 1));",
  2356. format("a = f(a, /* long long */ (1 + 1));",
  2357. getLLVMStyleWithColumns(15)));
  2358. }
  2359. TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  2360. verifyFormat("{\n"
  2361. " // a\n"
  2362. " // b");
  2363. }
  2364. TEST_F(FormatTestComments, AlignTrailingComments) {
  2365. EXPECT_EQ("#define MACRO(V) \\\n"
  2366. " V(Rt2) /* one more char */ \\\n"
  2367. " V(Rs) /* than here */ \\\n"
  2368. "/* comment 3 */\n",
  2369. format("#define MACRO(V)\\\n"
  2370. "V(Rt2) /* one more char */ \\\n"
  2371. "V(Rs) /* than here */ \\\n"
  2372. "/* comment 3 */\n",
  2373. getLLVMStyleWithColumns(40)));
  2374. EXPECT_EQ("int i = f(abc, // line 1\n"
  2375. " d, // line 2\n"
  2376. " // line 3\n"
  2377. " b);",
  2378. format("int i = f(abc, // line 1\n"
  2379. " d, // line 2\n"
  2380. " // line 3\n"
  2381. " b);",
  2382. getLLVMStyleWithColumns(40)));
  2383. // Align newly broken trailing comments.
  2384. EXPECT_EQ("int ab; // line\n"
  2385. "int a; // long\n"
  2386. " // long\n",
  2387. format("int ab; // line\n"
  2388. "int a; // long long\n",
  2389. getLLVMStyleWithColumns(15)));
  2390. EXPECT_EQ("int ab; // line\n"
  2391. "int a; // long\n"
  2392. " // long\n"
  2393. " // long",
  2394. format("int ab; // line\n"
  2395. "int a; // long long\n"
  2396. " // long",
  2397. getLLVMStyleWithColumns(15)));
  2398. EXPECT_EQ("int ab; // line\n"
  2399. "int a; // long\n"
  2400. " // long\n"
  2401. "pt c; // long",
  2402. format("int ab; // line\n"
  2403. "int a; // long long\n"
  2404. "pt c; // long",
  2405. getLLVMStyleWithColumns(15)));
  2406. EXPECT_EQ("int ab; // line\n"
  2407. "int a; // long\n"
  2408. " // long\n"
  2409. "\n"
  2410. "// long",
  2411. format("int ab; // line\n"
  2412. "int a; // long long\n"
  2413. "\n"
  2414. "// long",
  2415. getLLVMStyleWithColumns(15)));
  2416. // Don't align newly broken trailing comments if that would put them over the
  2417. // column limit.
  2418. EXPECT_EQ("int i, j; // line 1\n"
  2419. "int k; // line longg\n"
  2420. " // long",
  2421. format("int i, j; // line 1\n"
  2422. "int k; // line longg long",
  2423. getLLVMStyleWithColumns(20)));
  2424. // Always align if ColumnLimit = 0
  2425. EXPECT_EQ("int i, j; // line 1\n"
  2426. "int k; // line longg long",
  2427. format("int i, j; // line 1\n"
  2428. "int k; // line longg long",
  2429. getLLVMStyleWithColumns(0)));
  2430. // Align comment line sections aligned with the next token with the next
  2431. // token.
  2432. EXPECT_EQ("class A {\n"
  2433. "public: // public comment\n"
  2434. " // comment about a\n"
  2435. " int a;\n"
  2436. "};",
  2437. format("class A {\n"
  2438. "public: // public comment\n"
  2439. " // comment about a\n"
  2440. " int a;\n"
  2441. "};",
  2442. getLLVMStyleWithColumns(40)));
  2443. EXPECT_EQ("class A {\n"
  2444. "public: // public comment 1\n"
  2445. " // public comment 2\n"
  2446. " // comment 1 about a\n"
  2447. " // comment 2 about a\n"
  2448. " int a;\n"
  2449. "};",
  2450. format("class A {\n"
  2451. "public: // public comment 1\n"
  2452. " // public comment 2\n"
  2453. " // comment 1 about a\n"
  2454. " // comment 2 about a\n"
  2455. " int a;\n"
  2456. "};",
  2457. getLLVMStyleWithColumns(40)));
  2458. EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
  2459. " // comment line 2 on f\n"
  2460. " // comment line 1 before return\n"
  2461. " // comment line 2 before return\n"
  2462. " return n; // comment line 1 on return\n"
  2463. " // comment line 2 on return\n"
  2464. " // comment line 1 after return\n"
  2465. "}",
  2466. format("int f(int n) { // comment line 1 on f\n"
  2467. " // comment line 2 on f\n"
  2468. " // comment line 1 before return\n"
  2469. " // comment line 2 before return\n"
  2470. " return n; // comment line 1 on return\n"
  2471. " // comment line 2 on return\n"
  2472. " // comment line 1 after return\n"
  2473. "}",
  2474. getLLVMStyleWithColumns(40)));
  2475. EXPECT_EQ("int f(int n) {\n"
  2476. " switch (n) { // comment line 1 on switch\n"
  2477. " // comment line 2 on switch\n"
  2478. " // comment line 1 before case 1\n"
  2479. " // comment line 2 before case 1\n"
  2480. " case 1: // comment line 1 on case 1\n"
  2481. " // comment line 2 on case 1\n"
  2482. " // comment line 1 before return 1\n"
  2483. " // comment line 2 before return 1\n"
  2484. " return 1; // comment line 1 on return 1\n"
  2485. " // comment line 2 on return 1\n"
  2486. " // comment line 1 before default\n"
  2487. " // comment line 2 before default\n"
  2488. " default: // comment line 1 on default\n"
  2489. " // comment line 2 on default\n"
  2490. " // comment line 1 before return 2\n"
  2491. " return 2 * f(n - 1); // comment line 1 on return 2\n"
  2492. " // comment line 2 on return 2\n"
  2493. " // comment line 1 after return\n"
  2494. " // comment line 2 after return\n"
  2495. " }\n"
  2496. "}",
  2497. format("int f(int n) {\n"
  2498. " switch (n) { // comment line 1 on switch\n"
  2499. " // comment line 2 on switch\n"
  2500. " // comment line 1 before case 1\n"
  2501. " // comment line 2 before case 1\n"
  2502. " case 1: // comment line 1 on case 1\n"
  2503. " // comment line 2 on case 1\n"
  2504. " // comment line 1 before return 1\n"
  2505. " // comment line 2 before return 1\n"
  2506. " return 1; // comment line 1 on return 1\n"
  2507. " // comment line 2 on return 1\n"
  2508. " // comment line 1 before default\n"
  2509. " // comment line 2 before default\n"
  2510. " default: // comment line 1 on default\n"
  2511. " // comment line 2 on default\n"
  2512. " // comment line 1 before return 2\n"
  2513. " return 2 * f(n - 1); // comment line 1 on return 2\n"
  2514. " // comment line 2 on return 2\n"
  2515. " // comment line 1 after return\n"
  2516. " // comment line 2 after return\n"
  2517. " }\n"
  2518. "}",
  2519. getLLVMStyleWithColumns(80)));
  2520. // If all the lines in a sequence of line comments are aligned with the next
  2521. // token, the first line belongs to the previous token and the other lines
  2522. // belong to the next token.
  2523. EXPECT_EQ("int a; // line about a\n"
  2524. "long b;",
  2525. format("int a; // line about a\n"
  2526. " long b;",
  2527. getLLVMStyleWithColumns(80)));
  2528. EXPECT_EQ("int a; // line about a\n"
  2529. "// line about b\n"
  2530. "long b;",
  2531. format("int a; // line about a\n"
  2532. " // line about b\n"
  2533. " long b;",
  2534. getLLVMStyleWithColumns(80)));
  2535. EXPECT_EQ("int a; // line about a\n"
  2536. "// line 1 about b\n"
  2537. "// line 2 about b\n"
  2538. "long b;",
  2539. format("int a; // line about a\n"
  2540. " // line 1 about b\n"
  2541. " // line 2 about b\n"
  2542. " long b;",
  2543. getLLVMStyleWithColumns(80)));
  2544. }
  2545. TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
  2546. EXPECT_EQ("/*\n"
  2547. " */",
  2548. format("/*\n"
  2549. "*/", getLLVMStyle()));
  2550. EXPECT_EQ("/*\n"
  2551. " */",
  2552. format("/*\n"
  2553. " */", getLLVMStyle()));
  2554. EXPECT_EQ("/*\n"
  2555. " */",
  2556. format("/*\n"
  2557. " */", getLLVMStyle()));
  2558. // Align a single line.
  2559. EXPECT_EQ("/*\n"
  2560. " * line */",
  2561. format("/*\n"
  2562. "* line */",
  2563. getLLVMStyle()));
  2564. EXPECT_EQ("/*\n"
  2565. " * line */",
  2566. format("/*\n"
  2567. " * line */",
  2568. getLLVMStyle()));
  2569. EXPECT_EQ("/*\n"
  2570. " * line */",
  2571. format("/*\n"
  2572. " * line */",
  2573. getLLVMStyle()));
  2574. EXPECT_EQ("/*\n"
  2575. " * line */",
  2576. format("/*\n"
  2577. " * line */",
  2578. getLLVMStyle()));
  2579. EXPECT_EQ("/**\n"
  2580. " * line */",
  2581. format("/**\n"
  2582. "* line */",
  2583. getLLVMStyle()));
  2584. EXPECT_EQ("/**\n"
  2585. " * line */",
  2586. format("/**\n"
  2587. " * line */",
  2588. getLLVMStyle()));
  2589. EXPECT_EQ("/**\n"
  2590. " * line */",
  2591. format("/**\n"
  2592. " * line */",
  2593. getLLVMStyle()));
  2594. EXPECT_EQ("/**\n"
  2595. " * line */",
  2596. format("/**\n"
  2597. " * line */",
  2598. getLLVMStyle()));
  2599. EXPECT_EQ("/**\n"
  2600. " * line */",
  2601. format("/**\n"
  2602. " * line */",
  2603. getLLVMStyle()));
  2604. // Align the end '*/' after a line.
  2605. EXPECT_EQ("/*\n"
  2606. " * line\n"
  2607. " */",
  2608. format("/*\n"
  2609. "* line\n"
  2610. "*/", getLLVMStyle()));
  2611. EXPECT_EQ("/*\n"
  2612. " * line\n"
  2613. " */",
  2614. format("/*\n"
  2615. " * line\n"
  2616. " */", getLLVMStyle()));
  2617. EXPECT_EQ("/*\n"
  2618. " * line\n"
  2619. " */",
  2620. format("/*\n"
  2621. " * line\n"
  2622. " */", getLLVMStyle()));
  2623. // Align two lines.
  2624. EXPECT_EQ("/* line 1\n"
  2625. " * line 2 */",
  2626. format("/* line 1\n"
  2627. " * line 2 */",
  2628. getLLVMStyle()));
  2629. EXPECT_EQ("/* line 1\n"
  2630. " * line 2 */",
  2631. format("/* line 1\n"
  2632. "* line 2 */",
  2633. getLLVMStyle()));
  2634. EXPECT_EQ("/* line 1\n"
  2635. " * line 2 */",
  2636. format("/* line 1\n"
  2637. " * line 2 */",
  2638. getLLVMStyle()));
  2639. EXPECT_EQ("/* line 1\n"
  2640. " * line 2 */",
  2641. format("/* line 1\n"
  2642. " * line 2 */",
  2643. getLLVMStyle()));
  2644. EXPECT_EQ("/* line 1\n"
  2645. " * line 2 */",
  2646. format("/* line 1\n"
  2647. " * line 2 */",
  2648. getLLVMStyle()));
  2649. EXPECT_EQ("int i; /* line 1\n"
  2650. " * line 2 */",
  2651. format("int i; /* line 1\n"
  2652. "* line 2 */",
  2653. getLLVMStyle()));
  2654. EXPECT_EQ("int i; /* line 1\n"
  2655. " * line 2 */",
  2656. format("int i; /* line 1\n"
  2657. " * line 2 */",
  2658. getLLVMStyle()));
  2659. EXPECT_EQ("int i; /* line 1\n"
  2660. " * line 2 */",
  2661. format("int i; /* line 1\n"
  2662. " * line 2 */",
  2663. getLLVMStyle()));
  2664. // Align several lines.
  2665. EXPECT_EQ("/* line 1\n"
  2666. " * line 2\n"
  2667. " * line 3 */",
  2668. format("/* line 1\n"
  2669. " * line 2\n"
  2670. "* line 3 */",
  2671. getLLVMStyle()));
  2672. EXPECT_EQ("/* line 1\n"
  2673. " * line 2\n"
  2674. " * line 3 */",
  2675. format("/* line 1\n"
  2676. " * line 2\n"
  2677. "* line 3 */",
  2678. getLLVMStyle()));
  2679. EXPECT_EQ("/*\n"
  2680. "** line 1\n"
  2681. "** line 2\n"
  2682. "*/",
  2683. format("/*\n"
  2684. "** line 1\n"
  2685. " ** line 2\n"
  2686. "*/",
  2687. getLLVMStyle()));
  2688. // Align with different indent after the decorations.
  2689. EXPECT_EQ("/*\n"
  2690. " * line 1\n"
  2691. " * line 2\n"
  2692. " * line 3\n"
  2693. " * line 4\n"
  2694. " */",
  2695. format("/*\n"
  2696. "* line 1\n"
  2697. " * line 2\n"
  2698. " * line 3\n"
  2699. "* line 4\n"
  2700. "*/", getLLVMStyle()));
  2701. // Align empty or blank lines.
  2702. EXPECT_EQ("/**\n"
  2703. " *\n"
  2704. " *\n"
  2705. " *\n"
  2706. " */",
  2707. format("/**\n"
  2708. "* \n"
  2709. " * \n"
  2710. " *\n"
  2711. "*/", getLLVMStyle()));
  2712. // Align while breaking and reflowing.
  2713. EXPECT_EQ("/*\n"
  2714. " * long long long\n"
  2715. " * long long\n"
  2716. " *\n"
  2717. " * long */",
  2718. format("/*\n"
  2719. " * long long long long\n"
  2720. " * long\n"
  2721. " *\n"
  2722. "* long */",
  2723. getLLVMStyleWithColumns(20)));
  2724. }
  2725. TEST_F(FormatTestComments, NoCrush_Bug34236) {
  2726. // This is a test case from a crasher reported in:
  2727. // https://bugs.llvm.org/show_bug.cgi?id=34236
  2728. // Temporarily disable formatting for readability.
  2729. // clang-format off
  2730. EXPECT_EQ(
  2731. "/* */ /*\n"
  2732. " * a\n"
  2733. " * b c\n"
  2734. " * d*/",
  2735. format(
  2736. "/* */ /*\n"
  2737. " * a b\n"
  2738. " * c d*/",
  2739. getLLVMStyleWithColumns(80)));
  2740. // clang-format on
  2741. }
  2742. TEST_F(FormatTestComments, NonTrailingBlockComments) {
  2743. verifyFormat("const /** comment comment */ A = B;",
  2744. getLLVMStyleWithColumns(40));
  2745. verifyFormat("const /** comment comment comment */ A =\n"
  2746. " B;",
  2747. getLLVMStyleWithColumns(40));
  2748. EXPECT_EQ("const /** comment comment comment\n"
  2749. " comment */\n"
  2750. " A = B;",
  2751. format("const /** comment comment comment comment */\n"
  2752. " A = B;",
  2753. getLLVMStyleWithColumns(40)));
  2754. }
  2755. TEST_F(FormatTestComments, PythonStyleComments) {
  2756. // Keeps a space after '#'.
  2757. EXPECT_EQ("# comment\n"
  2758. "key: value",
  2759. format("#comment\n"
  2760. "key:value",
  2761. getTextProtoStyleWithColumns(20)));
  2762. EXPECT_EQ("# comment\n"
  2763. "key: value",
  2764. format("# comment\n"
  2765. "key:value",
  2766. getTextProtoStyleWithColumns(20)));
  2767. // Breaks long comment.
  2768. EXPECT_EQ("# comment comment\n"
  2769. "# comment\n"
  2770. "key: value",
  2771. format("# comment comment comment\n"
  2772. "key:value",
  2773. getTextProtoStyleWithColumns(20)));
  2774. // Indents comments.
  2775. EXPECT_EQ("data {\n"
  2776. " # comment comment\n"
  2777. " # comment\n"
  2778. " key: value\n"
  2779. "}",
  2780. format("data {\n"
  2781. "# comment comment comment\n"
  2782. "key: value}",
  2783. getTextProtoStyleWithColumns(20)));
  2784. EXPECT_EQ("data {\n"
  2785. " # comment comment\n"
  2786. " # comment\n"
  2787. " key: value\n"
  2788. "}",
  2789. format("data {# comment comment comment\n"
  2790. "key: value}",
  2791. getTextProtoStyleWithColumns(20)));
  2792. // Reflows long comments.
  2793. EXPECT_EQ("# comment comment\n"
  2794. "# comment comment\n"
  2795. "key: value",
  2796. format("# comment comment comment\n"
  2797. "# comment\n"
  2798. "key:value",
  2799. getTextProtoStyleWithColumns(20)));
  2800. // Breaks trailing comments.
  2801. EXPECT_EQ("k: val # comment\n"
  2802. " # comment\n"
  2803. "a: 1",
  2804. format("k:val#comment comment\n"
  2805. "a:1",
  2806. getTextProtoStyleWithColumns(20)));
  2807. EXPECT_EQ("id {\n"
  2808. " k: val # comment\n"
  2809. " # comment\n"
  2810. " # line line\n"
  2811. " a: 1\n"
  2812. "}",
  2813. format("id {k:val#comment comment\n"
  2814. "# line line\n"
  2815. "a:1}",
  2816. getTextProtoStyleWithColumns(20)));
  2817. // Aligns trailing comments.
  2818. EXPECT_EQ("k: val # commen1\n"
  2819. " # commen2\n"
  2820. " # commen3\n"
  2821. "# commen4\n"
  2822. "a: 1 # commen5\n"
  2823. " # commen6\n"
  2824. " # commen7",
  2825. format("k:val#commen1 commen2\n"
  2826. " # commen3\n"
  2827. "# commen4\n"
  2828. "a:1#commen5 commen6\n"
  2829. " #commen7",
  2830. getTextProtoStyleWithColumns(20)));
  2831. }
  2832. } // end namespace
  2833. } // end namespace format
  2834. } // end namespace clang