Skip to content

Commit

Permalink
Make C++11 compatible (#5)
Browse files Browse the repository at this point in the history
* Drop conflicting type aliasing.
* Force C linkage if compiling for C++.
* Cast explicitly and deal with C99 features.
* Handle array arguments properly.
* Avoid brace initializers for scalars.
* Force C++11.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored Jul 28, 2020
1 parent b8609de commit 5fc69c0
Show file tree
Hide file tree
Showing 20 changed files with 375 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
!.cmake/*

!*.c
!*.cpp
!*.h
!*.S
!*.asm
Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

cmake_minimum_required (VERSION 2.8)

project (Mimick C)
project (Mimick C CXX)
# Default to C++11
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()

if(POLICY CMP0042)
# Allow @rpath in target's install name
Expand Down
25 changes: 17 additions & 8 deletions include/mimick.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@

# include <stddef.h>
# include "mimick/preprocess.h"
# include "mimick/literal.h"

# ifdef __cplusplus
extern "C" {
# endif

struct mmk_va_param {
size_t size;
char data[];
char data[1];
};

typedef void(*mmk_fn)(void);
typedef struct mmk_stub *mmk_stub;
struct mmk_stub;

/**
* An invalid stub. Used for error checking if creating a stub fails.
*/
# define MMK_STUB_INVALID ((mmk_stub)0)
# define MMK_STUB_INVALID ((struct mmk_stub *)0)

/**
* An invalid mock. Used for error checking if creating a mock fails.
Expand All @@ -59,7 +64,7 @@ typedef struct mmk_stub *mmk_stub;
*
* @returns The current stub object.
*/
mmk_stub mmk_ctx(void);
struct mmk_stub *mmk_ctx(void);

/**
* Get the user context from a stub object.
Expand All @@ -73,7 +78,7 @@ mmk_stub mmk_ctx(void);
*
* @returns The user context or NULL
*/
void *mmk_stub_context(mmk_stub stub);
void *mmk_stub_context(struct mmk_stub *stub);

/**
* Creates a stub object and hijacks calls to the function targeted by
Expand All @@ -95,7 +100,7 @@ void *mmk_stub_context(mmk_stub stub);
*
* @returns The created stub object or MMK_STUB_INVALID.
*/
mmk_stub mmk_stub_create(const char *target, mmk_fn fn, void *ctx);
struct mmk_stub *mmk_stub_create(const char *target, mmk_fn fn, void *ctx);

/**
* Destroy a stub object and restores the original function it targets.
Expand All @@ -108,10 +113,14 @@ mmk_stub mmk_stub_create(const char *target, mmk_fn fn, void *ctx);
*
* @param[in] stub The stub object to destroy.
*/
void mmk_stub_destroy(mmk_stub stub);
void mmk_stub_destroy(struct mmk_stub *stub);

/** @} */

#ifdef __cplusplus
}
#endif

/* Mock API */

/**
Expand All @@ -122,7 +131,7 @@ void mmk_stub_destroy(mmk_stub stub);
/**
* Convenience wrapper to get an addressable temporary object from a lvalue.
*/
# define mmk_val(Type, ...) (&(Type) { __VA_ARGS__ })
# define mmk_val(Type, ...) &(mmk_literal(Type, {(__VA_ARGS__)}))

/**
* Defines a mock blueprint and typedef the Id to a function pointer type
Expand Down
8 changes: 8 additions & 0 deletions include/mimick/alloc.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#ifndef MMK_ALLOC_H_
# define MMK_ALLOC_H_

# ifdef __cplusplus
extern "C" {
# endif

void *mmk_malloc(size_t size);
void *mmk_realloc(void *ptr, size_t size);
void mmk_free(void *ptr);

# ifdef __cplusplus
}
# endif

#endif /* !MMK_ALLOC_H_ */
91 changes: 91 additions & 0 deletions include/mimick/literal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License (MIT)
*
* Copyright © 2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MMK_LITERAL_H_
# define MMK_LITERAL_H_

# undef mmk_struct_literal

# ifdef __cplusplus

# include <cstdarg>
# include <utility>

# include "preprocess.h"

template<typename T, int ID>
struct mmk_literal {
static constexpr int id = ID;
static T storage;
};

template<typename T, int ID>
T mmk_literal<T, ID>::storage{0};

template <typename T>
T & mmk_assign(T & dst, T src) {
return dst = std::move(src);
}

template <typename T, size_t N>
T (& mmk_assign(T (&dst)[N], T * src))[N] {
if (src) {
mmk_memcpy(dst, src, sizeof(T) * N);
} else {
mmk_memset(dst, 0, sizeof(T) * N);
}
return dst;
}

va_list & mmk_assign(va_list & dst, va_list src) {
// no-op
return dst;
}

# define MMK_COMMA_APPEND(_, prefix, content) (void) (prefix content),

