Skip to content

Commit b19aab5

Browse files
committed
Merge branch 'km/gettext-n'
* km/gettext-n: gettext.h: add parentheses around N_ expansion if supported
2 parents 1c4ebbc + 290c8e7 commit b19aab5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ all::
348348
#
349349
# Define NO_HMAC_CTX_CLEANUP if your OpenSSL is version 0.9.6b or earlier to
350350
# cleanup the HMAC context with the older HMAC_cleanup function.
351+
#
352+
# Define USE_PARENS_AROUND_GETTEXT_N to "yes" if your compiler happily
353+
# compiles the following initialization:
354+
#
355+
# static const char s[] = ("FOO");
356+
#
357+
# and define it to "no" if you need to remove the parentheses () around the
358+
# constant. The default is "auto", which means to use parentheses if your
359+
# compiler is detected to support it.
351360

352361
GIT-VERSION-FILE: FORCE
353362
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -955,6 +964,14 @@ ifneq (,$(SOCKLEN_T))
955964
BASIC_CFLAGS += -Dsocklen_t=$(SOCKLEN_T)
956965
endif
957966

967+
ifeq (yes,$(USE_PARENS_AROUND_GETTEXT_N))
968+
BASIC_CFLAGS += -DUSE_PARENS_AROUND_GETTEXT_N=1
969+
else
970+
ifeq (no,$(USE_PARENS_AROUND_GETTEXT_N))
971+
BASIC_CFLAGS += -DUSE_PARENS_AROUND_GETTEXT_N=0
972+
endif
973+
endif
974+
958975
ifeq ($(uname_S),Darwin)
959976
ifndef NO_FINK
960977
ifeq ($(shell test -d /sw/lib && echo y),y)

gettext.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ const char *Q_(const char *msgid, const char *plu, unsigned long n)
6363
}
6464

6565
/* Mark msgid for translation but do not translate it. */
66+
#if !USE_PARENS_AROUND_GETTEXT_N
6667
#define N_(msgid) msgid
68+
#else
69+
/*
70+
* Strictly speaking, this will lead to invalid C when
71+
* used this way:
72+
* static const char s[] = N_("FOO");
73+
* which will expand to
74+
* static const char s[] = ("FOO");
75+
* and in valid C, the initializer on the right hand side must
76+
* be without the parentheses. But many compilers do accept it
77+
* as a language extension and it will allow us to catch mistakes
78+
* like:
79+
* static const char *msgs[] = {
80+
* N_("one")
81+
* N_("two"),
82+
* N_("three"),
83+
* NULL
84+
* };
85+
* (notice the missing comma on one of the lines) by forcing
86+
* a compilation error, because parenthesised ("one") ("two")
87+
* will not get silently turned into ("onetwo").
88+
*/
89+
#define N_(msgid) (msgid)
90+
#endif
6791

6892
#endif

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,4 +875,8 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
875875
#define gmtime_r git_gmtime_r
876876
#endif
877877

878+
#if !defined(USE_PARENS_AROUND_GETTEXT_N) && defined(__GNUC__)
879+
#define USE_PARENS_AROUND_GETTEXT_N 1
880+
#endif
881+
878882
#endif

0 commit comments

Comments
 (0)