clang 19.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"
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 {
35 };
36
37 struct Edit {
39 StringRef Text;
43 unsigned Length;
45
49 };
50
51private:
52 const SourceManager &SourceMgr;
53 const LangOptions &LangOpts;
55 EditedSource *Editor = nullptr;
56
57 bool IsCommitable = true;
58 SmallVector<Edit, 8> CachedEdits;
59
60 llvm::BumpPtrAllocator StrAlloc;
61
62public:
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
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
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) {
116 CharSourceRange::getTokenRange(TokenInnerRange));
117 }
118
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 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
#define SM(sm)
Definition: Cuda.cpp:82
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Represents a character-granular source range.
static CharSourceRange getTokenRange(SourceRange R)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:449
Records preprocessor conditional directive regions and allows querying in which region source locatio...
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange)
Definition: Commit.cpp:132
bool remove(SourceRange TokenRange)
Definition: Commit.h:106
bool insertWrap(StringRef before, CharSourceRange range, StringRef after)
Definition: Commit.cpp:103
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:64
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:48
Commit(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
Definition: Commit.h:64
bool isCommitable() const
Definition: Commit.h:68
bool insertAfterToken(SourceLocation loc, StringRef text, bool beforePreviousInsertions=false)
Definition: Commit.h:73
edit_iterator edit_begin() const
Definition: Commit.h:121
SmallVectorImpl< Edit >::const_iterator edit_iterator
Definition: Commit.h:119
bool remove(CharSourceRange range)
Definition: Commit.cpp:91
bool insertBefore(SourceLocation loc, StringRef text)
Definition: Commit.h:78
bool insertWrap(StringRef before, SourceRange TokenRange, StringRef after)
Definition: Commit.h:102
bool replace(SourceRange TokenRange, StringRef text)
Definition: Commit.h:110
bool insertFromRange(SourceLocation loc, SourceRange TokenRange, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.h:95
bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange)
Definition: Commit.h:114
bool replace(CharSourceRange range, StringRef text)
Definition: Commit.cpp:116
edit_iterator edit_end() const
Definition: Commit.h:122
bool replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
Definition: Commit.cpp:165
The JSON file list parser is used to communicate input to InstallAPI.
float __ovld __cnfn length(float)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
SourceLocation getFileLocation(SourceManager &SM) const
Definition: Commit.cpp:24
CharSourceRange getFileRange(SourceManager &SM) const
Definition: Commit.cpp:31
FileOffset InsertFromRangeOffs
Definition: Commit.h:42
SourceLocation OrigLoc
Definition: Commit.h:40
CharSourceRange getInsertFromRange(SourceManager &SM) const
Definition: Commit.cpp:36