Skip to content

Commit 213d35e

Browse files
authored
Merge pull request libgit2#6558 from DimitryAndric/fix-qsort-variants-2
Work around -Werror problems when detecting qsort variants
2 parents 2bbcdee + 3335738 commit 213d35e

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ include(CheckLibraryExists)
9494
include(CheckFunctionExists)
9595
include(CheckSymbolExists)
9696
include(CheckStructHasMember)
97-
include(CheckPrototypeDefinition)
97+
include(CheckPrototypeDefinitionSafe)
9898
include(AddCFlagIfSupported)
9999
include(FindPkgLibraries)
100100
include(FindThreads)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include(CheckPrototypeDefinition)
2+
3+
function(check_prototype_definition_safe function prototype return header variable)
4+
# temporarily save CMAKE_C_FLAGS and disable warnings about unused
5+
# unused functions and parameters, otherwise they will always fail
6+
# if ENABLE_WERROR is on
7+
set(SAVED_CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
8+
9+
disable_warnings(unused-function)
10+
disable_warnings(unused-parameter)
11+
12+
check_prototype_definition("${function}" "${prototype}" "${return}" "${header}" "${variable}")
13+
14+
# restore CMAKE_C_FLAGS
15+
set(CMAKE_C_FLAGS "${SAVED_CMAKE_C_FLAGS}")
16+
endfunction()

src/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ add_feature_info(futimens GIT_USE_FUTIMENS "futimens support")
6060

6161
# old-style FreeBSD qsort_r() has the 'context' parameter as the first argument
6262
# of the comparison function:
63-
check_prototype_definition(qsort_r
63+
check_prototype_definition_safe(qsort_r
6464
"void (qsort_r)(void *base, size_t nmemb, size_t size, void *context, int (*compar)(void *, const void *, const void *))"
65-
"" "stdlib.h" GIT_QSORT_R_BSD)
65+
"" "stdlib.h" GIT_QSORT_BSD)
6666

6767
# GNU or POSIX qsort_r() has the 'context' parameter as the last argument of the
6868
# comparison function:
69-
check_prototype_definition(qsort_r
69+
check_prototype_definition_safe(qsort_r
7070
"void (qsort_r)(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *context)"
71-
"" "stdlib.h" GIT_QSORT_R_GNU)
71+
"" "stdlib.h" GIT_QSORT_GNU)
7272

7373
# C11 qsort_s() has the 'context' parameter as the last argument of the
7474
# comparison function, and returns an error status:
75-
check_prototype_definition(qsort_s
75+
check_prototype_definition_safe(qsort_s
7676
"errno_t (qsort_s)(void *base, rsize_t nmemb, rsize_t size, int (*compar)(const void *, const void *, void *), void *context)"
77-
"0" "stdlib.h" GIT_QSORT_S_C11)
77+
"0" "stdlib.h" GIT_QSORT_C11)
7878

7979
# MSC qsort_s() has the 'context' parameter as the first argument of the
8080
# comparison function, and as the last argument of qsort_s():
81-
check_prototype_definition(qsort_s
81+
check_prototype_definition_safe(qsort_s
8282
"void (qsort_s)(void *base, size_t num, size_t width, int (*compare )(void *, const void *, const void *), void *context)"
83-
"" "stdlib.h" GIT_QSORT_S_MSC)
83+
"" "stdlib.h" GIT_QSORT_MSC)
8484

8585
# random / entropy data
8686

src/util/git2_features.h.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#cmakedefine GIT_REGEX_PCRE2
2525
#cmakedefine GIT_REGEX_BUILTIN 1
2626

27-
#cmakedefine GIT_QSORT_R_BSD
28-
#cmakedefine GIT_QSORT_R_GNU
29-
#cmakedefine GIT_QSORT_S_C11
30-
#cmakedefine GIT_QSORT_S_MSC
27+
#cmakedefine GIT_QSORT_BSD
28+
#cmakedefine GIT_QSORT_GNU
29+
#cmakedefine GIT_QSORT_C11
30+
#cmakedefine GIT_QSORT_MSC
3131

3232
#cmakedefine GIT_SSH 1
3333
#cmakedefine GIT_SSH_MEMORY_CREDENTIALS 1

src/util/util.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# endif
1919
# include <windows.h>
2020

21-
# ifdef GIT_QSORT_S_MSC
21+
# ifdef GIT_QSORT_MSC
2222
# include <search.h>
2323
# endif
2424
#endif
@@ -673,7 +673,7 @@ size_t git__unescape(char *str)
673673
return (pos - str);
674674
}
675675

676-
#if defined(GIT_QSORT_S_MSC) || defined(GIT_QSORT_R_BSD)
676+
#if defined(GIT_QSORT_MSC) || defined(GIT_QSORT_BSD)
677677
typedef struct {
678678
git__sort_r_cmp cmp;
679679
void *payload;
@@ -688,10 +688,11 @@ static int GIT_LIBGIT2_CALL git__qsort_r_glue_cmp(
688688
#endif
689689

690690

691-
#if !defined(GIT_QSORT_R_BSD) && \
692-
!defined(GIT_QSORT_R_GNU) && \
693-
!defined(GIT_QSORT_S_C11) && \
694-
!defined(GIT_QSORT_S_MSC)
691+
#if !defined(GIT_QSORT_BSD) && \
692+
!defined(GIT_QSORT_GNU) && \
693+
!defined(GIT_QSORT_C11) && \
694+
!defined(GIT_QSORT_MSC)
695+
695696
static void swap(uint8_t *a, uint8_t *b, size_t elsize)
696697
{
697698
char tmp[256];
@@ -717,19 +718,20 @@ static void insertsort(
717718
for (j = i; j > base && cmp(j, j - elsize, payload) < 0; j -= elsize)
718719
swap(j, j - elsize, elsize);
719720
}
721+
720722
#endif
721723

722724
void git__qsort_r(
723725
void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload)
724726
{
725-
#if defined(GIT_QSORT_R_GNU)
727+
#if defined(GIT_QSORT_GNU)
726728
qsort_r(els, nel, elsize, cmp, payload);
727-
#elif defined(GIT_QSORT_S_C11)
729+
#elif defined(GIT_QSORT_C11)
728730
qsort_s(els, nel, elsize, cmp, payload);
729-
#elif defined(GIT_QSORT_R_BSD)
731+
#elif defined(GIT_QSORT_BSD)
730732
git__qsort_r_glue glue = { cmp, payload };
731733
qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp);
732-
#elif defined(GIT_QSORT_S_MSC)
734+
#elif defined(GIT_QSORT_MSC)
733735
git__qsort_r_glue glue = { cmp, payload };
734736
qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue);
735737
#else

0 commit comments

Comments
 (0)