FormatTestComments.cpp 107 KB

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