Refactor some code into a new Lexer::Stringify method.

llvm-svn: 38624
This commit is contained in:
Chris Lattner 2006-07-03 01:13:26 +00:00
parent d1236047a9
commit e3e81ea8aa
3 changed files with 20 additions and 11 deletions

View File

@ -59,9 +59,21 @@ Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp,
ParsingFilename = false;
}
//===----------------------------------------------------------------------===//
// LexerToken implementation.
//===----------------------------------------------------------------------===//
/// Stringify - Convert the specified string into a C string, with surrounding
/// ""'s, and with escaped \ and " characters.
std::string Lexer::Stringify(const std::string &Str) {
std::string Result = Str;
for (unsigned i = 0, e = Result.size(); i != e; ++i) {
if (Result[i] == '\\' || Result[i] == '"') {
Result.insert(Result.begin()+i, '\\');
++i; ++e;
}
}
// Add quotes.
return '"' + Result + '"';
}
//===----------------------------------------------------------------------===//
// Character information.

View File

@ -624,14 +624,7 @@ void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
std::string FN = SourceMgr.getSourceName(Loc);
for (unsigned i = 0, e = FN.size(); i != e; ++i)
if (FN[i] == '\\' || FN[i] == '"') {
FN.insert(FN.begin()+i, '\\');
++i; ++e;
}
// Add quotes.
FN = '"' + FN + '"';
FN = Lexer::Stringify(FN);
Tok.SetKind(tok::string_literal);
Tok.SetLength(FN.size());
Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));

View File

@ -121,6 +121,10 @@ public:
/// offset in the current file.
SourceLocation getSourceLocation(const char *Loc) const;
/// Stringify - Convert the specified string into a C string, with surrounding
/// ""'s, and with escaped \ and " characters.
static std::string Stringify(const std::string &Str);
//===--------------------------------------------------------------------===//
// Internal implementation interfaces.
private: