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

+2-2
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

+1-1
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

+3-3
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

+2-2
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

+7
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

+2-2
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

+10-10
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

+1-1
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

+1-1
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

+1-1
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
/**

include/git2/odb.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GIT_BEGIN_DECL
2424
/**
2525
* Function type for callbacks from git_odb_foreach.
2626
*/
27-
typedef int (*git_odb_foreach_cb)(const git_oid *id, void *payload);
27+
typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload);
2828

2929
/**
3030
* Create a new object database with no backends.

include/git2/odb_backend.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ struct git_odb_stream {
9292
/**
9393
* Write at most `len` bytes into `buffer` and advance the stream.
9494
*/
95-
int (*read)(git_odb_stream *stream, char *buffer, size_t len);
95+
int GIT_CALLBACK(read)(git_odb_stream *stream, char *buffer, size_t len);
9696

9797
/**
9898
* Write `len` bytes from `buffer` into the stream.
9999
*/
100-
int (*write)(git_odb_stream *stream, const char *buffer, size_t len);
100+
int GIT_CALLBACK(write)(git_odb_stream *stream, const char *buffer, size_t len);
101101

102102
/**
103103
* Store the contents of the stream as an object with the id
@@ -109,24 +109,24 @@ struct git_odb_stream {
109109
* - the final number of received bytes differs from the size declared
110110
* with `git_odb_open_wstream()`
111111
*/
112-
int (*finalize_write)(git_odb_stream *stream, const git_oid *oid);
112+
int GIT_CALLBACK(finalize_write)(git_odb_stream *stream, const git_oid *oid);
113113

114114
/**
115115
* Free the stream's memory.
116116
*
117117
* This method might be called without a call to `finalize_write` if
118118
* an error occurs or if the object is already present in the ODB.
119119
*/
120-
void (*free)(git_odb_stream *stream);
120+
void GIT_CALLBACK(free)(git_odb_stream *stream);
121121
};
122122

123123
/** A stream to write a pack file to the ODB */
124124
struct git_odb_writepack {
125125
git_odb_backend *backend;
126126

127-
int (*append)(git_odb_writepack *writepack, const void *data, size_t size, git_transfer_progress *stats);
128-
int (*commit)(git_odb_writepack *writepack, git_transfer_progress *stats);
129-
void (*free)(git_odb_writepack *writepack);
127+
int GIT_CALLBACK(append)(git_odb_writepack *writepack, const void *data, size_t size, git_transfer_progress *stats);
128+
int GIT_CALLBACK(commit)(git_odb_writepack *writepack, git_transfer_progress *stats);
129+
void GIT_CALLBACK(free)(git_odb_writepack *writepack);
130130
};
131131

132132
GIT_END_DECL

