Skip to content

Commit 22d2062

Browse files
committed
Introduce GIT_CALLBACK macro to enforce cdecl
Since we now always build the library with cdecl calling conventions, our callbacks should be decorated as such so that users will not be able to provide callbacks defined with other calling conventions. The `GIT_CALLBACK` macro will inject the `__cdecl` attribute as appropriate.
1 parent 57b753a commit 22d2062

34 files changed

+162
-155
lines changed

include/git2/apply.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ GIT_BEGIN_DECL
3333
* @param delta The delta to be applied
3434
* @param payload User-specified payload
3535
*/
36-
typedef int (*git_apply_delta_cb)(
36+
typedef int GIT_CALLBACK(git_apply_delta_cb)(
3737
const git_diff_delta *delta,
3838
void *payload);
3939

@@ -49,7 +49,7 @@ typedef int (*git_apply_delta_cb)(
4949
* @param hunk The hunk to be applied
5050
* @param payload User-specified payload
5151
*/
52-
typedef int (*git_apply_hunk_cb)(
52+
typedef int GIT_CALLBACK(git_apply_hunk_cb)(
5353
const git_diff_hunk *hunk,
5454
void *payload);
5555

include/git2/attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ GIT_EXTERN(int) git_attr_get_many(
202202
* @return 0 to continue looping, non-zero to stop. This value will be returned
203203
* from git_attr_foreach.
204204
*/
205-
typedef int (*git_attr_foreach_cb)(const char *name, const char *value, void *payload);
205+
typedef int GIT_CALLBACK(git_attr_foreach_cb)(const char *name, const char *value, void *payload);
206206

207207
/**
208208
* Loop over all the git attributes for a path.

include/git2/checkout.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ typedef struct {
220220
} git_checkout_perfdata;
221221

222222
/** Checkout notification callback function */
223-
typedef int (*git_checkout_notify_cb)(
223+
typedef int GIT_CALLBACK(git_checkout_notify_cb)(
224224
git_checkout_notify_t why,
225225
const char *path,
226226
const git_diff_file *baseline,
@@ -229,14 +229,14 @@ typedef int (*git_checkout_notify_cb)(
229229
void *payload);
230230

231231
/** Checkout progress notification function */
232-
typedef void (*git_checkout_progress_cb)(
232+
typedef void GIT_CALLBACK(git_checkout_progress_cb)(
233233
const char *path,
234234
size_t completed_steps,
235235
size_t total_steps,
236236
void *payload);
237237

238238
/** Checkout perfdata notification function */
239-
typedef void (*git_checkout_perfdata_cb)(
239+
typedef void GIT_CALLBACK(git_checkout_perfdata_cb)(
240240
const git_checkout_perfdata *perfdata,
241241
void *payload);
242242

include/git2/clone.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef enum {
6666
* @param payload an opaque payload
6767
* @return 0, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code
6868
*/
69-
typedef int (*git_remote_create_cb)(
69+
typedef int GIT_CALLBACK(git_remote_create_cb)(
7070
git_remote **out,
7171
git_repository *repo,
7272
const char *name,
@@ -87,7 +87,7 @@ typedef int (*git_remote_create_cb)(
8787
* @param payload payload specified by the options
8888
* @return 0, or a negative value to indicate error
8989
*/
90-
typedef int (*git_repository_create_cb)(
90+
typedef int GIT_CALLBACK(git_repository_create_cb)(
9191
git_repository **out,
9292
const char *path,
9393
int bare,

include/git2/common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ typedef size_t size_t;
4848
# define GIT_EXTERN(type) extern type
4949
#endif
5050

51+
/** Declare a callback function for application use. */
52+
#if defined(_MSC_VER)
53+
# define GIT_CALLBACK(name) (__cdecl *name)
54+
#else
55+
# define GIT_CALLBACK(name) (*name)
56+
#endif
57+
5158
/** Declare a function as deprecated. */
5259
#if defined(__GNUC__)
5360
# define GIT_DEPRECATED(func) \

include/git2/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef struct git_config_entry {
6666
const char *value; /**< String value of the entry */
6767
unsigned int include_depth; /**< Depth of includes where this variable was found */
6868
git_config_level_t level; /**< Which config file this was found in */
69-
void (*free)(struct git_config_entry *entry); /**< Free function for this entry */
69+
void GIT_CALLBACK(free)(struct git_config_entry *entry); /**< Free function for this entry */
7070
void *payload; /**< Opaque value for the free function. Do not read or write */
7171
} git_config_entry;
7272

@@ -81,7 +81,7 @@ GIT_EXTERN(void) git_config_entry_free(git_config_entry *);
8181
* @param entry the entry currently being enumerated
8282
* @param payload a user-specified pointer
8383
*/
84-
typedef int (*git_config_foreach_cb)(const git_config_entry *entry, void *payload);
84+
typedef int GIT_CALLBACK(git_config_foreach_cb)(const git_config_entry *entry, void *payload);
8585

8686
/**
8787
* An opaque structure for a configuration iterator

include/git2/diff.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ typedef struct {
328328
* - returns 0, the delta is inserted into the diff, and the diff process
329329
* continues.
330330
*/
331-
typedef int (*git_diff_notify_cb)(
331+
typedef int GIT_CALLBACK(git_diff_notify_cb)(
332332
const git_diff *diff_so_far,
333333
const git_diff_delta *delta_to_add,
334334
const char *matched_pathspec,
@@ -344,7 +344,7 @@ typedef int (*git_diff_notify_cb)(
344344
* @param new_path The path to the new file or NULL.
345345
* @return Non-zero to abort the diff.
346346
*/
347-
typedef int (*git_diff_progress_cb)(
347+
typedef int GIT_CALLBACK(git_diff_progress_cb)(
348348
const git_diff *diff_so_far,
349349
const char *old_path,
350350
const char *new_path,
@@ -462,7 +462,7 @@ GIT_EXTERN(int) git_diff_init_options(
462462
* @param progress Goes from 0 to 1 over the diff
463463
* @param payload User-specified pointer from foreach function
464464
*/
465-
typedef int (*git_diff_file_cb)(
465+
typedef int GIT_CALLBACK(git_diff_file_cb)(
466466
const git_diff_delta *delta,
467467
float progress,
468468
void *payload);
@@ -528,7 +528,7 @@ typedef struct {
528528
* When iterating over a diff, callback that will be made for
529529
* binary content within the diff.
530530
*/
531-
typedef int(*git_diff_binary_cb)(
531+
typedef int GIT_CALLBACK(git_diff_binary_cb)(
532532
const git_diff_delta *delta,
533533
const git_diff_binary *binary,
534534
void *payload);
@@ -554,7 +554,7 @@ typedef struct {
554554
/**
555555
* When iterating over a diff, callback that will be made per hunk.
556556
*/
557-
typedef int (*git_diff_hunk_cb)(
557+
typedef int GIT_CALLBACK(git_diff_hunk_cb)(
558558
const git_diff_delta *delta,
559559
const git_diff_hunk *hunk,
560560
void *payload);
@@ -615,7 +615,7 @@ typedef struct {
615615
* of text. This uses some extra GIT_DIFF_LINE_... constants for output
616616
* of lines of file and hunk headers.
617617
*/
618-
typedef int (*git_diff_line_cb)(
618+
typedef int GIT_CALLBACK(git_diff_line_cb)(
619619
const git_diff_delta *delta, /**< delta that contains this data */
620620
const git_diff_hunk *hunk, /**< hunk containing this data */
621621
const git_diff_line *line, /**< line data */
@@ -699,14 +699,14 @@ typedef enum {
699699
* Pluggable similarity metric
700700
*/
701701
typedef struct {
702-
int (*file_signature)(
702+
int GIT_CALLBACK(file_signature)(
703703
void **out, const git_diff_file *file,
704704
const char *fullpath, void *payload);
705-
int (*buffer_signature)(
705+
int GIT_CALLBACK(buffer_signature)(
706706
void **out, const git_diff_file *file,
707707
const char *buf, size_t buflen, void *payload);
708-
void (*free_signature)(void *sig, void *payload);
709-
int (*similarity)(int *score, void *siga, void *sigb, void *payload);
708+
void GIT_CALLBACK(free_signature)(void *sig, void *payload);
709+
int GIT_CALLBACK(similarity)(int *score, void *siga, void *sigb, void *payload);
710710
void *payload;
711711
} git_diff_similarity_metric;
712712

include/git2/index.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ typedef enum {
132132

133133

134134
/** Callback for APIs that add/remove/update files matching pathspec */
135-
typedef int (*git_index_matched_path_cb)(
135+
typedef int GIT_CALLBACK(git_index_matched_path_cb)(
136136
const char *path, const char *matched_pathspec, void *payload);
137137

138138
/** Flags for APIs that add files matching pathspec */

include/git2/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct git_remote_head {
5252
/**
5353
* Callback for listing the remote heads
5454
*/
55-
typedef int (*git_headlist_cb)(git_remote_head *rhead, void *payload);
55+
typedef int GIT_CALLBACK(git_headlist_cb)(git_remote_head *rhead, void *payload);
5656

5757
/** @} */
5858
GIT_END_DECL

include/git2/notes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ GIT_BEGIN_DECL
2626
* - annotated_object_id: Oid of the git object being annotated
2727
* - payload: Payload data passed to `git_note_foreach`
2828
*/
29-
typedef int (*git_note_foreach_cb)(
29+
typedef int GIT_CALLBACK(git_note_foreach_cb)(
3030
const git_oid *blob_id, const git_oid *annotated_object_id, void *payload);
3131

3232
/**

0 commit comments

Comments
 (0)