Re: [PATCH] D21749: Changes related to new implementation of tooling::Replacements as class.

2016-07-14 Thread Eric Liu via cfe-commits
ioeric updated this revision to Diff 64015.
ioeric added a comment.

- merged with origin/master
- Addressed reviewer's comments in the corresponding patch.


https://reviews.llvm.org/D21749

Files:
  
clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
  clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
  clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
  clang-rename/RenamingAction.cpp
  clang-rename/RenamingAction.h
  clang-rename/tool/ClangRename.cpp
  clang-tidy/ClangTidyDiagnosticConsumer.cpp
  include-fixer/IncludeFixer.cpp
  include-fixer/tool/ClangIncludeFixer.cpp
  tool-template/ToolTemplate.cpp
  unittests/clang-tidy/ClangTidyTest.h
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -89,18 +89,28 @@
   runOnCode(&Factory, Code, FakeFileName, ExtraArgs);
   if (FixerContext.getHeaderInfos().empty())
 return Code;
-  auto Replaces = clang::include_fixer::createInsertHeaderReplacements(
+  auto ReplacesOrError = clang::include_fixer::createInsertHeaderReplacements(
   Code, FakeFileName, FixerContext.getHeaderInfos().front().Header);
-  EXPECT_TRUE(static_cast(Replaces))
-  << llvm::toString(Replaces.takeError()) << "\n";
-  if (!Replaces)
+  EXPECT_TRUE(static_cast(ReplacesOrError))
+  << llvm::toString(ReplacesOrError.takeError()) << "\n";
+  if (!ReplacesOrError)
 return "";
+  auto Replaces = std::move(*ReplacesOrError);
+  auto R = tooling::Replacement(
+  FakeFileName, FixerContext.getSymbolRange().getOffset(),
+  FixerContext.getSymbolRange().getLength(),
+  FixerContext.getHeaderInfos().front().QualifiedName);
+  auto Err = Replaces.add(R);
+  if (Err) {
+llvm::consumeError(std::move(Err));
+R = tooling::Replacement(R.getFilePath(),
+ Replaces.getShiftedCodePosition(R.getOffset()),
+ R.getLength(), R.getReplacementText());
+Replaces = Replaces.merge(tooling::Replacements(R));
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile(FakeFileName, Code);
-  Replaces->insert({FakeFileName, FixerContext.getSymbolRange().getOffset(),
-FixerContext.getSymbolRange().getLength(),
-FixerContext.getHeaderInfos().front().QualifiedName});
-  clang::tooling::applyAllReplacements(*Replaces, Context.Rewrite);
+  EXPECT_TRUE(clang::tooling::applyAllReplacements(Replaces, Context.Rewrite));
   return Context.getRewrittenText(ID);
 }
 
Index: unittests/clang-tidy/ClangTidyTest.h
===
--- unittests/clang-tidy/ClangTidyTest.h
+++ unittests/clang-tidy/ClangTidyTest.h
@@ -119,7 +119,14 @@
   DiagConsumer.finish();
   tooling::Replacements Fixes;
   for (const ClangTidyError &Error : Context.getErrors())
-Fixes.insert(Error.Fix.begin(), Error.Fix.end());
+for (const auto &Fix : Error.Fix) {
+  auto Err = Fixes.add(Fix);
+  // FIXME: better error handling.
+  if (Err) {
+llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+return "";
+  }
+}
   if (Errors)
 *Errors = Context.getErrors();
   auto Result = tooling::applyAllReplacements(Code, Fixes);
Index: tool-template/ToolTemplate.cpp
===
--- tool-template/ToolTemplate.cpp
+++ tool-template/ToolTemplate.cpp
@@ -53,18 +53,20 @@
 
 namespace {
 class ToolTemplateCallback : public MatchFinder::MatchCallback {
- public:
-  ToolTemplateCallback(Replacements *Replace) : Replace(Replace) {}
+public:
+  ToolTemplateCallback(std::map *Replace)
+  : Replace(Replace) {}
 
   void run(const MatchFinder::MatchResult &Result) override {
-// TODO: This routine will get called for each thing that the matchers find.
+// TODO: This routine will get called for each thing that the matchers
+// find.
 // At this point, you can examine the match, and do whatever you want,
 // including replacing the matched text with other text
 (void)Replace; // This to prevent an "unused member variable" warning;
   }
 
- private:
-  Replacements *Replace;
+private:
+  std::map *Replace;
 };
 } // end anonymous namespace
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -262,12 +262,14 @@
   return 1;
 }
 