include/git2/pack.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ GIT_EXTERN(int) git_packbuilder_write(
178178
*/
179179
GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
180180

181-
typedef int (*git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload);
181+
typedef int GIT_CALLBACK(git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload);
182182

183183
/**
184184
* Create the new pack and pass each object to the callback
@@ -207,7 +207,7 @@ GIT_EXTERN(size_t) git_packbuilder_object_count(git_packbuilder *pb);
207207
GIT_EXTERN(size_t) git_packbuilder_written(git_packbuilder *pb);
208208

209209
/** Packbuilder progress notification function */
210-
typedef int (*git_packbuilder_progress)(
210+
typedef int GIT_CALLBACK(git_packbuilder_progress)(
211211
int stage,
212212
uint32_t current,
213213
uint32_t total,

include/git2/refs.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ GIT_EXTERN(int) git_reference_remove(git_repository *repo, const char *name);
422422
*/
423423
GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo);
424424

425-
typedef int (*git_reference_foreach_cb)(git_reference *reference, void *payload);
426-
typedef int (*git_reference_foreach_name_cb)(const char *name, void *payload);
425+
typedef int GIT_CALLBACK(git_reference_foreach_cb)(git_reference *reference, void *payload);
426+
typedef int GIT_CALLBACK(git_reference_foreach_name_cb)(const char *name, void *payload);
427427

428428
/**
429429
* Perform a callback on each reference in the repository.

include/git2/remote.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ typedef enum git_remote_completion_type {
422422
} git_remote_completion_type;
423423

424424
/** Push network progress notification function */
425-
typedef int (*git_push_transfer_progress)(
425+
typedef int GIT_CALLBACK(git_push_transfer_progress)(
426426
unsigned int current,
427427
unsigned int total,
428428
size_t bytes,
@@ -457,7 +457,7 @@ typedef struct {
457457
* @param len number of elements in `updates`
458458
* @param payload Payload provided by the caller
459459
*/
460-
typedef int (*git_push_negotiation)(const git_push_update **updates, size_t len, void *payload);
460+
typedef int GIT_CALLBACK(git_push_negotiation)(const git_push_update **updates, size_t len, void *payload);
461461

462462
/**
463463
* Callback used to inform of the update status from the remote.
@@ -471,7 +471,7 @@ typedef int (*git_push_negotiation)(const git_push_update **updates, size_t len,
471471
* @param data data provided by the caller
472472
* @return 0 on success, otherwise an error
473473
*/
474-
typedef int (*git_push_update_reference_cb)(const char *refname, const char *status, void *data);
474+
typedef int GIT_CALLBACK(git_push_update_reference_cb)(const char *refname, const char *status, void *data);
475475

476476
/**
477477
* The callback settings structure
@@ -492,7 +492,7 @@ struct git_remote_callbacks {
492492
* Completion is called when different parts of the download
493493
* process are done (currently unused).
494494
*/
495-
int (*completion)(git_remote_completion_type type, void *data);
495+
int GIT_CALLBACK(completion)(git_remote_completion_type type, void *data);
496496

497497
/**
498498
* This will be called if the remote host requires
@@ -522,7 +522,7 @@ struct git_remote_callbacks {
522522
* Each time a reference is updated locally, this function
523523
* will be called with information about it.
524524
*/
525-
int (*update_tips)(const char *refname, const git_oid *a, const git_oid *b, void *data);
525+
int GIT_CALLBACK(update_tips)(const char *refname, const git_oid *a, const git_oid *b, void *data);
526526

527527
/**
528528
* Function to call with progress information during pack

include/git2/repository.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ GIT_EXTERN(int) git_repository_message_remove(git_repository *repo);
627627
*/
628628
GIT_EXTERN(int) git_repository_state_cleanup(git_repository *repo);
629629

630-
typedef int (*git_repository_fetchhead_foreach_cb)(const char *ref_name,
630+
typedef int GIT_CALLBACK(git_repository_fetchhead_foreach_cb)(const char *ref_name,
631631
const char *remote_url,
632632
const git_oid *oid,
633633
unsigned int is_merge,
@@ -649,7 +649,7 @@ GIT_EXTERN(int) git_repository_fetchhead_foreach(
649649
git_repository_fetchhead_foreach_cb callback,
650650
void *payload);
651651

652-
typedef int (*git_repository_mergehead_foreach_cb)(const git_oid *oid,
652+
typedef int GIT_CALLBACK(git_repository_mergehead_foreach_cb)(const git_oid *oid,
653653
void *payload);
654654

655655
/**

include/git2/revwalk.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ GIT_EXTERN(git_repository *) git_revwalk_repository(git_revwalk *walk);
274274
* @param commit_id oid of Commit
275275
* @param payload User-specified pointer to data to be passed as data payload
276276
*/
277-
typedef int(*git_revwalk_hide_cb)(
277+
typedef int GIT_CALLBACK(git_revwalk_hide_cb)(
278278
const git_oid *commit_id,
279279
void *payload);
280280

include/git2/stash.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ typedef enum {
112112
* Return 0 to continue processing, or a negative value to
113113
* abort the stash application.
114114
*/
115-
typedef int (*git_stash_apply_progress_cb)(
115+
typedef int GIT_CALLBACK(git_stash_apply_progress_cb)(
116116
git_stash_apply_progress_t progress,
117117
void *payload);
118118

@@ -198,7 +198,7 @@ GIT_EXTERN(int) git_stash_apply(
198198
* @param payload Extra parameter to callback function.
199199
* @return 0 to continue iterating or non-zero to stop.
200200
*/
201-
typedef int (*git_stash_cb)(
201+
typedef int GIT_CALLBACK(git_stash_cb)(
202202
size_t index,
203203
const char* message,
204204
const git_oid *stash_id,

include/git2/status.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef enum {
6060
*
6161
* `payload` is the value you passed to the foreach function as payload.
6262
*/
63-
typedef int (*git_status_cb)(
63+
typedef int GIT_CALLBACK(git_status_cb)(
6464
const char *path, unsigned int status_flags, void *payload);
6565

6666
/**

include/git2/submodule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ typedef enum {
115115
* @param payload value you passed to the foreach function as payload
116116
* @return 0 on success or error code
117117
*/
118-
typedef int (*git_submodule_cb)(
118+
typedef int GIT_CALLBACK(git_submodule_cb)(
119119
git_submodule *sm, const char *name, void *payload);
120120

121121
/**

0 commit comments

Comments
 (0)