On Tue, 24 May 2022, Soft Works wrote:

-----Original Message-----
From: Martin Storsjö <mar...@martin.st>
Sent: Tuesday, May 24, 2022 11:23 AM
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: softworkz <softwo...@hotmail.com>; Hendrik Leppkes
<h.lepp...@gmail.com>
Subject: Re: [FFmpeg-devel] [PATCH v5 2/2] avformat/os_support: Support
long file names on Windows

On Tue, 24 May 2022, softworkz wrote:

From: softworkz <softwo...@hotmail.com>

Signed-off-by: softworkz <softwo...@hotmail.com>
---
libavformat/os_support.h | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/libavformat/os_support.h b/libavformat/os_support.h
index 5e6b32d2dc..d4c07803a5 100644
--- a/libavformat/os_support.h
+++ b/libavformat/os_support.h
@@ -49,7 +49,13 @@
#  ifdef stat
#   undef stat
#  endif
-#  define stat _stati64
+#  define stat win32_stat
+
+    struct win32_stat
+    {
+        struct _stati64;
+    };

Is it possible to work around this issue by doing "#define stat(a,b)"
which only should apply on the function, not to the struct?

How could this be possible? A define is only doing string replacements,
so I wouldn't know how it could be restricted to the function, but
not the struct.

If unsure about a tool feature, please try it out for yourself. Yes, a define is only a string replacement, but a define with parameters only matches the string occurs with parenthesis afterwards. Consider this example:

$ cat test.c
#define stat(a, b) win32_stat(a, b)

struct stat {
        int a, b, c;
};

void stat (struct stat *st, const char* filename);

void func(const char* filename) {
        struct stat st;
        stat (&st, filename);
}
$ cc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "test.c"


struct stat {
 int a, b, c;
};

void win32_stat(struct stat *st, const char* filename);

void func(const char* filename) {
 struct stat st;
 win32_stat(&st, filename);
}


So here, the stat -> win32_stat rewrite only applied on the function declaration and call, but not on the structs.

Not saying that this necessarily is the way forward, but I was just mentioning it as a potential option to consider.

A safe way forward could be to switch code to just using "struct
ff_stat_struct", and define ff_stat_struct to the name of the struct we
exepct to use. It's not pretty, and it affects users which no longer can
use the default POSIX stat form of the calls

That's why I took the effort to make this work.

but it would fix the issue
of redirecting the struct and function separately, without needing to know
what exactly is in the struct (because we really shouldn't be
hardcoding/assuming that).

That doesn't apply because the current code already does this:

DEF_FS_FUNCTION2(stat, _wstati64, _stati64, struct stat*)

Which means that it specifically chooses _stati64 which is a public
MS API:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=msvc-170

And we know that it takes _stati64 as the parameter.

Yes, there is no disagreement about that part.

This code:

   struct win32_stat
   {
       struct _stati64;
   };

makes use of a MS compiler feature called "anonymous structures":

https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/z2cx9y4f(v=vs.120)?redirectedfrom=MSDN

This way, we automatically "inherit" the members of the struct
(whatever their members would be).

This, as the article itself clearly declares, is a C language extension. GCC allows it in mingw mode, but Clang doesn't. (It's possible to use it in Clang too if you enable it with -fms-extensions though.)

$ cat test2.c
struct orig_struct {
        int a, b, c;
};
struct my_struct {
        struct orig_struct;
};
void set(struct my_struct *st) {
        st->a = 42;
}
$ clang -target x86_64-w64-mingw32 -c test2.c
test2.c:5:2: warning: declaration does not declare anything 
[-Wmissing-declarations]
        struct orig_struct;
        ^
test2.c:8:6: error: no member named 'a' in 'struct my_struct'
        st->a = 42;
        ~~  ^
1 warning and 1 error generated.
$ clang -target x86_64-w64-mingw32 -c test2.c -fms-extensions
test2.c:5:2: warning: anonymous structs are a Microsoft extension 
[-Wmicrosoft-anon-tag]
        struct orig_struct;
        ^
1 warning generated.


// Martin
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to