Skip to contentSkip to conversation

path: Make the 'get_st_mode_bits' symbol a file static

PATCH In review Ramsay Jones 4 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 2/6 path: Make the 'get_st_mode_bits' symbol a file static

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

In order to suppress the warning, since this symbol does not need more than file visibility, we simply remove the declaration from the header and add the static modifier to the function definition.

---
 git-compat-util.h | 1 -
 path.c            | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
git-compat-util.h +0 -1
index e955bb5..3a990b3 100644
@@ -163,7 +163,6 @@
 typedef long intptr_t;
 typedef unsigned long uintptr_t;
 #endif
-int get_st_mode_bits(const char *path, int *mode);
 #if defined(__CYGWIN__)
 #undef _XOPEN_SOURCE
 #include <grp.h>
path.c +1 -1
index 04ff148..cc2e9ac 100644
@@ -11,7 +11,7 @@
  * may return wrong permission bits. Most of the time we do not care,
  * but the callsites of this wrapper do care.
  */
-int get_st_mode_bits(const char *path, int *mode)
+static int get_st_mode_bits(const char *path, int *mode)
 {
 	struct stat st;
 	if (lstat(path, &st) < 0)
Signature · 1 line
1.8.2
#1 lore raw
Quoted 35 lines · Ramsay Jones

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

In order to suppress the warning, since this symbol does not need more than file visibility, we simply remove the declaration from the header and add the static modifier to the function definition.

---
 git-compat-util.h | 1 -
 path.c            | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
git-compat-util.h +0 -1
index e955bb5..3a990b3 100644
@@ -163,7 +163,6 @@
 typedef long intptr_t;
 typedef unsigned long uintptr_t;
 #endif
-int get_st_mode_bits(const char *path, int *mode);
 #if defined(__CYGWIN__)
 #undef _XOPEN_SOURCE
 #include <grp.h>
path.c +0 -0
index 04ff148..cc2e9ac 100644

path.c line 17

Quoted 8 lines
path.c +1 -1
@@ -11,7 +11,7 @@
  * may return wrong permission bits. Most of the time we do not care,
  * but the callsites of this wrapper do care.
  */
-int get_st_mode_bits(const char *path, int *mode)
+static int get_st_mode_bits(const char *path, int *mode)
 {
 	struct stat st;
 	if (lstat(path, &st) < 0)

Sorry for breaking the MiNGW/MSVC builds. It seams that the get_st_mode_bits is badly placed.

It should be in git compat-util.h, so that both compat/cygwin.c and path.c can see it. So from my understanding, it should be placed here:
(I will send an official patch later)
/Torsten

diff -C 3 git-compat-util.h.~9526aa461f6c6900cb892a6fe248150ad436c0d~ git-compat-util.h.new *** git-compat-util.h.~9526aa461f6c6900cb892a6fe248150ad436c0d~ 2013-04-28 07:53:28.000000000 +0200
--- git-compat-util.h.new 2013-04-28 07:53:58.000000000 +0200
***************
*** 127,132 ****
--- 127,133 ----
#else
#include <poll.h>
#endif
+ int get_st_mode_bits(const char *path, int *mode);
#if defined(__MINGW32__)
/* pull in Windows compatibility stuff */
#include "compat/mingw.h"
***************
*** 163,169 ****
typedef long intptr_t;
typedef unsigned long uintptr_t;
#endif
- int get_st_mode_bits(const char *path, int *mode);
#if defined(__CYGWIN__)
#undef _XOPEN_SOURCE
#include <grp.h>

#2 lore raw
Quoted 67 lines · Torsten Bögershausen
Quoted 35 lines · Ramsay Jones

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

In order to suppress the warning, since this symbol does not need more than file visibility, we simply remove the declaration from the header and add the static modifier to the function definition.

---
 git-compat-util.h | 1 -
 path.c            | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
git-compat-util.h +0 -1
index e955bb5..3a990b3 100644
@@ -163,7 +163,6 @@
 typedef long intptr_t;
 typedef unsigned long uintptr_t;
 #endif
-int get_st_mode_bits(const char *path, int *mode);
 #if defined(__CYGWIN__)
 #undef _XOPEN_SOURCE
 #include <grp.h>
path.c +0 -0
index 04ff148..cc2e9ac 100644

path.c line 17

Quoted 8 lines
path.c +1 -1
@@ -11,7 +11,7 @@
  * may return wrong permission bits. Most of the time we do not care,
  * but the callsites of this wrapper do care.
  */
-int get_st_mode_bits(const char *path, int *mode)
+static int get_st_mode_bits(const char *path, int *mode)
 {
 	struct stat st;
 	if (lstat(path, &st) < 0)

Sorry for breaking the MiNGW/MSVC builds. It seams that the get_st_mode_bits is badly placed.

It should be in git compat-util.h, so that both compat/cygwin.c and path.c can see it. So from my understanding, it should be placed here:
(I will send an official patch later)
/Torsten

diff -C 3 git-compat-util.h.~9526aa461f6c6900cb892a6fe248150ad436c0d~ git-compat-util.h.new *** git-compat-util.h.~9526aa461f6c6900cb892a6fe248150ad436c0d~ 2013-04-28 07:53:28.000000000 +0200
--- git-compat-util.h.new 2013-04-28 07:53:58.000000000 +0200
***************
*** 127,132 ****
--- 127,133 ----
#else
#include <poll.h>
#endif
+ int get_st_mode_bits(const char *path, int *mode);
#if defined(__MINGW32__)
/* pull in Windows compatibility stuff */
#include "compat/mingw.h"
***************

Quoted 8 lines

*** 163,169 ****
typedef long intptr_t;
typedef unsigned long uintptr_t;
#endif
- int get_st_mode_bits(const char *path, int *mode); #if defined(__CYGWIN__)
#undef _XOPEN_SOURCE
#include <grp.h>

I probably change my mind:
Declaring get_st_mode_bits() static in path.c makes sense.

  And for cygwin we have the re-define in compat/cygwin.h:
  int cygwin_get_st_mode_bits(const char *path, int *mode);
  #define get_st_mode_bits(p,m) cygwin_get_st_mode_bits((p),(m))

So, (besides the typo found by Eric),
Thanks. Reviewed,and tested OK here. In short:

#3 lore raw
In reply to Torsten Bögershausen
Quoted 38 lines · Torsten Bögershausen
Quoted 37 lines · Ramsay Jones

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

In order to suppress the warning, since this symbol does not need more than file visibility, we simply remove the declaration from the header and add the static modifier to the function definition.

---
 git-compat-util.h | 1 -
 path.c            | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
git-compat-util.h +0 -1
index e955bb5..3a990b3 100644
@@ -163,7 +163,6 @@
 typedef long intptr_t;
 typedef unsigned long uintptr_t;
 #endif
-int get_st_mode_bits(const char *path, int *mode);
 #if defined(__CYGWIN__)
 #undef _XOPEN_SOURCE
 #include <grp.h>
path.c +0 -0
index 04ff148..cc2e9ac 100644
@@ -11,7 +11,7 @@
  * may return wrong permission bits. Most of the time we do not care,
  * but the callsites of this wrapper do care.
  */
Quoted 8 lines

path.c line 17

Quoted 6 lines
path.c +1 -1
-int get_st_mode_bits(const char *path, int *mode)
+static int get_st_mode_bits(const char *path, int *mode)
 {
 	struct stat st;
 	if (lstat(path, &st) < 0)

Sorry for breaking the MiNGW/MSVC builds. It seams that the get_st_mode_bits is badly placed.

You didn't break the build; simply provoked a warning.

As I said in the commit message, an alternative is to simply move the declaration to the top level of the header (out of the preprocessor conditional).

ATB,
Ramsay Jones

#4 lore raw
In reply to Ramsay Jones
Quoted 5 lines · Ramsay Jones

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

Well, the idea was that the user of this function in path.c will call get_st_mode_bits(), and whatever platform that provides a replacement implementation would do

	#define get_st_mode_bits(a,b) cygwin_get_st_mode_bits((a),(b))

so that the calling site in path.c will end up calling that replacement implementation. So if anything get_st_mode_bits() declaration may want to go at the _end_ (not top) after including all the compatibility crufts.

We could make the declaration static to path.c, but then nobody other than path.c would be able to make use of it in the future, and we'll have the same discussion when somebody wants to hoist the declaration to git-compat-util.h, no?

#5 lore raw
Quoted 19 lines · Junio C Hamano
Quoted 5 lines · Ramsay Jones

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

Well, the idea was that the user of this function in path.c will call get_st_mode_bits(), and whatever platform that provides a replacement implementation would do

	#define get_st_mode_bits(a,b) cygwin_get_st_mode_bits((a),(b))

so that the calling site in path.c will end up calling that replacement implementation. So if anything get_st_mode_bits() declaration may want to go at the _end_ (not top) after including all the compatibility crufts.

Quoted 4 lines

We could make the declaration static to path.c, but then nobody other than path.c would be able to make use of it in the future, and we'll have the same discussion when somebody wants to hoist the declaration to git-compat-util.h, no?

I don't have a problem with keeping the declaration in git-compat-util.h (after moving it so that the MinGW and MSVC builds can see it, of course) if you would rather do that.

However, I'm always a little wary when I hear someone say "this may be useful to others in the future, so lets do X to make it easier ...". I have noticed that, much more often than not, that future user never does materialise ... ;-)

Having said that ...

Back in 2011, when I was trying to fix t7606-merge-custom.sh (see commit b8a9733377, "help.c: Fix detection of custom merge strategy on cygwin", 16-06-2011), I had a patch that looked very similar to the solution Torsten has arrived at here! (The main difference was that I called stat() rather than lstat() to get the mode bits).

I decided to go with the simpler solution in commit b8a9733377.

ATB,
Ramsay Jones

#6 lore raw
Quoted 4 lines · Ramsay Jones

However, I'm always a little wary when I hear someone say "this may be useful to others in the future, so lets do X to make it easier ...". I have noticed that, much more often than not, that future user never does materialise ... ;-)

I do not think you are reading the reason for the function correctly. It is not "We may find it useful elsewhere".

It is "We knew Windows benefits from having a 'quick, incorrect, but the incorrectness happens not matter to the caller' hack, and we used to use it everywhere. But we identified one location we need to disable the hack for correctness, hence this 'slow-but-correct' function. There may still be other places that needs fixing by calling it".

#7 lore raw
In reply to Ramsay Jones
Quoted 4 lines · Ramsay Jones

On MinGW, sparse issues an "'get_st_mode_bits' not declared. Should it be static?" warning. The MinGW and MSVC builds do not see the declaration of this function, within git-compat-util.h, due to it's

s/it's/its/

Quoted 8 lines

placement within an preprocessor conditional. (So, one solution would be to simply move the declaration to the top level of the header.)

In order to suppress the warning, since this symbol does not need more than file visibility, we simply remove the declaration from the header and add the static modifier to the function definition.