Don't generate no-op replacements.

No functional changes.

llvm-svn: 173916
This commit is contained in:
Daniel Jasper 2013-01-30 09:46:12 +00:00
parent 5fbfafcd8c
commit 7b038a2381
2 changed files with 21 additions and 0 deletions

View File

@ -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));
}

View File

@ -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.
//===----------------------------------------------------------------------===//