-auto Replacements = clang::include_fixer::createInsertHeaderReplacements(
+// FIXME: pull code for generating header insertion and namespace insertion
+// replacements into a function since similar stuff is performed below.
+auto ReplacesOrError = clang::include_fixer::cre

Re: [PATCH] D21749: Changes related to new implementation of tooling::Replacements as class.

2016-07-27 Thread Eric Liu via cfe-commits
ioeric updated this revision to Diff 65685.
ioeric added a comment.

- Merged with origin/master.


https://reviews.llvm.org/D21749

Files:
  
clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
  clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
  clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
  clang-rename/RenamingAction.cpp
  clang-rename/RenamingAction.h
  clang-rename/tool/ClangRename.cpp
  clang-tidy/ClangTidyDiagnosticConsumer.cpp
  include-fixer/IncludeFixer.cpp
  include-fixer/tool/ClangIncludeFixer.cpp
  tool-template/ToolTemplate.cpp
  unittests/clang-tidy/ClangTidyTest.h

Index: unittests/clang-tidy/ClangTidyTest.h
===
--- unittests/clang-tidy/ClangTidyTest.h
+++ unittests/clang-tidy/ClangTidyTest.h
@@ -119,7 +119,14 @@
   DiagConsumer.finish();
   tooling::Replacements Fixes;
   for (const ClangTidyError &Error : Context.getErrors())
-Fixes.insert(Error.Fix.begin(), Error.Fix.end());
+for (const auto &Fix : Error.Fix) {
+  auto Err = Fixes.add(Fix);
+  // FIXME: better error handling.
+  if (Err) {
+llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+return "";
+  }
+}
   if (Errors)
 *Errors = Context.getErrors();
   auto Result = tooling::applyAllReplacements(Code, Fixes);
Index: tool-template/ToolTemplate.cpp
===
--- tool-template/ToolTemplate.cpp
+++ tool-template/ToolTemplate.cpp
@@ -53,18 +53,20 @@
 
 namespace {
 class ToolTemplateCallback : public MatchFinder::MatchCallback {
- public:
-  ToolTemplateCallback(Replacements *Replace) : Replace(Replace) {}
+public:
+  ToolTemplateCallback(std::map *Replace)
+  : Replace(Replace) {}
 
   void run(const MatchFinder::MatchResult &Result) override {
-// TODO: This routine will get called for each thing that the matchers find.
+// TODO: This routine will get called for each thing that the matchers
+// find.
 // At this point, you can examine the match, and do whatever you want,
 // including replacing the matched text with other text
 (void)Replace; // This to prevent an "unused member variable" warning;
   }
 
- private:
-  Replacements *Replace;
+private:
+  std::map *Replace;
 };
 } // end anonymous namespace
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -350,8 +350,7 @@
   }
 
   if (!Quiet)
-errs() << "Added #include " << Context.getHeaderInfos().front().Header
-   << '\n';
+llvm::errs() << "Added #include" << Context.getHeaderInfos().front().Header;
 
   // Set up a new source manager for applying the resulting replacements.
   IntrusiveRefCntPtr DiagOpts(new DiagnosticOptions);
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -352,23 +352,36 @@
   std::string IncludeName =
   "#include " + Context.getHeaderInfos().front().Header + "\n";
   // Create replacements for the new header.
-  clang::tooling::Replacements Insertions = {
-  tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName)};
+  clang::tooling::Replacements Insertions;
+  // FIXME: remove this error comsuption when createInsertHeaderReplacements
+  // returns Error.
+  llvm::consumeError(
+  Insertions.add(tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName)));
 
   auto CleanReplaces = cleanupAroundReplacements(Code, Insertions, Style);
   if (!CleanReplaces)
 return CleanReplaces;
 
+  auto Replaces = std::move(*CleanReplaces);
   if (AddQualifiers) {
 for (const auto &Info : Context.getQuerySymbolInfos()) {
   // Ignore the empty range.
-  if (Info.Range.getLength() > 0)
-CleanReplaces->insert({FilePath, Info.Range.getOffset(),
-   Info.Range.getLength(),
-   Context.getHeaderInfos().front().QualifiedName});
+  if (Info.Range.getLength() > 0) {
+auto R = tooling::Replacement(
+{FilePath, Info.Range.getOffset(), Info.Range.getLength(),
+ Context.getHeaderInfos().front().QualifiedName});
+auto Err = Replaces.add(R);
+if (Err) {
+  llvm::consumeError(std::move(Err));
+  R = tooling::Replacement(
+  R.getFilePath(), Replaces.getShiftedCodePosition(R.getOffset()),
+  R.getLength(), R.getReplacementText());
+  Replaces = Replaces.merge(tooling::Replacements(R));
+}
+  }
 }
   }
