Skip to content

Commit 43c04ab

Browse files
authored
Merge pull request #6367 from diffblue/crangler
Crangler
2 parents 720f7c8 + 9412ce5 commit 43c04ab

35 files changed

+1986
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
150150
"$<TARGET_FILE:unit>"
151151
"$<TARGET_FILE:goto-harness>"
152152
"$<TARGET_FILE:cbmc>"
153+
"$<TARGET_FILE:crangler>"
153154
"$<TARGET_FILE:driver>"
154155
"$<TARGET_FILE:goto-analyzer>"
155156
"$<TARGET_FILE:goto-cc>"
@@ -213,6 +214,8 @@ cprover_default_properties(
213214
cbmc
214215
cbmc-lib
215216
cpp
217+
crangler
218+
crangler-lib
216219
driver
217220
goto-analyzer
218221
goto-analyzer-lib

regression/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ if(WITH_MEMORY_ANALYZER)
8383
add_subdirectory(memory-analyzer)
8484
add_subdirectory(extract_type_header)
8585
endif()
86+
87+
if(NOT WIN32)
88+
add_subdirectory(crangler)
89+
endif()

regression/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# listed with decreasing runtimes (i.e. longest running at the top)
44
DIRS = cbmc \
55
cbmc-library \
6+
crangler \
67
goto-analyzer \
78
ansi-c \
89
goto-instrument \

regression/crangler/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_test_pl_tests(
2+
"$<TARGET_FILE:crangler>"
3+
)

regression/crangler/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
default: tests.log
2+
3+
include ../../src/config.inc
4+
include ../../src/common
5+
6+
ifeq ($(BUILD_ENV_),MSVC)
7+
test:
8+
9+
tests.log:
10+
11+
else
12+
test:
13+
@../test.pl -e -p -c '../../../src/crangler/crangler'
14+
15+
tests.log:
16+
@../test.pl -e -p -c '../../../src/crangler/crangler'
17+
endif
18+
19+
clean:
20+
@for dir in *; do \
21+
$(RM) tests.log; \
22+
if [ -d "$$dir" ]; then \
23+
cd "$$dir"; \
24+
$(RM) *.out *.gb; \
25+
cd ..; \
26+
fi \
27+
done
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
int foo()
2+
{
3+
return 0;
4+
}
5+
6+
int bar();
7+
8+
static void foobar1()
9+
{
10+
}
11+
12+
void static foobar2()
13+
{
14+
}
15+
16+
static short x;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
remove_static1.json
3+
4+
^\s+void foobar1\(\)$
5+
^\s+short x;$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"sources": [
3+
"remove_static1.c"
4+
],
5+
"functions": [
6+
{
7+
"foobar1": [
8+
"remove static"
9+
]
10+
}
11+
],
12+
"objects": [
13+
{
14+
"x": [
15+
"remove static"
16+
]
17+
}
18+
],
19+
"output": "stdout"
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int foo()
2+
{
3+
return 0;
4+
}
5+
6+
int bar();
7+
8+
static void foobar1()
9+
{
10+
}
11+
12+
void static foobar2()
13+
{
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
remove_static1.json
3+
4+
^\s+void foobar1\(\)$
5+
^void\s+foobar2\(\)$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"sources": [
3+
"remove_static1.c"
4+
],
5+
"functions": [
6+
{
7+
"foobar[12]": [
8+
"remove static"
9+
]
10+
}
11+
],
12+
"output": "stdout"
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int foo()
2+
{
3+
return 0;
4+
}
5+
6+
int bar();
7+
8+
static void foobar1()
9+
{
10+
}
11+
12+
void static foobar2()
13+
{
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
remove_static1.json
3+
4+
^\s+void foobar1\(\)$
5+
^void\s+foobar2\(\)$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"sources": [
3+
"remove_static1.c"
4+
],
5+
"functions": [
6+
{
7+
"foobar1": [
8+
"remove static"
9+
]
10+
},
11+
{
12+
"foobar2": [
13+
"remove static"
14+
]
15+
}
16+
],
17+
"output": "stdout"
18+
}

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ add_subdirectory(statement-list)
106106
add_subdirectory(util)
107107

108108
add_subdirectory(cbmc)
109+
add_subdirectory(crangler)
109110
add_subdirectory(goto-cc)
110111
add_subdirectory(goto-instrument)
111112
add_subdirectory(goto-analyzer)

src/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DIRS = analyses \
44
big-int \
55
cbmc \
66
cpp \
7+
crangler \
78
goto-analyzer \
89
goto-cc \
910
goto-checker \
@@ -27,6 +28,7 @@ DIRS = analyses \
2728
# Empty last line
2829

2930
all: cbmc.dir \
31+
crangler.dir \
3032
goto-analyzer.dir \
3133
goto-cc.dir \
3234
goto-diff.dir \
@@ -59,6 +61,8 @@ $(patsubst %, %.dir, $(filter-out big-int util, $(DIRS))): util.dir
5961

6062
cpp.dir: ansi-c.dir linking.dir
6163

64+
crangler.dir: util.dir json.dir
65+
6266
languages: util.dir langapi.dir \
6367
cpp.dir ansi-c.dir xmllang.dir assembler.dir \
6468
jsil.dir json.dir json-symtab-language.dir statement-list.dir

src/crangler/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
generic_flex(c)
2+
3+
# Library
4+
file(GLOB_RECURSE sources "*.cpp" "*.h")
5+
list(REMOVE_ITEM sources
6+
${CMAKE_CURRENT_SOURCE_DIR}/crangler_main.cpp
7+
)
8+
9+
add_library(crangler-lib
10+
${sources}
11+
${FLEX_scanner_OUTPUTS}
12+
)
13+
14+
generic_includes(crangler-lib)
15+
16+
target_link_libraries(crangler-lib
17+
big-int
18+
util
19+
json
20+
)
21+
22+
# Executable
23+
add_executable(crangler crangler_main.cpp)
24+
target_link_libraries(crangler crangler-lib)
25+
26+
install(TARGETS crangler DESTINATION ${CMAKE_INSTALL_BINDIR})

src/crangler/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
SRC = c_defines.cpp \
2+
c_lex.yy.cpp \
3+
c_wrangler.cpp \
4+
crangler_main.cpp \
5+
crangler_parse_options.cpp \
6+
cscanner.cpp \
7+
ctokenit.cpp \
8+
mini_c_parser.cpp \
9+
# Empty last line
10+
11+
OBJ += ../big-int/big-int$(LIBEXT) \
12+
../json/json$(LIBEXT) \
13+
../util/util$(LIBEXT)
14+
15+
INCLUDES= -I ..
16+
17+
include ../config.inc
18+
include ../../$(CPROVER_DIR)/src/common
19+
20+
CLEANFILES = crangler$(LIBEXT)
21+
22+
all: crangler$(EXEEXT)
23+
24+
###############################################################################
25+
26+
c_lex.yy.cpp: scanner.l
27+
$(LEX) -Pyyc -o$@ scanner.l
28+
29+
###############################################################################
30+
31+
generated_files: c_lex.yy.cpp
32+
33+
###############################################################################
34+
35+
crangler$(EXEEXT): $(OBJ)
36+
$(LINKBIN)

src/crangler/c_defines.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*******************************************************************\
2+
3+
Module: C Defines
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// c_defines
11+
12+
#include "c_defines.h"
13+
14+
#include "cscanner.h"
15+
16+
#include <util/prefix.h>
17+
#include <util/string_utils.h>
18+
19+
#include <sstream>
20+
21+
void c_definest::parse(const std::string &src)
22+
{
23+
const auto lines = split_string(src, '\n');
24+
for(const auto &line : lines)
25+
{
26+
// #define __x86_64__ 1
27+
// #define getc_unlocked(fp) __sgetc(fp)
28+
if(!has_prefix(line, "#define "))
29+
continue;
30+
31+
auto space_pos = line.find(' ', 8);
32+
if(space_pos == std::string::npos)
33+
continue;
34+
35+
auto id = line.substr(8, space_pos - 8);
36+
auto value = line.substr(space_pos + 1, std::string::npos);
37+
map[id].value = value;
38+
}
39+
}
40+
41+
std::string c_definest::operator()(const std::string &src) const
42+
{
43+
// tokenize
44+
std::istringstream in(src);
45+
cscannert cscanner(in);
46+
const auto tokens = cscanner.get_tokens();
47+
48+
// output
49+
std::ostringstream out;
50+
for(auto &t : tokens)
51+
{
52+
if(is_identifier(t))
53+
{
54+
auto m_it = map.find(t.text);
55+
if(m_it != map.end())
56+
{
57+
out << m_it->second.value;
58+
}
59+
else
60+
out << t.text;
61+
}
62+
else
63+
out << t.text;
64+
}
65+
66+
return out.str();
67+
}

src/crangler/c_defines.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*******************************************************************\
2+
3+
Module: C Defines
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// c_defines
11+
12+
#ifndef CPROVER_CRANGLER_C_DEFINES_H
13+
#define CPROVER_CRANGLER_C_DEFINES_H
14+
15+
#include <util/optional.h>
16+
17+
#include <string>
18+
#include <unordered_map>
19+
#include <vector>
20+
21+
/// This class maintains a representation of one assignment to the
22+
/// preprocessor macros in a C program.
23+
class c_definest
24+
{
25+
public:
26+
struct definet
27+
{
28+
optionalt<std::vector<std::string>> parameters;
29+
std::string value;
30+
};
31+
32+
using mapt = std::unordered_map<std::string, definet>;
33+
mapt map;
34+
35+
void parse(const std::string &);
36+
std::string operator()(const std::string &) const;
37+
};
38+
39+
#endif // CPROVER_CRANGLER_C_DEFINES_H

0 commit comments

Comments
 (0)