18 #include "llvm/ADT/StringRef.h" 22 using namespace clang;
45 : SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()),
46 PPRec(Editor.getPPCondDirectiveRecord()),
50 bool afterToken,
bool beforePreviousInsertions) {
55 if ((!afterToken && !canInsert(loc, Offs)) ||
56 ( afterToken && !canInsertAfterToken(loc, Offs, loc))) {
61 addInsert(loc, Offs, text, beforePreviousInsertions);
67 bool afterToken,
bool beforePreviousInsertions) {
70 if (!canRemoveRange(range, RangeOffs, RangeLen)) {
76 if ((!afterToken && !canInsert(loc, Offs)) ||
77 ( afterToken && !canInsertAfterToken(loc, Offs, loc))) {
88 addInsertFromRange(loc, Offs, RangeOffs, RangeLen, beforePreviousInsertions);
95 if (!canRemoveRange(range, Offs, Len)) {
100 addRemove(range.
getBegin(), Offs, Len);
108 bool commitableAfter;
114 return commitableBefore && commitableAfter;
119 return remove(range);
123 if (!canInsert(range.
getBegin(), Offs) || !canRemoveRange(range, Offs, Len)) {
124 IsCommitable =
false;
128 addRemove(range.
getBegin(), Offs, Len);
137 if (!canRemoveRange(range, OuterBegin, OuterLen)) {
138 IsCommitable =
false;
144 if (!canRemoveRange(replacementRange, InnerBegin, InnerLen)) {
145 IsCommitable =
false;
152 InnerBegin < OuterBegin ||
153 InnerBegin > OuterEnd ||
154 InnerEnd > OuterEnd) {
155 IsCommitable =
false;
161 addRemove(replacementRange.
getEnd(),
167 StringRef replacementText) {
168 if (text.empty() || replacementText.empty())
173 if (!canReplaceText(loc, replacementText, Offs, Len)) {
174 IsCommitable =
false;
178 addRemove(loc, Offs, Len);
179 addInsert(loc, Offs, text,
false);
184 bool beforePreviousInsertions) {
194 CachedEdits.push_back(data);
199 bool beforePreviousInsertions) {
210 CachedEdits.push_back(data);
223 CachedEdits.push_back(data);
231 isAtStartOfMacroExpansion(loc, &loc);
237 if (!isAtStartOfMacroExpansion(loc, &loc))
244 if (locInfo.first.isInvalid())
246 offs =
FileOffset(locInfo.first, locInfo.second);
247 return canInsertInOffset(loc, offs);
261 isAtEndOfMacroExpansion(loc, &loc);
267 if (!isAtEndOfMacroExpansion(loc, &loc))
278 if (locInfo.first.isInvalid())
280 offs =
FileOffset(locInfo.first, locInfo.second);
281 return canInsertInOffset(loc, offs);
285 for (
const auto &act : CachedEdits)
287 if (act.Offset.getFID() == Offs.
getFID() &&
288 Offs > act.Offset && Offs < act.Offset.
getWithOffset(act.Length))
315 if (beginInfo.first != endInfo.first ||
316 beginInfo.second > endInfo.second)
319 Offs =
FileOffset(beginInfo.first, beginInfo.second);
320 Len = endInfo.second - beginInfo.second;
326 assert(!
text.empty());
328 if (!canInsert(loc, Offs))
332 bool invalidTemp =
false;
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
bool remove(CharSourceRange range)
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
bool insertWrap(StringRef before, CharSourceRange range, StringRef after)
Defines the SourceManager interface.
bool insertAfterToken(SourceLocation loc, StringRef text, bool beforePreviousInsertions=false)
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
static bool isAtStartOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroBegin=nullptr)
Returns true if the given MacroID location points at the first token of the macro expansion...
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
SourceLocation getBegin() const
bool replace(CharSourceRange range, StringRef text)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS, SourceLocation RHS) const
Returns true if the given locations are in different regions, separated by conditional directive bloc...
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID. ...
CharSourceRange getFileRange(SourceManager &SM) const
static bool isAtEndOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroEnd=nullptr)
Returns true if the given MacroID location points at the last token of the macro expansion.
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
Represents a character-granular source range.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
FileOffset InsertFromRangeOffs
FileOffset getWithOffset(unsigned offset) const
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
bool rangeIntersectsConditionalDirective(SourceRange Range) const
Returns true if the given range intersects with a conditional directive.
bool isTokenRange() const
Return true if the end of this range specifies the start of the last token.
SourceRange getAsRange() const
Dataflow Directional Tag Classes.
static CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
CharSourceRange getInsertFromRange(SourceManager &SM) const
unsigned getOffset() const
bool replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
Commit(EditedSource &Editor)
SourceLocation getEnd() const
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
Defines the clang::SourceLocation class and associated facilities.
SourceLocation getFileLocation(SourceManager &SM) const
bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange)
This class handles loading and caching of source files into memory.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const