16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Twine.h"
19 using namespace clang;
32 FileEditsTy::iterator FA = getActionForOffset(Offs);
33 if (FA != FileEdits.end()) {
34 if (FA->first != Offs)
43 llvm::DenseMap<unsigned, SourceLocation>::iterator
45 if (I != ExpansionToArgMap.end() && I->second != DefArgLoc)
55 bool beforePreviousInsertions) {
69 FileEdit &FA = FileEdits[Offs];
70 if (FA.Text.empty()) {
75 if (beforePreviousInsertions)
86 bool beforePreviousInsertions) {
93 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
94 if (I != FileEdits.begin())
97 for (; I != FileEdits.end(); ++I) {
98 FileEdit &FA = I->second;
114 for (; I != FileEdits.end() && EndOffs > I->first; ++I) {
115 FileEdit &FA = I->second;
120 bool Invalid =
false;
121 StringRef text = getSourceText(BeginOffs, B, Invalid);
130 if (BeginOffs < EndOffs) {
131 bool Invalid =
false;
132 StringRef text = getSourceText(BeginOffs, EndOffs, Invalid);
138 return commitInsert(OrigLoc, Offs, StrVec, beforePreviousInsertions);
147 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
148 if (I != FileEdits.begin())
151 for (; I != FileEdits.end(); ++I) {
152 FileEdit &FA = I->second;
161 FileEdit *TopFA =
nullptr;
163 if (I == FileEdits.end()) {
164 FileEditsTy::iterator
165 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
166 NewI->second.RemoveLen = Len;
170 FileEdit &FA = I->second;
174 FileEditsTy::iterator
175 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
176 TopBegin = BeginOffs;
178 TopFA = &NewI->second;
179 TopFA->RemoveLen = Len;
184 if (TopEnd >= EndOffs)
188 TopFA->RemoveLen += diff;
190 TopFA->Text = StringRef();
194 while (I != FileEdits.end()) {
195 FileEdit &FA = I->second;
203 FileEdits.erase(I++);
210 TopFA->RemoveLen += diff;
269 unsigned &len, StringRef &text) {
270 assert(len && text.empty());
272 if (BeginTokLoc != Loc)
275 bool Invalid =
false;
281 unsigned end = begin + len;
284 if (end == buffer.size())
287 assert(begin < buffer.size() && end < buffer.size() &&
"Invalid range!");
292 if (buffer[end] ==
' ')
297 if (buffer[end] ==
' ') {
298 assert((end + 1 != buffer.size() || buffer.data()[end + 1] == 0) &&
299 "buffer not zero-terminated!");
302 buffer.data()[end + 1],
308 if (!
canBeJoined(buffer[begin-1], buffer[end], LangOpts))
313 StringRef text,
FileOffset offs,
unsigned len,
318 assert(Loc.isFileID());
324 Loc.getLocWithOffset(len));
335 receiver.
insert(Loc, text);
343 if (FileEdits.empty())
346 FileEditsTy::iterator I = FileEdits.begin();
348 StrVec = I->second.Text;
349 CurLen = I->second.RemoveLen;
353 for (FileEditsTy::iterator E = FileEdits.end(); I != E; ++I) {
355 FileEdit act = I->second;
356 assert(offs >= CurEnd);
358 if (offs == CurEnd) {
360 CurLen += act.RemoveLen;
365 applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts);
368 CurLen = act.RemoveLen;
372 applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts);
383 assert(BeginOffs <= EndOffs);
390 SourceMgr, LangOpts, &Invalid);
393 EditedSource::FileEditsTy::iterator
394 EditedSource::getActionForOffset(
FileOffset Offs) {
395 FileEditsTy::iterator I = FileEdits.upper_bound(Offs);
396 if (I == FileEdits.begin())
397 return FileEdits.end();
399 FileEdit &FA = I->second;
402 if (Offs >= B && Offs < E)
405 return FileEdits.end();
static LLVM_READONLY bool isWhitespace(unsigned char c)
Defines the SourceManager interface.
unsigned getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it...
void applyRewrites(EditsReceiver &receiver)
static void applyRewrite(EditsReceiver &receiver, StringRef text, FileOffset offs, unsigned len, const SourceManager &SM, const LangOptions &LangOpts)
unsigned getOffset() const
FileOffset getWithOffset(unsigned offset) const
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
bool isCommitable() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
edit_iterator edit_begin() const
static StringRef getSourceText(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, bool *Invalid=nullptr)
Returns a string for the source that the range encompasses.
Represents a character-granular source range.
virtual void replace(CharSourceRange range, StringRef text)=0
FileOffset InsertFromRangeOffs
virtual void remove(CharSourceRange range)
By default it calls replace with an empty string.
edit_iterator edit_end() const
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
static bool canBeJoined(char left, char right, const LangOptions &LangOpts)
SmallVectorImpl< Edit >::const_iterator edit_iterator
static SourceLocation GetBeginningOfToken(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Given a location any where in a source buffer, find the location that corresponds to the beginning of...
StringRef copyString(StringRef str)
std::pair< SourceLocation, SourceLocation > getImmediateExpansionRange(SourceLocation Loc) const
Return the start/end of the expansion information for an expansion location.
bool commit(const Commit &commit)
static bool isIdentifierBodyChar(char c, const LangOptions &LangOpts)
Returns true if the given character could appear in an identifier.
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
static bool canRemoveWhitespace(char left, char beforeWSpace, char right, const LangOptions &LangOpts)
Returns true if it is ok to eliminate the trailing whitespace between the given characters.
virtual void insert(SourceLocation loc, StringRef text)=0
static void adjustRemoval(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation Loc, FileOffset offs, unsigned &len, StringRef &text)
Check the range that we are going to remove and: -Remove any trailing whitespace if possible...
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
This class handles loading and caching of source files into memory.
bool isMacroArgExpansion(SourceLocation Loc) const
Tests whether the given source location represents a macro argument's expansion into the function-lik...