GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/memory_mapped_input_buffer.cpp Lines: 0 33 0.0 %
Date: 2021-03-22 Branches: 0 14 0.0 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file memory_mapped_input_buffer.cpp
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Christopher L. Conway, Morgan Deters, Tim King
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief [[ Add file-specific comments here ]].
13
 **
14
 ** [[ Add file-specific comments here ]]
15
 **/
16
17
#include <fcntl.h>
18
#include <stdio.h>
19
20
#include <antlr3input.h>
21
22
#ifndef _WIN32
23
24
#include <cerrno>
25
#include <sys/mman.h>
26
#include <sys/stat.h>
27
28
#endif /* _WIN32 */
29
30
#include "base/exception.h"
31
#include "parser/memory_mapped_input_buffer.h"
32
33
namespace CVC4 {
34
namespace parser {
35
36
extern "C" {
37
38
#ifdef _WIN32
39
40
pANTLR3_INPUT_STREAM MemoryMappedInputBufferNew(const std::string& filename) {
41
  return 0;
42
}
43
44
#else /* ! _WIN32 */
45
46
static ANTLR3_UINT32
47
MemoryMapFile(pANTLR3_INPUT_STREAM input, const std::string& filename);
48
49
void
50
UnmapFile(pANTLR3_INPUT_STREAM input);
51
52
pANTLR3_INPUT_STREAM MemoryMappedInputBufferNew(const std::string& filename) {
53
  // Pointer to the input stream we are going to create
54
  //
55
  pANTLR3_INPUT_STREAM input;
56
  ANTLR3_UINT32 status;
57
58
  // Allocate memory for the input stream structure
59
  //
60
  input = (pANTLR3_INPUT_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM));
61
62
  if(input == NULL) {
63
    return NULL;
64
  }
65
66
  // Structure was allocated correctly, now we can read the file.
67
  //
68
  status = MemoryMapFile(input, filename);
69
70
  // Call the common 8 bit ASCII input stream handler
71
  // Initializer type thingy doobry function.
72
  //
73
#ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
74
  antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
75
#else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
76
  antlr38BitSetupStream(input);
77
#endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
78
79
  // Now we can set up the file name
80
  //
81
  input->istream->streamName
82
      = input->strFactory->newStr(input->strFactory,
83
                                  (uint8_t*) filename.c_str());
84
  input->fileName = input->istream->streamName;
85
  input->free = UnmapFile;
86
87
  if(status != ANTLR3_SUCCESS) {
88
    input->close(input);
89
    return NULL;
90
  }
91
92
  return input;
93
}
94
95
static ANTLR3_UINT32 MemoryMapFile(pANTLR3_INPUT_STREAM input,
96
                                   const std::string& filename) {
97
  errno = 0;
98
  struct stat st;
99
  if(stat(filename.c_str(), &st) == -1) {
100
    return ANTLR3_ERR_NOFILE;
101
  }
102
103
  input->sizeBuf = st.st_size;
104
105
  int fd = open(filename.c_str(), O_RDONLY);
106
  if(fd == -1) {
107
    return ANTLR3_ERR_NOFILE;
108
  }
109
110
  input->data = mmap(0, input->sizeBuf, PROT_READ, MAP_PRIVATE, fd, 0);
111
  errno = 0;
112
  close(fd);
113
  if(intptr_t(input->data) == -1) {
114
    return ANTLR3_ERR_NOMEM;
115
  }
116
117
  return ANTLR3_SUCCESS;
118
}
119
120
/* This is a bit shady. antlr3AsciiSetupStream has free and close as aliases.
121
 * We need to unmap the file somewhere, so we install this function as free and
122
 * call the default version of close to de-allocate everything else. */
123
void UnmapFile(pANTLR3_INPUT_STREAM input) {
124
  munmap((void*) input->data, input->sizeBuf);
125
  input->close(input);
126
}
127
128
#endif /* _WIN32 */
129
130
}/* extern "C" */
131
132
}/* CVC4::parser namespace */
133
}/* CVC4 namespace */