[Edit, Rewrite] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC).

llvm-svn: 328597
diff --git a/clang/lib/Edit/Commit.cpp b/clang/lib/Edit/Commit.cpp
index 0cc152b..afc1a13 100644
--- a/clang/lib/Edit/Commit.cpp
+++ b/clang/lib/Edit/Commit.cpp
@@ -1,4 +1,4 @@
-//===----- Commit.cpp - A unit of edits -----------------------------------===//
+//===- Commit.cpp - A unit of edits ---------------------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -8,10 +8,16 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Edit/Commit.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Edit/EditedSource.h"
+#include "clang/Edit/FileOffset.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Lex/PPConditionalDirectiveRecord.h"
+#include "llvm/ADT/StringRef.h"
+#include <cassert>
+#include <utility>
 
 using namespace clang;
 using namespace edit;
@@ -36,9 +42,9 @@
 }
 
 Commit::Commit(EditedSource &Editor)
-  : SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()),
-    PPRec(Editor.getPPCondDirectiveRecord()),
-    Editor(&Editor), IsCommitable(true) { }
+    : SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()),
+      PPRec(Editor.getPPCondDirectiveRecord()),
+      Editor(&Editor) {}
 
 bool Commit::insert(SourceLocation loc, StringRef text,
                     bool afterToken, bool beforePreviousInsertions) {
@@ -276,14 +282,12 @@
 }
 
 bool Commit::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
-  for (unsigned i = 0, e = CachedEdits.size(); i != e; ++i) {
-    Edit &act = CachedEdits[i];
+  for (const auto &act : CachedEdits)
     if (act.Kind == Act_Remove) {
       if (act.Offset.getFID() == Offs.getFID() &&
           Offs > act.Offset && Offs < act.Offset.getWithOffset(act.Length))
         return false; // position has been removed.
     }
-  }
 
   if (!Editor)
     return true;
@@ -338,6 +342,7 @@
                                        SourceLocation *MacroBegin) const {
   return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts, MacroBegin);
 }
+
 bool Commit::isAtEndOfMacroExpansion(SourceLocation loc,
                                      SourceLocation *MacroEnd) const {
   return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
diff --git a/clang/lib/Edit/EditedSource.cpp b/clang/lib/Edit/EditedSource.cpp
index 444d039..0ff2447f 100644
--- a/clang/lib/Edit/EditedSource.cpp
+++ b/clang/lib/Edit/EditedSource.cpp
@@ -1,4 +1,4 @@
-//===----- EditedSource.cpp - Collection of source edits ------------------===//
+//===- EditedSource.cpp - Collection of source edits ----------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -9,12 +9,21 @@
 
 #include "clang/Edit/EditedSource.h"
 #include "clang/Basic/CharInfo.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Edit/Commit.h"
 #include "clang/Edit/EditsReceiver.h"
+#include "clang/Edit/FileOffset.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
+#include <algorithm>
+#include <cassert>
+#include <tuple>
+#include <utility>
 
 using namespace clang;
 using namespace edit;
@@ -269,9 +278,11 @@
 
   struct CommitRAII {
     EditedSource &Editor;
+
     CommitRAII(EditedSource &Editor) : Editor(Editor) {
       Editor.startingCommit();
     }
+
     ~CommitRAII() {
       Editor.finishedCommit();
     }
diff --git a/clang/lib/Frontend/Rewrite/FixItRewriter.cpp b/clang/lib/Frontend/Rewrite/FixItRewriter.cpp
index dc787ac..17a35c3 100644
--- a/clang/lib/Frontend/Rewrite/FixItRewriter.cpp
+++ b/clang/lib/Frontend/Rewrite/FixItRewriter.cpp
@@ -1,4 +1,4 @@
-//===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===//
+//===- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -14,28 +14,32 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Rewrite/Frontend/FixItRewriter.h"
+#include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/FileManager.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Edit/Commit.h"
 #include "clang/Edit/EditsReceiver.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
-#include "llvm/Support/Path.h"
+#include "clang/Rewrite/Core/RewriteBuffer.h"
+#include "clang/Rewrite/Core/Rewriter.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cstdio>
 #include <memory>
+#include <string>
+#include <system_error>
+#include <utility>
 
 using namespace clang;
 
 FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
                              const LangOptions &LangOpts,
                              FixItOptions *FixItOpts)
-  : Diags(Diags),
-    Editor(SourceMgr, LangOpts),
-    Rewrite(SourceMgr, LangOpts),
-    FixItOpts(FixItOpts),
-    NumFailures(0),
-    PrevDiagSilenced(false) {
+    : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
+      FixItOpts(FixItOpts) {
   Owner = Diags.takeClient();
   Client = Diags.getClient();
   Diags.setClient(this, false);
@@ -59,20 +63,21 @@
   Rewriter &Rewrite;
 
 public:
-  RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
+  RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) {}
 
   void insert(SourceLocation loc, StringRef text) override {
     Rewrite.InsertText(loc, text);
   }
+
   void replace(CharSourceRange range, StringRef text) override {
     Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
   }
 };
 
-}
+} // namespace
 
 bool FixItRewriter::WriteFixedFiles(
-            std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
+             std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
   if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
     Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
     return true;
@@ -200,4 +205,4 @@
   Diags.setClient(this, false);
 }
 
