[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
This commit is contained in:
Eugene Zelenko 2018-03-27 00:01:49 +00:00
parent a21970a878
commit f06173045d
17 changed files with 307 additions and 238 deletions

View File

@ -1,4 +1,4 @@
//===----- Commit.h - A unit of edits ---------------------------*- C++ -*-===//
//===- Commit.h - A unit of edits -------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -10,17 +10,22 @@
#ifndef LLVM_CLANG_EDIT_COMMIT_H
#define LLVM_CLANG_EDIT_COMMIT_H
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Edit/FileOffset.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Allocator.h"
namespace clang {
class LangOptions;
class PPConditionalDirectiveRecord;
class LangOptions;
class PPConditionalDirectiveRecord;
class SourceManager;
namespace edit {
class EditedSource;
class EditedSource;
class Commit {
public:
@ -48,9 +53,9 @@ private:
const SourceManager &SourceMgr;
const LangOptions &LangOpts;
const PPConditionalDirectiveRecord *PPRec;
EditedSource *Editor;
EditedSource *Editor = nullptr;
bool IsCommitable;
bool IsCommitable = true;
SmallVector<Edit, 8> CachedEdits;
llvm::BumpPtrAllocator StrAlloc;
@ -59,21 +64,23 @@ public:
explicit Commit(EditedSource &Editor);
Commit(const SourceManager &SM, const LangOptions &LangOpts,
const PPConditionalDirectiveRecord *PPRec = nullptr)
: SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), Editor(nullptr),
IsCommitable(true) { }
: SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec) {}
bool isCommitable() const { return IsCommitable; }
bool insert(SourceLocation loc, StringRef text, bool afterToken = false,
bool beforePreviousInsertions = false);
bool insertAfterToken(SourceLocation loc, StringRef text,
bool beforePreviousInsertions = false) {
return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
}
bool insertBefore(SourceLocation loc, StringRef text) {
return insert(loc, text, /*afterToken=*/false,
/*beforePreviousInsertions=*/true);
}
bool insertFromRange(SourceLocation loc, CharSourceRange range,
bool afterToken = false,
bool beforePreviousInsertions = false);
@ -92,21 +99,26 @@ public:
return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
afterToken, beforePreviousInsertions);
}
bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after) {
return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
}
bool remove(SourceRange TokenRange) {
return remove(CharSourceRange::getTokenRange(TokenRange));
}
bool replace(SourceRange TokenRange, StringRef text) {
return replace(CharSourceRange::getTokenRange(TokenRange), text);
}
bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange) {
return replaceWithInner(CharSourceRange::getTokenRange(TokenRange),
CharSourceRange::getTokenRange(TokenInnerRange));
}
typedef SmallVectorImpl<Edit>::const_iterator edit_iterator;
using edit_iterator = SmallVectorImpl<Edit>::const_iterator;
edit_iterator edit_begin() const { return CachedEdits.begin(); }
edit_iterator edit_end() const { return CachedEdits.end(); }
@ -136,8 +148,8 @@ private:
SourceLocation *MacroEnd = nullptr) const;
};
}
} // namespace edit
} // end namespace clang
} // namespace clang
#endif
#endif // LLVM_CLANG_EDIT_COMMIT_H

View File

