diff options
author | Eugene Zelenko <eugene.zelenko@gmail.com> | 2018-03-27 00:01:49 +0000 |
---|---|---|
committer | Eugene Zelenko <eugene.zelenko@gmail.com> | 2018-03-27 00:01:49 +0000 |
commit | f06173045da016eb4a580be2a85da52a087ff72a (patch) | |
tree | 31a14887a36d13bf6b08ef41918272cf51248444 /lib/Rewrite | |
parent | a21970a87881c94771038020ed3aaae39d39fe4e (diff) | |
download | clang-f06173045da016eb4a580be2a85da52a087ff72a.tar.gz |
[Edit, Rewrite] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@328597 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Rewrite')
-rw-r--r-- | lib/Rewrite/DeltaTree.cpp | 48 | ||||
-rw-r--r-- | lib/Rewrite/RewriteRope.cpp | 77 | ||||
-rw-r--r-- | lib/Rewrite/Rewriter.cpp | 40 | ||||
-rw-r--r-- | lib/Rewrite/TokenRewriter.cpp | 15 |
4 files changed, 97 insertions, 83 deletions
diff --git a/lib/Rewrite/DeltaTree.cpp b/lib/Rewrite/DeltaTree.cpp index 352fab077a..1dfc26cc91 100644 --- a/lib/Rewrite/DeltaTree.cpp +++ b/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 @@ using namespace clang; /// 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 @@ namespace { 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 @@ namespace { /// 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 @@ namespace { 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 @@ namespace { 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 @@ void DeltaTreeNode::RecomputeFullDeltaLocally() { 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 @@ bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, } // 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 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { // 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 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { InsertRes.Split = Values[WidthFactor-1]; } - - //===----------------------------------------------------------------------===// // DeltaTree Implementation //===----------------------------------------------------------------------===// @@ -340,7 +344,7 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { /// 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 @@ static DeltaTreeNode *getRoot(void *Root) { 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 DeltaTree::getDeltaAt(unsigned FileIndex) const { 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 @@ int DeltaTree::getDeltaAt(unsigned FileIndex) const { // 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 @@ void DeltaTree::AddDelta(unsigned FileIndex, int Delta) { VerifyTree(MyRoot); #endif } - diff --git a/lib/Rewrite/RewriteRope.cpp b/lib/Rewrite/RewriteRope.cpp index 030ab7732f..5bc79f3edd 100644 --- a/lib/Rewrite/RewriteRope.cpp +++ b/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 @@ using namespace clang; /// 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 @@ namespace { /// 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 @@ namespace { /// 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 @@ namespace { 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 @@ namespace { } const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; } + void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) { assert(!PrevLeaf && !NextLeaf && "Already in ordering"); @@ -214,16 +217,16 @@ namespace { /// 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 @@ RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) { 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 @@ void RopePieceBTreeLeaf::erase(unsigned Offset, unsigned NumBytes) { //===----------------------------------------------------------------------===// 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 @@ namespace { 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 @@ namespace { 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 @@ namespace { /// 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 @@ namespace { /// 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 RopePieceBTreeInterior::erase(unsigned Offset, unsigned NumBytes) { //===----------------------------------------------------------------------===// 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 @@ void RopePieceBTreeNode::Destroy() { /// 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::split(unsigned Offset) { 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 @@ RopePieceBTreeNode *RopePieceBTreeNode::insert(unsigned Offset, /// 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 @@ static const RopePieceBTreeLeaf *getCN(const void *P) { // 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 @@ static RopePieceBTreeNode *getRoot(void *P) { 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 @@ unsigned RopePieceBTree::size() const { } 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 @@ RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) { // 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 @@ RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) { // 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 @@ RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) { return RopePiece(AllocBuffer, 0, Len); } - - diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp index ae41decc64..6041e0c0c3 100644 --- a/lib/Rewrite/Rewriter.cpp +++ b/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::RemoveText(unsigned OrigOffset, unsigned Size, void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str, bool InsertAfter) { - // Nothing to insert, exit early. if (Str.empty()) return; @@ -114,7 +126,6 @@ void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength); } - //===----------------------------------------------------------------------===// // Rewriter class //===----------------------------------------------------------------------===// @@ -127,10 +138,8 @@ int Rewriter::getRangeSize(const CharSourceRange &Range, !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 @@ int Rewriter::getRangeSize(const CharSourceRange &Range, 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 @@ int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const { 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 @@ std::string Rewriter::getRewrittenText(SourceRange Range) const { 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 @@ std::string Rewriter::getRewrittenText(SourceRange Range) const { 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 @@ bool Rewriter::IncreaseIndentation(CharSourceRange range, } 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 @@ class AtomicallyMovedFile { 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 @@ private: std::unique_ptr<llvm::raw_fd_ostream> FileStream; bool &AllWritten; }; -} // end anonymous namespace + +} // namespace bool Rewriter::overwriteChangedFiles() { bool AllWritten = true; diff --git a/lib/Rewrite/TokenRewriter.cpp b/lib/Rewrite/TokenRewriter.cpp index 494defdeda..1f5dec499c 100644 --- a/lib/Rewrite/TokenRewriter.cpp +++ b/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(FileID FID, SourceManager &SM, } } -TokenRewriter::~TokenRewriter() { -} - +TokenRewriter::~TokenRewriter() = default; /// RemapIterator - Convert from token_iterator (a const iterator) to /// TokenRefTy (a non-const iterator). @@ -63,7 +67,6 @@ TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) { return MapIt->second; } - /// AddToken - Add the specified token into the Rewriter before the other /// position. TokenRewriter::TokenRefTy @@ -77,7 +80,6 @@ TokenRewriter::AddToken(const Token &T, TokenRefTy Where) { return Where; } - TokenRewriter::token_iterator TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) { unsigned Len = strlen(Val); @@ -96,4 +98,3 @@ TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) { return AddToken(Tok, RemapIterator(I)); } - |