-FixItOptions::~FixItOptions() {}
+FixItOptions::~FixItOptions() = default;
diff --git a/clang/lib/Rewrite/DeltaTree.cpp b/clang/lib/Rewrite/DeltaTree.cpp
index 352fab0..1dfc26c 100644
--- a/clang/lib/Rewrite/DeltaTree.cpp
+++ b/clang/lib/Rewrite/DeltaTree.cpp
@@ -1,4 +1,4 @@
-//===--- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ----------------===//
+//===- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -13,8 +13,10 @@
 
 #include "clang/Rewrite/Core/DeltaTree.h"
 #include "clang/Basic/LLVM.h"
-#include <cstdio>
+#include "llvm/Support/Casting.h"
+#include <cassert>
 #include <cstring>
+
 using namespace clang;
 
 /// The DeltaTree class is a multiway search tree (BTree) structure with some
@@ -33,6 +35,7 @@
 /// full delta implied by a whole subtree in constant time.
 
 namespace {
+
   /// SourceDelta - As code in the original input buffer is added and deleted,
   /// SourceDelta records are used to keep track of how the input SourceLocation
   /// object is mapped into the output buffer.
@@ -67,12 +70,11 @@
     enum { WidthFactor = 8 };
 
     /// Values - This tracks the SourceDelta's currently in this node.
-    ///
     SourceDelta Values[2*WidthFactor-1];
 
     /// NumValuesUsed - This tracks the number of values this node currently
     /// holds.
-    unsigned char NumValuesUsed;
+    unsigned char NumValuesUsed = 0;
 
     /// IsLeaf - This is true if this is a leaf of the btree.  If false, this is
     /// an interior node, and is actually an instance of DeltaTreeInteriorNode.
@@ -80,20 +82,22 @@
 
     /// FullDelta - This is the full delta of all the values in this node and
     /// all children nodes.
-    int FullDelta;
+    int FullDelta = 0;
+
   public:
-    DeltaTreeNode(bool isLeaf = true)
-      : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {}
+    DeltaTreeNode(bool isLeaf = true) : IsLeaf(isLeaf) {}
 
     bool isLeaf() const { return IsLeaf; }
     int getFullDelta() const { return FullDelta; }
     bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
 
     unsigned getNumValuesUsed() const { return NumValuesUsed; }
+
     const SourceDelta &getValue(unsigned i) const {
       assert(i < NumValuesUsed && "Invalid value #");
       return Values[i];
     }
+
     SourceDelta &getValue(unsigned i) {
       assert(i < NumValuesUsed && "Invalid value #");
       return Values[i];
@@ -114,23 +118,24 @@
 
     void Destroy();
   };
-} // end anonymous namespace
 
