Clang Project

clang_source_code/include/clang/Edit/Commit.h
1//===- Commit.h - A unit of edits -------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_EDIT_COMMIT_H
10#define LLVM_CLANG_EDIT_COMMIT_H
11
12#include "clang/Basic/LLVM.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Edit/FileOffset.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Allocator.h"
18
19namespace clang {
20
21class LangOptions;
22class PPConditionalDirectiveRecord;
23class SourceManager;
24
25namespace edit {
26
27class EditedSource;
28
29class Commit {
30public:
31  enum EditKind {
32    Act_Insert,
33    Act_InsertFromRange,
34    Act_Remove
35  };
36
37  struct Edit {
38    EditKind Kind;
39    StringRef Text;
40    SourceLocation OrigLoc;
41    FileOffset Offset;
42    FileOffset InsertFromRangeOffs;
43    unsigned Length;
44    bool BeforePrev;
45
46    SourceLocation getFileLocation(SourceManager &SMconst;
47    CharSourceRange getFileRange(SourceManager &SMconst;
48    CharSourceRange getInsertFromRange(SourceManager &SMconst;
49  };
50
51private:
52  const SourceManager &SourceMgr;
53  const LangOptions &LangOpts;
54  const PPConditionalDirectiveRecord *PPRec;
55  EditedSource *Editor = nullptr;
56
57  bool IsCommitable = true;
58  SmallVector<Edit8CachedEdits;
59
60  llvm::BumpPtrAllocator StrAlloc;
61
62public:
63  explicit Commit(EditedSource &Editor);
64  Commit(const SourceManager &SMconst LangOptions &LangOpts,
65         const PPConditionalDirectiveRecord *PPRec = nullptr)
66      : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec) {}
67
68  bool isCommitable() const { return IsCommitable; }
69
70  bool insert(SourceLocation locStringRef textbool afterToken = false,
71              bool beforePreviousInsertions = false);
72
73  bool insertAfterToken(SourceLocation locStringRef text,
74                        bool beforePreviousInsertions = false) {
75    return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
76  }
77
78  bool insertBefore(SourceLocation locStringRef text) {
79    return insert(loc, text, /*afterToken=*/false,
80                  /*beforePreviousInsertions=*/true);
81  }
82
83  bool insertFromRange(SourceLocation locCharSourceRange range,
84                       bool afterToken = false,
85                       bool beforePreviousInsertions = false);
86  bool insertWrap(StringRef beforeCharSourceRange rangeStringRef after);
87
88  bool remove(CharSourceRange range);
89
90  bool replace(CharSourceRange rangeStringRef text);
91  bool replaceWithInner(CharSourceRange rangeCharSourceRange innerRange);
92  bool replaceText(SourceLocation locStringRef text,
93                   StringRef replacementText);
94
95  bool insertFromRange(SourceLocation locSourceRange TokenRange,
96                       bool afterToken = false,
97                       bool beforePreviousInsertions = false) {
98    return insertFromRange(locCharSourceRange::getTokenRange(TokenRange),
99                           afterTokenbeforePreviousInsertions);
100  }
101
102  bool insertWrap(StringRef beforeSourceRange TokenRangeStringRef after) {
103    return insertWrap(before, CharSourceRange::getTokenRange(TokenRange), after);
104  }
105
106  bool remove(SourceRange TokenRange) {
107    return remove(CharSourceRange::getTokenRange(TokenRange));
108  }
109
110  bool replace(SourceRange TokenRangeStringRef text) {
111    return replace(CharSourceRange::getTokenRange(TokenRange), text);
112  }
113
114  bool replaceWithInner(SourceRange TokenRangeSourceRange TokenInnerRange) {
115    return replaceWithInner(CharSourceRange::getTokenRange(TokenRange),
116                            CharSourceRange::getTokenRange(TokenInnerRange));
117  }
118
119  using edit_iterator = SmallVectorImpl<Edit>::const_iterator;
120
121  edit_iterator edit_begin() const { return CachedEdits.begin(); }
122  edit_iterator edit_end() const { return CachedEdits.end(); }
123
124private:
125  void addInsert(SourceLocation OrigLoc,
126                FileOffset OffsStringRef textbool beforePreviousInsertions);
127  void addInsertFromRange(SourceLocation OrigLocFileOffset Offs,
128                          FileOffset RangeOffsunsigned RangeLen,
129                          bool beforePreviousInsertions);
130  void addRemove(SourceLocation OrigLocFileOffset Offsunsigned Len);
131
132  bool canInsert(SourceLocation locFileOffset &Offset);
133  bool canInsertAfterToken(SourceLocation locFileOffset &Offset,
134                           SourceLocation &AfterLoc);
135  bool canInsertInOffset(SourceLocation OrigLocFileOffset Offs);
136  bool canRemoveRange(CharSourceRange rangeFileOffset &Offsunsigned &Len);
137  bool canReplaceText(SourceLocation locStringRef text,
138                      FileOffset &Offsunsigned &Len);
139
140  void commitInsert(FileOffset offsetStringRef text,
141                    bool beforePreviousInsertions);
142  void commitRemove(FileOffset offsetunsigned length);
143
144  bool isAtStartOfMacroExpansion(SourceLocation loc,
145                                 SourceLocation *MacroBegin = nullptrconst;
146  bool isAtEndOfMacroExpansion(SourceLocation loc,
147                               SourceLocation *MacroEnd = nullptrconst;
148};
149
150// namespace edit
151
152// namespace clang
153
154#endif // LLVM_CLANG_EDIT_COMMIT_H
155
clang::edit::Commit::EditKind
clang::edit::Commit::Edit
clang::edit::Commit::Edit::Kind
clang::edit::Commit::Edit::Text
clang::edit::Commit::Edit::OrigLoc
clang::edit::Commit::Edit::Offset
clang::edit::Commit::Edit::InsertFromRangeOffs
clang::edit::Commit::Edit::Length
clang::edit::Commit::Edit::BeforePrev
clang::edit::Commit::Edit::getFileLocation
clang::edit::Commit::Edit::getFileRange
clang::edit::Commit::Edit::getInsertFromRange
clang::edit::Commit::SourceMgr
clang::edit::Commit::LangOpts
clang::edit::Commit::PPRec
clang::edit::Commit::Editor
clang::edit::Commit::IsCommitable
clang::edit::Commit::CachedEdits
clang::edit::Commit::StrAlloc
clang::edit::Commit::isCommitable
clang::edit::Commit::insert
clang::edit::Commit::insertAfterToken
clang::edit::Commit::insertBefore
clang::edit::Commit::insertFromRange
clang::edit::Commit::insertWrap
clang::edit::Commit::remove
clang::edit::Commit::replace
clang::edit::Commit::replaceWithInner
clang::edit::Commit::replaceText
clang::edit::Commit::insertFromRange
clang::edit::Commit::insertWrap
clang::edit::Commit::remove
clang::edit::Commit::replace
clang::edit::Commit::replaceWithInner
clang::edit::Commit::edit_begin
clang::edit::Commit::edit_end
clang::edit::Commit::addInsert
clang::edit::Commit::addInsertFromRange
clang::edit::Commit::addRemove
clang::edit::Commit::canInsert
clang::edit::Commit::canInsertAfterToken
clang::edit::Commit::canInsertInOffset
clang::edit::Commit::canRemoveRange
clang::edit::Commit::canReplaceText
clang::edit::Commit::commitInsert
clang::edit::Commit::commitRemove
clang::edit::Commit::isAtStartOfMacroExpansion
clang::edit::Commit::isAtEndOfMacroExpansion