 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
75.0% |
3 / 4 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
100.0% |
4 / 4 |
| |
|
Line Coverage: |
100.0% |
22 / 22 |
| |
 |
|
 |
1 : //===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file implements the ScratchBuffer interface.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "clang/Lex/ScratchBuffer.h"
15 : #include "clang/Basic/SourceManager.h"
16 : #include "llvm/Support/MemoryBuffer.h"
17 : #include <cstring>
18 : using namespace clang;
19 :
20 : // ScratchBufSize - The size of each chunk of scratch memory. Slightly less
21 : //than a page, almost certainly enough for anything. :)
22 : static const unsigned ScratchBufSize = 4060;
23 :
24 2533: ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
25 : // Set BytesUsed so that the first call to getToken will require an alloc.
26 2533: BytesUsed = ScratchBufSize;
27 2533: }
28 :
29 : /// getToken - Splat the specified text into a temporary MemoryBuffer and
30 : /// return a SourceLocation that refers to the token. This is just like the
31 : /// method below, but returns a location that indicates the physloc of the
32 : /// token.
33 : SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
34 2124: const char *&DestPtr) {
131: branch 0 taken
1993: branch 1 taken
35 2124: if (BytesUsed+Len+2 > ScratchBufSize)
36 131: AllocScratchBuffer(Len+2);
37 :
38 : // Prefix the token with a \n, so that it looks like it is the first thing on
39 : // its own virtual line in caret diagnostics.
40 2124: CurBuffer[BytesUsed++] = '\n';
41 :
42 : // Return a pointer to the character data.
43 2124: DestPtr = CurBuffer+BytesUsed;
44 :
45 : // Copy the token data into the buffer.
46 2124: memcpy(CurBuffer+BytesUsed, Buf, Len);
47 :
48 : // Remember that we used these bytes.
49 2124: BytesUsed += Len+1;
50 :
51 : // Add a NUL terminator to the token. This keeps the tokens separated, in
52 : // case they get relexed, and puts them on their own virtual lines in case a
53 : // diagnostic points to one.
54 2124: CurBuffer[BytesUsed-1] = '\0';
55 :
56 2124: return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len-1);
57 : }
58 :
59 131: void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
60 : // Only pay attention to the requested length if it is larger than our default
61 : // page size. If it is, we allocate an entire chunk for it. This is to
62 : // support gigantic tokens, which almost certainly won't happen. :)
131: branch 0 taken
0: branch 1 not taken
63 131: if (RequestLen < ScratchBufSize)
64 131: RequestLen = ScratchBufSize;
65 :
66 : llvm::MemoryBuffer *Buf =
67 131: llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
68 131: FileID FID = SourceMgr.createFileIDForMemBuffer(Buf);
69 131: BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
70 131: CurBuffer = const_cast<char*>(Buf->getBufferStart());
71 131: BytesUsed = 1;
72 131: CurBuffer[0] = '0'; // Start out with a \0 for cleanliness.
73 131: }
Generated: 2010-02-10 01:31 by zcov