-namespace {
   /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
   /// This class tracks them.
   class DeltaTreeInteriorNode : public DeltaTreeNode {
+    friend class DeltaTreeNode;
+
     DeltaTreeNode *Children[2*WidthFactor];
+
     ~DeltaTreeInteriorNode() {
       for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
         Children[i]->Destroy();
     }
-    friend class DeltaTreeNode;
+
   public:
     DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
 
     DeltaTreeInteriorNode(const InsertResult &IR)
-      : DeltaTreeNode(false /*nonleaf*/) {
+        : DeltaTreeNode(false /*nonleaf*/) {
       Children[0] = IR.LHS;
       Children[1] = IR.RHS;
       Values[0] = IR.Split;
@@ -142,15 +147,16 @@
       assert(i < getNumValuesUsed()+1 && "Invalid child");
       return Children[i];
     }
+
     DeltaTreeNode *getChild(unsigned i) {
       assert(i < getNumValuesUsed()+1 && "Invalid child");
       return Children[i];
     }
 
-    static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
+    static bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
   };
-}
 
+} // namespace
 
 /// Destroy - A 'virtual' destructor.
 void DeltaTreeNode::Destroy() {
@@ -166,7 +172,7 @@
   int NewFullDelta = 0;
   for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
     NewFullDelta += Values[i].Delta;
-  if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this))
+  if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this))
     for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
       NewFullDelta += IN->getChild(i)->getFullDelta();
   FullDelta = NewFullDelta;
@@ -223,7 +229,7 @@
   }
 
   // Otherwise, this is an interior node.  Send the request down the tree.
-  DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this);
+  auto *IN = cast<DeltaTreeInteriorNode>(this);
   if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
     return false; // If there was space in the child, just return.
 
@@ -300,7 +306,7 @@
 
   // Create the new child node.
   DeltaTreeNode *NewNode;
-  if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
+  if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
     // If this is an interior node, also move over 'WidthFactor' children
     // into the new node.
     DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
@@ -328,8 +334,6 @@
   InsertRes.Split = Values[WidthFactor-1];
 }
 
-
-
 //===----------------------------------------------------------------------===//
 //                        DeltaTree Implementation
 //===----------------------------------------------------------------------===//
