Skip to content

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

Merged
merged 3 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
"$<TARGET_FILE:unit>"
"$<TARGET_FILE:goto-harness>"
"$<TARGET_FILE:cbmc>"
"$<TARGET_FILE:crangler>"
"$<TARGET_FILE:driver>"
"$<TARGET_FILE:goto-analyzer>"
"$<TARGET_FILE:goto-cc>"
Expand Down Expand Up @@ -213,6 +214,8 @@ cprover_default_properties(
cbmc
cbmc-lib
cpp
crangler
crangler-lib
driver
goto-analyzer
goto-analyzer-lib
Expand Down
4 changes: 4 additions & 0 deletions regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ if(WITH_MEMORY_ANALYZER)
add_subdirectory(memory-analyzer)
add_subdirectory(extract_type_header)
endif()

if(NOT WIN32)
add_subdirectory(crangler)
endif()
1 change: 1 addition & 0 deletions regression/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# listed with decreasing runtimes (i.e. longest running at the top)
DIRS = cbmc \
cbmc-library \
crangler \
goto-analyzer \
ansi-c \
goto-instrument \
Expand Down
3 changes: 3 additions & 0 deletions regression/crangler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_test_pl_tests(
"$<TARGET_FILE:crangler>"
)
27 changes: 27 additions & 0 deletions regression/crangler/Makefile
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
16 changes: 16 additions & 0 deletions regression/crangler/remove-static-object/remove_static1.c
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;
8 changes: 8 additions & 0 deletions regression/crangler/remove-static-object/remove_static1.desc
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$
--
20 changes: 20 additions & 0 deletions regression/crangler/remove-static-object/remove_static1.json
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"
}
14 changes: 14 additions & 0 deletions regression/crangler/remove-static-regex/remove_static1.c
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()
{
}
8 changes: 8 additions & 0 deletions regression/crangler/remove-static-regex/remove_static1.desc
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$
--
13 changes: 13 additions & 0 deletions regression/crangler/remove-static-regex/remove_static1.json
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"
}
14 changes: 14 additions & 0 deletions regression/crangler/remove-static/remove_static1.c
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()
{
}
8 changes: 8 additions & 0 deletions regression/crangler/remove-static/remove_static1.desc
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$
--
18 changes: 18 additions & 0 deletions regression/crangler/remove-static/remove_static1.json
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"
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ add_subdirectory(statement-list)
add_subdirectory(util)

add_subdirectory(cbmc)
add_subdirectory(crangler)
add_subdirectory(goto-cc)
add_subdirectory(goto-instrument)
add_subdirectory(goto-analyzer)
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DIRS = analyses \
big-int \
cbmc \
cpp \
crangler \
goto-analyzer \
goto-cc \
goto-checker \
Expand All @@ -27,6 +28,7 @@ DIRS = analyses \
# Empty last line

all: cbmc.dir \
crangler.dir \
goto-analyzer.dir \
goto-cc.dir \
goto-diff.dir \
Expand Down Expand Up @@ -59,6 +61,8 @@ $(patsubst %, %.dir, $(filter-out big-int util, $(DIRS))): util.dir

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

crangler.dir: util.dir json.dir

languages: util.dir langapi.dir \
cpp.dir ansi-c.dir xmllang.dir assembler.dir \
jsil.dir json.dir json-symtab-language.dir statement-list.dir
Expand Down
26 changes: 26 additions & 0 deletions src/crangler/CMakeLists.txt
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})
36 changes: 36 additions & 0 deletions src/crangler/Makefile
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)
67 changes: 67 additions & 0 deletions src/crangler/c_defines.cpp
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to care about #undef and a possible re-definition?

}
}

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 getc_unlocked(fp) example in the comment)?


return out.str();
}
39 changes: 39 additions & 0 deletions src/crangler/c_defines.h
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
{
public:
struct definet
{
optionalt<std::vector<std::string>> parameters;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Loading