@ -1,4 +1,4 @@
//===----- EditedSource.h - Collection of source edits ----------*- C++ -*-===//
//===- EditedSource.h - Collection of source edits --------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -11,21 +11,27 @@
#define LLVM_CLANG_EDIT_EDITEDSOURCE_H
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Edit/FileOffset.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/Allocator.h"
#include <map>
#include <tuple>
#include <utility>
namespace clang {
class LangOptions;
class PPConditionalDirectiveRecord;
class LangOptions;
class PPConditionalDirectiveRecord;
class SourceManager;
namespace edit {
class Commit;
class EditsReceiver;
class Commit;
class EditsReceiver;
class EditedSource {
const SourceManager &SourceMgr;
@ -34,17 +40,19 @@ class EditedSource {
struct FileEdit {
StringRef Text;
unsigned RemoveLen;
unsigned RemoveLen = 0;
FileEdit() : RemoveLen(0) {}
FileEdit() = default;
};
typedef std::map<FileOffset, FileEdit> FileEditsTy;
using FileEditsTy = std::map<FileOffset, FileEdit>;
FileEditsTy FileEdits;
struct MacroArgUse {
IdentifierInfo *Identifier;
SourceLocation ImmediateExpansionLoc;
// Location of argument use inside the top-level macro
SourceLocation UseLoc;
@ -65,11 +73,11 @@ class EditedSource {
public:
EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
const PPConditionalDirectiveRecord *PPRec = nullptr)
: SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts),
StrAlloc() { }
: SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts) {}
const SourceManager &getSourceManager() const { return SourceMgr; }
const LangOptions &getLangOpts() const { return LangOpts; }
const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
return PPRec;
}
@ -103,8 +111,8 @@ private:
void finishedCommit();
};
}
} // namespace edit
} // end namespace clang
} // namespace clang
#endif
#endif // LLVM_CLANG_EDIT_EDITEDSOURCE_H

View File

@ -1,4 +1,4 @@
//===----- EditedSource.h - Collection of source edits ----------*- C++ -*-===//
//===- EditedSource.h - Collection of source edits --------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -11,25 +11,24 @@
#define LLVM_CLANG_EDIT_EDITSRECEIVER_H
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/StringRef.h"
namespace clang {
class SourceLocation;
class CharSourceRange;
namespace edit {
class EditsReceiver {
public:
virtual ~EditsReceiver() { }
virtual ~EditsReceiver() = default;
virtual void insert(SourceLocation loc, StringRef text) = 0;
virtual void replace(CharSourceRange range, StringRef text) = 0;
/// \brief By default it calls replace with an empty string.
virtual void remove(CharSourceRange range);
};
}
} // namespace edit
} // namespace clang
} // end namespace clang
#endif
#endif // LLVM_CLANG_EDIT_EDITSRECEIVER_H

View File

@ -1,4 +1,4 @@
//===----- FileOffset.h - Offset in a file ----------------------*- C++ -*-===//
//===- FileOffset.h - Offset in a file --------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -11,17 +11,18 @@
#define LLVM_CLANG_EDIT_FILEOFFSET_H
#include "clang/Basic/SourceLocation.h"
#include <tuple>
namespace clang {
namespace edit {
class FileOffset {
FileID FID;
unsigned Offs;
unsigned Offs = 0;
public:
FileOffset() : Offs(0) { }
FileOffset(FileID fid, unsigned offs) : FID(fid), Offs(offs) { }
FileOffset() = default;
FileOffset(FileID fid, unsigned offs) : FID(fid), Offs(offs) {}
bool isInvalid() const { return FID.isInvalid(); }
@ -37,25 +38,29 @@ public:
friend bool operator==(FileOffset LHS, FileOffset RHS) {
return LHS.FID == RHS.FID && LHS.Offs == RHS.Offs;
}
friend bool operator!=(FileOffset LHS, FileOffset RHS) {
return !(LHS == RHS);
}
friend bool operator<(FileOffset LHS, FileOffset RHS) {
return std::tie(LHS.FID, LHS.Offs) < std::tie(RHS.FID, RHS.Offs);
}
friend bool operator>(FileOffset LHS, FileOffset RHS) {
return RHS < LHS;
}
friend bool operator>=(FileOffset LHS, FileOffset RHS) {
return !(LHS < RHS);
}
friend bool operator<=(FileOffset LHS, FileOffset RHS) {
return !(RHS < LHS);
}
};
}
} // namespace edit
} // namespace clang
} // end namespace clang
#endif
#endif // LLVM_CLANG_EDIT_FILEOFFSET_H

View File

