this massive patch introduces a simple new abstraction: it makes
"FileID" a concept that is now enforced by the compiler's type checker
instead of yet-another-random-unsigned floating around.
This is an important distinction from the "FileID" currently tracked by
SourceLocation. *That* FileID may refer to the start of a file or to a
chunk within it. The new FileID *only* refers to the file (and its
#include stack and eventually #line data), it cannot refer to a chunk.
FileID is a completely opaque datatype to all clients, only SourceManager
is allowed to poke and prod it.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62407 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Rewrite/HTMLRewrite.cpp b/lib/Rewrite/HTMLRewrite.cpp
index a605a1e..d61da40 100644
--- a/lib/Rewrite/HTMLRewrite.cpp
+++ b/lib/Rewrite/HTMLRewrite.cpp
@@ -33,8 +33,8 @@
SourceManager &SM = R.getSourceMgr();
B = SM.getInstantiationLoc(B);
E = SM.getInstantiationLoc(E);
- unsigned FileID = SM.getCanonicalFileID(B);
- assert(SM.getCanonicalFileID(E) == FileID && "B/E not in the same file!");
+ FileID FID = SM.getCanonicalFileID(B);
+ assert(SM.getCanonicalFileID(E) == FID && "B/E not in the same file!");
unsigned BOffset = SM.getFullFilePos(B);
unsigned EOffset = SM.getFullFilePos(E);
@@ -42,8 +42,8 @@
// Include the whole end token in the range.
EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr());
- HighlightRange(R.getEditBuffer(FileID), BOffset, EOffset,
- SM.getBufferData(FileID).first, StartTag, EndTag);
+ HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
+ SM.getBufferData(FID).first, StartTag, EndTag);
}
/// HighlightRange - This is the same as the above method, but takes
@@ -97,16 +97,16 @@
}
}
-void html::EscapeText(Rewriter& R, unsigned FileID,
+void html::EscapeText(Rewriter &R, FileID FID,
bool EscapeSpaces, bool ReplaceTabs) {
- const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
+ const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
const char* C = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
assert (C <= FileEnd);
- RewriteBuffer &RB = R.getEditBuffer(FileID);
+ RewriteBuffer &RB = R.getEditBuffer(FID);
unsigned ColNo = 0;
for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
@@ -217,13 +217,13 @@
}
}
-void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
+void html::AddLineNumbers(Rewriter& R, FileID FID) {
- const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
+ const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
const char* FileBeg = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
const char* C = FileBeg;
- RewriteBuffer &RB = R.getEditBuffer(FileID);
+ RewriteBuffer &RB = R.getEditBuffer(FID);
assert (C <= FileEnd);
@@ -263,15 +263,15 @@
RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
}
-void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID,
+void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
const char *title) {
- const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
+ const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
const char* FileStart = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
- SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
- SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
+ SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
+ SourceLocation EndLoc = StartLoc.getFileLocWithOffset(FileEnd-FileStart);
std::string s;
llvm::raw_string_ostream os(s);
@@ -340,15 +340,15 @@
/// information about keywords, macro expansions etc. This uses the macro
/// table state from the end of the file, so it won't be perfectly perfect,
/// but it will be reasonably close.
-void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) {
- RewriteBuffer &RB = R.getEditBuffer(FileID);
+void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
+ RewriteBuffer &RB = R.getEditBuffer(FID);
const SourceManager &SourceMgr = PP.getSourceManager();
- std::pair<const char*, const char*> File = SourceMgr.getBufferData(FileID);
+ std::pair<const char*, const char*> File = SourceMgr.getBufferData(FID);
const char *BufferStart = File.first;
- Lexer L(SourceLocation::getFileLoc(FileID, 0), PP.getLangOptions(),
- File.first, File.second);
+ Lexer L(SourceMgr.getLocForStartOfFile(FID),
+ PP.getLangOptions(), File.first, File.second);
// Inform the preprocessor that we want to retain comments as tokens, so we
// can highlight them.
@@ -421,9 +421,9 @@
/// file, to reexpand macros and insert (into the HTML) information about the
/// macro expansions. This won't be perfectly perfect, but it will be
/// reasonably close.
-void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor& PP) {
+void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
- RewriteBuffer &RB = R.getEditBuffer(FileID);
+ RewriteBuffer &RB = R.getEditBuffer(FID);
// Inform the preprocessor that we don't want comments.
PP.SetCommentRetentionState(false, false);
@@ -444,10 +444,10 @@
// Ignore tokens whose instantiation location was not the main file.
SourceLocation LLoc = SourceMgr.getInstantiationLoc(Tok.getLocation());
- std::pair<unsigned, unsigned> LLocInfo =
+ std::pair<FileID, unsigned> LLocInfo =
SourceMgr.getDecomposedFileLoc(LLoc);
- if (LLocInfo.first != FileID) {
+ if (LLocInfo.first != FID) {
PP.Lex(Tok);
continue;
}
@@ -496,9 +496,9 @@
}
}
-void html::HighlightMacros(Rewriter &R, unsigned FileID,
+void html::HighlightMacros(Rewriter &R, FileID FID,
PreprocessorFactory &PPF) {
llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor());
- HighlightMacros(R, FileID, *PP);
+ HighlightMacros(R, FID, *PP);
}
diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp
index ea8dcb9..e92bd7d 100644
--- a/lib/Rewrite/Rewriter.cpp
+++ b/lib/Rewrite/Rewriter.cpp
@@ -71,8 +71,8 @@
if (!isRewritable(Range.getBegin()) ||
!isRewritable(Range.getEnd())) return -1;
- unsigned StartOff, StartFileID;
- unsigned EndOff , EndFileID;
+ FileID StartFileID, EndFileID;
+ unsigned StartOff, EndOff;
StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
@@ -82,7 +82,7 @@
// If edits have been made to this buffer, the delta between the range may
// have changed.
- std::map<unsigned, RewriteBuffer>::const_iterator I =
+ std::map<FileID, RewriteBuffer>::const_iterator I =
RewriteBuffers.find(StartFileID);
if (I != RewriteBuffers.end()) {
const RewriteBuffer &RB = I->second;
@@ -109,8 +109,8 @@
!isRewritable(Range.getEnd()))
return "";
- unsigned StartOff, StartFileID;
- unsigned EndOff , EndFileID;
+ FileID StartFileID, EndFileID;
+ unsigned StartOff, EndOff;
StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
@@ -119,7 +119,7 @@
// If edits have been made to this buffer, the delta between the range may
// have changed.
- std::map<unsigned, RewriteBuffer>::const_iterator I =
+ std::map<FileID, RewriteBuffer>::const_iterator I =
RewriteBuffers.find(StartFileID);
if (I == RewriteBuffers.end()) {
// If the buffer hasn't been rewritten, just return the text from the input.
@@ -149,24 +149,24 @@
}
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
- unsigned &FileID) const {
+ FileID &FID) const {
assert(Loc.isValid() && "Invalid location");
- std::pair<unsigned,unsigned> V = SourceMgr->getDecomposedFileLoc(Loc);
- FileID = V.first;
+ std::pair<FileID,unsigned> V = SourceMgr->getDecomposedFileLoc(Loc);
+ FID = V.first;
return V.second;
}
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
///
-RewriteBuffer &Rewriter::getEditBuffer(unsigned FileID) {
- std::map<unsigned, RewriteBuffer>::iterator I =
- RewriteBuffers.lower_bound(FileID);
- if (I != RewriteBuffers.end() && I->first == FileID)
+RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
+ std::map<FileID, RewriteBuffer>::iterator I =
+ RewriteBuffers.lower_bound(FID);
+ if (I != RewriteBuffers.end() && I->first == FID)
return I->second;
- I = RewriteBuffers.insert(I, std::make_pair(FileID, RewriteBuffer()));
+ I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
- std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FileID);
+ std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FID);
I->second.Initialize(MB.first, MB.second);
return I->second;
@@ -177,18 +177,18 @@
bool Rewriter::InsertText(SourceLocation Loc, const char *StrData,
unsigned StrLen, bool InsertAfter) {
if (!isRewritable(Loc)) return true;
- unsigned FileID;
- unsigned StartOffs = getLocationOffsetAndFileID(Loc, FileID);
- getEditBuffer(FileID).InsertText(StartOffs, StrData, StrLen, InsertAfter);
+ FileID FID;
+ unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
+ getEditBuffer(FID).InsertText(StartOffs, StrData, StrLen, InsertAfter);
return false;
}
/// RemoveText - Remove the specified text region.
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length) {
if (!isRewritable(Start)) return true;
- unsigned FileID;
- unsigned StartOffs = getLocationOffsetAndFileID(Start, FileID);
- getEditBuffer(FileID).RemoveText(StartOffs, Length);
+ FileID FID;
+ unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
+ getEditBuffer(FID).RemoveText(StartOffs, Length);
return false;
}
@@ -198,7 +198,7 @@
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
const char *NewStr, unsigned NewLength) {
if (!isRewritable(Start)) return true;
- unsigned StartFileID;
+ FileID StartFileID;
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
diff --git a/lib/Rewrite/TokenRewriter.cpp b/lib/Rewrite/TokenRewriter.cpp
index f288ac2..85d83c2 100644
--- a/lib/Rewrite/TokenRewriter.cpp
+++ b/lib/Rewrite/TokenRewriter.cpp
@@ -18,14 +18,14 @@
#include "clang/Basic/SourceManager.h"
using namespace clang;
-TokenRewriter::TokenRewriter(unsigned FileID, SourceManager &SM,
+TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,
const LangOptions &LangOpts) {
ScratchBuf.reset(new ScratchBuffer(SM));
- std::pair<const char*,const char*> File = SM.getBufferData(FileID);
+ std::pair<const char*,const char*> File = SM.getBufferData(FID);
// Create a lexer to lex all the tokens of the main file in raw mode.
- Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
+ Lexer RawLex(SM.getLocForStartOfFile(FID),
LangOpts, File.first, File.second);
// Return all comments and whitespace as tokens.