-  return formatReplacements(Code, *CleanReplaces, Style);
+  return formatReplacements(Code, Replaces, Style);
 }
 
 } // namespace include_fixer
Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp
===

Re: [PATCH] D21749: Changes related to new implementation of tooling::Replacements as class.

2016-07-27 Thread Eric Liu via cfe-commits
ioeric updated this revision to Diff 65712.
ioeric added a comment.

- Minor cleanup/


https://reviews.llvm.org/D21749

Files:
  
clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
  clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
  clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
  clang-rename/RenamingAction.cpp
  clang-rename/RenamingAction.h
  clang-rename/tool/ClangRename.cpp
  clang-tidy/ClangTidyDiagnosticConsumer.cpp
  include-fixer/IncludeFixer.cpp
  include-fixer/tool/ClangIncludeFixer.cpp
  tool-template/ToolTemplate.cpp
  unittests/clang-tidy/ClangTidyTest.h

Index: unittests/clang-tidy/ClangTidyTest.h
===
--- unittests/clang-tidy/ClangTidyTest.h
+++ unittests/clang-tidy/ClangTidyTest.h
@@ -119,7 +119,14 @@
   DiagConsumer.finish();
   tooling::Replacements Fixes;
   for (const ClangTidyError &Error : Context.getErrors())
-Fixes.insert(Error.Fix.begin(), Error.Fix.end());
+for (const auto &Fix : Error.Fix) {
+  auto Err = Fixes.add(Fix);
+  // FIXME: better error handling. Keep the behavior for now.
+  if (Err) {
+llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+return "";
+  }
+}
   if (Errors)
 *Errors = Context.getErrors();
   auto Result = tooling::applyAllReplacements(Code, Fixes);
Index: tool-template/ToolTemplate.cpp
===
--- tool-template/ToolTemplate.cpp
+++ tool-template/ToolTemplate.cpp
@@ -53,18 +53,20 @@
 
 namespace {
 class ToolTemplateCallback : public MatchFinder::MatchCallback {
- public:
-  ToolTemplateCallback(Replacements *Replace) : Replace(Replace) {}
+public:
+  ToolTemplateCallback(std::map *Replace)
+  : Replace(Replace) {}
 
   void run(const MatchFinder::MatchResult &Result) override {
-// TODO: This routine will get called for each thing that the matchers find.
+// TODO: This routine will get called for each thing that the matchers
+// find.
 // At this point, you can examine the match, and do whatever you want,
 // including replacing the matched text with other text
 (void)Replace; // This to prevent an "unused member variable" warning;
   }
 
- private:
-  Replacements *Replace;
+private:
+  std::map *Replace;
 };
 } // end anonymous namespace
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -350,8 +350,8 @@
   }
 
   if (!Quiet)
-errs() << "Added #include " << Context.getHeaderInfos().front().Header
-   << '\n';
+llvm::errs() << "Added #include " << Context.getHeaderInfos().front().Header
+ << "\n";
 
   // Set up a new source manager for applying the resulting replacements.
   IntrusiveRefCntPtr DiagOpts(new DiagnosticOptions);
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -352,23 +352,36 @@
   std::string IncludeName =
   "#include " + Context.getHeaderInfos().front().Header + "\n";
   // Create replacements for the new header.
-  clang::tooling::Replacements Insertions = {
-  tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName)};
+  clang::tooling::Replacements Insertions;
+  auto Err =
+  Insertions.add(tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName));
+  if (Err)
+return std::move(Err);
 
   auto CleanReplaces = cleanupAroundReplacements(Code, Insertions, Style);
   if (!CleanReplaces)
 return CleanReplaces;
 
+  auto Replaces = std::move(*CleanReplaces);
   if (AddQualifiers) {
 for (const auto &Info : Context.getQuerySymbolInfos()) {
   // Ignore the empty range.
-  if (Info.Range.getLength() > 0)
-CleanReplaces->insert({FilePath, Info.Range.getOffset(),
-   Info.Range.getLength(),
-   Context.getHeaderInfos().front().QualifiedName});
+  if (Info.Range.getLength() > 0) {
+auto R = tooling::Replacement(
+{FilePath, Info.Range.getOffset(), Info.Range.getLength(),
+ Context.getHeaderInfos().front().QualifiedName});
+auto Err = Replaces.add(R);
+if (Err) {
+  llvm::consumeError(std::move(Err));
+  R = tooling::Replacement(
+  R.getFilePath(), Replaces.getShiftedCodePosition(R.getOffset()),
+  R.getLength(), R.getReplacementText());
+  Replaces = Replaces.merge(tooling::Replacements(R));
+}
+  }
 }
   }