@@ -340,7 +344,7 @@
 /// VerifyTree - Walk the btree performing assertions on various properties to
 /// verify consistency.  This is useful for debugging new changes to the tree.
 static void VerifyTree(const DeltaTreeNode *N) {
-  const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N);
+  const auto *IN = dyn_cast<DeltaTreeInteriorNode>(N);
   if (IN == 0) {
     // Verify leaves, just ensure that FullDelta matches up and the elements
     // are in proper order.
@@ -387,6 +391,7 @@
 DeltaTree::DeltaTree() {
   Root = new DeltaTreeNode();
 }
+
 DeltaTree::DeltaTree(const DeltaTree &RHS) {
   // Currently we only support copying when the RHS is empty.
   assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
@@ -407,7 +412,7 @@
   int Result = 0;
 
   // Walk down the tree.
-  while (1) {
+  while (true) {
     // For all nodes, include any local deltas before the specified file
     // index by summing them up directly.  Keep track of how many were
     // included.
@@ -423,7 +428,7 @@
 
     // If we have an interior node, include information about children and
     // recurse.  Otherwise, if we have a leaf, we're done.
-    const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
+    const auto *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
     if (!IN) return Result;
 
     // Include any children to the left of the values we skipped, all of
@@ -461,4 +466,3 @@
   VerifyTree(MyRoot);
 #endif
 }
-
diff --git a/clang/lib/Rewrite/RewriteRope.cpp b/clang/lib/Rewrite/RewriteRope.cpp
index 030ab77..5bc79f3 100644
--- a/clang/lib/Rewrite/RewriteRope.cpp
+++ b/clang/lib/Rewrite/RewriteRope.cpp
@@ -1,4 +1,4 @@
-//===--- RewriteRope.cpp - Rope specialized for rewriter --------*- C++ -*-===//
+//===- RewriteRope.cpp - Rope specialized for rewriter --------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -13,7 +13,11 @@
 
 #include "clang/Rewrite/Core/RewriteRope.h"
 #include "clang/Basic/LLVM.h"
+#include "llvm/Support/Casting.h"
 #include <algorithm>
+#include <cassert>
+#include <cstring>
+
 using namespace clang;
 
 /// RewriteRope is a "strong" string class, designed to make insertions and
@@ -59,12 +63,12 @@
 ///     RopePieceBTreeInterior - An interior node in the B+ Tree, which manages
 ///          up to '2*WidthFactor' other nodes in the tree.
 
+namespace {
 
 //===----------------------------------------------------------------------===//
 // RopePieceBTreeNode Class
 //===----------------------------------------------------------------------===//
 
-namespace {
   /// RopePieceBTreeNode - Common base class of RopePieceBTreeLeaf and
   /// RopePieceBTreeInterior.  This provides some 'virtual' dispatching methods
   /// and a flag that determines which subclass the instance is.  Also
@@ -82,13 +86,13 @@
 
     /// Size - This is the number of bytes of file this node (including any
     /// potential children) covers.
-    unsigned Size;
+    unsigned Size = 0;
 
     /// IsLeaf - True if this is an instance of RopePieceBTreeLeaf, false if it
     /// is an instance of RopePieceBTreeInterior.
     bool IsLeaf;
 
-    RopePieceBTreeNode(bool isLeaf) : Size(0), IsLeaf(isLeaf) {}
+    RopePieceBTreeNode(bool isLeaf) : IsLeaf(isLeaf) {}
     ~RopePieceBTreeNode() = default;
 
   public:
@@ -116,15 +120,12 @@
     /// erase - Remove NumBytes from this node at the specified offset.  We are
     /// guaranteed that there is a split at Offset.
     void erase(unsigned Offset, unsigned NumBytes);
-
   };
-} // end anonymous namespace
 
 //===----------------------------------------------------------------------===//
 // RopePieceBTreeLeaf Class
 //===----------------------------------------------------------------------===//
 
-namespace {
   /// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
   /// nodes.  This directly represents a chunk of the string with those
   /// RopePieces contatenated.  Since this is a B+Tree, all values (in this case
@@ -135,18 +136,19 @@
   class RopePieceBTreeLeaf : public RopePieceBTreeNode {
     /// NumPieces - This holds the number of rope pieces currently active in the
     /// Pieces array.
-    unsigned char NumPieces;
+    unsigned char NumPieces = 0;
 
     /// Pieces - This tracks the file chunks currently in this leaf.
-    ///
     RopePiece Pieces[2*WidthFactor];
 
     /// NextLeaf - This is a pointer to the next leaf in the tree, allowing
     /// efficient in-order forward iteration of the tree without traversal.
-    RopePieceBTreeLeaf **PrevLeaf, *NextLeaf;
+    RopePieceBTreeLeaf **PrevLeaf = nullptr;
+    RopePieceBTreeLeaf *NextLeaf = nullptr;
+
   public:
-    RopePieceBTreeLeaf() : RopePieceBTreeNode(true), NumPieces(0),
-                           PrevLeaf(nullptr), NextLeaf(nullptr) {}
+    RopePieceBTreeLeaf() : RopePieceBTreeNode(true) {}
+
     ~RopePieceBTreeLeaf() {
       if (PrevLeaf || NextLeaf)
         removeFromLeafInOrder();
@@ -170,6 +172,7 @@
     }
 
     const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; }
+
     void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) {
       assert(!PrevLeaf && !NextLeaf && "Already in ordering");
 
@@ -214,16 +217,16 @@
     /// node is returned and must be inserted into a parent.
     RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
 
-
     /// erase - Remove NumBytes from this node at the specified offset.  We are
     /// guaranteed that there is a split at Offset.
     void erase(unsigned Offset, unsigned NumBytes);
 
-    static inline bool classof(const RopePieceBTreeNode *N) {
+    static bool classof(const RopePieceBTreeNode *N) {
       return N->isLeaf();
     }
   };
-} // end anonymous namespace
+
+} // namespace
 
 /// split - Split the range containing the specified offset so that we are
 /// guaranteed that there is a place to do an insertion at the specified
@@ -266,7 +269,6 @@
   return insert(Offset, Tail);
 }
 
-
 /// insert - Insert the specified RopePiece into this tree node at the
 /// specified offset.  The offset is relative, so "0" is the start of the node.
 ///
@@ -388,18 +390,21 @@
 //===----------------------------------------------------------------------===//
 
 namespace {
+
   /// RopePieceBTreeInterior - This represents an interior node in the B+Tree,
   /// which holds up to 2*WidthFactor pointers to child nodes.
   class RopePieceBTreeInterior : public RopePieceBTreeNode {
     /// NumChildren - This holds the number of children currently active in the
     /// Children array.
-    unsigned char NumChildren;
+    unsigned char NumChildren = 0;
+
     RopePieceBTreeNode *Children[2*WidthFactor];
+
   public:
-    RopePieceBTreeInterior() : RopePieceBTreeNode(false), NumChildren(0) {}
+    RopePieceBTreeInterior() : RopePieceBTreeNode(false) {}
 
     RopePieceBTreeInterior(RopePieceBTreeNode *LHS, RopePieceBTreeNode *RHS)
-    : RopePieceBTreeNode(false) {
+        : RopePieceBTreeNode(false) {
       Children[0] = LHS;
       Children[1] = RHS;
       NumChildren = 2;
@@ -414,10 +419,12 @@
     bool isFull() const { return NumChildren == 2*WidthFactor; }
 
     unsigned getNumChildren() const { return NumChildren; }
+
     const RopePieceBTreeNode *getChild(unsigned i) const {
       assert(i < NumChildren && "invalid child #");
       return Children[i];
     }
+
     RopePieceBTreeNode *getChild(unsigned i) {
       assert(i < NumChildren && "invalid child #");
       return Children[i];
@@ -431,7 +438,6 @@
         Size += getChild(i)->size();
     }
 
-
     /// split - Split the range containing the specified offset so that we are
     /// guaranteed that there is a place to do an insertion at the specified
     /// offset.  The offset is relative, so "0" is the start of the node.
@@ -440,7 +446,6 @@
     /// node is returned and must be inserted into a parent.
     RopePieceBTreeNode *split(unsigned Offset);
 
-
     /// insert - Insert the specified ropepiece into this tree node at the
     /// specified offset.  The offset is relative, so "0" is the start of the
     /// node.
@@ -457,11 +462,12 @@
     /// guaranteed that there is a split at Offset.
     void erase(unsigned Offset, unsigned NumBytes);
 
-    static inline bool classof(const RopePieceBTreeNode *N) {
+    static bool classof(const RopePieceBTreeNode *N) {
       return !N->isLeaf();
     }
   };
-} // end anonymous namespace
+
+} // namespace
 
 /// split - Split the range containing the specified offset so that we are
 /// guaranteed that there is a place to do an insertion at the specified
@@ -613,7 +619,7 @@
 //===----------------------------------------------------------------------===//
 
 void RopePieceBTreeNode::Destroy() {
-  if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
+  if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
     delete Leaf;
   else
     delete cast<RopePieceBTreeInterior>(this);
@@ -627,7 +633,7 @@
 /// node is returned and must be inserted into a parent.
 RopePieceBTreeNode *RopePieceBTreeNode::split(unsigned Offset) {
   assert(Offset <= size() && "Invalid offset to split!");
-  if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
+  if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
     return Leaf->split(Offset);
   return cast<RopePieceBTreeInterior>(this)->split(Offset);
 }
@@ -641,7 +647,7 @@
 RopePieceBTreeNode *RopePieceBTreeNode::insert(unsigned Offset,
                                                const RopePiece &R) {
   assert(Offset <= size() && "Invalid offset to insert!");
-  if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
+  if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
     return Leaf->insert(Offset, R);
   return cast<RopePieceBTreeInterior>(this)->insert(Offset, R);
 }
@@ -650,12 +656,11 @@
 /// guaranteed that there is a split at Offset.
 void RopePieceBTreeNode::erase(unsigned Offset, unsigned NumBytes) {
   assert(Offset+NumBytes <= size() && "Invalid offset to erase!");
-  if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
+  if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
     return Leaf->erase(Offset, NumBytes);
   return cast<RopePieceBTreeInterior>(this)->erase(Offset, NumBytes);
 }
 
-
 //===----------------------------------------------------------------------===//
 // RopePieceBTreeIterator Implementation
 //===----------------------------------------------------------------------===//
@@ -666,10 +671,10 @@
 
 // begin iterator.
 RopePieceBTreeIterator::RopePieceBTreeIterator(const void *n) {
-  const RopePieceBTreeNode *N = static_cast<const RopePieceBTreeNode*>(n);
+  const auto *N = static_cast<const RopePieceBTreeNode *>(n);
 
   // Walk down the left side of the tree until we get to a leaf.
-  while (const RopePieceBTreeInterior *IN = dyn_cast<RopePieceBTreeInterior>(N))
+  while (const auto *IN = dyn_cast<RopePieceBTreeInterior>(N))
     N = IN->getChild(0);
 
   // We must have at least one leaf.
@@ -717,10 +722,12 @@
 RopePieceBTree::RopePieceBTree() {
   Root = new RopePieceBTreeLeaf();
 }
+
 RopePieceBTree::RopePieceBTree(const RopePieceBTree &RHS) {
   assert(RHS.empty() && "Can't copy non-empty tree yet");
   Root = new RopePieceBTreeLeaf();
 }
+
 RopePieceBTree::~RopePieceBTree() {
   getRoot(Root)->Destroy();
 }
@@ -730,7 +737,7 @@
 }
 
 void RopePieceBTree::clear() {
-  if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(getRoot(Root)))
+  if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(getRoot(Root)))
     Leaf->clear();
   else {
     getRoot(Root)->Destroy();
@@ -780,8 +787,7 @@
   // just allocate a new rope piece for it alone.
   if (Len > AllocChunkSize) {
     unsigned Size = End-Start+sizeof(RopeRefCountString)-1;
-    RopeRefCountString *Res =
-      reinterpret_cast<RopeRefCountString *>(new char[Size]);
+    auto *Res = reinterpret_cast<RopeRefCountString *>(new char[Size]);
     Res->RefCount = 0;
     memcpy(Res->Data, Start, End-Start);
     return RopePiece(Res, 0, End-Start);
@@ -791,8 +797,7 @@
   // Make a new chunk and share it with later allocations.
 
   unsigned AllocSize = offsetof(RopeRefCountString, Data) + AllocChunkSize;
-  RopeRefCountString *Res =
-      reinterpret_cast<RopeRefCountString *>(new char[AllocSize]);
+  auto *Res = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]);
   Res->RefCount = 0;
   memcpy(Res->Data, Start, Len);
   AllocBuffer = Res;
@@ -800,5 +805,3 @@
 
   return RopePiece(AllocBuffer, 0, Len);
 }
-
-
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index ae41dec..6041e0c 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -1,4 +1,4 @@
-//===--- Rewriter.cpp - Code rewriting interface --------------------------===//
+//===- Rewriter.cpp - Code rewriting interface ----------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -15,11 +15,24 @@
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
+#include "clang/Rewrite/Core/RewriteBuffer.h"
+#include "clang/Rewrite/Core/RewriteRope.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
+#include <cassert>
+#include <iterator>
+#include <map>
+#include <memory>
+#include <system_error>
+#include <utility>
+
 using namespace clang;
 
 raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
@@ -91,7 +104,6 @@
 
 void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
                                bool InsertAfter) {
-
   // Nothing to insert, exit early.
   if (Str.empty()) return;
 
@@ -114,7 +126,6 @@
     AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
 }
 
-
 //===----------------------------------------------------------------------===//
 // Rewriter class
 //===----------------------------------------------------------------------===//
@@ -127,10 +138,8 @@
       !isRewritable(Range.getEnd())) return -1;
 
   FileID StartFileID, EndFileID;