@ -1,4 +1,4 @@
//===--- DeltaTree.h - B-Tree for Rewrite Delta tracking --------*- C++ -*-===//
//===- DeltaTree.h - B-Tree for Rewrite Delta tracking ----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -14,8 +14,6 @@
#ifndef LLVM_CLANG_REWRITE_CORE_DELTATREE_H
#define LLVM_CLANG_REWRITE_CORE_DELTATREE_H
#include "llvm/Support/Compiler.h"
namespace clang {
/// DeltaTree - a multiway search tree (BTree) structure with some fancy
@ -27,12 +25,14 @@ namespace clang {
/// as well, without traversing the whole tree.
class DeltaTree {
void *Root; // "DeltaTreeNode *"
void operator=(const DeltaTree &) = delete;
public:
DeltaTree();
// Note: Currently we only support copying when the RHS is empty.
DeltaTree(const DeltaTree &RHS);
DeltaTree &operator=(const DeltaTree &) = delete;
~DeltaTree();
/// getDeltaAt - Return the accumulated delta at the specified file offset.
@ -45,6 +45,7 @@ namespace clang {
/// into the current DeltaTree at offset FileIndex.
void AddDelta(unsigned FileIndex, int Delta);
};
} // end namespace clang
#endif
} // namespace clang
#endif // LLVM_CLANG_REWRITE_CORE_DELTATREE_H

View File

@ -1,4 +1,4 @@
//===--- RewriteBuffer.h - Buffer rewriting interface -----------*- C++ -*-===//
//===- RewriteBuffer.h - Buffer rewriting interface -------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -16,7 +16,6 @@
#include "llvm/ADT/StringRef.h"
namespace clang {
class Rewriter;
/// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
/// input with modifications get a new RewriteBuffer associated with them. The
@ -26,12 +25,16 @@ namespace clang {
/// locations after the insertion point have to be mapped.
class RewriteBuffer {
friend class Rewriter;
/// Deltas - Keep track of all the deltas in the source code due to insertions
/// and deletions.
DeltaTree Deltas;
RewriteRope Buffer;
public:
typedef RewriteRope::const_iterator iterator;
using iterator = RewriteRope::const_iterator;
iterator begin() const { return Buffer.begin(); }
iterator end() const { return Buffer.end(); }
unsigned size() const { return Buffer.size(); }
@ -61,7 +64,6 @@ public:
/// InsertText - Insert some text at the specified point, where the offset in
/// the buffer is specified relative to the original SourceBuffer. The
/// text is inserted after the specified location.
///
void InsertText(unsigned OrigOffset, StringRef Str,
bool InsertAfter = true);
@ -87,8 +89,7 @@ public:
void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
StringRef NewStr);
private: // Methods only usable by Rewriter.
private:
/// getMappedOffset - Given an offset into the original SourceBuffer that this
/// RewriteBuffer is based on, map it into the offset space of the
/// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a
@ -112,6 +113,6 @@ private: // Methods only usable by Rewriter.
}
};
} // end namespace clang
} // namespace clang
#endif
#endif // LLVM_CLANG_REWRITE_CORE_REWRITEBUFFER_H

View File

