[cfe-commits] r38588 - in /cfe/cfe/trunk: Lex/ScratchBuffer.cpp include/clang/Lex/ScratchBuffer.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:22:59 PDT 2007


Author: sabre
Date: Wed Jul 11 11:22:59 2007
New Revision: 38588

URL: http://llvm.org/viewvc/llvm-project?rev=38588&view=rev
Log:
Initial implementation of the ScratchBuffer class.

Added:
    cfe/cfe/trunk/Lex/ScratchBuffer.cpp   (with props)
    cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h   (with props)

Added: cfe/cfe/trunk/Lex/ScratchBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/ScratchBuffer.cpp?rev=38588&view=auto

==============================================================================
--- cfe/cfe/trunk/Lex/ScratchBuffer.cpp (added)
+++ cfe/cfe/trunk/Lex/ScratchBuffer.cpp Wed Jul 11 11:22:59 2007
@@ -0,0 +1,67 @@
+//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements the ScratchBuffer interface.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Lex/ScratchBuffer.h"
+#include "clang/Basic/SourceBuffer.h"
+#include "clang/Basic/SourceManager.h"
+using namespace llvm;
+using namespace clang;
+
+// ScratchBufSize - The size of each chunk of scratch memory.  Slightly less
+//than a page, almost certainly enough for anything. :)
+static const unsigned ScratchBufSize = 4060;
+
+ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
+  // Set BytesUsed so that the first call to getToken will require an alloc.
+  BytesUsed = ScratchBufSize;
+  FileID = 0;
+}
+
+
+/// getToken - Splat the specified text into a temporary SourceBuffer and
+/// return a SourceLocation that refers to the token.  The SourceLoc value
+/// gives a virtual location that the token will appear to be from.
+SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
+                                       SourceLocation SourceLoc) {
+  if (BytesUsed+Len > ScratchBufSize)
+    AllocScratchBuffer(Len);
+
+  // Copy the token data into the buffer.
+  memcpy(CurBuffer+BytesUsed, Buf, Len);
+
+  // Create the initial SourceLocation.
+  SourceLocation Loc(FileID, BytesUsed);
+  assert(BytesUsed < (1 << SourceLocation::FilePosBits) &&
+         "Out of range file position!");
+  
+  // FIXME: Merge SourceLoc into it.
+  
+  // Remember that we used these bytes.
+  BytesUsed += Len;
+  
+  return Loc;
+}
+
+void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
+  // Only pay attention to the requested length if it is larger than our default
+  // page size.  If it is, we allocate an entire chunk for it.  This is to
+  // support gigantic tokens, which almost certainly won't happen. :)
+  if (RequestLen < ScratchBufSize)
+    RequestLen = ScratchBufSize;
+  
+  SourceBuffer *Buf = 
+    SourceBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
+  FileID = SourceMgr.createFileIDForMemBuffer(Buf);
+  CurBuffer = const_cast<char*>(Buf->getBufferStart());
+  BytesUsed = 0;
+}

Propchange: cfe/cfe/trunk/Lex/ScratchBuffer.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/Lex/ScratchBuffer.cpp

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h?rev=38588&view=auto

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h (added)
+++ cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h Wed Jul 11 11:22:59 2007
@@ -0,0 +1,47 @@
+//===--- ScratchBuffer.h - Scratch space for forming tokens -----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the ScratchBuffer interface.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_SCRATCHBUFFER_H
+#define LLVM_CLANG_SCRATCHBUFFER_H
+
+namespace llvm {
+namespace clang {
+  class SourceManager;
+  class SourceBuffer;
+  class SourceLocation;
+
+/// ScratchBuffer - This class exposes a simple interface for the dynamic
+/// construction of tokens.  This is used for builtin macros (e.g. __LINE__) as
+/// well as token pasting, etc.
+class ScratchBuffer {
+  SourceManager &SourceMgr;
+  char *CurBuffer;
+  unsigned FileID;
+  unsigned BytesUsed;
+public:
+  ScratchBuffer(SourceManager &SM);
+  
+  /// getToken - Splat the specified text into a temporary SourceBuffer and
+  /// return a SourceLocation that refers to the token.  The SourceLoc value
+  /// gives a virtual location that the token will appear to be from.
+  SourceLocation getToken(const char *Buf, unsigned Len,
+                          SourceLocation SourceLoc);
+  
+private:
+  void AllocScratchBuffer(unsigned RequestLen);
+};
+
+} // end namespace clang
+} // end namespace llvm
+
+#endif

Propchange: cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/include/clang/Lex/ScratchBuffer.h

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision





More information about the cfe-commits mailing list