clang 10.0.0git

Commit.h
Go to the documentation of this file.
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
19 namespace clang {
20
21 class LangOptions;
22 class PPConditionalDirectiveRecord;
23 class SourceManager;
24
25 namespace edit {
26
27 class EditedSource;
28
29 class Commit {
30 public:
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 &SM) const;
47 CharSourceRange getFileRange(SourceManager &SM) const;
48 CharSourceRange getInsertFromRange(SourceManager &SM) const;
49 };
50
51 private:
52 const SourceManager &SourceMgr;
53 const LangOptions &LangOpts;
54 const PPConditionalDirectiveRecord *PPRec;
55 EditedSource *Editor = nullptr;
56
57 bool IsCommitable = true;
58 SmallVector<Edit, 8> CachedEdits;
59
60 llvm::BumpPtrAllocator StrAlloc;
61
62 public:
63 explicit Commit(EditedSource &Editor);
64 Commit(const SourceManager &SM, const 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 loc, StringRef text, bool afterToken = false,
71 bool beforePreviousInsertions = false);
72
73 bool insertAfterToken(SourceLocation loc, StringRef text,
74 bool beforePreviousInsertions = false) {
75 return insert(loc, text, /*afterToken=*/true, beforePreviousInsertions);
76 }
77
78 bool insertBefore(SourceLocation loc, StringRef text) {
79 return insert(loc, text, /*afterToken=*/false,
80 /*beforePreviousInsertions=*/true);
81 }
82
83 bool insertFromRange(SourceLocation loc, CharSourceRange range,
84 bool afterToken = false,
85 bool beforePreviousInsertions = false);
86 bool insertWrap(StringRef before, CharSourceRange range, StringRef after);
87
88 bool remove(CharSourceRange range);
89
90 bool replace(CharSourceRange range, StringRef text);
91 bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange);
92 bool replaceText(SourceLocation loc, StringRef text,
93 StringRef replacementText);
94
95 bool insertFromRange(SourceLocation loc, SourceRange TokenRange,
96 bool afterToken = false,
97 bool beforePreviousInsertions = false) {
98 return insertFromRange(loc, CharSourceRange::getTokenRange(TokenRange),
99 afterToken, beforePreviousInsertions);
100 }
101
102 bool insertWrap(StringRef before, SourceRange TokenRange, StringRef 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 TokenRange, StringRef text) {
111 return replace(CharSourceRange::getTokenRange(TokenRange), text);
112 }
113
114 bool replaceWithInner(SourceRange TokenRange, SourceRange 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
124 private:
125 void addInsert(SourceLocation OrigLoc,
126 FileOffset Offs, StringRef text, bool beforePreviousInsertions);
127 void addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
128 FileOffset RangeOffs, unsigned RangeLen,
129 bool beforePreviousInsertions);
130 void addRemove(SourceLocation OrigLoc, FileOffset Offs, unsigned Len);
131
132 bool canInsert(SourceLocation loc, FileOffset &Offset);
133 bool canInsertAfterToken(SourceLocation loc, FileOffset &Offset,
134 SourceLocation &AfterLoc);
135 bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
136 bool canRemoveRange(CharSourceRange range, FileOffset &Offs, unsigned &Len);
137 bool canReplaceText(SourceLocation loc, StringRef text,
138 FileOffset &Offs, unsigned &Len);
139
140 void commitInsert(FileOffset offset, StringRef text,
141 bool beforePreviousInsertions);
142 void commitRemove(FileOffset offset, unsigned length);
143
144 bool isAtStartOfMacroExpansion(SourceLocation loc,
145 SourceLocation *MacroBegin = nullptr) const;
146 bool isAtEndOfMacroExpansion(SourceLocation loc,
147 SourceLocation *MacroEnd = nullptr) const;
148 };
149
150 } // namespace edit
151
152 } // namespace clang
153
154 #endif // LLVM_CLANG_EDIT_COMMIT_H
clang::edit::Commit::insertWrap
bool insertWrap(StringRef before, CharSourceRange range, StringRef after)
Definition: Commit.cpp:103
clang::CharSourceRange::getTokenRange
static CharSourceRange getTokenRange(SourceRange R)
Definition: SourceLocation.h:258
clang::edit::Commit::edit_iterator
SmallVectorImpl< Edit >::const_iterator edit_iterator
Definition: Commit.h:119
clang::edit::Commit::insertAfterToken
bool insertAfterToken(SourceLocation loc, StringRef text, bool beforePreviousInsertions=false)
Definition: Commit.h:73
clang::edit::EditedSource
Definition: EditedSource.h:35
clang::edit::Commit::replace
bool replace(SourceRange TokenRange, StringRef text)
Definition: Commit.h:110
clang::edit::Commit::insertFromRange
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:64
clang::transformer::range
RangeSelector range(RangeSelector Begin, RangeSelector End)
Selects from the start of Begin and to the end of End.
Definition: RangeSelector.cpp:151
clang::edit::Commit::insert
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:48
clang::edit::Commit::edit_end
edit_iterator edit_end() const
Definition: Commit.h:122
clang::transformer::before
RangeSelector before(RangeSelector Selector)
Selects the (empty) range [B,B) when Selector selects the range [B,E).
Definition: RangeSelector.cpp:107
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
clang::edit::Commit::Edit::BeforePrev
bool BeforePrev
Definition: Commit.h:44
clang::edit::Commit::Commit
Commit(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
Definition: Commit.h:64
llvm::SmallVectorImpl
Definition: LLVM.h:36
clang::edit::Commit::replace
bool replace(CharSourceRange range, StringRef text)
Definition: Commit.cpp:116
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::edit::Commit::Edit::OrigLoc
SourceLocation OrigLoc
Definition: Commit.h:40
clang::edit::Commit::Act_Remove
Definition: Commit.h:34
clang::edit::Commit::Edit::getFileRange
CharSourceRange getFileRange(SourceManager &SM) const
Definition: Commit.cpp:31
clang::edit::Commit::Edit::Length
unsigned Length
Definition: Commit.h:43
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:250
clang::edit::Commit::edit_begin
edit_iterator edit_begin() const
Definition: Commit.h:121
clang::edit::Commit::Edit::InsertFromRangeOffs
FileOffset InsertFromRangeOffs
Definition: Commit.h:42
length
float __ovld __cnfn length(float p)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
SM
const SourceManager & SM
Definition: Format.cpp:1685
clang::edit::Commit::Edit
Definition: Commit.h:37
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::PPConditionalDirectiveRecord
Records preprocessor conditional directive regions and allows querying in which region source locatio...
Definition: PPConditionalDirectiveRecord.h:25
clang::edit::Commit::Act_Insert
Definition: Commit.h:32
clang::edit::Commit::insertWrap
bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after)
Definition: Commit.h:102
FileOffset.h
llvm::SmallVector
Definition: LLVM.h:35
clang::transformer::after
RangeSelector after(RangeSelector Selector)
Selects the the point immediately following Selector.
Definition: RangeSelector.cpp:116
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::edit::Commit::isCommitable
bool isCommitable() const
Definition: Commit.h:68
clang::edit::Commit::Edit::getInsertFromRange
CharSourceRange getInsertFromRange(SourceManager &SM) const
Definition: Commit.cpp:36
clang::comments::tok::text
Definition: CommentLexer.h:35
clang::edit::Commit
Definition: Commit.h:29
clang::edit::Commit::replaceText
bool replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
Definition: Commit.cpp:165
clang::edit::Commit::insertFromRange
bool insertFromRange(SourceLocation loc, SourceRange TokenRange, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.h:95
clang::edit::Commit::Edit::Offset
FileOffset Offset
Definition: Commit.h:41
clang::edit::Commit::Edit::Kind
EditKind Kind
Definition: Commit.h:38
clang::edit::Commit::Commit
Commit(EditedSource &Editor)
Definition: Commit.cpp:43
clang::edit::Commit::EditKind
EditKind
Definition: Commit.h:31
clang::edit::FileOffset
Definition: FileOffset.h:18
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::edit::Commit::replaceWithInner
bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange)
Definition: Commit.h:114
clang::edit::Commit::insertBefore
bool insertBefore(SourceLocation loc, StringRef text)
Definition: Commit.h:78
clang::edit::Commit::Edit::getFileLocation
SourceLocation getFileLocation(SourceManager &SM) const
Definition: Commit.cpp:24
clang::edit::Commit::replaceWithInner
bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange)
Definition: Commit.cpp:132
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:207
clang::edit::Commit::Edit::Text
StringRef Text
Definition: Commit.h:39
clang::edit::Commit::Act_InsertFromRange
Definition: Commit.h:33
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596