1 //===----- Commit.h - A unit of edits ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_EDIT_COMMIT_H
11 #define LLVM_CLANG_EDIT_COMMIT_H
13 #include "clang/Edit/FileOffset.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Allocator.h"
20 class PPConditionalDirectiveRecord;
36 SourceLocation OrigLoc;
38 FileOffset InsertFromRangeOffs;
42 SourceLocation getFileLocation(SourceManager &SM) const;
43 CharSourceRange getFileRange(SourceManager &SM) const;
44 CharSourceRange getInsertFromRange(SourceManager &SM) const;
48 const SourceManager &SourceMgr;
49 const LangOptions &LangOpts;
50 const PPConditionalDirectiveRecord *PPRec;
54 SmallVector<Edit, 8> CachedEdits;
56 llvm::BumpPtrAllocator StrAlloc;
59 explicit Commit(EditedSource &Editor);
60 Commit(const SourceManager &SM, const LangOptions &LangOpts,
61 const PPConditionalDirectiveRecord *PPRec = nullptr)
62 : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), Editor(nullptr),
63 IsCommitable(true) { }
65 bool isCommitable() const { return IsCommitable; }
67 bool insert(SourceLocation loc, StringRef text, bool afterToken = false,
68 bool beforePreviousInsertions = false);
69 bool insertAfterToken(SourceLocation loc, StringRef text,
70 bool beforePreviousInsertions = false) {
71 return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
73 bool insertBefore(SourceLocation loc, StringRef text) {
74 return insert(loc, text, /*afterToken=*/false,
75 /*beforePreviousInsertions=*/true);
77 bool insertFromRange(SourceLocation loc, CharSourceRange range,
78 bool afterToken = false,
79 bool beforePreviousInsertions = false);
80 bool insertWrap(StringRef before, CharSourceRange range, StringRef after);
82 bool remove(CharSourceRange range);
84 bool replace(CharSourceRange range, StringRef text);
85 bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange);
86 bool replaceText(SourceLocation loc, StringRef text,
87 StringRef replacementText);
89 bool insertFromRange(SourceLocation loc, SourceRange TokenRange,
90 bool afterToken = false,
91 bool beforePreviousInsertions = false) {
92 return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
93 afterToken, beforePreviousInsertions);
95 bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after) {
96 return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
98 bool remove(SourceRange TokenRange) {
99 return remove(CharSourceRange::getTokenRange(TokenRange));
101 bool replace(SourceRange TokenRange, StringRef text) {
102 return replace(CharSourceRange::getTokenRange(TokenRange), text);
104 bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange) {
105 return replaceWithInner(CharSourceRange::getTokenRange(TokenRange),
106 CharSourceRange::getTokenRange(TokenInnerRange));
109 typedef SmallVectorImpl<Edit>::const_iterator edit_iterator;
110 edit_iterator edit_begin() const { return CachedEdits.begin(); }
111 edit_iterator edit_end() const { return CachedEdits.end(); }
114 void addInsert(SourceLocation OrigLoc,
115 FileOffset Offs, StringRef text, bool beforePreviousInsertions);
116 void addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
117 FileOffset RangeOffs, unsigned RangeLen,
118 bool beforePreviousInsertions);
119 void addRemove(SourceLocation OrigLoc, FileOffset Offs, unsigned Len);
121 bool canInsert(SourceLocation loc, FileOffset &Offset);
122 bool canInsertAfterToken(SourceLocation loc, FileOffset &Offset,
123 SourceLocation &AfterLoc);
124 bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
125 bool canRemoveRange(CharSourceRange range, FileOffset &Offs, unsigned &Len);
126 bool canReplaceText(SourceLocation loc, StringRef text,
127 FileOffset &Offs, unsigned &Len);
129 void commitInsert(FileOffset offset, StringRef text,
130 bool beforePreviousInsertions);
131 void commitRemove(FileOffset offset, unsigned length);
133 bool isAtStartOfMacroExpansion(SourceLocation loc,
134 SourceLocation *MacroBegin = nullptr) const;
135 bool isAtEndOfMacroExpansion(SourceLocation loc,
136 SourceLocation *MacroEnd = nullptr) const;
141 } // end namespace clang