Skip to content
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

Updating testing and (minor) MSVC improvement #78

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,17 @@ class ASTIterBlock : public ASTBlock {

PycRef<ASTNode> iter() const { return m_iter; }
PycRef<ASTNode> index() const { return m_idx; }
PycRef<ASTNode> condition() const { return m_cond; }
bool isComprehension() const { return m_comp; }

void setIndex(PycRef<ASTNode> idx) { m_idx = idx; init(); }
void setCondition(PycRef<ASTNode> cond) { m_cond = cond; }
void setComprehension(bool comp) { m_comp = comp; }

private:
PycRef<ASTNode> m_iter;
PycRef<ASTNode> m_idx;
PycRef<ASTNode> m_cond;
bool m_comp;
};

Expand Down
72 changes: 43 additions & 29 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,20 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break;
case Pyc::BUILD_CLASS:
{
PycRef<ASTNode> code = stack.top();
PycRef<ASTNode> class_code = stack.top();
stack.pop();
PycRef<ASTNode> bases = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
stack.push(new ASTClass(code, bases, name));
stack.push(new ASTClass(class_code, bases, name));
}
break;
case Pyc::BUILD_FUNCTION:
{
PycRef<ASTNode> code = stack.top();
PycRef<ASTNode> function_code = stack.top();
stack.pop();
stack.push(new ASTFunction(code, ASTFunction::defarg_t()));
stack.push(new ASTFunction(function_code, ASTFunction::defarg_t()));
}
break;
case Pyc::BUILD_LIST_A:
Expand Down Expand Up @@ -423,17 +423,17 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> param = stack.top();
stack.pop();
if (param->type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<ASTNode> function_code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = function_code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<PycString> function_name = code_src->name();
if (function_name->isEqual("<lambda>")) {
pparamList.push_front(param);
} else {
// Decorator used
PycRef<ASTNode> name = new ASTName(function_name);
curblock->append(new ASTStore(param, name));
PycRef<ASTNode> decorator_name = new ASTName(function_name);
curblock->append(new ASTStore(param, decorator_name));

pparamList.push_front(name);
pparamList.push_front(decorator_name);
}
} else {
pparamList.push_front(param);
Expand Down Expand Up @@ -1016,6 +1016,15 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
newcond = new ASTBinary(cond1, cond, ASTBinary::BIN_LOG_OR);
}
ifblk = new ASTCondBlock(top->blktype(), offs, newcond, neg);
} else if (curblock->blktype() == ASTBlock::BLK_FOR &&
curblock.cast<ASTIterBlock>()->isComprehension() &&
mod->verCompare(2, 7) >= 0) {
/* comprehension condition */
curblock.cast<ASTIterBlock>()->setCondition(cond);
stack_hist.pop();
// TODO: Handle older python versions 2.7, where condition
// is laid out a little differently.
break;
} else {
/* Plain old if statement */
ifblk = new ASTCondBlock(ASTBlock::BLK_IF, offs, cond, neg);
Expand Down Expand Up @@ -1043,7 +1052,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)

blocks.pop();
curblock = blocks.top();
} if (curblock->blktype() == ASTBlock::BLK_ELSE) {
} else if (curblock->blktype() == ASTBlock::BLK_ELSE) {
stack = stack_hist.top();
stack_hist.pop();

Expand Down Expand Up @@ -1258,6 +1267,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)

if (curblock->blktype() == ASTBlock::BLK_FOR
&& curblock.cast<ASTIterBlock>()->isComprehension()) {
stack.pop();
stack.push(new ASTComprehension(value));
} else {
stack.push(new ASTSubscr(list, value)); /* Total hack */
Expand Down Expand Up @@ -1313,14 +1323,14 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case Pyc::MAKE_CLOSURE_A:
case Pyc::MAKE_FUNCTION_A:
{
PycRef<ASTNode> code = stack.top();
PycRef<ASTNode> function_code = stack.top();
stack.pop();

/* Test for the qualified name of the function (at TOS) */
int tos_type = code.cast<ASTObject>()->object()->type();
int tos_type = function_code.cast<ASTObject>()->object()->type();
if (tos_type != PycObject::TYPE_CODE &&
tos_type != PycObject::TYPE_CODE2) {
code = stack.top();
function_code = stack.top();
stack.pop();
}

Expand All @@ -1329,7 +1339,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
defArgs.push_front(stack.top());
stack.pop();
}
stack.push(new ASTFunction(code, defArgs));
stack.push(new ASTFunction(function_code, defArgs));
}
break;
case Pyc::POP_BLOCK:
Expand Down Expand Up @@ -1704,12 +1714,12 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

