Skip to content

Commit f352e4d

Browse files
committed
embedd files using hex literals instead of string literals
String constants are limited to 64k, which requires splitting the file with the compiler headers. This switches the embedding to use an array of hex literals, which have no size limit. There is a prospect to use the #embed preprocessor command in C++26, moves the conversion from file to hex literal array into the compiler's preprocessor.
1 parent 2a5151c commit f352e4d

File tree

4 files changed

+41
-47
lines changed

4 files changed

+41
-47
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,35 @@ Author: Daniel Kroening, kroening@kroening.com
1818
#include "ansi_c_parser.h"
1919

2020
const char gcc_builtin_headers_types[] =
21-
"#line 1 \"gcc_builtin_headers_types.h\"\n"
2221
#include "compiler_headers/gcc_builtin_headers_types.inc" // IWYU pragma: keep
2322
; // NOLINT(whitespace/semicolon)
2423

2524
const char gcc_builtin_headers_generic[] =
26-
"#line 1 \"gcc_builtin_headers_generic.h\"\n"
2725
#include "compiler_headers/gcc_builtin_headers_generic.inc" // IWYU pragma: keep
2826
; // NOLINT(whitespace/semicolon)
2927

3028
const char gcc_builtin_headers_math[] =
31-
"#line 1 \"gcc_builtin_headers_math.h\"\n"
3229
#include "compiler_headers/gcc_builtin_headers_math.inc" // IWYU pragma: keep
3330
; // NOLINT(whitespace/semicolon)
3431

3532
const char gcc_builtin_headers_mem_string[] =
36-
"#line 1 \"gcc_builtin_headers_mem_string.h\"\n"
3733
// NOLINTNEXTLINE(whitespace/line_length)
3834
#include "compiler_headers/gcc_builtin_headers_mem_string.inc" // IWYU pragma: keep
3935
; // NOLINT(whitespace/semicolon)
4036

41-
const char gcc_builtin_headers_omp[] = "#line 1 \"gcc_builtin_headers_omp.h\"\n"
37+
const char gcc_builtin_headers_omp[] =
4238
#include "compiler_headers/gcc_builtin_headers_omp.inc" // IWYU pragma: keep
4339
; // NOLINT(whitespace/semicolon)
4440

45-
const char gcc_builtin_headers_tm[] = "#line 1 \"gcc_builtin_headers_tm.h\"\n"
41+
const char gcc_builtin_headers_tm[] =
4642
#include "compiler_headers/gcc_builtin_headers_tm.inc" // IWYU pragma: keep
4743
; // NOLINT(whitespace/semicolon)
4844

4945
const char gcc_builtin_headers_ubsan[] =
50-
"#line 1 \"gcc_builtin_headers_ubsan.h\"\n"
5146
#include "compiler_headers/gcc_builtin_headers_ubsan.inc" // IWYU pragma: keep
5247
; // NOLINT(whitespace/semicolon)
5348

5449
const char gcc_builtin_headers_ia32[] =
55-
"#line 1 \"gcc_builtin_headers_ia32.h\"\n"
5650
#include "compiler_headers/gcc_builtin_headers_ia32.inc" // IWYU pragma: keep
5751
; // NOLINT(whitespace/semicolon)
5852
const char gcc_builtin_headers_ia32_2[] =
@@ -81,41 +75,38 @@ const char gcc_builtin_headers_ia32_9[] =
8175
; // NOLINT(whitespace/semicolon)
8276

8377
const char gcc_builtin_headers_alpha[] =
84-
"#line 1 \"gcc_builtin_headers_alpha.h\"\n"
8578
#include "compiler_headers/gcc_builtin_headers_alpha.inc" // IWYU pragma: keep
8679
; // NOLINT(whitespace/semicolon)
8780

88-
const char gcc_builtin_headers_arm[] = "#line 1 \"gcc_builtin_headers_arm.h\"\n"
81+
const char gcc_builtin_headers_arm[] =
8982
#include "compiler_headers/gcc_builtin_headers_arm.inc" // IWYU pragma: keep
9083
; // NOLINT(whitespace/semicolon)
9184