-  unsigned StartOff, EndOff;
-
-  StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
-  EndOff   = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
+  unsigned StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
+  unsigned EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
 
   if (StartFileID != EndFileID)
     return -1;
@@ -145,7 +154,6 @@
     StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
   }
 
-
   // Adjust the end offset to the end of the last token, instead of being the
   // start of the last token if this is a token range.
   if (Range.isTokenRange())
@@ -158,17 +166,15 @@
   return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
 }
 
-
 /// getRewrittenText - Return the rewritten form of the text in the specified
 /// range.  If the start or end of the range was unrewritable or if they are
 /// in different buffers, this returns an empty string.
 ///
 /// Note that this method is not particularly efficient.
-///
 std::string Rewriter::getRewrittenText(SourceRange Range) const {
   if (!isRewritable(Range.getBegin()) ||
       !isRewritable(Range.getEnd()))
-    return "";
+    return {};
 
   FileID StartFileID, EndFileID;
   unsigned StartOff, EndOff;
@@ -176,7 +182,7 @@
   EndOff   = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
 
   if (StartFileID != EndFileID)
-    return ""; // Start and end in different buffers.
+    return {}; // Start and end in different buffers.
 
   // If edits have been made to this buffer, the delta between the range may
   // have changed.
@@ -212,14 +218,12 @@
 unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
                                               FileID &FID) const {
   assert(Loc.isValid() && "Invalid location");
-  std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
+  std::pair<FileID, unsigned> V = SourceMgr->getDecomposedLoc(Loc);
   FID = V.first;
   return V.second;
 }
 
