Skip to content

Commit 085d678

Browse files
committed
Fix MSVC 2017 warnings
/W4
1 parent f881105 commit 085d678

File tree

5 files changed

+139
-19
lines changed

5 files changed

+139
-19
lines changed

benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef double (*function1)(double);
3838
static void bench(const char *expr, function1 func) {
3939
int i, j;
4040
volatile double d;
41-
double tmp;
41+
static double tmp;
4242
clock_t start;
4343

4444
te_variable lk = {"a", {&tmp}, TE_VARIABLE, NULL};

cmake/CMakeLists.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright (C) 2016-2018 |Meso|Star>
2+
#
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
cmake_minimum_required(VERSION 3.8)
17+
project(tinyexpr C)
18+
enable_testing()
19+
20+
option(LIB_ONLY "Do not compile the test pograms nor the benchmark" OFF)
21+
22+
set(TINYEXPR_SOURCE_DIR ${PROJECT_SOURCE_DIR}/..)
23+
24+
include_directories(${TINYEXPR_SOURCE_DIR})
25+
26+
################################################################################
27+
# Configure and define targets
28+
################################################################################
29+
set(VERSION_MAJOR 1)
30+
set(VERSION_MINOR 0)
31+
set(VERSION_PATCH 0)
32+
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
33+
34+
if(CMAKE_COMPILER_IS_GNUCC)
35+
set(MATH_LIB m)
36+
endif()
37+
38+
if(MSVC)
39+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
40+
elseif(CMAKE_COMPILER_IS_GNUCC)
41+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wshadow -Wcast-align -Wmissing-declarations -Wmissing-prototypes -Wconversion -Wno-long-long -pedantic")
42+
endif()
43+
44+
ADD_LIBRARY(tinyexpr STATIC
45+
${TINYEXPR_SOURCE_DIR}/tinyexpr.c ${TINYEXPR_SOURCE_DIR}/tinyexpr.h)
46+
target_compile_features(tinyexpr PUBLIC c_std_99)
47+
set_target_properties(tinyexpr PROPERTIES
48+
VERSION ${VERSION}
49+
SOVERSION ${VERSION_MAJOR})
50+
51+
################################################################################
52+
# Define tests
53+
################################################################################
54+
if(NOT LIB_ONLY)
55+
add_executable(bench ${TINYEXPR_SOURCE_DIR}/benchmark.c)
56+
target_link_libraries(bench tinyexpr ${MATH_LIB})
57+
58+
add_executable(example ${TINYEXPR_SOURCE_DIR}/example.c)
59+
target_link_libraries(example tinyexpr ${MATH_LIB})
60+
add_test(example example)
61+
62+
add_executable(example2 ${TINYEXPR_SOURCE_DIR}/example2.c)
63+
target_link_libraries(example2 tinyexpr ${MATH_LIB})
64+
add_test(example2 example2)
65+
66+
add_executable(example3 ${TINYEXPR_SOURCE_DIR}/example3.c)
67+
target_link_libraries(example3 tinyexpr ${MATH_LIB})
68+
add_test(example3 example3)
69+
70+
add_executable(test_pow_left ${TINYEXPR_SOURCE_DIR}/test.c)
71+
target_link_libraries(test_pow_left tinyexpr ${MATH_LIB})
72+
add_test(test_pow_left test_pow_left)
73+
74+
file(READ ${TINYEXPR_SOURCE_DIR}/tinyexpr.c tinyexpr_c_file_content)
75+
file(WRITE ${PROJECT_BINARY_DIR}/tinyexpr_pow_right.c
76+
"#define TE_POW_FROM_RIGHT 1
77+
#define TE_NAT_LOG 1
78+
${tinyexpr_c_file_content}")
79+
80+
file(READ ${TINYEXPR_SOURCE_DIR}/test.c test_c_file_content)
81+
file(WRITE ${PROJECT_BINARY_DIR}/test_pow_right.c
82+
"#define TE_POW_FROM_RIGHT 1
83+
#define TE_NAT_LOG 1
84+
${test_c_file_content}")
85+
86+
ADD_LIBRARY(tinyexpr_pow_right STATIC
87+
${PROJECT_BINARY_DIR}/tinyexpr_pow_right.c ${TINYEXPR_SOURCE_DIR}/tinyexpr.h)
88+
target_compile_features(tinyexpr_pow_right PUBLIC c_std_99)
89+
set_target_properties(tinyexpr_pow_right PROPERTIES VERSION ${VERSION})
90+
91+
add_executable(test_pow_right ${PROJECT_BINARY_DIR}/test_pow_right.c)
92+
add_dependencies(test_pow_right tinyexpr_pow_right)
93+
target_link_libraries(test_pow_right tinyexpr_pow_right ${MATH_LIB})
94+
add_test(test_pow_right test_pow_right)
95+
endif(NOT LIB_ONLY)

example2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int main(int argc, char *argv[])
1414

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

2020
/* This will compile the expression and check for errors. */

test.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static void test_infs() {
277277

278278
static void test_variables() {
279279

280-
double x, y, test;
280+
static double x, y, test;
281281
te_variable lookup[] =
282282
{{"x", {&x}, TE_VARIABLE, NULL},
283283
{"y", {&y}, TE_VARIABLE, NULL},
@@ -356,7 +356,7 @@ static void test_variables() {
356356

357357
static void test_functions() {
358358

359-
double x, y;
359+
static double x, y;
360360
te_variable lookup[] =
361361
{{"x", {&x}, TE_VARIABLE, NULL}, {"y", {&y}, TE_VARIABLE, NULL}};
362362

@@ -390,7 +390,7 @@ static void test_functions() {
390390
}
391391

392392

393-
static double sum0() {
393+
static double sum0(void) {
394394
return 6;
395395
}
396396
static double sum1(double a) {
@@ -418,7 +418,7 @@ static double sum7(double a, double b, double c, double d, double e, double f, d
418418

419419
static void test_dynamic() {
420420

421-
double x, f;
421+
static double x, f;
422422
te_variable lookup[] = {
423423
{"x", {&x}, TE_VARIABLE, NULL},
424424
{"f", {&f}, TE_VARIABLE, NULL},
@@ -494,8 +494,8 @@ static double cell(void *context, double a) {
494494

495495
static void test_closure() {
496496

497-
double extra;
498-
double c[] = {5,6,7,8,9};
497+
static double extra;
498+
static double c[] = {5,6,7,8,9};
499499

500500
te_variable lookup[] = {
501501
{"c0", {.cl0=clo0}, TE_CLOSURE0, &extra},
@@ -602,7 +602,7 @@ static void test_pow() {
602602
};
603603
#endif
604604

605-
double a = 2, b = 3;
605+
static double a = 2, b = 3;
606606

607607
te_variable lookup[] = {
608608
{"a", {&a}, TE_VARIABLE, NULL},

tinyexpr.c

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ For log = natural log uncomment the next line. */
4040
#include <string.h>
4141
#include <stdio.h>
4242
#include <limits.h>
43+
#include <assert.h>
4344

4445
#ifndef NAN
4546
#define NAN (0.0/0.0)
@@ -95,6 +96,26 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
9596
return ret;
9697
}
9798

99+
static te_expr *new_expr1(const int type, te_expr *p1) {
100+
const size_t size = sizeof(te_expr) + (IS_CLOSURE(type) ? sizeof(void*) : 0);
101+
assert(p1 && ARITY(type) == 1);
102+
te_expr *ret = malloc(size);
103+
ret->type = type;
104+
ret->v.bound = 0;
105+
ret->parameters[0] = p1;
106+
return ret;
107+
}
108+
109+
static te_expr *new_expr2(const int type, te_expr *p1, te_expr *p2) {
110+
const size_t size = sizeof(te_expr) + sizeof(void*) + (IS_CLOSURE(type) ? sizeof(void*) : 0);
111+
assert(p1 && p2 && ARITY(type) == 2);
112+
te_expr *ret = malloc(size);
113+
ret->type = type;
114+
ret->v.bound = 0;
115+
ret->parameters[0] = p1;
116+
ret->parameters[1] = p2;
117+
return ret;
118+
}
98119

99120
static void te_free_parameters(te_expr *n) {
100121
if (!n) return;
@@ -149,20 +170,24 @@ static double ncr(double n, double r) {
149170
}
150171
static double npr(double n, double r) {return ncr(n, r) * fac(r);}
151172

173+
/* Workaround for a VC 2017 problem */
174+
static double ceil_(double x) { return ceil(x); }
175+
static double floor_(double x) { return floor(x); }
176+
152177
static const te_variable functions[] = {
153178
/* must be in alphabetical order */
154179
{"abs", {.f1=fabs}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
155180
{"acos", {.f1=acos}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
156181
{"asin", {.f1=asin}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
157182
{"atan", {.f1=atan}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
158183
{"atan2", {.f2=atan2}, TE_FUNCTION2 | TE_FLAG_PURE, 0},
159-
{"ceil", {.f1=ceil}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
184+
{"ceil", {.f1=ceil_}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
160185
{"cos", {.f1=cos}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
161186
{"cosh", {.f1=cosh}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
162187
{"e", {.f0=e}, TE_FUNCTION0 | TE_FLAG_PURE, 0},
163188
{"exp", {.f1=exp}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
164189
{"fac", {.f1=fac}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
165-
{"floor", {.f1=floor}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
190+
{"floor", {.f1=floor_}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
166191
{"ln", {.f1=log}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
167192
#ifdef TE_NAT_LOG
168193
{"log", {.f1=log}, TE_FUNCTION1 | TE_FLAG_PURE, 0},
@@ -405,7 +430,7 @@ static te_expr *power(state *s) {
405430
if (sign == 1) {
406431
ret = base(s);
407432
} else {
408-
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
433+
ret = new_expr1(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
409434
ret->v.f.f1 = negate;
410435
}
411436

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

434459
if (insertion) {
435460
/* Make exponentiation go right-to-left. */
436-
te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
461+
te_expr *insert = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
437462
insert->v.f.f2 = t;
438463
insertion->parameters[1] = insert;
439464
insertion = insert;
440465
} else {
441-
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
466+
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
442467
ret->v.f.f2 = t;
443468
insertion = ret;
444469
}
445470
}
446471

447472
if (neg) {
448-
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
473+
ret = new_expr1(TE_FUNCTION1 | TE_FLAG_PURE, ret);
449474
ret->v.f.f1 = negate;
450475
}
451476

@@ -459,7 +484,7 @@ static te_expr *factor(state *s) {
459484
while (s->type == TOK_INFIX && (s->v.f.f2 == pow)) {
460485
te_fun2 t = s->v.f.f2;
461486
next_token(s);
462-
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
487+
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
463488
ret->v.f.f2 = t;
464489
}
465490

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

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

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

506531
while (s->type == TOK_SEP) {
507532
next_token(s);
508-
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
533+
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
509534
ret->v.f.f2 = comma;
510535
}
511536

0 commit comments

Comments
 (0)