stringref'ize a bunch of filename handling logic. Much
nicer than passing around two const char*'s.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93094 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index d926617..aa807f8 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -404,8 +404,7 @@
/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
/// return null on failure. isAngled indicates whether the file reference is
/// for system #include's or not (i.e. using <> instead of "").
-const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
- const char *FilenameEnd,
+const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename,
SourceLocation FilenameTokLoc,
bool isAngled,
const DirectoryLookup *FromDir,
@@ -432,8 +431,7 @@
// Do a standard file entry lookup.
CurDir = CurDirLookup;
const FileEntry *FE =
- HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
- isAngled, FromDir, CurDir, CurFileEnt);
+ HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt);
if (FE) {
// Warn about normal quoted #include from framework headers. Since
// framework headers are published (both public and private ones) they
@@ -450,8 +448,7 @@
// headers on the #include stack and pass them to HeaderInfo.
if (IsFileLexer()) {
if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
- if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
- CurFileEnt)))
+ if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
return FE;
}
@@ -460,8 +457,7 @@
if (IsFileLexer(ISEntry)) {
if ((CurFileEnt =
SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
- if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
- FilenameEnd, CurFileEnt)))
+ if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
return FE;
}
}
@@ -932,43 +928,41 @@
/// spelling of the filename, but is also expected to handle the case when
/// this method decides to use a different buffer.
bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
- const char *&BufStart,
- const char *&BufEnd) {
+ llvm::StringRef &Buffer) {
// Get the text form of the filename.
- assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
+ assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
// Make sure the filename is <x> or "x".
bool isAngled;
- if (BufStart[0] == '<') {
- if (BufEnd[-1] != '>') {
+ if (Buffer[0] == '<') {
+ if (Buffer.back() != '>') {
Diag(Loc, diag::err_pp_expects_filename);
- BufStart = 0;
+ Buffer = llvm::StringRef();
return true;
}
isAngled = true;
- } else if (BufStart[0] == '"') {
- if (BufEnd[-1] != '"') {
+ } else if (Buffer[0] == '"') {
+ if (Buffer.back() != '"') {
Diag(Loc, diag::err_pp_expects_filename);
- BufStart = 0;
+ Buffer = llvm::StringRef();
return true;
}
isAngled = false;
} else {
Diag(Loc, diag::err_pp_expects_filename);
- BufStart = 0;
+ Buffer = llvm::StringRef();
return true;
}
// Diagnose #include "" as invalid.
- if (BufEnd-BufStart <= 2) {
+ if (Buffer.size() <= 2) {
Diag(Loc, diag::err_pp_empty_filename);
- BufStart = 0;
- return "";
+ Buffer = llvm::StringRef();
+ return true;
}
// Skip the brackets.
- ++BufStart;
- --BufEnd;
+ Buffer = Buffer.substr(1, Buffer.size()-2);
return isAngled;
}
@@ -1034,8 +1028,8 @@
CurPPLexer->LexIncludeFilename(FilenameTok);
// Reserve a buffer to get the spelling.
- llvm::SmallVector<char, 128> FilenameBuffer;
- const char *FilenameStart, *FilenameEnd;
+ llvm::SmallString<128> FilenameBuffer;
+ llvm::StringRef Filename;
switch (FilenameTok.getKind()) {
case tok::eom:
@@ -1045,9 +1039,9 @@
case tok::angle_string_literal:
case tok::string_literal: {
FilenameBuffer.resize(FilenameTok.getLength());
- FilenameStart = &FilenameBuffer[0];
+ const char *FilenameStart = &FilenameBuffer[0];
unsigned Len = getSpelling(FilenameTok, FilenameStart);
- FilenameEnd = FilenameStart+Len;
+ Filename = llvm::StringRef(FilenameStart, Len);
break;
}
@@ -1057,8 +1051,7 @@
FilenameBuffer.push_back('<');
if (ConcatenateIncludeName(FilenameBuffer))
return; // Found <eom> but no ">"? Diagnostic already emitted.
- FilenameStart = FilenameBuffer.data();
- FilenameEnd = FilenameStart + FilenameBuffer.size();
+ Filename = FilenameBuffer.str();
break;
default:
Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
@@ -1066,11 +1059,11 @@
return;
}
- bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
- FilenameStart, FilenameEnd);
+ bool isAngled =
+ GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
// error.
- if (FilenameStart == 0) {
+ if (Filename.empty()) {
DiscardUntilEndOfDirective();
return;
}
@@ -1089,12 +1082,10 @@
// Search include directories.
const DirectoryLookup *CurDir;
- const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
- FilenameTok.getLocation(),
+ const FileEntry *File = LookupFile(Filename, FilenameTok.getLocation(),
isAngled, LookupFrom, CurDir);
if (File == 0) {
- Diag(FilenameTok, diag::err_pp_file_not_found)
- << std::string(FilenameStart, FilenameEnd);
+ Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
return;
}
@@ -1114,8 +1105,7 @@
FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
FileCharacter);
if (FID.isInvalid()) {
- Diag(FilenameTok, diag::err_pp_file_not_found)
- << std::string(FilenameStart, FilenameEnd);
+ Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
return;
}