]> granicus.if.org Git - clang/commitdiff
Don't generate no-op replacements.
authorDaniel Jasper <djasper@google.com>
Wed, 30 Jan 2013 09:46:12 +0000 (09:46 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 30 Jan 2013 09:46:12 +0000 (09:46 +0000)
No functional changes.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173916 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 9dfde34e8b5bf18c7cf44694803835f214676a10..08ea8e97de5db9996123bb54c9850efc139ff4ca 100644 (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));
   }
index 32b2ff7f750ae3cfad05e40538e0be8527267b38..97358982cf60d4b10114c87348565fb12bee1104 100644 (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.
 //===----------------------------------------------------------------------===//