File indexing completed on 2026-05-10 08:36:53
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_CLANG_EDIT_COMMIT_H
0010 #define LLVM_CLANG_EDIT_COMMIT_H
0011
0012 #include "clang/Basic/LLVM.h"
0013 #include "clang/Basic/SourceLocation.h"
0014 #include "clang/Edit/FileOffset.h"
0015 #include "llvm/ADT/SmallVector.h"
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/Support/Allocator.h"
0018
0019 namespace clang {
0020
0021 class LangOptions;
0022 class PPConditionalDirectiveRecord;
0023 class SourceManager;
0024
0025 namespace edit {
0026
0027 class EditedSource;
0028
0029 class Commit {
0030 public:
0031 enum EditKind {
0032 Act_Insert,
0033 Act_InsertFromRange,
0034 Act_Remove
0035 };
0036
0037 struct Edit {
0038 EditKind Kind;
0039 StringRef Text;
0040 SourceLocation OrigLoc;
0041 FileOffset Offset;
0042 FileOffset InsertFromRangeOffs;
0043 unsigned Length;
0044 bool BeforePrev;
0045
0046 SourceLocation getFileLocation(SourceManager &SM) const;
0047 CharSourceRange getFileRange(SourceManager &SM) const;
0048 CharSourceRange getInsertFromRange(SourceManager &SM) const;
0049 };
0050
0051 private:
0052 const SourceManager &SourceMgr;
0053 const LangOptions &LangOpts;
0054 const PPConditionalDirectiveRecord *PPRec;
0055 EditedSource *Editor = nullptr;
0056
0057 bool IsCommitable = true;
0058 SmallVector<Edit, 8> CachedEdits;
0059
0060 llvm::BumpPtrAllocator StrAlloc;
0061
0062 public:
0063 explicit Commit(EditedSource &Editor);
0064 Commit(const SourceManager &SM, const LangOptions &LangOpts,
0065 const PPConditionalDirectiveRecord *PPRec = nullptr)
0066 : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec) {}
0067
0068 bool isCommitable() const { return IsCommitable; }
0069
0070 bool insert(SourceLocation loc, StringRef text, bool afterToken = false,
0071 bool beforePreviousInsertions = false);
0072
0073 bool insertAfterToken(SourceLocation loc, StringRef text,
0074 bool beforePreviousInsertions = false) {
0075 return insert(loc, text, true, beforePreviousInsertions);
0076 }
0077
0078 bool insertBefore(SourceLocation loc, StringRef text) {
0079 return insert(loc, text, false,
0080 true);
0081 }
0082
0083 bool insertFromRange(SourceLocation loc, CharSourceRange range,
0084 bool afterToken = false,
0085 bool beforePreviousInsertions = false);
0086 bool insertWrap(StringRef before, CharSourceRange range, StringRef after);
0087
0088 bool remove(CharSourceRange range);
0089
0090 bool replace(CharSourceRange range, StringRef text);
0091 bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange);
0092 bool replaceText(SourceLocation loc, StringRef text,
0093 StringRef replacementText);
0094
0095 bool insertFromRange(SourceLocation loc, SourceRange TokenRange,
0096 bool afterToken = false,
0097 bool beforePreviousInsertions = false) {
0098 return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
0099 afterToken, beforePreviousInsertions);
0100 }
0101
0102 bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after) {
0103 return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
0104 }
0105
0106 bool remove(SourceRange TokenRange) {
0107 return remove(CharSourceRange::getTokenRange(TokenRange));
0108 }
0109
0110 bool replace(SourceRange TokenRange, StringRef text) {
0111 return replace(CharSourceRange::getTokenRange(TokenRange), text);
0112 }
0113
0114 bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange) {
0115 return replaceWithInner(CharSourceRange::getTokenRange(TokenRange),
0116 CharSourceRange::getTokenRange(TokenInnerRange));
0117 }
0118
0119 using edit_iterator = SmallVectorImpl<Edit>::const_iterator;
0120
0121 edit_iterator edit_begin() const { return CachedEdits.begin(); }
0122 edit_iterator edit_end() const { return CachedEdits.end(); }
0123
0124 private:
0125 void addInsert(SourceLocation OrigLoc,
0126 FileOffset Offs, StringRef text, bool beforePreviousInsertions);
0127 void addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
0128 FileOffset RangeOffs, unsigned RangeLen,
0129 bool beforePreviousInsertions);
0130 void addRemove(SourceLocation OrigLoc, FileOffset Offs, unsigned Len);
0131
0132 bool canInsert(SourceLocation loc, FileOffset &Offset);
0133 bool canInsertAfterToken(SourceLocation loc, FileOffset &Offset,
0134 SourceLocation &AfterLoc);
0135 bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
0136 bool canRemoveRange(CharSourceRange range, FileOffset &Offs, unsigned &Len);
0137 bool canReplaceText(SourceLocation loc, StringRef text,
0138 FileOffset &Offs, unsigned &Len);
0139
0140 void commitInsert(FileOffset offset, StringRef text,
0141 bool beforePreviousInsertions);
0142 void commitRemove(FileOffset offset, unsigned length);
0143
0144 bool isAtStartOfMacroExpansion(SourceLocation loc,
0145 SourceLocation *MacroBegin = nullptr) const;
0146 bool isAtEndOfMacroExpansion(SourceLocation loc,
0147 SourceLocation *MacroEnd = nullptr) const;
0148 };
0149
0150 }
0151
0152 }
0153
0154 #endif