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

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