-  return formatReplacements(Code, *CleanReplaces, Style);
+  return formatReplacements(Code, Replaces, Style);
 }
 
 } // namespace include_fixer
Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp
==

Re: [PATCH] D21749: Changes related to new implementation of tooling::Replacements as class.

2016-07-27 Thread Manuel Klimek via cfe-commits
klimek accepted this revision.
klimek added a comment.
This revision is now accepted and ready to land.

lg


https://reviews.llvm.org/D21749



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21749: Changes related to new implementation of tooling::Replacements as class.

2016-08-01 Thread Eric Liu via cfe-commits
ioeric updated this revision to Diff 66295.
ioeric added a comment.

- merged with origin/master


https://reviews.llvm.org/D21749

Files:
  
clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
  clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
  clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
  clang-rename/RenamingAction.cpp
  clang-rename/RenamingAction.h
  clang-rename/tool/ClangRename.cpp
  clang-tidy/ClangTidyDiagnosticConsumer.cpp
  include-fixer/IncludeFixer.cpp
  include-fixer/tool/ClangIncludeFixer.cpp
  tool-template/ToolTemplate.cpp
  unittests/clang-tidy/ClangTidyTest.h

Index: unittests/clang-tidy/ClangTidyTest.h
===
--- unittests/clang-tidy/ClangTidyTest.h
+++ unittests/clang-tidy/ClangTidyTest.h
@@ -119,7 +119,14 @@
   DiagConsumer.finish();
   tooling::Replacements Fixes;
   for (const ClangTidyError &Error : Context.getErrors())
-Fixes.insert(Error.Fix.begin(), Error.Fix.end());
+for (const auto &Fix : Error.Fix) {
+  auto Err = Fixes.add(Fix);
+  // FIXME: better error handling. Keep the behavior for now.
+  if (Err) {
+llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+return "";
+  }
+}
   if (Errors)
 *Errors = Context.getErrors();
   auto Result = tooling::applyAllReplacements(Code, Fixes);
Index: tool-template/ToolTemplate.cpp
===
--- tool-template/ToolTemplate.cpp
+++ tool-template/ToolTemplate.cpp
@@ -53,18 +53,20 @@
 
 namespace {
 class ToolTemplateCallback : public MatchFinder::MatchCallback {
- public:
-  ToolTemplateCallback(Replacements *Replace) : Replace(Replace) {}
+public:
+  ToolTemplateCallback(std::map *Replace)
+  : Replace(Replace) {}
 
   void run(const MatchFinder::MatchResult &Result) override {
-// TODO: This routine will get called for each thing that the matchers find.
+// TODO: This routine will get called for each thing that the matchers
+// find.
 // At this point, you can examine the match, and do whatever you want,
 // including replacing the matched text with other text
 (void)Replace; // This to prevent an "unused member variable" warning;
   }
 
- private:
-  Replacements *Replace;
+private:
+  std::map *Replace;
 };
 } // end anonymous namespace
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -350,8 +350,8 @@
   }
 
   if (!Quiet)
-errs() << "Added #include " << Context.getHeaderInfos().front().Header
-   << '\n';
+llvm::errs() << "Added #include " << Context.getHeaderInfos().front().Header
+ << "\n";
 
   // Set up a new source manager for applying the resulting replacements.
   IntrusiveRefCntPtr DiagOpts(new DiagnosticOptions);
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -352,23 +352,36 @@
   std::string IncludeName =
   "#include " + Context.getHeaderInfos().front().Header + "\n";
   // Create replacements for the new header.
-  clang::tooling::Replacements Insertions = {
-  tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName)};
+  clang::tooling::Replacements Insertions;
+  auto Err =
+  Insertions.add(tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName));
+  if (Err)
+return std::move(Err);
 
   auto CleanReplaces = cleanupAroundReplacements(Code, Insertions, Style);
   if (!CleanReplaces)
 return CleanReplaces;
 
