22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/raw_os_ostream.h"
35 StringRef ReplacementText)
36 : FilePath(FilePath), ReplacementRange(Offset, Length),
37 ReplacementText(ReplacementText) {}
40 unsigned Length, StringRef ReplacementText) {
41 setFromSourceLocation(Sources, Start, Length, ReplacementText);
46 StringRef ReplacementText,
48 setFromSourceRange(Sources, Range, ReplacementText, LangOpts);
70 getLocWithOffset(ReplacementRange.
getOffset());
75 Start, ReplacementRange.
getLength(), ReplacementText);
76 assert(RewriteSucceeded);
77 return RewriteSucceeded;
82 llvm::raw_string_ostream Stream(Result);
83 Stream << FilePath <<
": " << ReplacementRange.
getOffset() <<
":+"
84 << ReplacementRange.
getLength() <<
":\"" << ReplacementText <<
"\"";
110 void Replacement::setFromSourceLocation(
const SourceManager &Sources,
112 StringRef ReplacementText) {
113 const std::pair<FileID, unsigned> DecomposedLocation =
120 std::error_code EC = llvm::sys::fs::make_absolute(FilePath);
121 this->FilePath = EC ? FilePath.c_str() : Entry->
getName();
125 this->ReplacementRange = Range(DecomposedLocation.second, Length);
126 this->ReplacementText = ReplacementText;
137 std::pair<FileID, unsigned> Start = Sources.
getDecomposedLoc(SpellingBegin);
139 if (Start.first != End.first)
return -1;
142 return End.second - Start.second;
145 void Replacement::setFromSourceRange(
const SourceManager &Sources,
147 StringRef ReplacementText,
156 for (Replacements::iterator I = Replaces.begin(), E = Replaces.end(); I != E;
160 if (I->getOffset() + I->getLength() >
Position)
161 NewPosition += I->getOffset() + I->getLength() -
Position;
162 NewPosition += I->getReplacementText().size() - I->getLength();
172 for (std::vector<Replacement>::const_iterator I = Replaces.begin(),
177 if (I->getOffset() + I->getLength() >
Position)
178 NewPosition += I->getOffset() + I->getLength() -
Position;
179 NewPosition += I->getReplacementText().size() - I->getLength();
185 std::vector<Range> &Conflicts) {
186 if (Replaces.empty())
191 return LHS.
getOffset() < RHS.getOffset();
193 return LHS.
getLength() < RHS.getLength();
198 return LHS.
getOffset() == RHS.getOffset() &&
205 std::sort(Replaces.begin(), Replaces.end(), LessNoPath);
206 Replaces.erase(std::unique(Replaces.begin(), Replaces.end(), EqualNoPath),
210 Range ConflictRange(Replaces.front().getOffset(),
211 Replaces.front().getLength());
212 unsigned ConflictStart = 0;
213 unsigned ConflictLength = 1;
214 for (
unsigned i = 1; i < Replaces.size(); ++i) {
215 Range
Current(Replaces[i].getOffset(), Replaces[i].getLength());
216 if (ConflictRange.overlapsWith(Current)) {
218 ConflictRange = Range(ConflictRange.getOffset(),
219 std::max(ConflictRange.getLength(),
221 ConflictRange.getOffset()));
224 if (ConflictLength > 1)
225 Conflicts.push_back(Range(ConflictStart, ConflictLength));
232 if (ConflictLength > 1)
233 Conflicts.push_back(Range(ConflictStart, ConflictLength));
238 for (Replacements::const_iterator I = Replaces.begin(),
241 if (I->isApplicable()) {
242 Result = I->apply(Rewrite) && Result;
255 for (std::vector<Replacement>::const_iterator I = Replaces.begin(),
258 if (I->isApplicable()) {
259 Result = I->apply(Rewrite) && Result;
274 std::unique_ptr<llvm::MemoryBuffer> Buf =
275 llvm::MemoryBuffer::getMemBuffer(Code,
"<stdin>");
281 for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end();
283 Replacement Replace(
"<stdin>", I->getOffset(), I->getLength(),
284 I->getReplacementText());
285 if (!Replace.apply(Rewrite))
289 llvm::raw_string_ostream OS(Result);
SourceLocation getBegin() const
Implements support for file system lookup, file system caching, and directory search management...
SourceManager & getSourceMgr() const
Defines the clang::FileManager interface and associated types.
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID. ...
Defines the SourceManager interface.
RewriteBuffer & getEditBuffer(FileID FID)
bool ReplaceText(SourceLocation Start, unsigned OrigLength, StringRef NewStr)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Concrete class used by the front-end to report problems and issues.
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
ID
Defines the set of possible language-specific address spaces.
Represents a character-granular source range.
SourceLocation getEnd() const
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, unsigned LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
FileManager & getFileManager() const
The result type of a method or function.
void overrideFileContents(const FileEntry *SourceFile, llvm::MemoryBuffer *Buffer, bool DoNotFree)
Override the contents of the given source file by providing an already-allocated buffer.
bool isTokenRange() const
Return true if the end of this range specifies the start of the last token. Return false if the end o...
const char * getName() const
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
bool isValid() const
Return true if this is a valid SourceLocation object.
Options for controlling the compiler diagnostics engine.
Cached information about one file (either on disk or in the virtual file system). ...
const FileEntry * getVirtualFile(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Used for handling and querying diagnostic IDs.
Defines the Diagnostic-related interfaces.
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer. Note that it isn't safe to...
Keeps track of options that affect how file operations are performed.
Defines the Diagnostic IDs-related interfaces.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
This class handles loading and caching of source files into memory.