17#include "llvm/ADT/StringRef.h"
44 : SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()),
45 PPRec(Editor.getPPCondDirectiveRecord()),
49 bool afterToken,
bool beforePreviousInsertions) {
54 if ((!afterToken && !canInsert(loc, Offs)) ||
55 ( afterToken && !canInsertAfterToken(loc, Offs, loc))) {
60 addInsert(loc, Offs, text, beforePreviousInsertions);
66 bool afterToken,
bool beforePreviousInsertions) {
69 if (!canRemoveRange(range, RangeOffs, RangeLen)) {
75 if ((!afterToken && !canInsert(loc, Offs)) ||
76 ( afterToken && !canInsertAfterToken(loc, Offs, loc))) {
87 addInsertFromRange(loc, Offs, RangeOffs, RangeLen, beforePreviousInsertions);
94 if (!canRemoveRange(range, Offs, Len)) {
99 addRemove(range.getBegin(), Offs, Len);
105 bool commitableBefore =
insert(range.getBegin(), before,
false,
107 bool commitableAfter;
108 if (range.isTokenRange())
111 commitableAfter =
insert(range.getEnd(), after);
113 return commitableBefore && commitableAfter;
122 if (!canInsert(range.getBegin(), Offs) || !canRemoveRange(range, Offs, Len)) {
123 IsCommitable =
false;
127 addRemove(range.getBegin(), Offs, Len);
128 addInsert(range.getBegin(), Offs, text,
false);
136 if (!canRemoveRange(range, OuterBegin, OuterLen)) {
137 IsCommitable =
false;
143 if (!canRemoveRange(replacementRange, InnerBegin, InnerLen)) {
144 IsCommitable =
false;
151 InnerBegin < OuterBegin ||
152 InnerBegin > OuterEnd ||
153 InnerEnd > OuterEnd) {
154 IsCommitable =
false;
158 addRemove(range.getBegin(),
160 addRemove(replacementRange.
getEnd(),
166 StringRef replacementText) {
167 if (text.empty() || replacementText.empty())
172 if (!canReplaceText(loc, replacementText, Offs, Len)) {
173 IsCommitable =
false;
177 addRemove(loc, Offs, Len);
178 addInsert(loc, Offs, text,
false);
183 bool beforePreviousInsertions) {
189 data.OrigLoc = OrigLoc;
191 data.Text = text.copy(StrAlloc);
192 data.BeforePrev = beforePreviousInsertions;
193 CachedEdits.push_back(data);
198 bool beforePreviousInsertions) {
204 data.OrigLoc = OrigLoc;
206 data.InsertFromRangeOffs = RangeOffs;
207 data.Length = RangeLen;
208 data.BeforePrev = beforePreviousInsertions;
209 CachedEdits.push_back(data);
219 data.OrigLoc = OrigLoc;
222 CachedEdits.push_back(data);
230 isAtStartOfMacroExpansion(loc, &loc);
233 loc =
SM.getTopMacroCallerLoc(loc);
236 if (!isAtStartOfMacroExpansion(loc, &loc))
239 if (
SM.isInSystemHeader(loc))
242 std::pair<FileID, unsigned> locInfo =
SM.getDecomposedLoc(loc);
243 if (locInfo.first.isInvalid())
245 offs =
FileOffset(locInfo.first, locInfo.second);
246 return canInsertInOffset(loc, offs);
260 isAtEndOfMacroExpansion(loc, &loc);
263 loc =
SM.getTopMacroCallerLoc(loc);
266 if (!isAtEndOfMacroExpansion(loc, &loc))
269 if (
SM.isInSystemHeader(loc))
276 std::pair<FileID, unsigned> locInfo =
SM.getDecomposedLoc(loc);
277 if (locInfo.first.isInvalid())
279 offs =
FileOffset(locInfo.first, locInfo.second);
280 return canInsertInOffset(loc, offs);
284 for (
const auto &act : CachedEdits)
286 if (act.Offset.getFID() == Offs.
getFID() &&
287 Offs > act.Offset && Offs < act.Offset.getWithOffset(act.Length))
300 if (
range.isInvalid())
303 if (
range.getBegin().isMacroID() ||
range.getEnd().isMacroID())
305 if (
SM.isInSystemHeader(
range.getBegin()) ||
306 SM.isInSystemHeader(
range.getEnd()))
312 std::pair<FileID, unsigned> beginInfo =
SM.getDecomposedLoc(
range.getBegin());
313 std::pair<FileID, unsigned> endInfo =
SM.getDecomposedLoc(
range.getEnd());
314 if (beginInfo.first != endInfo.first ||
315 beginInfo.second > endInfo.second)
318 Offs =
FileOffset(beginInfo.first, beginInfo.second);
319 Len = endInfo.second - beginInfo.second;
325 assert(!
text.empty());
327 if (!canInsert(loc, Offs))
331 bool invalidTemp =
false;
337 return file.substr(Offs.
getOffset()).starts_with(text);
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
SourceLocation getEnd() const
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.
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 CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
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 ...
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.
bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS, SourceLocation RHS) const
Returns true if the given locations are in different regions, separated by conditional directive bloc...
bool rangeIntersectsConditionalDirective(SourceRange Range) const
Returns true if the given range intersects with a conditional directive.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID.
bool replaceWithInner(CharSourceRange range, CharSourceRange innerRange)
bool insertWrap(StringRef before, CharSourceRange range, StringRef after)
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
Commit(EditedSource &Editor)
bool insertAfterToken(SourceLocation loc, StringRef text, bool beforePreviousInsertions=false)
bool remove(CharSourceRange range)
bool replace(CharSourceRange range, StringRef text)
bool replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
FileOffset getWithOffset(unsigned offset) const
unsigned getOffset() const
The JSON file list parser is used to communicate input to InstallAPI.
SourceLocation getFileLocation(SourceManager &SM) const
CharSourceRange getFileRange(SourceManager &SM) const
CharSourceRange getInsertFromRange(SourceManager &SM) const