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

Make C++ compatible #13

Closed
wants to merge 20 commits into from
Closed
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
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
22 changes: 21 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

cmake_minimum_required (VERSION 2.8)

project (Mimick C)
project (Mimick C CXX)

if(POLICY CMP0042)
# Allow @rpath in target's install name
cmake_policy(SET CMP0042 NEW)
endif()

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

include (CheckSymbolExists)
include (GNUInstallDirs)
Expand Down Expand Up @@ -157,13 +167,15 @@ include_directories(include src "${PROJECT_BINARY_DIR}/src")
add_subdirectory (src)

add_library (mimick STATIC ${SOURCE_FILES})
target_include_directories(mimick INTERFACE $<INSTALL_INTERFACE:include>)

foreach (F ${INTERFACE_FILES})
get_filename_component(DEST "${F}" PATH)
install(FILES "${F}" DESTINATION "${DEST}")
endforeach ()

install(TARGETS mimick
EXPORT mimick-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
Expand Down Expand Up @@ -196,3 +208,11 @@ endfunction ()
enable_testing ()
add_subdirectory (test)
add_subdirectory (sample)

install(EXPORT mimick-targets DESTINATION lib/cmake/mimick)
include(CMakePackageConfigHelpers)
configure_package_config_file(
"mimickConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/mimickConfig.cmake"
INSTALL_DESTINATION "lib/cmake/mimick"
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mimickConfig.cmake" DESTINATION "lib/cmake/mimick")
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;
Copy link
Owner

Choose a reason for hiding this comment

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

This breaks the API as far as I'm concerned. I'd rather have mmk_stub be an alias for struct mmk_stub *. I would be fine if struct mmk_stub was renamed to struct mmk_stub_ however.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does break API, but I could only find one place where the mmk_stub alias was being used instead of struct mmk_stub *. I figured I'd normalize to the struct <name> syntax instead.

Copy link
Owner

Choose a reason for hiding this comment

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

This is mostly about API usage -- mmk_stub is an opaque type, so having the user type struct mmk_stub in C is just misleading because you'd expect to be able to change or read some fields in there.

In other words, I am fine with keeping struct and union keywords when it's possible to access the underlying type, and this is not one of these situations.


/**
* 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_ */
87 changes: 87 additions & 0 deletions include/mimick/literal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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;

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

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) {
Copy link
Owner

Choose a reason for hiding this comment

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

That should probably be inline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point.

// 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__)) \
Copy link
Owner

Choose a reason for hiding this comment

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

Hah, what a clever hack -- I love it!

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, value))

# 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__ })

# endif

#endif /* !MMK_LITERAL_H_ */
14 changes: 12 additions & 2 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,7 +66,7 @@ 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 })
# undef mmk_eq
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, { 0 }))

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_ */
Loading