-
 /// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
-///
 RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
   std::map<FileID, RewriteBuffer>::iterator I =
     RewriteBuffers.lower_bound(FID);
@@ -393,6 +397,7 @@
 }
 
 namespace {
+
 // A wrapper for a file stream that atomically overwrites the target.
 //
 // Creates a file output stream for a temporary file in the constructor,
@@ -403,7 +408,7 @@
 public:
   AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
                       bool &AllWritten)
-    : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
+      : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
     TempFilename = Filename;
     TempFilename += "-%%%%%%%%";
     int FD;
@@ -441,7 +446,8 @@
   std::unique_ptr<llvm::raw_fd_ostream> FileStream;
   bool &AllWritten;
 };
-} // end anonymous namespace
+
+} // namespace
 
 bool Rewriter::overwriteChangedFiles() {
   bool AllWritten = true;
diff --git a/clang/lib/Rewrite/TokenRewriter.cpp b/clang/lib/Rewrite/TokenRewriter.cpp
index 494defd..1f5dec4 100644
--- a/clang/lib/Rewrite/TokenRewriter.cpp
+++ b/clang/lib/Rewrite/TokenRewriter.cpp
@@ -1,4 +1,4 @@
-//===--- TokenRewriter.cpp - Token-based code rewriting interface ---------===//
+//===- TokenRewriter.cpp - Token-based code rewriting interface -----------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -16,6 +16,12 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Lex/ScratchBuffer.h"
+#include "clang/Lex/Token.h"
+#include <cassert>
+#include <cstring>
+#include <map>
+#include <utility>
+
 using namespace clang;
 
 TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,
@@ -46,9 +52,7 @@
   }
 }
 
-TokenRewriter::~TokenRewriter() {
-}
-
+TokenRewriter::~TokenRewriter() = default;
 
 /// RemapIterator - Convert from token_iterator (a const iterator) to
 /// TokenRefTy (a non-const iterator).
@@ -63,7 +67,6 @@
   return MapIt->second;
 }
 
-
 /// AddToken - Add the specified token into the Rewriter before the other
 /// position.
 TokenRewriter::TokenRefTy
@@ -77,7 +80,6 @@
   return Where;
 }
 
-
 TokenRewriter::token_iterator
 TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) {
   unsigned Len = strlen(Val);
@@ -96,4 +98,3 @@
 
   return AddToken(Tok, RemapIterator(I));
 }
-