r173916 - Don't generate no-op replacements.
Daniel Jasper
djasper at google.com
Wed Jan 30 01:46:12 PST 2013
Author: djasper
Date: Wed Jan 30 03:46:12 2013
New Revision: 173916
URL: http://llvm.org/viewvc/llvm-project?rev=173916&view=rev
Log:
Don't generate no-op replacements.
No functional changes.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=173916&r1=173915&r2=173916&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Jan 30 03:46:12 2013
@@ -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));
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173916&r1=173915&r2=173916&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan 30 03:46:12 2013
@@ -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.
//===----------------------------------------------------------------------===//
More information about the cfe-commits
mailing list