@ -1,4 +1,4 @@
//===--- RewriteRope.h - Rope specialized for rewriter ----------*- C++ -*-===//
//===- RewriteRope.h - Rope specialized for rewriter ------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -16,13 +16,13 @@
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <utility>
namespace clang {
//===--------------------------------------------------------------------===//
// RopeRefCountString Class
//===--------------------------------------------------------------------===//
@ -58,11 +58,10 @@ namespace clang {
/// different offsets) which is a nice constant time operation.
struct RopePiece {
llvm::IntrusiveRefCntPtr<RopeRefCountString> StrData;
unsigned StartOffs;
unsigned EndOffs;
RopePiece() : StrData(nullptr), StartOffs(0), EndOffs(0) {}
unsigned StartOffs = 0;
unsigned EndOffs = 0;
RopePiece() = default;
RopePiece(llvm::IntrusiveRefCntPtr<RopeRefCountString> Str, unsigned Start,
unsigned End)
: StrData(std::move(Str)), StartOffs(Start), EndOffs(End) {}
@ -88,18 +87,18 @@ namespace clang {
class RopePieceBTreeIterator :
public std::iterator<std::forward_iterator_tag, const char, ptrdiff_t> {
/// CurNode - The current B+Tree node that we are inspecting.
const void /*RopePieceBTreeLeaf*/ *CurNode;
const void /*RopePieceBTreeLeaf*/ *CurNode = nullptr;
/// CurPiece - The current RopePiece in the B+Tree node that we're
/// inspecting.
const RopePiece *CurPiece;
const RopePiece *CurPiece = nullptr;
/// CurChar - The current byte in the RopePiece we are pointing to.
unsigned CurChar;
unsigned CurChar = 0;
public:
// begin iterator.
RopePieceBTreeIterator() = default;
RopePieceBTreeIterator(const void /*RopePieceBTreeNode*/ *N);
// end iterator
RopePieceBTreeIterator()
: CurNode(nullptr), CurPiece(nullptr), CurChar(0) {}
char operator*() const {
return (*CurPiece)[CurChar];
@ -119,7 +118,8 @@ namespace clang {
MoveToNextPiece();
return *this;
}
inline RopePieceBTreeIterator operator++(int) { // Postincrement
RopePieceBTreeIterator operator++(int) { // Postincrement
RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
}
@ -136,13 +136,15 @@ namespace clang {
class RopePieceBTree {
void /*RopePieceBTreeNode*/ *Root;
void operator=(const RopePieceBTree &) = delete;
public:
RopePieceBTree();
RopePieceBTree(const RopePieceBTree &RHS);
RopePieceBTree &operator=(const RopePieceBTree &) = delete;
~RopePieceBTree();
typedef RopePieceBTreeIterator iterator;
using iterator = RopePieceBTreeIterator;
iterator begin() const { return iterator(Root); }
iterator end() const { return iterator(); }
unsigned size() const;
@ -168,19 +170,18 @@ class RewriteRope {
/// We allocate space for string data out of a buffer of size AllocChunkSize.
/// This keeps track of how much space is left.
llvm::IntrusiveRefCntPtr<RopeRefCountString> AllocBuffer;
unsigned AllocOffs;
enum { AllocChunkSize = 4080 };
unsigned AllocOffs = AllocChunkSize;
public:
RewriteRope() : AllocBuffer(nullptr), AllocOffs(AllocChunkSize) {}
RewriteRope(const RewriteRope &RHS)
: Chunks(RHS.Chunks), AllocBuffer(nullptr), AllocOffs(AllocChunkSize) {
}
RewriteRope() = default;
RewriteRope(const RewriteRope &RHS) : Chunks(RHS.Chunks) {}
using iterator = RopePieceBTree::iterator;
using const_iterator = RopePieceBTree::iterator;
typedef RopePieceBTree::iterator iterator;
typedef RopePieceBTree::iterator const_iterator;
iterator begin() const { return Chunks.begin(); }
iterator end() const { return Chunks.end(); }
iterator end() const { return Chunks.end(); }
unsigned size() const { return Chunks.size(); }
void clear() {
@ -209,6 +210,6 @@ private:
RopePiece MakeRopeString(const char *Start, const char *End);
};
} // end namespace clang
} // namespace clang
#endif
#endif // LLVM_CLANG_REWRITE_CORE_REWRITEROPE_H

View File

@ -1,4 +1,4 @@
//===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===//
//===- Rewriter.h - Code rewriting interface --------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -15,52 +15,55 @@
#ifndef LLVM_CLANG_REWRITE_CORE_REWRITER_H
#define LLVM_CLANG_REWRITE_CORE_REWRITER_H
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Rewrite/Core/RewriteBuffer.h"
#include <cstring>
#include "llvm/ADT/StringRef.h"
#include <map>
#include <string>
namespace clang {
class LangOptions;
class SourceManager;
class LangOptions;
class SourceManager;
/// Rewriter - This is the main interface to the rewrite buffers. Its primary
/// job is to dispatch high-level requests to the low-level RewriteBuffers that
/// are involved.
class Rewriter {
SourceManager *SourceMgr;
const LangOptions *LangOpts;
SourceManager *SourceMgr = nullptr;
const LangOptions *LangOpts = nullptr;
std::map<FileID, RewriteBuffer> RewriteBuffers;
public:
struct RewriteOptions {
/// \brief Given a source range, true to include previous inserts at the
/// beginning of the range as part of the range itself (true by default).
bool IncludeInsertsAtBeginOfRange;
bool IncludeInsertsAtBeginOfRange = true;
/// \brief Given a source range, true to include previous inserts at the
/// end of the range as part of the range itself (true by default).
bool IncludeInsertsAtEndOfRange;
bool IncludeInsertsAtEndOfRange = true;
/// \brief If true and removing some text leaves a blank line
/// also remove the empty line (false by default).
bool RemoveLineIfEmpty;
bool RemoveLineIfEmpty = false;
RewriteOptions()
: IncludeInsertsAtBeginOfRange(true),
IncludeInsertsAtEndOfRange(true),
RemoveLineIfEmpty(false) { }
RewriteOptions() {}
};
typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator;
typedef std::map<FileID, RewriteBuffer>::const_iterator const_buffer_iterator;
using buffer_iterator = std::map<FileID, RewriteBuffer>::iterator;
using const_buffer_iterator = std::map<FileID, RewriteBuffer>::const_iterator;
explicit Rewriter() = default;
explicit Rewriter(SourceManager &SM, const LangOptions &LO)
: SourceMgr(&SM), LangOpts(&LO) {}
explicit Rewriter() : SourceMgr(nullptr), LangOpts(nullptr) {}
: SourceMgr(&SM), LangOpts(&LO) {}
void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
SourceMgr = &SM;
LangOpts = &LO;
}
SourceManager &getSourceMgr() const { return *SourceMgr; }
const LangOptions &getLangOpts() const { return *LangOpts; }
@ -82,7 +85,6 @@ public:
/// in different buffers, this returns an empty string.
///
/// Note that this method is not particularly efficient.
///
std::string getRewrittenText(SourceRange Range) const;
/// InsertText - Insert the specified string at the specified location in the
@ -190,6 +192,6 @@ private:
unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
};
} // end namespace clang
} // namespace clang
#endif
#endif // LLVM_CLANG_REWRITE_CORE_REWRITER_H

View File

@ -1,4 +1,4 @@
//===--- TokenRewriter.h - Token-based Rewriter -----------------*- C++ -*-===//
//===- TokenRewriter.h - Token-based Rewriter -------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -17,13 +17,16 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/Token.h"
#include <cassert>
#include <list>
#include <map>
#include <memory>
namespace clang {
class LangOptions;
class ScratchBuffer;
class LangOptions;
class ScratchBuffer;
class SourceManager;
class TokenRewriter {
/// TokenList - This is the list of raw tokens that make up this file. Each
@ -31,7 +34,7 @@ namespace clang {
std::list<Token> TokenList;
/// TokenRefTy - This is the type used to refer to a token in the TokenList.
typedef std::list<Token>::iterator TokenRefTy;
using TokenRefTy = std::list<Token>::iterator;
/// TokenAtLoc - This map indicates which token exists at a specific
/// SourceLocation. Since each token has a unique SourceLocation, this is a
@ -40,23 +43,24 @@ namespace clang {
std::map<SourceLocation, TokenRefTy> TokenAtLoc;
/// ScratchBuf - This is the buffer that we create scratch tokens from.
///
std::unique_ptr<ScratchBuffer> ScratchBuf;
TokenRewriter(const TokenRewriter &) = delete;
void operator=(const TokenRewriter &) = delete;
public:
/// TokenRewriter - This creates a TokenRewriter for the file with the
/// specified FileID.
TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LO);
TokenRewriter(const TokenRewriter &) = delete;
TokenRewriter &operator=(const TokenRewriter &) = delete;
~TokenRewriter();
typedef std::list<Token>::const_iterator token_iterator;
using token_iterator = std::list<Token>::const_iterator;
token_iterator token_begin() const { return TokenList.begin(); }
token_iterator token_end() const { return TokenList.end(); }
token_iterator AddTokenBefore(token_iterator I, const char *Val);
token_iterator AddTokenAfter(token_iterator I, const char *Val) {
assert(I != token_end() && "Cannot insert after token_end()!");
return AddTokenBefore(++I, Val);
@ -72,8 +76,6 @@ namespace clang {
TokenRefTy AddToken(const Token &T, TokenRefTy Where);
};
} // namespace clang
} // end namespace clang
#endif
#endif // LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H

View File

@ -1,4 +1,4 @@
//===--- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ----*- C++ -*-===//
//===- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -12,24 +12,28 @@
// then forwards any diagnostics to the adapted diagnostic client.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
#define LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Edit/EditedSource.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace clang {
class LangOptions;
class SourceManager;
class FileEntry;
class FixItOptions {
public:
FixItOptions() : InPlace(false), FixWhatYouCan(false),
FixOnlyWarnings(false), Silent(false) { }
FixItOptions() = default;
virtual ~FixItOptions();
/// \brief This file is about to be rewritten. Return the name of the file
@ -38,23 +42,22 @@ public:
/// \param fd out parameter for file descriptor. After the call it may be set
/// to an open file descriptor for the returned filename, or it will be -1
/// otherwise.
///
virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
/// True if files should be updated in place. RewriteFilename is only called
/// if this is false.
bool InPlace;
bool InPlace = false;
/// \brief Whether to abort fixing a file when not all errors could be fixed.
bool FixWhatYouCan;
bool FixWhatYouCan = false;
/// \brief Whether to only fix warnings and not errors.
bool FixOnlyWarnings;
bool FixOnlyWarnings = false;
/// \brief If true, only pass the diagnostic to the actual diagnostic consumer
/// if it is an error or a fixit was applied as part of the diagnostic.
/// It basically silences warnings without accompanying fixits.
bool Silent;
bool Silent = false;
};
class FixItRewriter : public DiagnosticConsumer {
@ -77,14 +80,12 @@ class FixItRewriter : public DiagnosticConsumer {
FixItOptions *FixItOpts;
/// \brief The number of rewriter failures.
unsigned NumFailures;
unsigned NumFailures = 0;
/// \brief Whether the previous diagnostic was not passed to the consumer.
bool PrevDiagSilenced;
bool PrevDiagSilenced = false;
public:
typedef Rewriter::buffer_iterator iterator;
/// \brief Initialize a new fix-it rewriter.
FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
const LangOptions &LangOpts, FixItOptions *FixItOpts);
@ -97,6 +98,8 @@ public:
return Rewrite.getRewriteBufferFor(ID) != nullptr;
}
using iterator = Rewriter::buffer_iterator;
// Iteration over files with changes.
iterator buffer_begin() { return Rewrite.buffer_begin(); }
iterator buffer_end() { return Rewrite.buffer_end(); }
@ -110,7 +113,7 @@ public:
///
/// \returns true if there was an error, false otherwise.
bool WriteFixedFiles(
std::vector<std::pair<std::string, std::string> > *RewrittenFiles=nullptr);
std::vector<std::pair<std::string, std::string>> *RewrittenFiles = nullptr);
/// IncludeInDiagnosticCounts - This method (whose default implementation
/// returns true) indicates whether the diagnostics handled by this
@ -127,6 +130,6 @@ public:
void Diag(SourceLocation Loc, unsigned DiagID);
};
}
} // namespace clang
#endif
#endif // LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H

View File

@ -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 @@ CharSourceRange Commit::Edit::getInsertFromRange(SourceManager &SM) const {
}
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::canInsertAfterToken(SourceLocation loc, FileOffset &offs,
}
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 @@ bool Commit::isAtStartOfMacroExpansion(SourceLocation loc,
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);

View File

@ -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 @@ bool EditedSource::commit(const Commit &commit) {
struct CommitRAII {
EditedSource &Editor;
CommitRAII(EditedSource &Editor) : Editor(Editor) {
Editor.startingCommit();
}
~CommitRAII() {
Editor.finishedCommit();
}

View File

@ -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 @@ class RewritesReceiver : public edit::EditsReceiver {
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 @@ void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Diags.setClient(this, false);
}
FixItOptions::~FixItOptions() {}
FixItOptions::~FixItOptions() = default;

View File

@ -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
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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));
}