+  auto Replaces = std::move(*CleanReplaces);
   if (AddQualifiers) {
 for (const auto &Info : Context.getQuerySymbolInfos()) {
   // Ignore the empty range.
-  if (Info.Range.getLength() > 0)
-CleanReplaces->insert({FilePath, Info.Range.getOffset(),
-   Info.Range.getLength(),
-   Context.getHeaderInfos().front().QualifiedName});
+  if (Info.Range.getLength() > 0) {
+auto R = tooling::Replacement(
+{FilePath, Info.Range.getOffset(), Info.Range.getLength(),
+ Context.getHeaderInfos().front().QualifiedName});
+auto Err = Replaces.add(R);
+if (Err) {
+  llvm::consumeError(std::move(Err));
+  R = tooling::Replacement(
+  R.getFilePath(), Replaces.getShiftedCodePosition(R.getOffset()),
+  R.getLength(), R.getReplacementText());
+  Replaces = Replaces.merge(tooling::Replacements(R));
+}
+  }
 }
   }
-  return formatReplacements(Code, *CleanReplaces, Style);
+  return formatReplacements(Code, Replaces, Style);
 }
 
 } // namespace include_fixer
Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp
===

Re: [PATCH] D21749: Changes related to new implementation of tooling::Replacements as class.

2016-08-01 Thread Eric Liu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL277336: Changes related to new implementation of 
tooling::Replacements as class. (authored by ioeric).

Changed prior to commit:
  https://reviews.llvm.org/D21749?vs=66295&id=66298#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D21749

Files:
  
clang-tools-extra/trunk/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
  
clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
  
clang-tools-extra/trunk/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
  clang-tools-extra/trunk/clang-rename/RenamingAction.cpp
  clang-tools-extra/trunk/clang-rename/RenamingAction.h
  clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp
  clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
  clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
  clang-tools-extra/trunk/tool-template/ToolTemplate.cpp
  clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h

Index: clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
===
--- clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
+++ clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
@@ -119,7 +119,14 @@
   DiagConsumer.finish();
   tooling::Replacements Fixes;
   for (const ClangTidyError &Error : Context.getErrors())
-Fixes.insert(Error.Fix.begin(), Error.Fix.end());
+for (const auto &Fix : Error.Fix) {
+  auto Err = Fixes.add(Fix);
+  // FIXME: better error handling. Keep the behavior for now.
+  if (Err) {
+llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+return "";
+  }
+}
   if (Errors)
 *Errors = Context.getErrors();
   auto Result = tooling::applyAllReplacements(Code, Fixes);
Index: clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
@@ -350,8 +350,8 @@
   }
 
   if (!Quiet)
-errs() << "Added #include " << Context.getHeaderInfos().front().Header
-   << '\n';
+llvm::errs() << "Added #include " << Context.getHeaderInfos().front().Header
+ << "\n";
 
   // Set up a new source manager for applying the resulting replacements.
   IntrusiveRefCntPtr DiagOpts(new DiagnosticOptions);
Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
@@ -352,23 +352,36 @@
   std::string IncludeName =
   "#include " + Context.getHeaderInfos().front().Header + "\n";
   // Create replacements for the new header.
-  clang::tooling::Replacements Insertions = {
-  tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName)};
+  clang::tooling::Replacements Insertions;
+  auto Err =
+  Insertions.add(tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName));
+  if (Err)
+return std::move(Err);
 
   auto CleanReplaces = cleanupAroundReplacements(Code, Insertions, Style);
   if (!CleanReplaces)
 return CleanReplaces;
 
+  auto Replaces = std::move(*CleanReplaces);
   if (AddQualifiers) {
 for (const auto &Info : Context.getQuerySymbolInfos()) {
   // Ignore the empty range.
-  if (Info.Range.getLength() > 0)
-CleanReplaces->insert({FilePath, Info.Range.getOffset(),
-   Info.Range.getLength(),
-   Context.getHeaderInfos().front().QualifiedName});
+  if (Info.Range.getLength() > 0) {
+auto R = tooling::Replacement(
+{FilePath, Info.Range.getOffset(), Info.Range.getLength(),
+ Context.getHeaderInfos().front().QualifiedName});
+auto Err = Replaces.add(R);
+if (Err) {
+  llvm::consumeError(std::move(Err));
+  R = tooling::Replacement(
+  R.getFilePath(), Replaces.getShiftedCodePosition(R.getOffset()),
+  R.getLength(), R.getReplacementText());
+  Replaces = Replaces.merge(tooling::Replacements(R));
+}
+  }
 }
   }
-  return formatReplacements(Code, *CleanReplaces, Style);
+  return formatReplacements(Code, Replaces, Style);
 }
 
 } // namespace include_fixer
Index: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -77,7 +77,14 @@
   assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
  "Only file location