Skip to content

Commit c608235

Browse files
authored
Merge pull request #5314 from tautschnig/converter-read-files
Build system: make C string converters read files
2 parents 004fd96 + f20278a commit c608235

File tree

6 files changed

+94
-62
lines changed

6 files changed

+94
-62
lines changed

src/ansi-c/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ add_executable(converter library/converter.cpp)
66
file(GLOB ansi_c_library_sources "library/*.c")
77

88
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
9-
COMMAND cat ${ansi_c_library_sources} | $<TARGET_FILE:converter> > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
9+
COMMAND $<TARGET_FILE:converter> ${ansi_c_library_sources} > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
1010
DEPENDS converter ${ansi_c_library_sources})
1111

1212
add_executable(file_converter file_converter.cpp)
1313

1414
function(make_inc name)
1515
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${name}.inc"
16-
COMMAND file_converter < "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h" > "${CMAKE_CURRENT_BINARY_DIR}/${name}.inc"
16+
COMMAND file_converter "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h" > "${CMAKE_CURRENT_BINARY_DIR}/${name}.inc"
1717
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h" file_converter
1818
)
1919
endfunction(make_inc)

src/ansi-c/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ library_check: library/*.c
110110
touch $@
111111

112112
cprover_library.inc: library/converter$(EXEEXT) library/*.c
113-
cat library/*.c | library/converter$(EXEEXT) > $@
113+
library/converter$(EXEEXT) library/*.c > $@
114114

115115
%.inc: %.h file_converter$(EXEEXT)
116-
./file_converter$(EXEEXT) < $< > $@
116+
./file_converter$(EXEEXT) $< > $@
117117

118118
ansi_c_internal_additions$(OBJEXT): $(BUILTIN_FILES)
119119

src/ansi-c/file_converter.cpp

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,52 @@ Author: Daniel Kroening, [email protected]
99
/// \file
1010
/// Convert file contents to C strings
1111

12+
#include <fstream>
1213
#include <iostream>
1314
#include <string>
1415

15-
int main()
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+
41+
int main(int argc, char *argv[])
1642
{
1743
std::string line;
1844

19-
while(getline(std::cin, line))
45+
for(int i = 1; i < argc; ++i)
2046
{
21-
std::cout << "\"";
47+
std::ifstream input_file(argv[i]);
2248

23-
for(std::size_t i=0; i<line.size(); i++)
49+
if(!input_file)
2450
{
25-
const char ch=line[i];
26-
if(ch=='\\')
27-
std::cout << "\\\\";
28-
else if(ch=='"')
29-
std::cout << "\\\"";
30-
else if(ch=='\r' || ch=='\n')
31-
{
32-
}
33-
else if((ch&0x80)!=0)
34-
{
35-
std::cout << "\\x"
36-
<< std::hex << (unsigned(ch)&0xff)
37-
<< std::dec;
38-
}
39-
else
40-
std::cout << ch;
51+
std::cerr << "Failed to open " << argv[i] << '\n';
52+
return 1;
4153
}
4254

43-
std::cout << "\\n\"\n";
55+
while(getline(input_file, line))
56+
convert_line(line);
4457
}
58+
59+
return 0;
4560
}

src/ansi-c/library/converter.cpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
9+
#include <fstream>
1010
#include <iostream>
1111
#include <string>
1212

@@ -15,50 +15,65 @@ bool has_prefix(const std::string &s, const std::string &prefix)
1515
return std::string(s, 0, prefix.size())==prefix;
1616
}
1717

18-
int main()
18+
static void convert_line(const std::string &line, bool first)
1919
{
20-
std::string line;
21-
bool first=true;
20+
if(has_prefix(line, "/* FUNCTION: "))
21+
{
22+
if(!first)
23+
std::cout << "},\n";
2224

23-
std::cout << "{\n";
25+
std::string function = std::string(line, 13, std::string::npos);
26+
std::size_t pos = function.find(' ');
27+
if(pos != std::string::npos)
28+
function = std::string(function, 0, pos);
2429

25-
while(getline(std::cin, line))
30+
std::cout << "{ \"" << function << "\",\n";
31+
std::cout << " \"#line 1 \\\"<builtin-library-" << function
32+
<< ">\\\"\\n\"\n";
33+
}
34+
else if(!first)
2635
{
27-
if(has_prefix(line, "/* FUNCTION: "))
36+
std::cout << " \"";
37+
38+
for(unsigned i = 0; i < line.size(); i++)
2839
{
29-
if(first)
30-
first=false;
40+
const char ch = line[i];
41+
if(ch == '\\')
42+
std::cout << "\\\\";
43+
else if(ch == '"')
44+
std::cout << "\\\"";
45+
else if(ch == '\r' || ch == '\n')
46+
{
47+
}
3148
else
32-
std::cout << "},\n";
49+
std::cout << ch;
50+
}
3351

34-
std::string function=std::string(line, 13, std::string::npos);
35-
std::size_t pos=function.find(' ');
36-
if(pos!=std::string::npos)
37-
function=std::string(function, 0, pos);
52+
std::cout << "\\n\"\n";
53+
}
54+
}
3855

39-
std::cout << "{ \"" << function << "\",\n";
40-
std::cout << " \"#line 1 \\\"<builtin-library-"
41-
<< function << ">\\\"\\n\"\n";
42-
}
43-
else if(!first)
44-
{
45-
std::cout << " \"";
56+
int main(int argc, char *argv[])
57+
{
58+
std::string line;
59+
bool first = true;
4660

47-
for(unsigned i=0; i<line.size(); i++)
48-
{
49-
const char ch=line[i];
50-
if(ch=='\\')
51-
std::cout << "\\\\";
52-
else if(ch=='"')
53-
std::cout << "\\\"";
54-
else if(ch=='\r' || ch=='\n')
55-
{
56-
}
57-
else
58-
std::cout << ch;
59-
}
61+
std::cout << "{\n";
62+
63+
for(int i = 1; i < argc; ++i)
64+
{
65+
std::ifstream input_file(argv[i]);
66+
67+
if(!input_file)
68+
{
69+
std::cerr << "Failed to open " << argv[i] << '\n';
70+
return 1;
71+
}
6072

61-
std::cout << "\\n\"\n";
73+
while(getline(input_file, line))
74+
{
75+
convert_line(line, first);
76+
first = false;
6277
}
6378
}
6479

@@ -68,4 +83,6 @@ int main()
6883
std::cout <<
6984
"{ 0, 0 }\n"
7085
"}";
86+
87+
return 0;
7188
}

src/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
file(GLOB cpp_library_sources "library/*.c")
22

33
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
4-
COMMAND cat ${cpp_library_sources} | $<TARGET_FILE:converter> > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
4+
COMMAND $<TARGET_FILE:converter> ${cpp_library_sources} > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
55
DEPENDS converter ${cpp_library_sources})
66

77
################################################################################

src/cpp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ library_check: library/*.c
7373
touch $@
7474

7575
cprover_library.inc: ../ansi-c/library/converter$(EXEEXT) library/*.c
76-
cat library/*.c | ../ansi-c/library/converter$(EXEEXT) > $@
76+
../ansi-c/library/converter$(EXEEXT) library/*.c > $@
7777

7878
generated_files: cprover_library.inc
7979

0 commit comments

Comments
 (0)