12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110 |
- //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Format/Format.h"
- #include "../Tooling/ReplacementTest.h"
- #include "FormatTestUtils.h"
- #include "clang/Frontend/TextDiagnosticPrinter.h"
- #include "llvm/Support/Debug.h"
- #include "llvm/Support/MemoryBuffer.h"
- #include "gtest/gtest.h"
- #define DEBUG_TYPE "format-test"
- using clang::tooling::ReplacementTest;
- namespace clang {
- namespace format {
- namespace {
- FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
- class FormatTestComments : public ::testing::Test {
- protected:
- enum StatusCheck {
- SC_ExpectComplete,
- SC_ExpectIncomplete,
- SC_DoNotCheck
- };
- std::string format(llvm::StringRef Code,
- const FormatStyle &Style = getLLVMStyle(),
- StatusCheck CheckComplete = SC_ExpectComplete) {
- LLVM_DEBUG(llvm::errs() << "---\n");
- LLVM_DEBUG(llvm::errs() << Code << "\n\n");
- std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
- FormattingAttemptStatus Status;
- tooling::Replacements Replaces =
- reformat(Style, Code, Ranges, "<stdin>", &Status);
- if (CheckComplete != SC_DoNotCheck) {
- bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
- EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
- << Code << "\n\n";
- }
- ReplacementCount = Replaces.size();
- auto Result = applyAllReplacements(Code, Replaces);
- EXPECT_TRUE(static_cast<bool>(Result));
- LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
- return *Result;
- }
- FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
- FormatStyle Style = getLLVMStyle();
- Style.ColumnLimit = ColumnLimit;
- return Style;
- }
- FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) {
- FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto);
- Style.ColumnLimit = ColumnLimit;
- return Style;
- }
- void verifyFormat(llvm::StringRef Code,
- const FormatStyle &Style = getLLVMStyle()) {
- EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
- EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
- }
- void verifyGoogleFormat(llvm::StringRef Code) {
- verifyFormat(Code, getGoogleStyle());
- }
- /// \brief Verify that clang-format does not crash on the given input.
- void verifyNoCrash(llvm::StringRef Code,
- const FormatStyle &Style = getLLVMStyle()) {
- format(Code, Style, SC_DoNotCheck);
- }
- int ReplacementCount;
- };
- //===----------------------------------------------------------------------===//
- // Tests for comments.
- //===----------------------------------------------------------------------===//
- TEST_F(FormatTestComments, UnderstandsSingleLineComments) {
- verifyFormat("//* */");
- verifyFormat("// line 1\n"
- "// line 2\n"
- "void f() {}\n");
- verifyFormat("void f() {\n"
- " // Doesn't do anything\n"
- "}");
- verifyFormat("SomeObject\n"
- " // Calling someFunction on SomeObject\n"
- " .someFunction();");
- verifyFormat("auto result = SomeObject\n"
- " // Calling someFunction on SomeObject\n"
- " .someFunction();");
- verifyFormat("void f(int i, // some comment (probably for i)\n"
- " int j, // some comment (probably for j)\n"
- " int k); // some comment (probably for k)");
- verifyFormat("void f(int i,\n"
- " // some comment (probably for j)\n"
- " int j,\n"
- " // some comment (probably for k)\n"
- " int k);");
- verifyFormat("int i // This is a fancy variable\n"
- " = 5; // with nicely aligned comment.");
- verifyFormat("// Leading comment.\n"
- "int a; // Trailing comment.");
- verifyFormat("int a; // Trailing comment\n"
- " // on 2\n"
- " // or 3 lines.\n"
- "int b;");
- verifyFormat("int a; // Trailing comment\n"
- "\n"
- "// Leading comment.\n"
- "int b;");
- verifyFormat("int a; // Comment.\n"
- " // More details.\n"
- "int bbbb; // Another comment.");
- verifyFormat(
- "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
- "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
- "int cccccccccccccccccccccccccccccc; // comment\n"
- "int ddd; // looooooooooooooooooooooooong comment\n"
- "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
- "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
- "int ccccccccccccccccccc; // comment");
- verifyFormat("#include \"a\" // comment\n"
- "#include \"a/b/c\" // comment");
- verifyFormat("#include <a> // comment\n"
- "#include <a/b/c> // comment");
- EXPECT_EQ("#include \"a\" // comment\n"
- "#include \"a/b/c\" // comment",
- format("#include \\\n"
- " \"a\" // comment\n"
- "#include \"a/b/c\" // comment"));
- verifyFormat("enum E {\n"
- " // comment\n"
- " VAL_A, // comment\n"
- " VAL_B\n"
- "};");
- EXPECT_EQ("enum A {\n"
- " // line a\n"
- " a,\n"
- " b, // line b\n"
- "\n"
- " // line c\n"
- " c\n"
- "};",
- format("enum A {\n"
- " // line a\n"
- " a,\n"
- " b, // line b\n"
- "\n"
- " // line c\n"
- " c\n"
- "};",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- "};",
- format("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- "};",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- "};",
- format("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- "};",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- " b\n"
- "};",
- format("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- " b\n"
- "};",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- " b\n"
- "};",
- format("enum A {\n"
- " a, // line 1\n"
- " // line 2\n"
- " b\n"
- "};",
- getLLVMStyleWithColumns(20)));
- verifyFormat(
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
- verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " // Comment inside a statement.\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
- verifyFormat("SomeFunction(a,\n"
- " // comment\n"
- " b + x);");
- verifyFormat("SomeFunction(a, a,\n"
- " // comment\n"
- " b + x);");
- verifyFormat(
- "bool aaaaaaaaaaaaa = // comment\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
- verifyFormat("int aaaa; // aaaaa\n"
- "int aa; // aaaaaaa",
- getLLVMStyleWithColumns(20));
- EXPECT_EQ("void f() { // This does something ..\n"
- "}\n"
- "int a; // This is unrelated",
- format("void f() { // This does something ..\n"
- " }\n"
- "int a; // This is unrelated"));
- EXPECT_EQ("class C {\n"
- " void f() { // This does something ..\n"
- " } // awesome..\n"
- "\n"
- " int a; // This is unrelated\n"
- "};",
- format("class C{void f() { // This does something ..\n"
- " } // awesome..\n"
- " \n"
- "int a; // This is unrelated\n"
- "};"));
- EXPECT_EQ("int i; // single line trailing comment",
- format("int i;\\\n// single line trailing comment"));
- verifyGoogleFormat("int a; // Trailing comment.");
- verifyFormat("someFunction(anotherFunction( // Force break.\n"
- " parameter));");
- verifyGoogleFormat("#endif // HEADER_GUARD");
- verifyFormat("const char *test[] = {\n"
- " // A\n"
- " \"aaaa\",\n"
- " // B\n"
- " \"aaaaa\"};");
- verifyGoogleFormat(
- "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
- EXPECT_EQ("D(a, {\n"
- " // test\n"
- " int a;\n"
- "});",
- format("D(a, {\n"
- "// test\n"
- "int a;\n"
- "});"));
- EXPECT_EQ("lineWith(); // comment\n"
- "// at start\n"
- "otherLine();",
- format("lineWith(); // comment\n"
- "// at start\n"
- "otherLine();"));
- EXPECT_EQ("lineWith(); // comment\n"
- "/*\n"
- " * at start */\n"
- "otherLine();",
- format("lineWith(); // comment\n"
- "/*\n"
- " * at start */\n"
- "otherLine();"));
- EXPECT_EQ("lineWith(); // comment\n"
- " // at start\n"
- "otherLine();",
- format("lineWith(); // comment\n"
- " // at start\n"
- "otherLine();"));
- EXPECT_EQ("lineWith(); // comment\n"
- "// at start\n"
- "otherLine(); // comment",
- format("lineWith(); // comment\n"
- "// at start\n"
- "otherLine(); // comment"));
- EXPECT_EQ("lineWith();\n"
- "// at start\n"
- "otherLine(); // comment",
- format("lineWith();\n"
- " // at start\n"
- "otherLine(); // comment"));
- EXPECT_EQ("// first\n"
- "// at start\n"
- "otherLine(); // comment",
- format("// first\n"
- " // at start\n"
- "otherLine(); // comment"));
- EXPECT_EQ("f();\n"
- "// first\n"
- "// at start\n"
- "otherLine(); // comment",
- format("f();\n"
- "// first\n"
- " // at start\n"
- "otherLine(); // comment"));
- verifyFormat("f(); // comment\n"
- "// first\n"
- "// at start\n"
- "otherLine();");
- EXPECT_EQ("f(); // comment\n"
- "// first\n"
- "// at start\n"
- "otherLine();",
- format("f(); // comment\n"
- "// first\n"
- " // at start\n"
- "otherLine();"));
- EXPECT_EQ("f(); // comment\n"
- " // first\n"
- "// at start\n"
- "otherLine();",
- format("f(); // comment\n"
- " // first\n"
- "// at start\n"
- "otherLine();"));
- EXPECT_EQ("void f() {\n"
- " lineWith(); // comment\n"
- " // at start\n"
- "}",
- format("void f() {\n"
- " lineWith(); // comment\n"
- " // at start\n"
- "}"));
- EXPECT_EQ("int xy; // a\n"
- "int z; // b",
- format("int xy; // a\n"
- "int z; //b"));
- EXPECT_EQ("int xy; // a\n"
- "int z; // bb",
- format("int xy; // a\n"
- "int z; //bb",
- getLLVMStyleWithColumns(12)));
- verifyFormat("#define A \\\n"
- " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
- " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
- getLLVMStyleWithColumns(60));
- verifyFormat(
- "#define A \\\n"
- " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
- " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
- getLLVMStyleWithColumns(61));
- verifyFormat("if ( // This is some comment\n"
- " x + 3) {\n"
- "}");
- EXPECT_EQ("if ( // This is some comment\n"
- " // spanning two lines\n"
- " x + 3) {\n"
- "}",
- format("if( // This is some comment\n"
- " // spanning two lines\n"
- " x + 3) {\n"
- "}"));
- verifyNoCrash("/\\\n/");
- verifyNoCrash("/\\\n* */");
- // The 0-character somehow makes the lexer return a proper comment.
- verifyNoCrash(StringRef("/*\\\0\n/", 6));
- }
- TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
- EXPECT_EQ("SomeFunction(a,\n"
- " b, // comment\n"
- " c);",
- format("SomeFunction(a,\n"
- " b, // comment\n"
- " c);"));
- EXPECT_EQ("SomeFunction(a, b,\n"
- " // comment\n"
- " c);",
- format("SomeFunction(a,\n"
- " b,\n"
- " // comment\n"
- " c);"));
- EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
- " c);",
- format("SomeFunction(a, b, // comment (unclear relation)\n"
- " c);"));
- EXPECT_EQ("SomeFunction(a, // comment\n"
- " b,\n"
- " c); // comment",
- format("SomeFunction(a, // comment\n"
- " b,\n"
- " c); // comment"));
- EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
- " aaaa), //\n"
- " aaaa, bbbbb);",
- format("aaaaaaaaaa(aaaa(aaaa,\n"
- "aaaa), //\n"
- "aaaa, bbbbb);"));
- }
- TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) {
- EXPECT_EQ("// comment", format("// comment "));
- EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
- format("int aaaaaaa, bbbbbbb; // comment ",
- getLLVMStyleWithColumns(33)));
- EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
- EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
- }
- TEST_F(FormatTestComments, UnderstandsBlockComments) {
- verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
- verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
- EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbb);",
- format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
- "/* Trailing comment for aa... */\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
- EXPECT_EQ(
- "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
- format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
- "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
- EXPECT_EQ(
- "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
- "}",
- format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaa ,\n"
- " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
- "}"));
- verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
- FormatStyle NoBinPacking = getLLVMStyle();
- NoBinPacking.BinPackParameters = false;
- verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
- " /* parameter 2 */ aaaaaa,\n"
- " /* parameter 3 */ aaaaaa,\n"
- " /* parameter 4 */ aaaaaa);",
- NoBinPacking);
- // Aligning block comments in macros.
- verifyGoogleFormat("#define A \\\n"
- " int i; /*a*/ \\\n"
- " int jjj; /*b*/");
- }
- TEST_F(FormatTestComments, AlignsBlockComments) {
- EXPECT_EQ("/*\n"
- " * Really multi-line\n"
- " * comment.\n"
- " */\n"
- "void f() {}",
- format(" /*\n"
- " * Really multi-line\n"
- " * comment.\n"
- " */\n"
- " void f() {}"));
- EXPECT_EQ("class C {\n"
- " /*\n"
- " * Another multi-line\n"
- " * comment.\n"
- " */\n"
- " void f() {}\n"
- "};",
- format("class C {\n"
- "/*\n"
- " * Another multi-line\n"
- " * comment.\n"
- " */\n"
- "void f() {}\n"
- "};"));
- EXPECT_EQ("/*\n"
- " 1. This is a comment with non-trivial formatting.\n"
- " 1.1. We have to indent/outdent all lines equally\n"
- " 1.1.1. to keep the formatting.\n"
- " */",
- format(" /*\n"
- " 1. This is a comment with non-trivial formatting.\n"
- " 1.1. We have to indent/outdent all lines equally\n"
- " 1.1.1. to keep the formatting.\n"
- " */"));
- EXPECT_EQ("/*\n"
- "Don't try to outdent if there's not enough indentation.\n"
- "*/",
- format(" /*\n"
- " Don't try to outdent if there's not enough indentation.\n"
- " */"));
- EXPECT_EQ("int i; /* Comment with empty...\n"
- " *\n"
- " * line. */",
- format("int i; /* Comment with empty...\n"
- " *\n"
- " * line. */"));
- EXPECT_EQ("int foobar = 0; /* comment */\n"
- "int bar = 0; /* multiline\n"
- " comment 1 */\n"
- "int baz = 0; /* multiline\n"
- " comment 2 */\n"
- "int bzz = 0; /* multiline\n"
- " comment 3 */",
- format("int foobar = 0; /* comment */\n"
- "int bar = 0; /* multiline\n"
- " comment 1 */\n"
- "int baz = 0; /* multiline\n"
- " comment 2 */\n"
- "int bzz = 0; /* multiline\n"
- " comment 3 */"));
- EXPECT_EQ("int foobar = 0; /* comment */\n"
- "int bar = 0; /* multiline\n"
- " comment */\n"
- "int baz = 0; /* multiline\n"
- "comment */",
- format("int foobar = 0; /* comment */\n"
- "int bar = 0; /* multiline\n"
- "comment */\n"
- "int baz = 0; /* multiline\n"
- "comment */"));
- }
- TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) {
- FormatStyle Style = getLLVMStyleWithColumns(20);
- Style.ReflowComments = false;
- verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
- verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
- }
- TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) {
- EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
- format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
- EXPECT_EQ(
- "void ffffffffffff(\n"
- " int aaaaaaaa, int bbbbbbbb,\n"
- " int cccccccccccc) { /*\n"
- " aaaaaaaaaa\n"
- " aaaaaaaaaaaaa\n"
- " bbbbbbbbbbbbbb\n"
- " bbbbbbbbbb\n"
- " */\n"
- "}",
- format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
- "{ /*\n"
- " aaaaaaaaaa aaaaaaaaaaaaa\n"
- " bbbbbbbbbbbbbb bbbbbbbbbb\n"
- " */\n"
- "}",
- getLLVMStyleWithColumns(40)));
- }
- TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) {
- EXPECT_EQ("void ffffffffff(\n"
- " int aaaaa /* test */);",
- format("void ffffffffff(int aaaaa /* test */);",
- getLLVMStyleWithColumns(35)));
- }
- TEST_F(FormatTestComments, SplitsLongCxxComments) {
- EXPECT_EQ("// A comment that\n"
- "// doesn't fit on\n"
- "// one line",
- format("// A comment that doesn't fit on one line",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/// A comment that\n"
- "/// doesn't fit on\n"
- "/// one line",
- format("/// A comment that doesn't fit on one line",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//! A comment that\n"
- "//! doesn't fit on\n"
- "//! one line",
- format("//! A comment that doesn't fit on one line",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// a b c d\n"
- "// e f g\n"
- "// h i j k",
- format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
- EXPECT_EQ(
- "// a b c d\n"
- "// e f g\n"
- "// h i j k",
- format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
- EXPECT_EQ("if (true) // A comment that\n"
- " // doesn't fit on\n"
- " // one line",
- format("if (true) // A comment that doesn't fit on one line ",
- getLLVMStyleWithColumns(30)));
- EXPECT_EQ("// Don't_touch_leading_whitespace",
- format("// Don't_touch_leading_whitespace",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// Add leading\n"
- "// whitespace",
- format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/// Add leading\n"
- "/// whitespace",
- format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//! Add leading\n"
- "//! whitespace",
- format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
- EXPECT_EQ("// Even if it makes the line exceed the column\n"
- "// limit",
- format("//Even if it makes the line exceed the column limit",
- getLLVMStyleWithColumns(51)));
- EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
- EXPECT_EQ("/// line 1\n"
- "// add leading whitespace",
- format("/// line 1\n"
- "//add leading whitespace",
- getLLVMStyleWithColumns(30)));
- EXPECT_EQ("/// line 1\n"
- "/// line 2\n"
- "//! line 3\n"
- "//! line 4\n"
- "//! line 5\n"
- "// line 6\n"
- "// line 7",
- format("///line 1\n"
- "///line 2\n"
- "//! line 3\n"
- "//!line 4\n"
- "//!line 5\n"
- "// line 6\n"
- "//line 7", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// aa bb cc dd",
- format("// aa bb cc dd ",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("// A comment before\n"
- "// a macro\n"
- "// definition\n"
- "#define a b",
- format("// A comment before a macro definition\n"
- "#define a b",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("void ffffff(\n"
- " int aaaaaaaaa, // wwww\n"
- " int bbbbbbbbbb, // xxxxxxx\n"
- " // yyyyyyyyyy\n"
- " int c, int d, int e) {}",
- format("void ffffff(\n"
- " int aaaaaaaaa, // wwww\n"
- " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
- " int c, int d, int e) {}",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
- format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ(
- "#define XXX // a b c d\n"
- " // e f g h",
- format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
- EXPECT_EQ(
- "#define XXX // q w e r\n"
- " // t y u i",
- format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
- EXPECT_EQ("{\n"
- " //\n"
- " //\\\n"
- " // long 1 2 3 4 5\n"
- "}",
- format("{\n"
- " //\n"
- " //\\\n"
- " // long 1 2 3 4 5\n"
- "}",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("{\n"
- " //\n"
- " //\\\n"
- " // long 1 2 3 4 5\n"
- " // 6\n"
- "}",
- format("{\n"
- " //\n"
- " //\\\n"
- " // long 1 2 3 4 5 6\n"
- "}",
- getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) {
- EXPECT_EQ("// A comment\n"
- "// that doesn't\n"
- "// fit on one\n"
- "// line",
- format("// A comment that doesn't fit on one line",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/// A comment\n"
- "/// that doesn't\n"
- "/// fit on one\n"
- "/// line",
- format("/// A comment that doesn't fit on one line",
- getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) {
- EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
- "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
- "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
- format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
- "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
- "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
- EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
- format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
- getLLVMStyleWithColumns(50)));
- // FIXME: One day we might want to implement adjustment of leading whitespace
- // of the consecutive lines in this kind of comment:
- EXPECT_EQ("double\n"
- " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
- format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
- " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
- getLLVMStyleWithColumns(49)));
- }
- TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) {
- FormatStyle Pragmas = getLLVMStyleWithColumns(30);
- Pragmas.CommentPragmas = "^ IWYU pragma:";
- EXPECT_EQ(
- "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
- format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
- EXPECT_EQ(
- "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
- format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
- }
- TEST_F(FormatTestComments, PriorityOfCommentBreaking) {
- EXPECT_EQ("if (xxx ==\n"
- " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
- " zzz)\n"
- " q();",
- format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
- " zzz) q();",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("if (xxxxxxxxxx ==\n"
- " yyy && // aaaaaa bbbbbbbb cccc\n"
- " zzz)\n"
- " q();",
- format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
- " zzz) q();",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("if (xxxxxxxxxx &&\n"
- " yyy || // aaaaaa bbbbbbbb cccc\n"
- " zzz)\n"
- " q();",
- format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
- " zzz) q();",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("fffffffff(\n"
- " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
- " zzz);",
- format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
- " zzz);",
- getLLVMStyleWithColumns(40)));
- }
- TEST_F(FormatTestComments, MultiLineCommentsInDefines) {
- EXPECT_EQ("#define A(x) /* \\\n"
- " a comment \\\n"
- " inside */ \\\n"
- " f();",
- format("#define A(x) /* \\\n"
- " a comment \\\n"
- " inside */ \\\n"
- " f();",
- getLLVMStyleWithColumns(17)));
- EXPECT_EQ("#define A( \\\n"
- " x) /* \\\n"
- " a comment \\\n"
- " inside */ \\\n"
- " f();",
- format("#define A( \\\n"
- " x) /* \\\n"
- " a comment \\\n"
- " inside */ \\\n"
- " f();",
- getLLVMStyleWithColumns(17)));
- }
- TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
- EXPECT_EQ("namespace {}\n// Test\n#define A",
- format("namespace {}\n // Test\n#define A"));
- EXPECT_EQ("namespace {}\n/* Test */\n#define A",
- format("namespace {}\n /* Test */\n#define A"));
- EXPECT_EQ("namespace {}\n/* Test */ #define A",
- format("namespace {}\n /* Test */ #define A"));
- }
- TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
- // Keep the current level if the comment was originally not aligned with
- // the preprocessor directive.
- EXPECT_EQ("void f() {\n"
- " int i;\n"
- " /* comment */\n"
- "#ifdef A\n"
- " int j;\n"
- "}",
- format("void f() {\n"
- " int i;\n"
- " /* comment */\n"
- "#ifdef A\n"
- " int j;\n"
- "}"));
- EXPECT_EQ("void f() {\n"
- " int i;\n"
- " /* comment */\n"
- "\n"
- "#ifdef A\n"
- " int j;\n"
- "}",
- format("void f() {\n"
- " int i;\n"
- " /* comment */\n"
- "\n"
- "#ifdef A\n"
- " int j;\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " ++i;\n"
- " }\n"
- " // comment\n"
- "#ifdef A\n"
- " int j;\n"
- "#endif\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " ++i;\n"
- " }\n"
- " // comment\n"
- "#ifdef A\n"
- "int j;\n"
- "#endif\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " // comment in else\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " // comment in else\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " /* comment in else */\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " /* comment in else */\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}"));
- // Keep the current level if there is an empty line between the comment and
- // the preprocessor directive.
- EXPECT_EQ("void f() {\n"
- " int i;\n"
- " /* comment */\n"
- "\n"
- "#ifdef A\n"
- " int j;\n"
- "}",
- format("void f() {\n"
- " int i;\n"
- "/* comment */\n"
- "\n"
- "#ifdef A\n"
- " int j;\n"
- "}"));
- EXPECT_EQ("void f() {\n"
- " int i;\n"
- " return i;\n"
- "}\n"
- "// comment\n"
- "\n"
- "#ifdef A\n"
- "int i;\n"
- "#endif // A",
- format("void f() {\n"
- " int i;\n"
- " return i;\n"
- "}\n"
- "// comment\n"
- "\n"
- "#ifdef A\n"
- "int i;\n"
- "#endif // A"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " ++i;\n"
- " }\n"
- " // comment\n"
- "\n"
- "#ifdef A\n"
- " int j;\n"
- "#endif\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " ++i;\n"
- " }\n"
- " // comment\n"
- "\n"
- "#ifdef A\n"
- " int j;\n"
- "#endif\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " // comment in else\n"
- "\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- "// comment in else\n"
- "\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " /* comment in else */\n"
- "\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- "/* comment in else */\n"
- "\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}"));
- // Align with the preprocessor directive if the comment was originally aligned
- // with the preprocessor directive and there is no newline between the comment
- // and the preprocessor directive.
- EXPECT_EQ("void f() {\n"
- " int i;\n"
- "/* comment */\n"
- "#ifdef A\n"
- " int j;\n"
- "}",
- format("void f() {\n"
- " int i;\n"
- "/* comment */\n"
- "#ifdef A\n"
- " int j;\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " ++i;\n"
- " }\n"
- "// comment\n"
- "#ifdef A\n"
- " int j;\n"
- "#endif\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " ++i;\n"
- " }\n"
- "// comment\n"
- "#ifdef A\n"
- " int j;\n"
- "#endif\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- "// comment in else\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " // comment in else\n"
- " #ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}"));
- EXPECT_EQ("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- "/* comment in else */\n"
- "#ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}",
- format("int f(int i) {\n"
- " if (true) {\n"
- " i++;\n"
- " } else {\n"
- " /* comment in else */\n"
- " #ifdef A\n"
- " j++;\n"
- "#endif\n"
- " }\n"
- "}"));
- }
- TEST_F(FormatTestComments, SplitsLongLinesInComments) {
- // FIXME: Do we need to fix up the " */" at the end?
- // It doesn't look like any of our current logic triggers this.
- EXPECT_EQ("/* This is a long\n"
- " * comment that\n"
- " * doesn't fit on\n"
- " * one line. */",
- format("/* "
- "This is a long "
- "comment that "
- "doesn't "
- "fit on one line. */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ(
- "/* a b c d\n"
- " * e f g\n"
- " * h i j k\n"
- " */",
- format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
- EXPECT_EQ(
- "/* a b c d\n"
- " * e f g\n"
- " * h i j k\n"
- " */",
- format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
- EXPECT_EQ("/*\n"
- "This is a long\n"
- "comment that doesn't\n"
- "fit on one line.\n"
- "*/",
- format("/*\n"
- "This is a long "
- "comment that doesn't "
- "fit on one line. \n"
- "*/",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/*\n"
- " * This is a long\n"
- " * comment that\n"
- " * doesn't fit on\n"
- " * one line.\n"
- " */",
- format("/* \n"
- " * This is a long "
- " comment that "
- " doesn't fit on "
- " one line. \n"
- " */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/*\n"
- " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
- " * so_it_should_be_broken\n"
- " * wherever_a_space_occurs\n"
- " */",
- format("/*\n"
- " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
- " so_it_should_be_broken "
- " wherever_a_space_occurs \n"
- " */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/*\n"
- " * This_comment_can_not_be_broken_into_lines\n"
- " */",
- format("/*\n"
- " * This_comment_can_not_be_broken_into_lines\n"
- " */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("{\n"
- " /*\n"
- " This is another\n"
- " long comment that\n"
- " doesn't fit on one\n"
- " line 1234567890\n"
- " */\n"
- "}",
- format("{\n"
- "/*\n"
- "This is another "
- " long comment that "
- " doesn't fit on one"
- " line 1234567890\n"
- "*/\n"
- "}",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("{\n"
- " /*\n"
- " * This i s\n"
- " * another comment\n"
- " * t hat doesn' t\n"
- " * fit on one l i\n"
- " * n e\n"
- " */\n"
- "}",
- format("{\n"
- "/*\n"
- " * This i s"
- " another comment"
- " t hat doesn' t"
- " fit on one l i"
- " n e\n"
- " */\n"
- "}",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/*\n"
- " * This is a long\n"
- " * comment that\n"
- " * doesn't fit on\n"
- " * one line\n"
- " */",
- format(" /*\n"
- " * This is a long comment that doesn't fit on one line\n"
- " */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("{\n"
- " if (something) /* This is a\n"
- " long\n"
- " comment */\n"
- " ;\n"
- "}",
- format("{\n"
- " if (something) /* This is a long comment */\n"
- " ;\n"
- "}",
- getLLVMStyleWithColumns(30)));
- EXPECT_EQ("/* A comment before\n"
- " * a macro\n"
- " * definition */\n"
- "#define a b",
- format("/* A comment before a macro definition */\n"
- "#define a b",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* some comment\n"
- " * a comment that\n"
- " * we break another\n"
- " * comment we have\n"
- " * to break a left\n"
- " * comment\n"
- " */",
- format(" /* some comment\n"
- " * a comment that we break\n"
- " * another comment we have to break\n"
- "* a left comment\n"
- " */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/**\n"
- " * multiline block\n"
- " * comment\n"
- " *\n"
- " */",
- format("/**\n"
- " * multiline block comment\n"
- " *\n"
- " */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/*\n"
- "\n"
- "\n"
- " */\n",
- format(" /* \n"
- " \n"
- " \n"
- " */\n"));
- EXPECT_EQ("/* a a */",
- format("/* a a */", getLLVMStyleWithColumns(15)));
- EXPECT_EQ("/* a a bc */",
- format("/* a a bc */", getLLVMStyleWithColumns(15)));
- EXPECT_EQ("/* aaa aaa\n"
- " * aaaaa */",
- format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
- EXPECT_EQ("/* aaa aaa\n"
- " * aaaaa */",
- format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
- }
- TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) {
- EXPECT_EQ("#define X \\\n"
- " /* \\\n"
- " Test \\\n"
- " Macro comment \\\n"
- " with a long \\\n"
- " line \\\n"
- " */ \\\n"
- " A + B",
- format("#define X \\\n"
- " /*\n"
- " Test\n"
- " Macro comment with a long line\n"
- " */ \\\n"
- " A + B",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("#define X \\\n"
- " /* Macro comment \\\n"
- " with a long \\\n"
- " line */ \\\n"
- " A + B",
- format("#define X \\\n"
- " /* Macro comment with a long\n"
- " line */ \\\n"
- " A + B",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("#define X \\\n"
- " /* Macro comment \\\n"
- " * with a long \\\n"
- " * line */ \\\n"
- " A + B",
- format("#define X \\\n"
- " /* Macro comment with a long line */ \\\n"
- " A + B",
- getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
- verifyFormat("#ifdef A // line about A\n"
- "// section comment\n"
- "#endif",
- getLLVMStyleWithColumns(80));
- verifyFormat("#ifdef A // line 1 about A\n"
- " // line 2 about A\n"
- "// section comment\n"
- "#endif",
- getLLVMStyleWithColumns(80));
- EXPECT_EQ("#ifdef A // line 1 about A\n"
- " // line 2 about A\n"
- "// section comment\n"
- "#endif",
- format("#ifdef A // line 1 about A\n"
- " // line 2 about A\n"
- "// section comment\n"
- "#endif",
- getLLVMStyleWithColumns(80)));
- verifyFormat("int f() {\n"
- " int i;\n"
- "#ifdef A // comment about A\n"
- " // section comment 1\n"
- " // section comment 2\n"
- " i = 2;\n"
- "#else // comment about #else\n"
- " // section comment 3\n"
- " i = 4;\n"
- "#endif\n"
- "}", getLLVMStyleWithColumns(80));
- }
- TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
- verifyFormat("#if A\n"
- "#else // A\n"
- "int iiii;\n"
- "#endif // B",
- getLLVMStyleWithColumns(20));
- verifyFormat("#if A\n"
- "#else // A\n"
- "int iiii; // CC\n"
- "#endif // B",
- getLLVMStyleWithColumns(20));
- EXPECT_EQ("#if A\n"
- "#else // A1\n"
- " // A2\n"
- "int ii;\n"
- "#endif // B",
- format("#if A\n"
- "#else // A1\n"
- " // A2\n"
- "int ii;\n"
- "#endif // B",
- getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, CommentsInStaticInitializers) {
- EXPECT_EQ(
- "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
- " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
- " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaa, // comment\n"
- " aaaaaaaaaaaaaaaaaaaa};",
- format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
- " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
- " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
- " aaaaaaaaaaaaaaaaaaaa , // comment\n"
- " aaaaaaaaaaaaaaaaaaaa };"));
- verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
- " bbbbbbbbbbb, ccccccccccc};");
- verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
- " // comment for bb....\n"
- " bbbbbbbbbbb, ccccccccccc};");
- verifyGoogleFormat(
- "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
- " bbbbbbbbbbb, ccccccccccc};");
- verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
- " // comment for bb....\n"
- " bbbbbbbbbbb, ccccccccccc};");
- verifyFormat("S s = {{a, b, c}, // Group #1\n"
- " {d, e, f}, // Group #2\n"
- " {g, h, i}}; // Group #3");
- verifyFormat("S s = {{// Group #1\n"
- " a, b, c},\n"
- " {// Group #2\n"
- " d, e, f},\n"
- " {// Group #3\n"
- " g, h, i}};");
- EXPECT_EQ("S s = {\n"
- " // Some comment\n"
- " a,\n"
- "\n"
- " // Comment after empty line\n"
- " b}",
- format("S s = {\n"
- " // Some comment\n"
- " a,\n"
- " \n"
- " // Comment after empty line\n"
- " b\n"
- "}"));
- EXPECT_EQ("S s = {\n"
- " /* Some comment */\n"
- " a,\n"
- "\n"
- " /* Comment after empty line */\n"
- " b}",
- format("S s = {\n"
- " /* Some comment */\n"
- " a,\n"
- " \n"
- " /* Comment after empty line */\n"
- " b\n"
- "}"));
- verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
- " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
- " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
- " 0x00, 0x00, 0x00, 0x00}; // comment\n");
- }
- TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
- EXPECT_EQ("if (true) { // comment about branch\n"
- " // comment about f\n"
- " f();\n"
- "}",
- format("if (true) { // comment about branch\n"
- " // comment about f\n"
- " f();\n"
- "}",
- getLLVMStyleWithColumns(80)));
- EXPECT_EQ("if (1) { // if line 1\n"
- " // if line 2\n"
- " // if line 3\n"
- " // f line 1\n"
- " // f line 2\n"
- " f();\n"
- "} else { // else line 1\n"
- " // else line 2\n"
- " // else line 3\n"
- " // g line 1\n"
- " g();\n"
- "}",
- format("if (1) { // if line 1\n"
- " // if line 2\n"
- " // if line 3\n"
- " // f line 1\n"
- " // f line 2\n"
- " f();\n"
- "} else { // else line 1\n"
- " // else line 2\n"
- " // else line 3\n"
- " // g line 1\n"
- " g();\n"
- "}"));
- EXPECT_EQ("do { // line 1\n"
- " // line 2\n"
- " // line 3\n"
- " f();\n"
- "} while (true);",
- format("do { // line 1\n"
- " // line 2\n"
- " // line 3\n"
- " f();\n"
- "} while (true);",
- getLLVMStyleWithColumns(80)));
- EXPECT_EQ("while (a < b) { // line 1\n"
- " // line 2\n"
- " // line 3\n"
- " f();\n"
- "}",
- format("while (a < b) {// line 1\n"
- " // line 2\n"
- " // line 3\n"
- " f();\n"
- "}",
- getLLVMStyleWithColumns(80)));
- }
- TEST_F(FormatTestComments, ReflowsComments) {
- // Break a long line and reflow with the full next line.
- EXPECT_EQ("// long long long\n"
- "// long long",
- format("// long long long long\n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Keep the trailing newline while reflowing.
- EXPECT_EQ("// long long long\n"
- "// long long\n",
- format("// long long long long\n"
- "// long\n",
- getLLVMStyleWithColumns(20)));
- // Break a long line and reflow with a part of the next line.
- EXPECT_EQ("// long long long\n"
- "// long long\n"
- "// long_long",
- format("// long long long long\n"
- "// long long_long",
- getLLVMStyleWithColumns(20)));
- // Break but do not reflow if the first word from the next line is too long.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// long_long_long\n",
- format("// long long long long\n"
- "// long_long_long\n",
- getLLVMStyleWithColumns(20)));
- // Don't break or reflow short lines.
- verifyFormat("// long\n"
- "// long long long lo\n"
- "// long long long lo\n"
- "// long",
- getLLVMStyleWithColumns(20));
- // Keep prefixes and decorations while reflowing.
- EXPECT_EQ("/// long long long\n"
- "/// long long\n",
- format("/// long long long long\n"
- "/// long\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//! long long long\n"
- "//! long long\n",
- format("//! long long long long\n"
- "//! long\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* long long long\n"
- " * long long */",
- format("/* long long long long\n"
- " * long */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("///< long long long\n"
- "///< long long\n",
- format("///< long long long long\n"
- "///< long\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//!< long long long\n"
- "//!< long long\n",
- format("//!< long long long long\n"
- "//!< long\n",
- getLLVMStyleWithColumns(20)));
- // Don't bring leading whitespace up while reflowing.
- EXPECT_EQ("/* long long long\n"
- " * long long long\n"
- " */",
- format("/* long long long long\n"
- " * long long\n"
- " */",
- getLLVMStyleWithColumns(20)));
- // Reflow the last line of a block comment with its trailing '*/'.
- EXPECT_EQ("/* long long long\n"
- " long long */",
- format("/* long long long long\n"
- " long */",
- getLLVMStyleWithColumns(20)));
- // Reflow two short lines; keep the postfix of the last one.
- EXPECT_EQ("/* long long long\n"
- " * long long long */",
- format("/* long long long long\n"
- " * long\n"
- " * long */",
- getLLVMStyleWithColumns(20)));
- // Put the postfix of the last short reflow line on a newline if it doesn't
- // fit.
- EXPECT_EQ("/* long long long\n"
- " * long long longg\n"
- " */",
- format("/* long long long long\n"
- " * long\n"
- " * longg */",
- getLLVMStyleWithColumns(20)));
- // Reflow lines with leading whitespace.
- EXPECT_EQ("{\n"
- " /*\n"
- " * long long long\n"
- " * long long long\n"
- " * long long long\n"
- " */\n"
- "}",
- format("{\n"
- "/*\n"
- " * long long long long\n"
- " * long\n"
- " * long long long long\n"
- " */\n"
- "}",
- getLLVMStyleWithColumns(20)));
- // Break single line block comments that are first in the line with ' *'
- // decoration.
- EXPECT_EQ("/* long long long\n"
- " * long */",
- format("/* long long long long */", getLLVMStyleWithColumns(20)));
- // Break single line block comment that are not first in the line with ' '
- // decoration.
- EXPECT_EQ("int i; /* long long\n"
- " long */",
- format("int i; /* long long long */", getLLVMStyleWithColumns(20)));
- // Reflow a line that goes just over the column limit.
- EXPECT_EQ("// long long long\n"
- "// lon long",
- format("// long long long lon\n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Stop reflowing if the next line has a different indentation than the
- // previous line.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// long long\n"
- "// long",
- format("// long long long long\n"
- "// long long\n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Reflow into the last part of a really long line that has been broken into
- // multiple lines.
- EXPECT_EQ("// long long long\n"
- "// long long long\n"
- "// long long long\n",
- format("// long long long long long long long long\n"
- "// long\n",
- getLLVMStyleWithColumns(20)));
- // Break the first line, then reflow the beginning of the second and third
- // line up.
- EXPECT_EQ("// long long long\n"
- "// lon1 lon2 lon2\n"
- "// lon2 lon3 lon3",
- format("// long long long lon1\n"
- "// lon2 lon2 lon2\n"
- "// lon3 lon3",
- getLLVMStyleWithColumns(20)));
- // Reflow the beginning of the second line, then break the rest.
- EXPECT_EQ("// long long long\n"
- "// lon1 lon2 lon2\n"
- "// lon2 lon2 lon2\n"
- "// lon3",
- format("// long long long lon1\n"
- "// lon2 lon2 lon2 lon2 lon2 lon3",
- getLLVMStyleWithColumns(20)));
- // Shrink the first line, then reflow the second line up.
- EXPECT_EQ("// long long long", format("// long long\n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Don't shrink leading whitespace.
- EXPECT_EQ("int i; /// a",
- format("int i; /// a", getLLVMStyleWithColumns(20)));
- // Shrink trailing whitespace if there is no postfix and reflow.
- EXPECT_EQ("// long long long\n"
- "// long long",
- format("// long long long long \n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Shrink trailing whitespace to a single one if there is postfix.
- EXPECT_EQ("/* long long long */",
- format("/* long long long */", getLLVMStyleWithColumns(20)));
- // Break a block comment postfix if exceeding the line limit.
- EXPECT_EQ("/* long\n"
- " */",
- format("/* long */", getLLVMStyleWithColumns(20)));
- // Reflow indented comments.
- EXPECT_EQ("{\n"
- " // long long long\n"
- " // long long\n"
- " int i; /* long lon\n"
- " g long\n"
- " */\n"
- "}",
- format("{\n"
- " // long long long long\n"
- " // long\n"
- " int i; /* long lon g\n"
- " long */\n"
- "}",
- getLLVMStyleWithColumns(20)));
- // Don't realign trailing comments after reflow has happened.
- EXPECT_EQ("// long long long\n"
- "// long long\n"
- "long i; // long",
- format("// long long long long\n"
- "// long\n"
- "long i; // long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// long long long\n"
- "// longng long long\n"
- "// long lo",
- format("// long long long longng\n"
- "// long long long\n"
- "// lo",
- getLLVMStyleWithColumns(20)));
- // Reflow lines after a broken line.
- EXPECT_EQ("int a; // Trailing\n"
- " // comment on\n"
- " // 2 or 3\n"
- " // lines.\n",
- format("int a; // Trailing comment\n"
- " // on 2\n"
- " // or 3\n"
- " // lines.\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/// This long line\n"
- "/// gets reflown.\n",
- format("/// This long line gets\n"
- "/// reflown.\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//! This long line\n"
- "//! gets reflown.\n",
- format(" //! This long line gets\n"
- " //! reflown.\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* This long line\n"
- " * gets reflown.\n"
- " */\n",
- format("/* This long line gets\n"
- " * reflown.\n"
- " */\n",
- getLLVMStyleWithColumns(20)));
- // Reflow after indentation makes a line too long.
- EXPECT_EQ("{\n"
- " // long long long\n"
- " // lo long\n"
- "}\n",
- format("{\n"
- "// long long long lo\n"
- "// long\n"
- "}\n",
- getLLVMStyleWithColumns(20)));
- // Break and reflow multiple lines.
- EXPECT_EQ("/*\n"
- " * Reflow the end of\n"
- " * line by 11 22 33\n"
- " * 4.\n"
- " */\n",
- format("/*\n"
- " * Reflow the end of line\n"
- " * by\n"
- " * 11\n"
- " * 22\n"
- " * 33\n"
- " * 4.\n"
- " */\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/// First line gets\n"
- "/// broken. Second\n"
- "/// line gets\n"
- "/// reflown and\n"
- "/// broken. Third\n"
- "/// gets reflown.\n",
- format("/// First line gets broken.\n"
- "/// Second line gets reflown and broken.\n"
- "/// Third gets reflown.\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("int i; // first long\n"
- " // long snd\n"
- " // long.\n",
- format("int i; // first long long\n"
- " // snd long.\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("{\n"
- " // first long line\n"
- " // line second\n"
- " // long line line\n"
- " // third long line\n"
- " // line\n"
- "}\n",
- format("{\n"
- " // first long line line\n"
- " // second long line line\n"
- " // third long line line\n"
- "}\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("int i; /* first line\n"
- " * second\n"
- " * line third\n"
- " * line\n"
- " */",
- format("int i; /* first line\n"
- " * second line\n"
- " * third line\n"
- " */",
- getLLVMStyleWithColumns(20)));
- // Reflow the last two lines of a section that starts with a line having
- // different indentation.
- EXPECT_EQ(
- "// long\n"
- "// long long long\n"
- "// long long",
- format("// long\n"
- "// long long long long\n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Keep the block comment endling '*/' while reflowing.
- EXPECT_EQ("/* Long long long\n"
- " * line short */\n",
- format("/* Long long long line\n"
- " * short */\n",
- getLLVMStyleWithColumns(20)));
- // Don't reflow between separate blocks of comments.
- EXPECT_EQ("/* First comment\n"
- " * block will */\n"
- "/* Snd\n"
- " */\n",
- format("/* First comment block\n"
- " * will */\n"
- "/* Snd\n"
- " */\n",
- getLLVMStyleWithColumns(20)));
- // Don't reflow across blank comment lines.
- EXPECT_EQ("int i; // This long\n"
- " // line gets\n"
- " // broken.\n"
- " //\n"
- " // keep.\n",
- format("int i; // This long line gets broken.\n"
- " // \n"
- " // keep.\n",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("{\n"
- " /// long long long\n"
- " /// long long\n"
- " ///\n"
- " /// long\n"
- "}",
- format("{\n"
- " /// long long long long\n"
- " /// long\n"
- " ///\n"
- " /// long\n"
- "}",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//! long long long\n"
- "//! long\n"
- "\n"
- "//! long",
- format("//! long long long long\n"
- "\n"
- "//! long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* long long long\n"
- " long\n"
- "\n"
- " long */",
- format("/* long long long long\n"
- "\n"
- " long */",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* long long long\n"
- " * long\n"
- " *\n"
- " * long */",
- format("/* long long long long\n"
- " *\n"
- " * long */",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines having content that is a single character.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// l",
- format("// long long long long\n"
- "// l",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines starting with two punctuation characters.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// ... --- ...",
- format(
- "// long long long long\n"
- "// ... --- ...",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines starting with '@'.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// @param arg",
- format("// long long long long\n"
- "// @param arg",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines starting with 'TODO'.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// TODO: long",
- format("// long long long long\n"
- "// TODO: long",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines starting with 'FIXME'.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// FIXME: long",
- format("// long long long long\n"
- "// FIXME: long",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines starting with 'XXX'.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// XXX: long",
- format("// long long long long\n"
- "// XXX: long",
- getLLVMStyleWithColumns(20)));
- // Don't reflow comment pragmas.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// IWYU pragma:",
- format("// long long long long\n"
- "// IWYU pragma:",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* long long long\n"
- " * long\n"
- " * IWYU pragma:\n"
- " */",
- format("/* long long long long\n"
- " * IWYU pragma:\n"
- " */",
- getLLVMStyleWithColumns(20)));
- // Reflow lines that have a non-punctuation character among their first 2
- // characters.
- EXPECT_EQ("// long long long\n"
- "// long 'long'",
- format(
- "// long long long long\n"
- "// 'long'",
- getLLVMStyleWithColumns(20)));
- // Don't reflow between separate blocks of comments.
- EXPECT_EQ("/* First comment\n"
- " * block will */\n"
- "/* Snd\n"
- " */\n",
- format("/* First comment block\n"
- " * will */\n"
- "/* Snd\n"
- " */\n",
- getLLVMStyleWithColumns(20)));
- // Don't reflow lines having different indentation.
- EXPECT_EQ("// long long long\n"
- "// long\n"
- "// long",
- format("// long long long long\n"
- "// long",
- getLLVMStyleWithColumns(20)));
- // Don't reflow separate bullets in list
- EXPECT_EQ("// - long long long\n"
- "// long\n"
- "// - long",
- format("// - long long long long\n"
- "// - long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// * long long long\n"
- "// long\n"
- "// * long",
- format("// * long long long long\n"
- "// * long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// + long long long\n"
- "// long\n"
- "// + long",
- format("// + long long long long\n"
- "// + long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// 1. long long long\n"
- "// long\n"
- "// 2. long",
- format("// 1. long long long long\n"
- "// 2. long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// -# long long long\n"
- "// long\n"
- "// -# long",
- format("// -# long long long long\n"
- "// -# long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// - long long long\n"
- "// long long long\n"
- "// - long",
- format("// - long long long long\n"
- "// long long\n"
- "// - long",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("// - long long long\n"
- "// long long long\n"
- "// long\n"
- "// - long",
- format("// - long long long long\n"
- "// long long long\n"
- "// - long",
- getLLVMStyleWithColumns(20)));
- // Large number (>2 digits) are not list items
- EXPECT_EQ("// long long long\n"
- "// long 1024. long.",
- format("// long long long long\n"
- "// 1024. long.",
- getLLVMStyleWithColumns(20)));
- // Do not break before number, to avoid introducing a non-reflowable doxygen
- // list item.
- EXPECT_EQ("// long long\n"
- "// long 10. long.",
- format("// long long long 10.\n"
- "// long.",
- getLLVMStyleWithColumns(20)));
- // Don't break or reflow after implicit string literals.
- verifyFormat("#include <t> // l l l\n"
- " // l",
- getLLVMStyleWithColumns(20));
- // Don't break or reflow comments on import lines.
- EXPECT_EQ("#include \"t\" /* l l l\n"
- " * l */",
- format("#include \"t\" /* l l l\n"
- " * l */",
- getLLVMStyleWithColumns(20)));
- // Don't reflow between different trailing comment sections.
- EXPECT_EQ("int i; // long long\n"
- " // long\n"
- "int j; // long long\n"
- " // long\n",
- format("int i; // long long long\n"
- "int j; // long long long\n",
- getLLVMStyleWithColumns(20)));
- // Don't reflow if the first word on the next line is longer than the
- // available space at current line.
- EXPECT_EQ("int i; // trigger\n"
- " // reflow\n"
- " // longsec\n",
- format("int i; // trigger reflow\n"
- " // longsec\n",
- getLLVMStyleWithColumns(20)));
- // Simple case that correctly handles reflow in parameter lists.
- EXPECT_EQ("a = f(/* looooooooong\n"
- " * long long\n"
- " */\n"
- " a);",
- format("a = f(/* looooooooong long\n* long\n*/ a);",
- getLLVMStyleWithColumns(22)));
- // Tricky case that has fewer lines if we reflow the comment, ending up with
- // fewer lines.
- EXPECT_EQ("a = f(/* loooooong\n"
- " * long long\n"
- " */\n"
- " a);",
- format("a = f(/* loooooong long\n* long\n*/ a);",
- getLLVMStyleWithColumns(22)));
- // Keep empty comment lines.
- EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20)));
- EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, ReflowsCommentsPrecise) {
- // FIXME: This assumes we do not continue compressing whitespace once we are
- // in reflow mode. Consider compressing whitespace.
- // Test that we stop reflowing precisely at the column limit.
- // After reflowing, "// reflows into foo" does not fit the column limit,
- // so we compress the whitespace.
- EXPECT_EQ("// some text that\n"
- "// reflows into foo\n",
- format("// some text that reflows\n"
- "// into foo\n",
- getLLVMStyleWithColumns(20)));
- // Given one more column, "// reflows into foo" does fit the limit, so we
- // do not compress the whitespace.
- EXPECT_EQ("// some text that\n"
- "// reflows into foo\n",
- format("// some text that reflows\n"
- "// into foo\n",
- getLLVMStyleWithColumns(21)));
- // Make sure that we correctly account for the space added in the reflow case
- // when making the reflowing decision.
- // First, when the next line ends precisely one column over the limit, do not
- // reflow.
- EXPECT_EQ("// some text that\n"
- "// reflows\n"
- "// into1234567\n",
- format("// some text that reflows\n"
- "// into1234567\n",
- getLLVMStyleWithColumns(21)));
- // Secondly, when the next line ends later, but the first word in that line
- // is precisely one column over the limit, do not reflow.
- EXPECT_EQ("// some text that\n"
- "// reflows\n"
- "// into1234567 f\n",
- format("// some text that reflows\n"
- "// into1234567 f\n",
- getLLVMStyleWithColumns(21)));
- }
- TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) {
- // Baseline.
- EXPECT_EQ("// some text\n"
- "// that re flows\n",
- format("// some text that\n"
- "// re flows\n",
- getLLVMStyleWithColumns(16)));
- EXPECT_EQ("// some text\n"
- "// that re flows\n",
- format("// some text that\n"
- "// re flows\n",
- getLLVMStyleWithColumns(16)));
- EXPECT_EQ("/* some text\n"
- " * that re flows\n"
- " */\n",
- format("/* some text that\n"
- "* re flows\n"
- "*/\n",
- getLLVMStyleWithColumns(16)));
- // FIXME: We do not reflow if the indent of two subsequent lines differs;
- // given that this is different behavior from block comments, do we want
- // to keep this?
- EXPECT_EQ("// some text\n"
- "// that\n"
- "// re flows\n",
- format("// some text that\n"
- "// re flows\n",
- getLLVMStyleWithColumns(16)));
- // Space within parts of a line that fit.
- // FIXME: Use the earliest possible split while reflowing to compress the
- // whitespace within the line.
- EXPECT_EQ("// some text that\n"
- "// does re flow\n"
- "// more here\n",
- format("// some text that does\n"
- "// re flow more here\n",
- getLLVMStyleWithColumns(21)));
- }
- TEST_F(FormatTestComments, IgnoresIf0Contents) {
- EXPECT_EQ("#if 0\n"
- "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
- "#endif\n"
- "void f() {}",
- format("#if 0\n"
- "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
- "#endif\n"
- "void f( ) { }"));
- EXPECT_EQ("#if false\n"
- "void f( ) { }\n"
- "#endif\n"
- "void g() {}\n",
- format("#if false\n"
- "void f( ) { }\n"
- "#endif\n"
- "void g( ) { }\n"));
- EXPECT_EQ("enum E {\n"
- " One,\n"
- " Two,\n"
- "#if 0\n"
- "Three,\n"
- " Four,\n"
- "#endif\n"
- " Five\n"
- "};",
- format("enum E {\n"
- " One,Two,\n"
- "#if 0\n"
- "Three,\n"
- " Four,\n"
- "#endif\n"
- " Five};"));
- EXPECT_EQ("enum F {\n"
- " One,\n"
- "#if 1\n"
- " Two,\n"
- "#if 0\n"
- "Three,\n"
- " Four,\n"
- "#endif\n"
- " Five\n"
- "#endif\n"
- "};",
- format("enum F {\n"
- "One,\n"
- "#if 1\n"
- "Two,\n"
- "#if 0\n"
- "Three,\n"
- " Four,\n"
- "#endif\n"
- "Five\n"
- "#endif\n"
- "};"));
- EXPECT_EQ("enum G {\n"
- " One,\n"
- "#if 0\n"
- "Two,\n"
- "#else\n"
- " Three,\n"
- "#endif\n"
- " Four\n"
- "};",
- format("enum G {\n"
- "One,\n"
- "#if 0\n"
- "Two,\n"
- "#else\n"
- "Three,\n"
- "#endif\n"
- "Four\n"
- "};"));
- EXPECT_EQ("enum H {\n"
- " One,\n"
- "#if 0\n"
- "#ifdef Q\n"
- "Two,\n"
- "#else\n"
- "Three,\n"
- "#endif\n"
- "#endif\n"
- " Four\n"
- "};",
- format("enum H {\n"
- "One,\n"
- "#if 0\n"
- "#ifdef Q\n"
- "Two,\n"
- "#else\n"
- "Three,\n"
- "#endif\n"
- "#endif\n"
- "Four\n"
- "};"));
- EXPECT_EQ("enum I {\n"
- " One,\n"
- "#if /* test */ 0 || 1\n"
- "Two,\n"
- "Three,\n"
- "#endif\n"
- " Four\n"
- "};",
- format("enum I {\n"
- "One,\n"
- "#if /* test */ 0 || 1\n"
- "Two,\n"
- "Three,\n"
- "#endif\n"
- "Four\n"
- "};"));
- EXPECT_EQ("enum J {\n"
- " One,\n"
- "#if 0\n"
- "#if 0\n"
- "Two,\n"
- "#else\n"
- "Three,\n"
- "#endif\n"
- "Four,\n"
- "#endif\n"
- " Five\n"
- "};",
- format("enum J {\n"
- "One,\n"
- "#if 0\n"
- "#if 0\n"
- "Two,\n"
- "#else\n"
- "Three,\n"
- "#endif\n"
- "Four,\n"
- "#endif\n"
- "Five\n"
- "};"));
- // Ignore stuff in SWIG-blocks.
- EXPECT_EQ("#ifdef SWIG\n"
- "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
- "#endif\n"
- "void f() {}",
- format("#ifdef SWIG\n"
- "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
- "#endif\n"
- "void f( ) { }"));
- EXPECT_EQ("#ifndef SWIG\n"
- "void f() {}\n"
- "#endif",
- format("#ifndef SWIG\n"
- "void f( ) { }\n"
- "#endif"));
- }
- TEST_F(FormatTestComments, DontCrashOnBlockComments) {
- EXPECT_EQ(
- "int xxxxxxxxx; /* "
- "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
- "zzzzzz\n"
- "0*/",
- format("int xxxxxxxxx; /* "
- "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
- "0*/"));
- }
- TEST_F(FormatTestComments, BlockCommentsInControlLoops) {
- verifyFormat("if (0) /* a comment in a strange place */ {\n"
- " f();\n"
- "}");
- verifyFormat("if (0) /* a comment in a strange place */ {\n"
- " f();\n"
- "} /* another comment */ else /* comment #3 */ {\n"
- " g();\n"
- "}");
- verifyFormat("while (0) /* a comment in a strange place */ {\n"
- " f();\n"
- "}");
- verifyFormat("for (;;) /* a comment in a strange place */ {\n"
- " f();\n"
- "}");
- verifyFormat("do /* a comment in a strange place */ {\n"
- " f();\n"
- "} /* another comment */ while (0);");
- }
- TEST_F(FormatTestComments, BlockComments) {
- EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
- format("/* *//* */ /* */\n/* *//* */ /* */"));
- EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
- EXPECT_EQ("#define A /*123*/ \\\n"
- " b\n"
- "/* */\n"
- "someCall(\n"
- " parameter);",
- format("#define A /*123*/ b\n"
- "/* */\n"
- "someCall(parameter);",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("#define A\n"
- "/* */ someCall(\n"
- " parameter);",
- format("#define A\n"
- "/* */someCall(parameter);",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
- EXPECT_EQ("/*\n"
- " *\n"
- " * aaaaaa\n"
- " * aaaaaa\n"
- " */",
- format("/*\n"
- "*\n"
- " * aaaaaa aaaaaa\n"
- "*/",
- getLLVMStyleWithColumns(10)));
- EXPECT_EQ("/*\n"
- "**\n"
- "* aaaaaa\n"
- "*aaaaaa\n"
- "*/",
- format("/*\n"
- "**\n"
- "* aaaaaa aaaaaa\n"
- "*/",
- getLLVMStyleWithColumns(10)));
- EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " /* line 1\n"
- " bbbbbbbbbbbb */\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
- format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " /* line 1\n"
- " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
- getLLVMStyleWithColumns(50)));
- FormatStyle NoBinPacking = getLLVMStyle();
- NoBinPacking.BinPackParameters = false;
- EXPECT_EQ("someFunction(1, /* comment 1 */\n"
- " 2, /* comment 2 */\n"
- " 3, /* comment 3 */\n"
- " aaaa,\n"
- " bbbb);",
- format("someFunction (1, /* comment 1 */\n"
- " 2, /* comment 2 */ \n"
- " 3, /* comment 3 */\n"
- "aaaa, bbbb );",
- NoBinPacking));
- verifyFormat(
- "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
- EXPECT_EQ(
- "bool aaaaaaaaaaaaa = /* trailing comment */\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
- format(
- "bool aaaaaaaaaaaaa = /* trailing comment */\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
- EXPECT_EQ(
- "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
- "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
- "int cccccccccccccccccccccccccccccc; /* comment */\n",
- format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
- "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
- "int cccccccccccccccccccccccccccccc; /* comment */\n"));
- verifyFormat("void f(int * /* unused */) {}");
- EXPECT_EQ("/*\n"
- " **\n"
- " */",
- format("/*\n"
- " **\n"
- " */"));
- EXPECT_EQ("/*\n"
- " *q\n"
- " */",
- format("/*\n"
- " *q\n"
- " */"));
- EXPECT_EQ("/*\n"
- " * q\n"
- " */",
- format("/*\n"
- " * q\n"
- " */"));
- EXPECT_EQ("/*\n"
- " **/",
- format("/*\n"
- " **/"));
- EXPECT_EQ("/*\n"
- " ***/",
- format("/*\n"
- " ***/"));
- }
- TEST_F(FormatTestComments, BlockCommentsInMacros) {
- EXPECT_EQ("#define A \\\n"
- " { \\\n"
- " /* one line */ \\\n"
- " someCall();",
- format("#define A { \\\n"
- " /* one line */ \\\n"
- " someCall();",
- getLLVMStyleWithColumns(20)));
- EXPECT_EQ("#define A \\\n"
- " { \\\n"
- " /* previous */ \\\n"
- " /* one line */ \\\n"
- " someCall();",
- format("#define A { \\\n"
- " /* previous */ \\\n"
- " /* one line */ \\\n"
- " someCall();",
- getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) {
- EXPECT_EQ("a = {\n"
- " 1111 /* */\n"
- "};",
- format("a = {1111 /* */\n"
- "};",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("a = {\n"
- " 1111 /* */\n"
- "};",
- format("a = {1111 /* */\n"
- "};",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("a = {\n"
- " 1111 /* a\n"
- " */\n"
- "};",
- format("a = {1111 /* a */\n"
- "};",
- getLLVMStyleWithColumns(15)));
- }
- TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) {
- EXPECT_EQ("a = f(/* long\n"
- " long */\n"
- " a);",
- format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16)));
- EXPECT_EQ("a = f(\n"
- " /* long\n"
- " long */\n"
- " a);",
- format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15)));
- EXPECT_EQ("a = f(/* long\n"
- " long\n"
- " */\n"
- " a);",
- format("a = f(/* long\n"
- " long\n"
- " */a);",
- getLLVMStyleWithColumns(16)));
- EXPECT_EQ("a = f(/* long\n"
- " long\n"
- " */\n"
- " a);",
- format("a = f(/* long\n"
- " long\n"
- " */ a);",
- getLLVMStyleWithColumns(16)));
- EXPECT_EQ("a = f(/* long\n"
- " long\n"
- " */\n"
- " (1 + 1));",
- format("a = f(/* long\n"
- " long\n"
- " */ (1 + 1));",
- getLLVMStyleWithColumns(16)));
- EXPECT_EQ(
- "a = f(a,\n"
- " /* long\n"
- " long */\n"
- " b);",
- format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16)));
- EXPECT_EQ(
- "a = f(\n"
- " a,\n"
- " /* long\n"
- " long */\n"
- " b);",
- format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15)));
- EXPECT_EQ("a = f(a,\n"
- " /* long\n"
- " long */\n"
- " (1 + 1));",
- format("a = f(a, /* long long */ (1 + 1));",
- getLLVMStyleWithColumns(16)));
- EXPECT_EQ("a = f(\n"
- " a,\n"
- " /* long\n"
- " long */\n"
- " (1 + 1));",
- format("a = f(a, /* long long */ (1 + 1));",
- getLLVMStyleWithColumns(15)));
- }
- TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) {
- verifyFormat("{\n"
- " // a\n"
- " // b");
- }
- TEST_F(FormatTestComments, AlignTrailingComments) {
- EXPECT_EQ("#define MACRO(V) \\\n"
- " V(Rt2) /* one more char */ \\\n"
- " V(Rs) /* than here */ \\\n"
- "/* comment 3 */\n",
- format("#define MACRO(V)\\\n"
- "V(Rt2) /* one more char */ \\\n"
- "V(Rs) /* than here */ \\\n"
- "/* comment 3 */\n",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("int i = f(abc, // line 1\n"
- " d, // line 2\n"
- " // line 3\n"
- " b);",
- format("int i = f(abc, // line 1\n"
- " d, // line 2\n"
- " // line 3\n"
- " b);",
- getLLVMStyleWithColumns(40)));
- // Align newly broken trailing comments.
- EXPECT_EQ("int ab; // line\n"
- "int a; // long\n"
- " // long\n",
- format("int ab; // line\n"
- "int a; // long long\n",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("int ab; // line\n"
- "int a; // long\n"
- " // long\n"
- " // long",
- format("int ab; // line\n"
- "int a; // long long\n"
- " // long",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("int ab; // line\n"
- "int a; // long\n"
- " // long\n"
- "pt c; // long",
- format("int ab; // line\n"
- "int a; // long long\n"
- "pt c; // long",
- getLLVMStyleWithColumns(15)));
- EXPECT_EQ("int ab; // line\n"
- "int a; // long\n"
- " // long\n"
- "\n"
- "// long",
- format("int ab; // line\n"
- "int a; // long long\n"
- "\n"
- "// long",
- getLLVMStyleWithColumns(15)));
- // Don't align newly broken trailing comments if that would put them over the
- // column limit.
- EXPECT_EQ("int i, j; // line 1\n"
- "int k; // line longg\n"
- " // long",
- format("int i, j; // line 1\n"
- "int k; // line longg long",
- getLLVMStyleWithColumns(20)));
- // Always align if ColumnLimit = 0
- EXPECT_EQ("int i, j; // line 1\n"
- "int k; // line longg long",
- format("int i, j; // line 1\n"
- "int k; // line longg long",
- getLLVMStyleWithColumns(0)));
- // Align comment line sections aligned with the next token with the next
- // token.
- EXPECT_EQ("class A {\n"
- "public: // public comment\n"
- " // comment about a\n"
- " int a;\n"
- "};",
- format("class A {\n"
- "public: // public comment\n"
- " // comment about a\n"
- " int a;\n"
- "};",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("class A {\n"
- "public: // public comment 1\n"
- " // public comment 2\n"
- " // comment 1 about a\n"
- " // comment 2 about a\n"
- " int a;\n"
- "};",
- format("class A {\n"
- "public: // public comment 1\n"
- " // public comment 2\n"
- " // comment 1 about a\n"
- " // comment 2 about a\n"
- " int a;\n"
- "};",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("int f(int n) { // comment line 1 on f\n"
- " // comment line 2 on f\n"
- " // comment line 1 before return\n"
- " // comment line 2 before return\n"
- " return n; // comment line 1 on return\n"
- " // comment line 2 on return\n"
- " // comment line 1 after return\n"
- "}",
- format("int f(int n) { // comment line 1 on f\n"
- " // comment line 2 on f\n"
- " // comment line 1 before return\n"
- " // comment line 2 before return\n"
- " return n; // comment line 1 on return\n"
- " // comment line 2 on return\n"
- " // comment line 1 after return\n"
- "}",
- getLLVMStyleWithColumns(40)));
- EXPECT_EQ("int f(int n) {\n"
- " switch (n) { // comment line 1 on switch\n"
- " // comment line 2 on switch\n"
- " // comment line 1 before case 1\n"
- " // comment line 2 before case 1\n"
- " case 1: // comment line 1 on case 1\n"
- " // comment line 2 on case 1\n"
- " // comment line 1 before return 1\n"
- " // comment line 2 before return 1\n"
- " return 1; // comment line 1 on return 1\n"
- " // comment line 2 on return 1\n"
- " // comment line 1 before default\n"
- " // comment line 2 before default\n"
- " default: // comment line 1 on default\n"
- " // comment line 2 on default\n"
- " // comment line 1 before return 2\n"
- " return 2 * f(n - 1); // comment line 1 on return 2\n"
- " // comment line 2 on return 2\n"
- " // comment line 1 after return\n"
- " // comment line 2 after return\n"
- " }\n"
- "}",
- format("int f(int n) {\n"
- " switch (n) { // comment line 1 on switch\n"
- " // comment line 2 on switch\n"
- " // comment line 1 before case 1\n"
- " // comment line 2 before case 1\n"
- " case 1: // comment line 1 on case 1\n"
- " // comment line 2 on case 1\n"
- " // comment line 1 before return 1\n"
- " // comment line 2 before return 1\n"
- " return 1; // comment line 1 on return 1\n"
- " // comment line 2 on return 1\n"
- " // comment line 1 before default\n"
- " // comment line 2 before default\n"
- " default: // comment line 1 on default\n"
- " // comment line 2 on default\n"
- " // comment line 1 before return 2\n"
- " return 2 * f(n - 1); // comment line 1 on return 2\n"
- " // comment line 2 on return 2\n"
- " // comment line 1 after return\n"
- " // comment line 2 after return\n"
- " }\n"
- "}",
- getLLVMStyleWithColumns(80)));
- // If all the lines in a sequence of line comments are aligned with the next
- // token, the first line belongs to the previous token and the other lines
- // belong to the next token.
- EXPECT_EQ("int a; // line about a\n"
- "long b;",
- format("int a; // line about a\n"
- " long b;",
- getLLVMStyleWithColumns(80)));
- EXPECT_EQ("int a; // line about a\n"
- "// line about b\n"
- "long b;",
- format("int a; // line about a\n"
- " // line about b\n"
- " long b;",
- getLLVMStyleWithColumns(80)));
- EXPECT_EQ("int a; // line about a\n"
- "// line 1 about b\n"
- "// line 2 about b\n"
- "long b;",
- format("int a; // line about a\n"
- " // line 1 about b\n"
- " // line 2 about b\n"
- " long b;",
- getLLVMStyleWithColumns(80)));
- }
- TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
- EXPECT_EQ("/*\n"
- " */",
- format("/*\n"
- "*/", getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " */",
- format("/*\n"
- " */", getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " */",
- format("/*\n"
- " */", getLLVMStyle()));
- // Align a single line.
- EXPECT_EQ("/*\n"
- " * line */",
- format("/*\n"
- "* line */",
- getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " * line */",
- format("/*\n"
- " * line */",
- getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " * line */",
- format("/*\n"
- " * line */",
- getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " * line */",
- format("/*\n"
- " * line */",
- getLLVMStyle()));
- EXPECT_EQ("/**\n"
- " * line */",
- format("/**\n"
- "* line */",
- getLLVMStyle()));
- EXPECT_EQ("/**\n"
- " * line */",
- format("/**\n"
- " * line */",
- getLLVMStyle()));
- EXPECT_EQ("/**\n"
- " * line */",
- format("/**\n"
- " * line */",
- getLLVMStyle()));
- EXPECT_EQ("/**\n"
- " * line */",
- format("/**\n"
- " * line */",
- getLLVMStyle()));
- EXPECT_EQ("/**\n"
- " * line */",
- format("/**\n"
- " * line */",
- getLLVMStyle()));
- // Align the end '*/' after a line.
- EXPECT_EQ("/*\n"
- " * line\n"
- " */",
- format("/*\n"
- "* line\n"
- "*/", getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " * line\n"
- " */",
- format("/*\n"
- " * line\n"
- " */", getLLVMStyle()));
- EXPECT_EQ("/*\n"
- " * line\n"
- " */",
- format("/*\n"
- " * line\n"
- " */", getLLVMStyle()));
- // Align two lines.
- EXPECT_EQ("/* line 1\n"
- " * line 2 */",
- format("/* line 1\n"
- " * line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("/* line 1\n"
- " * line 2 */",
- format("/* line 1\n"
- "* line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("/* line 1\n"
- " * line 2 */",
- format("/* line 1\n"
- " * line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("/* line 1\n"
- " * line 2 */",
- format("/* line 1\n"
- " * line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("/* line 1\n"
- " * line 2 */",
- format("/* line 1\n"
- " * line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("int i; /* line 1\n"
- " * line 2 */",
- format("int i; /* line 1\n"
- "* line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("int i; /* line 1\n"
- " * line 2 */",
- format("int i; /* line 1\n"
- " * line 2 */",
- getLLVMStyle()));
- EXPECT_EQ("int i; /* line 1\n"
- " * line 2 */",
- format("int i; /* line 1\n"
- " * line 2 */",
- getLLVMStyle()));
- // Align several lines.
- EXPECT_EQ("/* line 1\n"
- " * line 2\n"
- " * line 3 */",
- format("/* line 1\n"
- " * line 2\n"
- "* line 3 */",
- getLLVMStyle()));
- EXPECT_EQ("/* line 1\n"
- " * line 2\n"
- " * line 3 */",
- format("/* line 1\n"
- " * line 2\n"
- "* line 3 */",
- getLLVMStyle()));
- EXPECT_EQ("/*\n"
- "** line 1\n"
- "** line 2\n"
- "*/",
- format("/*\n"
- "** line 1\n"
- " ** line 2\n"
- "*/",
- getLLVMStyle()));
- // Align with different indent after the decorations.
- EXPECT_EQ("/*\n"
- " * line 1\n"
- " * line 2\n"
- " * line 3\n"
- " * line 4\n"
- " */",
- format("/*\n"
- "* line 1\n"
- " * line 2\n"
- " * line 3\n"
- "* line 4\n"
- "*/", getLLVMStyle()));
- // Align empty or blank lines.
- EXPECT_EQ("/**\n"
- " *\n"
- " *\n"
- " *\n"
- " */",
- format("/**\n"
- "* \n"
- " * \n"
- " *\n"
- "*/", getLLVMStyle()));
- // Align while breaking and reflowing.
- EXPECT_EQ("/*\n"
- " * long long long\n"
- " * long long\n"
- " *\n"
- " * long */",
- format("/*\n"
- " * long long long long\n"
- " * long\n"
- " *\n"
- "* long */",
- getLLVMStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, NoCrash_Bug34236) {
- // This is a test case from a crasher reported in:
- // https://bugs.llvm.org/show_bug.cgi?id=34236
- // Temporarily disable formatting for readability.
- // clang-format off
- EXPECT_EQ(
- "/* */ /*\n"
- " * a\n"
- " * b c d*/",
- format(
- "/* */ /*\n"
- " * a b\n"
- " * c d*/",
- getLLVMStyleWithColumns(80)));
- // clang-format on
- }
- TEST_F(FormatTestComments, NonTrailingBlockComments) {
- verifyFormat("const /** comment comment */ A = B;",
- getLLVMStyleWithColumns(40));
- verifyFormat("const /** comment comment comment */ A =\n"
- " B;",
- getLLVMStyleWithColumns(40));
- EXPECT_EQ("const /** comment comment comment\n"
- " comment */\n"
- " A = B;",
- format("const /** comment comment comment comment */\n"
- " A = B;",
- getLLVMStyleWithColumns(40)));
- }
- TEST_F(FormatTestComments, PythonStyleComments) {
- // Keeps a space after '#'.
- EXPECT_EQ("# comment\n"
- "key: value",
- format("#comment\n"
- "key:value",
- getTextProtoStyleWithColumns(20)));
- EXPECT_EQ("# comment\n"
- "key: value",
- format("# comment\n"
- "key:value",
- getTextProtoStyleWithColumns(20)));
- // Breaks long comment.
- EXPECT_EQ("# comment comment\n"
- "# comment\n"
- "key: value",
- format("# comment comment comment\n"
- "key:value",
- getTextProtoStyleWithColumns(20)));
- // Indents comments.
- EXPECT_EQ("data {\n"
- " # comment comment\n"
- " # comment\n"
- " key: value\n"
- "}",
- format("data {\n"
- "# comment comment comment\n"
- "key: value}",
- getTextProtoStyleWithColumns(20)));
- EXPECT_EQ("data {\n"
- " # comment comment\n"
- " # comment\n"
- " key: value\n"
- "}",
- format("data {# comment comment comment\n"
- "key: value}",
- getTextProtoStyleWithColumns(20)));
- // Reflows long comments.
- EXPECT_EQ("# comment comment\n"
- "# comment comment\n"
- "key: value",
- format("# comment comment comment\n"
- "# comment\n"
- "key:value",
- getTextProtoStyleWithColumns(20)));
- // Breaks trailing comments.
- EXPECT_EQ("k: val # comment\n"
- " # comment\n"
- "a: 1",
- format("k:val#comment comment\n"
- "a:1",
- getTextProtoStyleWithColumns(20)));
- EXPECT_EQ("id {\n"
- " k: val # comment\n"
- " # comment\n"
- " # line line\n"
- " a: 1\n"
- "}",
- format("id {k:val#comment comment\n"
- "# line line\n"
- "a:1}",
- getTextProtoStyleWithColumns(20)));
- // Aligns trailing comments.
- EXPECT_EQ("k: val # commen1\n"
- " # commen2\n"
- " # commen3\n"
- "# commen4\n"
- "a: 1 # commen5\n"
- " # commen6\n"
- " # commen7",
- format("k:val#commen1 commen2\n"
- " # commen3\n"
- "# commen4\n"
- "a:1#commen5 commen6\n"
- " #commen7",
- getTextProtoStyleWithColumns(20)));
- }
- TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) {
- // The end of /* trail */ is exactly at 80 columns, but the unbreakable
- // trailing sequence ); after it exceeds the column limit. Make sure we
- // correctly break the line in that case.
- verifyFormat("int a =\n"
- " foo(/* trail */);",
- getLLVMStyleWithColumns(23));
- }
- TEST_F(FormatTestComments, ReflowBackslashCrash) {
- // clang-format off
- EXPECT_EQ(
- "// How to run:\n"
- "// bbbbb run \\\n"
- "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n"
- "// \\ <log_file> -- --output_directory=\"<output_directory>\"",
- format(
- "// How to run:\n"
- "// bbbbb run \\\n"
- "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n"
- "// <log_file> -- --output_directory=\"<output_directory>\""));
- // clang-format on
- }
- } // end namespace
- } // end namespace format
- } // end namespace clang
|