9285
const char gcc_builtin_headers_mips[] =
93-
"#line 1 \"gcc_builtin_headers_mips.h\"\n"
9486
#include "compiler_headers/gcc_builtin_headers_mips.inc" // IWYU pragma: keep
9587
; // NOLINT(whitespace/semicolon)
9688

9789
const char gcc_builtin_headers_power[] =
98-
"#line 1 \"gcc_builtin_headers_power.h\"\n"
9990
#include "compiler_headers/gcc_builtin_headers_power.inc" // IWYU pragma: keep
10091
; // NOLINT(whitespace/semicolon)
10192

102-
const char arm_builtin_headers[] = "#line 1 \"arm_builtin_headers.h\"\n"
93+
const char arm_builtin_headers[] =
10394
#include "compiler_headers/arm_builtin_headers.inc" // IWYU pragma: keep
10495
; // NOLINT(whitespace/semicolon)
10596

106-
const char cw_builtin_headers[] = "#line 1 \"cw_builtin_headers.h\"\n"
97+
const char cw_builtin_headers[] =
10798
#include "compiler_headers/cw_builtin_headers.inc" // IWYU pragma: keep
10899
; // NOLINT(whitespace/semicolon)
109100

110-
const char clang_builtin_headers[] = "#line 1 \"clang_builtin_headers.h\"\n"
101+
const char clang_builtin_headers[] =
111102
#include "compiler_headers/clang_builtin_headers.inc" // IWYU pragma: keep
112103
; // NOLINT(whitespace/semicolon)
113104

114-
const char cprover_builtin_headers[] = "#line 1 \"cprover_builtin_headers.h\"\n"
105+
const char cprover_builtin_headers[] =
115106
#include "cprover_builtin_headers.inc" // IWYU pragma: keep
116107
; // NOLINT(whitespace/semicolon)
117108

118-
const char windows_builtin_headers[] = "#line 1 \"windows_builtin_headers.h\"\n"
109+
const char windows_builtin_headers[] =
119110
#include "compiler_headers/windows_builtin_headers.inc" // IWYU pragma: keep
120111
; // NOLINT(whitespace/semicolon)
121112

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

src/ansi-c/file_converter.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
11
/*******************************************************************\
22
3-
Module: Convert file contents to C strings
3+
Module: Convert file contents to a C array
44
55
Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

99
/// \file
10-
/// Convert file contents to C strings
10+
/// Convert file contents to a C array
1111

1212
#include <fstream> // IWYU pragma: keep
13+
#include <iomanip>
1314
#include <iostream>
1415
#include <string>
1516

16-
static void convert_line(const std::string &line)
17-
{
18-
std::cout << "\"";
19-
20-
for(std::size_t i = 0; i < line.size(); i++)
21-
{
22-
const char ch = line[i];
23-
if(ch == '\\')
24-
std::cout << "\\\\";
25-
else if(ch == '"')
26-
std::cout << "\\\"";
27-
else if(ch == '\r' || ch == '\n')
28-
{
29-
}
30-
else if((ch & 0x80) != 0)
31-
{
32-
std::cout << "\\x" << std::hex << (unsigned(ch) & 0xff) << std::dec;
33-
}
34-
else
35-
std::cout << ch;
36-
}
37-
38-
std::cout << "\\n\"\n";
39-
}
40-
4117
int main(int argc, char *argv[])
4218
{
43-
std::string line;
19+
std::size_t counter = 0;
20+
bool first = true;
21+
22+
std::cout << "{";
4423

4524
for(int i = 1; i < argc; ++i)
4625
{
@@ -52,9 +31,31 @@ int main(int argc, char *argv[])
5231
return 1;
5332
}
5433

55-
while(getline(input_file, line))
56-
convert_line(line);
34+
char ch;
35+
while(input_file.get(ch))
36+
{
37+
if(first)
38+
first = false;
39+
else
40+
std::cout << ',';
41+
42+
if(counter == 15)
43+
{
44+
std::cout << '\n';
45+
counter = 0;
46+
}
47+
else
48+
{
49+
counter++;
50+
std::cout << ' ';
51+
}
52+
53+
std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0')
54+
<< int(ch);
55+
}
5756
}
5857

58+
std::cout << " }\n";
59+
5960
return 0;
6061
}

0 commit comments

Comments
 (0)