-
Notifications
You must be signed in to change notification settings - Fork 274
Crangler #6367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crangler #6367
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_test_pl_tests( | ||
"$<TARGET_FILE:crangler>" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
default: tests.log | ||
|
||
include ../../src/config.inc | ||
include ../../src/common | ||
|
||
ifeq ($(BUILD_ENV_),MSVC) | ||
test: | ||
|
||
tests.log: | ||
|
||
else | ||
test: | ||
@../test.pl -e -p -c '../../../src/crangler/crangler' | ||
|
||
tests.log: | ||
@../test.pl -e -p -c '../../../src/crangler/crangler' | ||
endif | ||
|
||
clean: | ||
@for dir in *; do \ | ||
$(RM) tests.log; \ | ||
if [ -d "$$dir" ]; then \ | ||
cd "$$dir"; \ | ||
$(RM) *.out *.gb; \ | ||
cd ..; \ | ||
fi \ | ||
done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
int foo() | ||
{ | ||
return 0; | ||
} | ||
|
||
int bar(); | ||
|
||
static void foobar1() | ||
{ | ||
} | ||
|
||
void static foobar2() | ||
{ | ||
} | ||
|
||
static short x; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
remove_static1.json | ||
|
||
^\s+void foobar1\(\)$ | ||
^\s+short x;$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"sources": [ | ||
"remove_static1.c" | ||
], | ||
"functions": [ | ||
{ | ||
"foobar1": [ | ||
"remove static" | ||
] | ||
} | ||
], | ||
"objects": [ | ||
{ | ||
"x": [ | ||
"remove static" | ||
] | ||
} | ||
], | ||
"output": "stdout" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
int foo() | ||
{ | ||
return 0; | ||
} | ||
|
||
int bar(); | ||
|
||
static void foobar1() | ||
{ | ||
} | ||
|
||
void static foobar2() | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
remove_static1.json | ||
|
||
^\s+void foobar1\(\)$ | ||
^void\s+foobar2\(\)$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"sources": [ | ||
"remove_static1.c" | ||
], | ||
"functions": [ | ||
{ | ||
"foobar[12]": [ | ||
"remove static" | ||
] | ||
} | ||
], | ||
"output": "stdout" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
int foo() | ||
{ | ||
return 0; | ||
} | ||
|
||
int bar(); | ||
|
||
static void foobar1() | ||
{ | ||
} | ||
|
||
void static foobar2() | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
remove_static1.json | ||
|
||
^\s+void foobar1\(\)$ | ||
^void\s+foobar2\(\)$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"sources": [ | ||
"remove_static1.c" | ||
], | ||
"functions": [ | ||
{ | ||
"foobar1": [ | ||
"remove static" | ||
] | ||
}, | ||
{ | ||
"foobar2": [ | ||
"remove static" | ||
] | ||
} | ||
], | ||
"output": "stdout" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
generic_flex(c) | ||
|
||
# Library | ||
file(GLOB_RECURSE sources "*.cpp" "*.h") | ||
list(REMOVE_ITEM sources | ||
${CMAKE_CURRENT_SOURCE_DIR}/crangler_main.cpp | ||
) | ||
|
||
add_library(crangler-lib | ||
${sources} | ||
${FLEX_scanner_OUTPUTS} | ||
) | ||
|
||
generic_includes(crangler-lib) | ||
|
||
target_link_libraries(crangler-lib | ||
big-int | ||
util | ||
json | ||
) | ||
|
||
# Executable | ||
add_executable(crangler crangler_main.cpp) | ||
target_link_libraries(crangler crangler-lib) | ||
|
||
install(TARGETS crangler DESTINATION ${CMAKE_INSTALL_BINDIR}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
SRC = c_defines.cpp \ | ||
c_lex.yy.cpp \ | ||
c_wrangler.cpp \ | ||
crangler_main.cpp \ | ||
crangler_parse_options.cpp \ | ||
cscanner.cpp \ | ||
ctokenit.cpp \ | ||
mini_c_parser.cpp \ | ||
# Empty last line | ||
|
||
OBJ += ../big-int/big-int$(LIBEXT) \ | ||
../json/json$(LIBEXT) \ | ||
../util/util$(LIBEXT) | ||
|
||
INCLUDES= -I .. | ||
|
||
include ../config.inc | ||
include ../../$(CPROVER_DIR)/src/common | ||
|
||
CLEANFILES = crangler$(LIBEXT) | ||
|
||
all: crangler$(EXEEXT) | ||
|
||
############################################################################### | ||
|
||
c_lex.yy.cpp: scanner.l | ||
$(LEX) -Pyyc -o$@ scanner.l | ||
|
||
############################################################################### | ||
|
||
generated_files: c_lex.yy.cpp | ||
|
||
############################################################################### | ||
|
||
crangler$(EXEEXT): $(OBJ) | ||
$(LINKBIN) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*******************************************************************\ | ||
Module: C Defines | ||
Author: Daniel Kroening, [email protected] | ||
\*******************************************************************/ | ||
|
||
/// \file | ||
/// c_defines | ||
|
||
#include "c_defines.h" | ||
|
||
#include "cscanner.h" | ||
|
||
#include <util/prefix.h> | ||
#include <util/string_utils.h> | ||
|
||
#include <sstream> | ||
|
||
void c_definest::parse(const std::string &src) | ||
{ | ||
const auto lines = split_string(src, '\n'); | ||
for(const auto &line : lines) | ||
{ | ||
// #define __x86_64__ 1 | ||
// #define getc_unlocked(fp) __sgetc(fp) | ||
if(!has_prefix(line, "#define ")) | ||
continue; | ||
|
||
auto space_pos = line.find(' ', 8); | ||
if(space_pos == std::string::npos) | ||
continue; | ||
|
||
auto id = line.substr(8, space_pos - 8); | ||
auto value = line.substr(space_pos + 1, std::string::npos); | ||
map[id].value = value; | ||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to care about |
||
} | ||
} | ||
|
||
std::string c_definest::operator()(const std::string &src) const | ||
{ | ||
// tokenize | ||
std::istringstream in(src); | ||
cscannert cscanner(in); | ||
const auto tokens = cscanner.get_tokens(); | ||
|
||
// output | ||
std::ostringstream out; | ||
for(auto &t : tokens) | ||
{ | ||
if(is_identifier(t)) | ||
{ | ||
auto m_it = map.find(t.text); | ||
if(m_it != map.end()) | ||
{ | ||
out << m_it->second.value; | ||
} | ||
else | ||
out << t.text; | ||
} | ||
else | ||
out << t.text; | ||
} | ||
Comment on lines
+50
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about macros that use other macros? What about macros that take parameters (see |
||
|
||
return out.str(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/*******************************************************************\ | ||
|
||
Module: C Defines | ||
|
||
Author: Daniel Kroening, [email protected] | ||
|
||
\*******************************************************************/ | ||
|
||
/// \file | ||
/// c_defines | ||
|
||
#ifndef CPROVER_CRANGLER_C_DEFINES_H | ||
#define CPROVER_CRANGLER_C_DEFINES_H | ||
|
||
#include <util/optional.h> | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
/// This class maintains a representation of one assignment to the | ||
/// preprocessor macros in a C program. | ||
class c_definest | ||
tautschnig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public: | ||
struct definet | ||
{ | ||
optionalt<std::vector<std::string>> parameters; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never used (although they'd need to be taken into account). |
||
std::string value; | ||
}; | ||
|
||
using mapt = std::unordered_map<std::string, definet>; | ||
mapt map; | ||
|
||
void parse(const std::string &); | ||
std::string operator()(const std::string &) const; | ||
}; | ||
|
||
#endif // CPROVER_CRANGLER_C_DEFINES_H |
Uh oh!
There was an error while loading. Please reload this page.