18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Twine.h" 27 using namespace clang;
36 MacroArgUse &ArgUse) {
37 assert(SourceMgr.isMacroArgExpansion(Loc));
39 SourceMgr.getImmediateExpansionRange(Loc).getBegin();
41 SourceMgr.getImmediateExpansionRange(DefArgLoc).getBegin();
42 ExpansionLoc = ImmediateExpansionLoc;
43 while (SourceMgr.isMacroBodyExpansion(ExpansionLoc))
45 SourceMgr.getImmediateExpansionRange(ExpansionLoc).getBegin();
48 Buf, SourceMgr, LangOpts);
51 ArgUse = {&IdentTable.get(ArgName), ImmediateExpansionLoc,
52 SourceMgr.getSpellingLoc(DefArgLoc)};
55 void EditedSource::startingCommit() {}
57 void EditedSource::finishedCommit() {
58 for (
auto &ExpArg : CurrCommitMacroArgExps) {
61 std::tie(ExpLoc, ArgUse) = ExpArg;
63 if (llvm::find(ArgUses, ArgUse) == ArgUses.end())
64 ArgUses.push_back(ArgUse);
66 CurrCommitMacroArgExps.clear();
75 FileEditsTy::iterator FA = getActionForOffset(Offs);
76 if (FA != FileEdits.end()) {
77 if (FA->first != Offs)
81 if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
84 deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
86 if (I != ExpansionToArgMap.end() &&
87 find_if(I->second, [&](
const MacroArgUse &U) {
88 return ArgUse.Identifier == U.Identifier &&
89 std::tie(ArgUse.ImmediateExpansionLoc, ArgUse.UseLoc) !=
90 std::tie(U.ImmediateExpansionLoc, U.UseLoc);
91 }) != I->second.end()) {
113 bool beforePreviousInsertions) {
114 if (!canInsertInOffset(OrigLoc, Offs))
119 if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
122 deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
123 if (ArgUse.Identifier)
124 CurrCommitMacroArgExps.emplace_back(ExpLoc, ArgUse);
127 FileEdit &FA = FileEdits[Offs];
128 if (FA.Text.empty()) {
133 if (beforePreviousInsertions)
144 bool beforePreviousInsertions) {
151 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
152 if (I != FileEdits.begin())
155 for (; I != FileEdits.end(); ++I) {
156 FileEdit &FA = I->second;
172 for (; I != FileEdits.end() && EndOffs > I->first; ++I) {
173 FileEdit &FA = I->second;
178 bool Invalid =
false;
179 StringRef
text = getSourceText(BeginOffs, B, Invalid);
188 if (BeginOffs < EndOffs) {
189 bool Invalid =
false;
190 StringRef
text = getSourceText(BeginOffs, EndOffs, Invalid);
196 return commitInsert(OrigLoc, Offs, StrVec, beforePreviousInsertions);
205 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
206 if (I != FileEdits.begin())
209 for (; I != FileEdits.end(); ++I) {
210 FileEdit &FA = I->second;
219 FileEdit *TopFA =
nullptr;
221 if (I == FileEdits.end()) {
222 FileEditsTy::iterator
223 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
224 NewI->second.RemoveLen = Len;
228 FileEdit &FA = I->second;
232 FileEditsTy::iterator
233 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
234 TopBegin = BeginOffs;
236 TopFA = &NewI->second;
237 TopFA->RemoveLen = Len;
242 if (TopEnd >= EndOffs)
246 TopFA->RemoveLen += diff;
248 TopFA->Text = StringRef();
252 while (I != FileEdits.end()) {
253 FileEdit &FA = I->second;
261 FileEdits.erase(I++);
268 TopFA->RemoveLen += diff;
284 Editor.startingCommit();
288 Editor.finishedCommit();
339 unsigned &len, StringRef &
text) {
340 assert(len && text.empty());
342 if (BeginTokLoc != Loc)
345 bool Invalid =
false;
351 unsigned end = begin + len;
354 if (end == buffer.size())
357 assert(begin < buffer.size() && end < buffer.size() &&
"Invalid range!");
362 if (buffer[end] ==
' ')
367 if (buffer[end] ==
' ') {
368 assert((end + 1 != buffer.size() || buffer.data()[end + 1] == 0) &&
369 "buffer not zero-terminated!");
372 buffer.data()[end + 1],
378 if (!
canBeJoined(buffer[begin-1], buffer[end], LangOpts))
385 bool shouldAdjustRemovals) {
389 assert(Loc.isFileID());
391 if (text.empty() && shouldAdjustRemovals)
395 Loc.getLocWithOffset(len));
406 receiver.
insert(Loc, text);
410 bool shouldAdjustRemovals) {
415 if (FileEdits.empty())
418 FileEditsTy::iterator I = FileEdits.begin();
420 StrVec = I->second.Text;
421 CurLen = I->second.RemoveLen;
425 for (FileEditsTy::iterator E = FileEdits.end(); I != E; ++I) {
427 FileEdit act = I->second;
428 assert(offs >= CurEnd);
430 if (offs == CurEnd) {
432 CurLen += act.RemoveLen;
437 applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts,
438 shouldAdjustRemovals);
441 CurLen = act.RemoveLen;
445 applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts,
446 shouldAdjustRemovals);
457 assert(BeginOffs <= EndOffs);
464 SourceMgr, LangOpts, &Invalid);
467 EditedSource::FileEditsTy::iterator
468 EditedSource::getActionForOffset(
FileOffset Offs) {
469 FileEditsTy::iterator I = FileEdits.upper_bound(Offs);
470 if (I == FileEdits.begin())
471 return FileEdits.end();
473 FileEdit &FA = I->second;
476 if (Offs >= B && Offs < E)
479 return FileEdits.end();
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer...
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
unsigned getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it...
Defines the SourceManager interface.
SmallVectorImpl< Edit >::const_iterator edit_iterator
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
edit_iterator edit_end() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t', '\f', '\v', '\n', '\r'.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
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
edit_iterator edit_begin() const
FileOffset InsertFromRangeOffs
virtual void remove(CharSourceRange range)
By default it calls replace with an empty string.
FileOffset getWithOffset(unsigned offset) const
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
static bool canBeJoined(char left, char right, const LangOptions &LangOpts)
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)
Dataflow Directional Tag Classes.
bool isCommitable() const
unsigned getOffset() const
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)
void applyRewrites(EditsReceiver &receiver, bool adjustRemovals=true)
Defines the clang::SourceLocation class and associated facilities.
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...
static void applyRewrite(EditsReceiver &receiver, StringRef text, FileOffset offs, unsigned len, const SourceManager &SM, const LangOptions &LangOpts, bool shouldAdjustRemovals)
This class handles loading and caching of source files into memory.