From: Daniel Jasper Date: Wed, 30 Jan 2013 09:46:12 +0000 (+0000) Subject: Don't generate no-op replacements. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=afcbd859f839db4133311147c1bc7db29dfaea10;p=clang Don't generate no-op replacements. No functional changes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173916 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 9dfde34e8b..08ea8e97de 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -194,6 +194,11 @@ private: /// \brief Stores \p Text as the replacement for the whitespace in front of /// \p Tok. void storeReplacement(const FormatToken &Tok, const std::string Text) { + // Don't create a replacement, if it does not change anything. + if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart), + Tok.WhiteSpaceLength) == Text) + return; + Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength, Text)); } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 32b2ff7f75..97358982cf 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -38,6 +38,7 @@ protected: tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer()); + ReplacementCount = Replace.size(); EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); return Context.getRewrittenText(ID); @@ -105,6 +106,8 @@ protected: verifyFormat(text); verifyFormat(llvm::Twine("void f() { " + text + " }").str()); } + + int ReplacementCount; }; TEST_F(FormatTest, MessUp) { @@ -148,6 +151,19 @@ TEST_F(FormatTest, ImportantSpaces) { verifyFormat("vector< ::Type> v;"); } +TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { + EXPECT_EQ("if (a) {\n" + " f();\n" + "}", format("if(a){f();}")); + EXPECT_EQ(4, ReplacementCount); + EXPECT_EQ("if (a) {\n" + " f();\n" + "}", format("if (a) {\n" + " f();\n" + "}")); + EXPECT_EQ(0, ReplacementCount); +} + //===----------------------------------------------------------------------===// // Tests for control statements. //===----------------------------------------------------------------------===//