if (--unpack <= 0) {
PycRef<ASTNode> tup = stack.top();
PycRef<ASTNode> lastTup = stack.top();
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();

curblock->append(new ASTStore(seq, tup));
curblock->append(new ASTStore(seq, lastTup));
}
} else {
PycRef<ASTNode> name = stack.top();
Expand Down Expand Up @@ -1740,12 +1750,12 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

if (--unpack <= 0) {
PycRef<ASTNode> tup = stack.top();
PycRef<ASTNode> lastTup = stack.top();
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();

curblock->append(new ASTStore(seq, tup));
curblock->append(new ASTStore(seq, lastTup));
}
} else {
PycRef<ASTNode> value = stack.top();
Expand Down Expand Up @@ -1778,16 +1788,16 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

if (--unpack <= 0) {
PycRef<ASTNode> tup = stack.top();
PycRef<ASTNode> lastTup = stack.top();
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();

if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) {
curblock.cast<ASTIterBlock>()->setIndex(tup);
curblock.cast<ASTIterBlock>()->setIndex(lastTup);
} else {
curblock->append(new ASTStore(seq, tup));
curblock->append(new ASTStore(seq, lastTup));
}
}
} else {
Expand Down Expand Up @@ -1837,16 +1847,16 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

if (--unpack <= 0) {
PycRef<ASTNode> tup = stack.top();
PycRef<ASTNode> lastTup = stack.top();
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();

if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) {
curblock.cast<ASTIterBlock>()->setIndex(tup);
curblock.cast<ASTIterBlock>()->setIndex(lastTup);
} else {
curblock->append(new ASTStore(seq, tup));
curblock->append(new ASTStore(seq, lastTup));
}
}
} else {
Expand Down Expand Up @@ -1877,16 +1887,16 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

if (--unpack <= 0) {
PycRef<ASTNode> tup = stack.top();
PycRef<ASTNode> lastTup = stack.top();
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();

if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) {
curblock.cast<ASTIterBlock>()->setIndex(tup);
curblock.cast<ASTIterBlock>()->setIndex(lastTup);
} else {
curblock->append(new ASTStore(seq, tup));
curblock->append(new ASTStore(seq, lastTup));
}
}
} else {
Expand Down Expand Up @@ -1992,12 +2002,12 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

if (--unpack <= 0) {
PycRef<ASTNode> tup = stack.top();
PycRef<ASTNode> lastTup = stack.top();
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();

curblock->append(new ASTStore(seq, tup));
curblock->append(new ASTStore(seq, lastTup));
}
} else {
PycRef<ASTNode> subscr = stack.top();
Expand Down Expand Up @@ -2347,6 +2357,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_src((*it)->index(), mod);
fprintf(pyc_output, " in ");
print_src((*it)->iter(), mod);
if ((*it)->condition()) {
fprintf(pyc_output, " if ");
print_src((*it)->condition(), mod);
}
}
fprintf(pyc_output, " ]");
}
Expand Down
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ cmake_minimum_required(VERSION 2.8)
# For generating the bytes tables
find_package(PythonInterp REQUIRED)

if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror ${CMAKE_CXX_FLAGS}")
elseif(MSVC)
# Make MSVC treat compiler warnings as errors
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")

# Increase warning levels to 4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()

# Disable CRT secure warnings (This project isn't windows specific)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

set(PYTHON_VERSIONS
Expand Down Expand Up @@ -61,6 +74,6 @@ install(TARGETS pycdc
RUNTIME DESTINATION bin)

# For tests
add_custom_target(test ${CMAKE_CURRENT_SOURCE_DIR}/pycdc_test.sh
add_custom_target(pycdc_test ${CMAKE_CURRENT_SOURCE_DIR}/pycdc_test.sh
${CMAKE_CURRENT_SOURCE_DIR}/tests)
add_dependencies(test pycdc)
add_dependencies(pycdc_test pycdc)
50 changes: 38 additions & 12 deletions pycdc_test.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
#!/bin/bash

# TODO: Consider making a non-empty stderr be a failure
# result, however this can make understanding new versions
# harder to upgrade with.
#
# Possibly need a 'warning' level from test output.
mkdir -p tests

fails=0
files=()
errors=()
for f in $1/*.pyc
diffs=()
for prog in pycdc pycdas
do
base=tests/$( basename "$f" )
stderr=$( ./pycdc "$f" 2>$base.err 1>$base.src )
if [ "$?" -eq "0" -a -z "$stderr" ]
then
echo -ne "\033[32m.\033[m"
else
let fails+=1
files=("${files[@]}" "$f")
errors=("${errors[@]}" "$stderr")
echo -ne "\033[31m.\033[m"
fi
for f in $(find $1/inputs -name '*.pyc' -or -name '*.pyo')
do
rel=$(realpath --relative-to=$1/inputs $f)
baseline=$1/baseline/$prog/$rel
out=tests/$prog/$rel
mkdir -p $(dirname $out)
if ! ./$prog "$f" 2>$out.err 1>$out.src
then
let fails+=1
files=("${files[@]}" "$rel")
stderr=$(cat $out.err)
errors=("${errors[@]}" "Bad exit code\n$stderr")
echo -ne "\033[31m.\033[m"
elif ! diff $out.err $baseline.err >/dev/null 2>&1
then
let fails+=1
files=("${files[@]}" "$rel")
errdiff=$(diff $out.err $baseline.err)
errors=("${errors[@]}" "Mismatch stderr\n$errdiff")
echo -ne "\033[31m.\033[m"
elif ! diff $out.src $baseline.src >/dev/null 2>&1
then
let fails+=1
files=("${files[@]}" "$rel")
srcdiff=$(diff $out.src $baseline.src)
errors=("${errors[@]}" "Mismatch stdout\n$srcdiff")
echo -ne "\033[31m.\033[m"
else
echo -ne "\033[32m.\033[m"
fi
done
done
echo -e "\n\n$fails tests failed:"
for ((i=0; i<${#files[@]}; i++))
Expand Down
Empty file.
Loading