# define mmk_struct_initialize(variable, ...) \
(MMK_EXPAND(MMK_APPLY_N_(MMK_COMMA_APPEND, MMK_VA_NARGS(__VA_ARGS__), variable, __VA_ARGS__)) \
variable)

# define mmk_struct_literal(type, ...) \
mmk_struct_initialize((mmk_literal<type, __COUNTER__>::storage), __VA_ARGS__)

# define mmk_literal(type, value) \
(mmk_assign(mmk_literal<type, __COUNTER__>::storage, (type) value))

# define mmk_default_value(type) type{0}

# else /* !defined __cplusplus */

# define mmk_assign(dst, src) (dst) = (src)

# define mmk_literal(type, value) ((type) value)

# define mmk_struct_literal(type, ...) ((type) { __VA_ARGS__ })

# define mmk_default_value(type) {0}

# endif

#endif /* !MMK_LITERAL_H_ */
16 changes: 13 additions & 3 deletions include/mimick/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@

# include <stddef.h>

# include "literal.h"

/* Using anonymous structs in compound literals is perfectly valid C,
and we use this behaviour in Mimick. This MSVC warning does nothing
useful in that regard, so we simply disable it. */
# ifdef _MSC_VER
# pragma warning (disable: 4116)
# endif

# ifdef __cplusplus
extern "C" {
# endif

# define MMK_MATCHER_BIT_CMP (1 << (8 * sizeof (int) - 2))
# define MMK_MATCHER_BIT_EQ (1 << 1)
# define MMK_MATCHER_BIT_LT (1 << 2)
Expand Down Expand Up @@ -60,9 +66,9 @@ struct mmk_matcher {
struct mmk_matcher *next;
};

# define mmk_matcher_val_(Kind, Type, Val) (mmk_matcher_add(Kind, __COUNTER__), ((Type) Val))
# define mmk_matcher_val_(Kind, Type, Val) (mmk_matcher_add(Kind, __COUNTER__), mmk_literal(Type, Val))
# undef mmk_any
# define mmk_any(Type) mmk_matcher_val_(MMK_MATCHER_ANY, Type, { 0 })
# define mmk_any(Type) mmk_matcher_val_(MMK_MATCHER_ANY, Type, mmk_default_value(Type))
# undef mmk_eq
# define mmk_eq(Type, Val) mmk_matcher_val_(MMK_MATCHER_EQ, Type, Val)
# undef mmk_ne
Expand All @@ -76,7 +82,7 @@ struct mmk_matcher {
# undef mmk_ge
# define mmk_ge(Type, Val) mmk_matcher_val_(MMK_MATCHER_GEQ, Type, Val)
# undef mmk_that
# define mmk_that(Type, Predicate) (mmk_matcher_add_fn(MMK_MATCHER_THAT, __COUNTER__, (void(*)(void))*(int(*[1])(Type)) {(Predicate)}), ((Type) { 0 }))
# define mmk_that(Type, Predicate) (mmk_matcher_add_fn(MMK_MATCHER_THAT, __COUNTER__, (void(*)(void))(Predicate)), mmk_literal(Type, mmk_default_value(Type)))

void mmk_matcher_init(int kind);
void mmk_matcher_add(enum mmk_matcher_kind kind, int counter);
Expand All @@ -86,4 +92,8 @@ void mmk_matcher_term(void);
struct mmk_matcher *mmk_matcher_ctx(void);
void (*mmk_matcher_get_predicate(struct mmk_matcher *m))(void);

# ifdef __cplusplus
}
# endif

#endif /* !MIMICK_MATCHER_H_ */
45 changes: 30 additions & 15 deletions include/mimick/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@
#ifndef MIMICK_MOCK_H_
# define MIMICK_MOCK_H_

typedef struct mmk_mock_ctx *mmk_mock_ctx;

# include <errno.h>
# include <stdarg.h>

# include "matcher.h"
# include "alloc.h"
# include "literal.h"
# include "string.h"
# include "when.h"
# include "verify.h"
# include "va.h"

# ifdef __cplusplus
extern "C" {
# endif

struct mmk_mock_ctx;

struct mmk_params {
struct mmk_matcher *matcher_ctx;
struct mmk_params *next;
Expand All @@ -59,14 +64,18 @@ void mmk_mock_reset_call(const char *file, int line);
void mmk_reset(mmk_fn fn);
# define mmk_reset(Fn) mmk_reset((mmk_fn) Fn);

# ifdef __cplusplus
}
# endif

# undef mmk_mock
# define mmk_mock(Target, ...) \
(MMK_MANGLE(MMK_VA_HEAD(__VA_ARGS__), create)( \
(Target), \
(struct mmk_mock_options) { \
mmk_struct_literal(struct mmk_mock_options, \
.sentinel_ = 1, \
MMK_VA_TAIL(__VA_ARGS__) \
}))
)))

# define MMK_MK_ARG_STR(_, X) #X,

