[cfe-commits] r38706 - in /cfe/cfe/trunk: Lex/Preprocessor.cpp include/clang/Lex/Preprocessor.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:23:58 PDT 2007
Author: sabre
Date: Wed Jul 11 11:23:58 2007
New Revision: 38706
URL: http://llvm.org/viewvc/llvm-project?rev=38706&view=rev
Log:
Add an API so that external clients can create strings in the scratch buffer.
Modified:
cfe/cfe/trunk/Lex/Preprocessor.cpp
cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
Modified: cfe/cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Preprocessor.cpp?rev=38706&r1=38705&r2=38706&view=diff
==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:23:58 2007
@@ -276,6 +276,18 @@
return OutBuf-Buffer;
}
+
+/// CreateString - Plop the specified string into a scratch buffer and return a
+/// location for it. If specified, the source location provides a source
+/// location for the token.
+SourceLocation Preprocessor::
+CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
+ if (SLoc.isValid())
+ return ScratchBuf->getToken(Buf, Len, SLoc);
+ return ScratchBuf->getToken(Buf, Len);
+}
+
+
//===----------------------------------------------------------------------===//
// Source File Location Methods.
//===----------------------------------------------------------------------===//
@@ -768,7 +780,7 @@
/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
/// the identifier tokens inserted.
static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
- ScratchBuffer *ScratchBuf) {
+ Preprocessor &PP) {
time_t TT = time(0);
struct tm *TM = localtime(&TT);
@@ -779,10 +791,10 @@
char TmpBuffer[100];
sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
TM->tm_year+1900);
- DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
+ DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
- TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
+ TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
}
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
@@ -811,7 +823,7 @@
unsigned Length = strlen(TmpBuffer);
Tok.SetKind(tok::numeric_constant);
Tok.SetLength(Length);
- Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
+ Tok.SetLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
SourceLocation Loc = Tok.getLocation();
if (II == Ident__BASE_FILE__) {
@@ -828,16 +840,16 @@
FN = Lexer::Stringify(FN);
Tok.SetKind(tok::string_literal);
Tok.SetLength(FN.size());
- Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
+ Tok.SetLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
} else if (II == Ident__DATE__) {
if (!DATELoc.isValid())
- ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
+ ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Tok.SetKind(tok::string_literal);
Tok.SetLength(strlen("\"Mmm dd yyyy\""));
Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
} else if (II == Ident__TIME__) {
if (!TIMELoc.isValid())
- ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
+ ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Tok.SetKind(tok::string_literal);
Tok.SetLength(strlen("\"hh:mm:ss\""));
Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
@@ -855,7 +867,7 @@
unsigned Length = strlen(TmpBuffer);
Tok.SetKind(tok::numeric_constant);
Tok.SetLength(Length);
- Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
+ Tok.SetLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
} else if (II == Ident__TIMESTAMP__) {
// MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
// of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
@@ -884,7 +896,7 @@
TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
Tok.SetKind(tok::string_literal);
Tok.SetLength(Len);
- Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
+ Tok.SetLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
} else {
assert(0 && "Unknown identifier!");
}
Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38706&r1=38705&r2=38706&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:23:58 2007
@@ -375,6 +375,13 @@
/// if an internal buffer is returned.
unsigned getSpelling(const LexerToken &Tok, const char *&Buffer) const;
+
+ /// CreateString - Plop the specified string into a scratch buffer and return
+ /// a location for it. If specified, the source location provides a source
+ /// location for the token.
+ SourceLocation CreateString(const char *Buf, unsigned Len,
+ SourceLocation SourceLoc = SourceLocation());
+
/// DumpToken - Print the token to stderr, used for debugging.
///
void DumpToken(const LexerToken &Tok, bool DumpFlags = false) const;
More information about the cfe-commits
mailing list