Skip to content

Commit

Permalink
Fix MSVC 2017 warnings
Browse files Browse the repository at this point in the history
/W4
  • Loading branch information
ccoustet committed Oct 29, 2018
1 parent f881105 commit 085d678
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef double (*function1)(double);
static void bench(const char *expr, function1 func) {
int i, j;
volatile double d;
double tmp;
static double tmp;
clock_t start;

te_variable lk = {"a", {&tmp}, TE_VARIABLE, NULL};
Expand Down
95 changes: 95 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright (C) 2016-2018 |Meso|Star>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.8)
project(tinyexpr C)
enable_testing()

option(LIB_ONLY "Do not compile the test pograms nor the benchmark" OFF)

set(TINYEXPR_SOURCE_DIR ${PROJECT_SOURCE_DIR}/..)

include_directories(${TINYEXPR_SOURCE_DIR})

################################################################################
# Configure and define targets
################################################################################
set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})

if(CMAKE_COMPILER_IS_GNUCC)
set(MATH_LIB m)
endif()

if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
elseif(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wshadow -Wcast-align -Wmissing-declarations -Wmissing-prototypes -Wconversion -Wno-long-long -pedantic")
endif()

ADD_LIBRARY(tinyexpr STATIC
${TINYEXPR_SOURCE_DIR}/tinyexpr.c ${TINYEXPR_SOURCE_DIR}/tinyexpr.h)
target_compile_features(tinyexpr PUBLIC c_std_99)
set_target_properties(tinyexpr PROPERTIES
VERSION ${VERSION}
SOVERSION ${VERSION_MAJOR})

################################################################################
# Define tests
################################################################################
if(NOT LIB_ONLY)
add_executable(bench ${TINYEXPR_SOURCE_DIR}/benchmark.c)
target_link_libraries(bench tinyexpr ${MATH_LIB})

add_executable(example ${TINYEXPR_SOURCE_DIR}/example.c)
target_link_libraries(example tinyexpr ${MATH_LIB})
add_test(example example)

add_executable(example2 ${TINYEXPR_SOURCE_DIR}/example2.c)
target_link_libraries(example2 tinyexpr ${MATH_LIB})
add_test(example2 example2)

add_executable(example3 ${TINYEXPR_SOURCE_DIR}/example3.c)
target_link_libraries(example3 tinyexpr ${MATH_LIB})
add_test(example3 example3)

add_executable(test_pow_left ${TINYEXPR_SOURCE_DIR}/test.c)
target_link_libraries(test_pow_left tinyexpr ${MATH_LIB})
add_test(test_pow_left test_pow_left)

file(READ ${TINYEXPR_SOURCE_DIR}/tinyexpr.c tinyexpr_c_file_content)
file(WRITE ${PROJECT_BINARY_DIR}/tinyexpr_pow_right.c
"#define TE_POW_FROM_RIGHT 1
#define TE_NAT_LOG 1
${tinyexpr_c_file_content}")

file(READ ${TINYEXPR_SOURCE_DIR}/test.c test_c_file_content)
file(WRITE ${PROJECT_BINARY_DIR}/test_pow_right.c
"#define TE_POW_FROM_RIGHT 1
#define TE_NAT_LOG 1
${test_c_file_content}")

ADD_LIBRARY(tinyexpr_pow_right STATIC
${PROJECT_BINARY_DIR}/tinyexpr_pow_right.c ${TINYEXPR_SOURCE_DIR}/tinyexpr.h)
target_compile_features(tinyexpr_pow_right PUBLIC c_std_99)
set_target_properties(tinyexpr_pow_right PROPERTIES VERSION ${VERSION})

add_executable(test_pow_right ${PROJECT_BINARY_DIR}/test_pow_right.c)
add_dependencies(test_pow_right tinyexpr_pow_right)
target_link_libraries(test_pow_right tinyexpr_pow_right ${MATH_LIB})
add_test(test_pow_right test_pow_right)
endif(NOT LIB_ONLY)
2 changes: 1 addition & 1 deletion example2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char *argv[])

/* This shows an example where the variables
* x and y are bound at eval-time. */
double x, y;
static double x, y;
te_variable vars[] = {{"x", {&x}, TE_VARIABLE, NULL}, {"y", {&y}, TE_VARIABLE, NULL}};

/* This will compile the expression and check for errors. */
Expand Down
14 changes: 7 additions & 7 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ static void test_infs() {

static void test_variables() {

double x, y, test;
static double x, y, test;
te_variable lookup[] =
{{"x", {&x}, TE_VARIABLE, NULL},
{"y", {&y}, TE_VARIABLE, NULL},
Expand Down Expand Up @@ -356,7 +356,7 @@ static void test_variables() {

static void test_functions() {

double x, y;
static double x, y;
te_variable lookup[] =
{{"x", {&x}, TE_VARIABLE, NULL}, {"y", {&y}, TE_VARIABLE, NULL}};

Expand Down Expand Up @@ -390,7 +390,7 @@ static void test_functions() {
}


static double sum0() {
static double sum0(void) {
return 6;
}
static double sum1(double a) {
Expand Down Expand Up @@ -418,7 +418,7 @@ static double sum7(double a, double b, double c, double d, double e, double f, d

static void test_dynamic() {

double x, f;
static double x, f;
te_variable lookup[] = {
{"x", {&x}, TE_VARIABLE, NULL},
{"f", {&f}, TE_VARIABLE, NULL},
Expand Down Expand Up @@ -494,8 +494,8 @@ static double cell(void *context, double a) {

static void test_closure() {

double extra;
double c[] = {5,6,7,8,9};
static double extra;
static double c[] = {5,6,7,8,9};

te_variable lookup[] = {
{"c0", {.cl0=clo0}, TE_CLOSURE0, &extra},
Expand Down Expand Up @@ -602,7 +602,7 @@ static void test_pow() {
};
#endif

double a = 2, b = 3;
static double a = 2, b = 3;

te_variable lookup[] = {
{"a", {&a}, TE_VARIABLE, NULL},
Expand Down
45 changes: 35 additions & 10 deletions tinyexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ For log = natural log uncomment the next line. */
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>

#ifndef NAN
#define NAN (0.0/0.0)
Expand Down Expand Up @@ -95,6 +96,26 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
return ret;
}

static te_expr *new_expr1(const int type, te_expr *p1) {
const size_t size = sizeof(te_expr) + (IS_CLOSURE(type) ? sizeof(void*) : 0);
assert(p1 && ARITY(type) == 1);
te_expr *ret = malloc(size);
ret->type = type;
ret->v.bound = 0;
ret->parameters[0] = p1;
return ret;
}

static te_expr *new_expr2(const int type, te_expr *p1, te_expr *p2) {
const size_t size = sizeof(te_expr) + sizeof(void*) + (IS_CLOSURE(type) ? sizeof(void*) : 0);
assert(p1 && p2 && ARITY(type) == 2);
te_expr *ret = malloc(size);
ret->type = type;
ret->v.bound = 0;
ret->parameters[0] = p1;
ret->parameters[1] = p2;
return ret;
}

static void te_free_parameters(te_expr *n) {
if (!n) return;
Expand Down Expand Up @@ -149,20 +170,24 @@ static double ncr(double n, double r) {
}
static double npr(double n, double r) {return ncr(n, r) * fac(r);}

/* Workaround for a VC 2017 problem */
static double ceil_(double x) { return ceil(x); }
static double floor_(double x) { return floor(x); }

static const te_variable functions[] = {
/* must be in alphabetical order */
{"abs", {.f1=fabs}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"acos", {.f1=acos}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"asin", {.f1=asin}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"atan", {.f1=atan}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"atan2", {.f2=atan2}, TE_FUNCTION2 | TE_FLAG_PURE, 0},
{"ceil", {.f1=ceil}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"ceil", {.f1=ceil_}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"cos", {.f1=cos}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"cosh", {.f1=cosh}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"e", {.f0=e}, TE_FUNCTION0 | TE_FLAG_PURE, 0},
{"exp", {.f1=exp}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"fac", {.f1=fac}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"floor", {.f1=floor}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"floor", {.f1=floor_}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
{"ln", {.f1=log}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
#ifdef TE_NAT_LOG
{"log", {.f1=log}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
Expand Down Expand Up @@ -405,7 +430,7 @@ static te_expr *power(state *s) {
if (sign == 1) {
ret = base(s);
} else {
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
ret = new_expr1(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
ret->v.f.f1 = negate;
}

Expand Down Expand Up @@ -433,19 +458,19 @@ static te_expr *factor(state *s) {

if (insertion) {
/* Make exponentiation go right-to-left. */
te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
te_expr *insert = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
insert->v.f.f2 = t;
insertion->parameters[1] = insert;
insertion = insert;
} else {
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret->v.f.f2 = t;
insertion = ret;
}
}

if (neg) {
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
ret = new_expr1(TE_FUNCTION1 | TE_FLAG_PURE, ret);
ret->v.f.f1 = negate;
}

Expand All @@ -459,7 +484,7 @@ static te_expr *factor(state *s) {
while (s->type == TOK_INFIX && (s->v.f.f2 == pow)) {
te_fun2 t = s->v.f.f2;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret->v.f.f2 = t;
}

Expand All @@ -476,7 +501,7 @@ static te_expr *term(state *s) {
while (s->type == TOK_INFIX && (s->v.f.f2 == mul || s->v.f.f2 == divide || s->v.f.f2 == fmod)) {
te_fun2 t = s->v.f.f2;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
ret->v.f.f2 = t;
}

Expand All @@ -491,7 +516,7 @@ static te_expr *expr(state *s) {
while (s->type == TOK_INFIX && (s->v.f.f2 == add || s->v.f.f2 == sub)) {
te_fun2 t = s->v.f.f2;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
ret->v.f.f2 = t;
}

Expand All @@ -505,7 +530,7 @@ static te_expr *list(state *s) {

while (s->type == TOK_SEP) {
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
ret->v.f.f2 = comma;
}

Expand Down

0 comments on commit 085d678

Please sign in to comment.