/// \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));
}
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);
verifyFormat(text);
verifyFormat(llvm::Twine("void f() { " + text + " }").str());
}
+
+ int ReplacementCount;
};
TEST_F(FormatTest, MessUp) {
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.
//===----------------------------------------------------------------------===//