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;
53 const bool ForceCommitInSystemHeader;
55 SmallVector<Edit, 8> CachedEdits;
57 llvm::BumpPtrAllocator StrAlloc;
60 explicit Commit(EditedSource &Editor);
61 Commit(const SourceManager &SM, const LangOptions &LangOpts,
62 const PPConditionalDirectiveRecord *PPRec = 0)
63 : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), Editor(0),
64 ForceCommitInSystemHeader(true), IsCommitable(true) { }
66 bool isCommitable() const { return IsCommitable; }
68 bool insert(SourceLocation loc, StringRef text, bool afterToken = false,
69 bool beforePreviousInsertions = false);
70 bool insertAfterToken(SourceLocation loc, StringRef text,
71 bool beforePreviousInsertions = false) {
72 return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
74 bool insertBefore(SourceLocation loc, StringRef text) {
75 return insert(loc, text, /*afterToken=*/false,
76 /*beforePreviousInsertions=*/true);
78 bool insertFromRange(SourceLocation loc, CharSourceRange range,
79 bool afterToken = false,
80 bool beforePreviousInsertions = false);
81 bool insertWrap(StringRef before, CharSourceRange range, StringRef after);
83 bool remove(CharSourceRange range);
85 bool replace(CharSourceRange range, StringRef text);
86 bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange);
87 bool replaceText(SourceLocation loc, StringRef text,
88 StringRef replacementText);
90 bool insertFromRange(SourceLocation loc, SourceRange TokenRange,
91 bool afterToken = false,
92 bool beforePreviousInsertions = false) {
93 return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
94 afterToken, beforePreviousInsertions);
96 bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after) {
97 return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
99 bool remove(SourceRange TokenRange) {
100 return remove(CharSourceRange::getTokenRange(TokenRange));
102 bool replace(SourceRange TokenRange, StringRef text) {
103 return replace(CharSourceRange::getTokenRange(TokenRange), text);
105 bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange) {
106 return replaceWithInner(CharSourceRange::getTokenRange(TokenRange),
107 CharSourceRange::getTokenRange(TokenInnerRange));
110 typedef SmallVectorImpl<Edit>::const_iterator edit_iterator;
111 edit_iterator edit_begin() const { return CachedEdits.begin(); }
112 edit_iterator edit_end() const { return CachedEdits.end(); }
115 void addInsert(SourceLocation OrigLoc,
116 FileOffset Offs, StringRef text, bool beforePreviousInsertions);
117 void addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
118 FileOffset RangeOffs, unsigned RangeLen,
119 bool beforePreviousInsertions);
120 void addRemove(SourceLocation OrigLoc, FileOffset Offs, unsigned Len);
122 bool canInsert(SourceLocation loc, FileOffset &Offset);
123 bool canInsertAfterToken(SourceLocation loc, FileOffset &Offset,
124 SourceLocation &AfterLoc);
125 bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
126 bool canRemoveRange(CharSourceRange range, FileOffset &Offs, unsigned &Len);
127 bool canReplaceText(SourceLocation loc, StringRef text,
128 FileOffset &Offs, unsigned &Len);
130 void commitInsert(FileOffset offset, StringRef text,
131 bool beforePreviousInsertions);
132 void commitRemove(FileOffset offset, unsigned length);
134 bool isAtStartOfMacroExpansion(SourceLocation loc,
135 SourceLocation *MacroBegin = 0) const;
136 bool isAtEndOfMacroExpansion(SourceLocation loc,
137 SourceLocation *MacroEnd = 0) const;
139 StringRef copyString(StringRef str) {
140 char *buf = StrAlloc.Allocate<char>(str.size());
141 std::memcpy(buf, str.data(), str.size());
142 return StringRef(buf, str.size());
148 } // end namespace clang