Expand All @@ -77,7 +86,7 @@ void mmk_reset(mmk_fn fn);
MMK_EXPAND(MMK_DEF_VERIFY_PARAM_VA_ ## Id (__VA_ARGS__))
# define MMK_DEF_VERIFY_PARAM_VA_WITH(...)

# define MMK_DEF_VERIFY_PARAM__(X) .X = X,
# define MMK_DEF_VERIFY_PARAM__(X) mmk_assign(reg_params.X, X);
# define MMK_DEF_VERIFY_PARAM_VA_WITHOUT(N, Id, T, ...) \
MMK_DEF_VERIFY_PARAM__(param ## N)

Expand Down Expand Up @@ -167,8 +176,9 @@ void mmk_reset(mmk_fn fn);
MMK_EXPAND(MMK_SET_PARAMS_VA_ ## Id (__VA_ARGS__))

# define MMK_SET_PARAMS_VA_WITH(...)

# define MMK_SET_PARAMS_VA_WITHOUT(N, Id, T, ...) \
bind->params.param ## N = param ## N;
mmk_assign(bind->params.param ## N, param ## N);

# define MMK_SET_PARAMS_(N, Id, T) \
MMK_COND_VA(MMK_SET_PARAMS_VA, (WITHOUT, N, Id, T,), (WITH,), T)
Expand Down Expand Up @@ -298,20 +308,24 @@ void mmk_reset(mmk_fn fn);
PreDecl(Id); \
struct mmk_matcher *matcher_ctx = mmk_matcher_ctx(); \
if (matcher_ctx) { \
struct mmk_mock_ctx *mock = mmk_stub_context(mmk_ctx ()); \
struct mmk_mock_ctx *mock = \
(struct mmk_mock_ctx *)mmk_stub_context(mmk_ctx ()); \
mmk_mock_report_call(); \
if (matcher_ctx->kind == 0) { \
struct MMK_MANGLE(Id, binding) *bind = \
mmk_malloc(sizeof (struct MMK_MANGLE(Id, binding))); \
(struct MMK_MANGLE(Id, binding) *)mmk_malloc( \
sizeof (struct MMK_MANGLE(Id, binding))); \
bind->result = *mmk_when_get_result(); \
MMK_APPLY_N(MMK_SET_PARAMS, Id, __VA_ARGS__) \
MMK_EXPAND(VaMacro(BIND)(&bind->params, __VA_ARGS__)); \
mmk_when_impl(mock, bind); \
} else if (matcher_ctx->kind == 1) { \
size_t times = 0; \
for (struct MMK_MANGLE(Id, params) *p = \
mmk_mock_params_begin(mock); \
p; p = mmk_mock_params_next(mock, p)) \
(struct MMK_MANGLE(Id, params) *) \
mmk_mock_params_begin(mock); p; \
p = (struct MMK_MANGLE(Id, params) *) \
mmk_mock_params_next(mock, p)) \
{ \
struct mmk_matcher *m = matcher_ctx; \
(void) m; \
Expand All @@ -331,15 +345,16 @@ void mmk_reset(mmk_fn fn);
VaMacro(END)(_); \
Return(zero__); \
} \
struct MMK_MANGLE(Id, params) reg_params = { \
.mmk_times__ = 0, \
MMK_APPLY_N(MMK_DEF_VERIFY_PARAM, Id, __VA_ARGS__) \
}; \
struct MMK_MANGLE(Id, params) reg_params; \
mmk_memset(&reg_params, 0, sizeof(struct MMK_MANGLE(Id, params))); \
reg_params.mmk_times__ = 0; \
MMK_APPLY_N(MMK_DEF_VERIFY_PARAM, Id, __VA_ARGS__); \
MMK_EXPAND(VaMacro(REGISTER_CALL)(&reg_params, __VA_ARGS__)); \
mmk_verify_register_call(&reg_params, sizeof (reg_params)); \
struct mmk_params *param = mmk_mock_get_params(); \
for (; param; param = param->next) { \
struct MMK_MANGLE(Id, binding) *bind = (void*) param; \
struct MMK_MANGLE(Id, binding) *bind = \
(struct MMK_MANGLE(Id, binding) *) param; \
struct mmk_matcher *m = param->matcher_ctx; \
(void) m; \
MMK_EXPAND(MMK_APPLY_N(MMK_TRYMATCH, Id, __VA_ARGS__)) \
Expand Down
9 changes: 9 additions & 0 deletions include/mimick/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
#ifndef MMK_STRING_H_
# define MMK_STRING_H_

# ifdef __cplusplus
extern "C" {
# endif

int mmk_memcmp(const void *s1, const void *s2, size_t n);
void *mmk_memcpy(void *dst, const void *src, size_t n);
void *mmk_memset(void *dst, int value, size_t n);

# ifdef __cplusplus
}
# endif

#endif /* !MMK_STRING_H_ */
Loading

0 comments on commit 5fc69c0

Please sign in to comment.