Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit de9ce95

Browse files
committed
Bug 730805 - Provide mozilla/IntegerPrintfMacros.h to implement the PRI* macros portion of the <inttypes.h> interface. r=espindola
--HG-- extra : rebase_source : be80333003c6fec659e736a77463568c836d8348
1 parent 2e1d99c commit de9ce95

12 files changed

Lines changed: 1358 additions & 135 deletions

File tree

configure.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,16 @@ case "$host" in
17331733
;;
17341734
esac
17351735

1736+
dnl Check for using a custom <inttypes.h> implementation
1737+
dnl ========================================================
1738+
AC_MSG_CHECKING(for custom <inttypes.h> implementation)
1739+
if test "$MOZ_CUSTOM_INTTYPES_H"; then
1740+
AC_DEFINE_UNQUOTED(MOZ_CUSTOM_INTTYPES_H, "$MOZ_CUSTOM_INTTYPES_H")
1741+
AC_MSG_RESULT(using $MOZ_CUSTOM_INTTYPES_H)
1742+
else
1743+
AC_MSG_RESULT(none specified)
1744+
fi
1745+
17361746
dnl Get mozilla version from central milestone file
17371747
MOZILLA_VERSION=`$PERL $srcdir/config/milestone.pl -topsrcdir $srcdir`
17381748
MOZILLA_UAVERSION=`$PERL $srcdir/config/milestone.pl -topsrcdir $srcdir -uaversion`

ipc/chromium/src/base/basictypes.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "base/port.h" // Types that only need exist on certain systems
1313

1414
#include "mozilla/Assertions.h"
15-
#include <stdint.h>
15+
#include "mozilla/IntegerPrintfMacros.h"
1616

1717
// A type to represent a Unicode code-point value. As of Unicode 4.0,
1818
// such values require up to 21 bits.
@@ -35,15 +35,10 @@ const int64_t kint64max = (( int64_t) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
3535

3636
// Platform- and hardware-dependent printf specifiers
3737
# if defined(OS_POSIX)
38-
# define __STDC_FORMAT_MACROS 1
39-
# include <inttypes.h> // for 64-bit integer format macros
4038
# define PRId64L "I64d"
4139
# define PRIu64L "I64u"
4240
# define PRIx64L "I64x"
4341
# elif defined(OS_WIN)
44-
# define PRId64 "I64d"
45-
# define PRIu64 "I64u"
46-
# define PRIx64 "I64x"
4742
# define PRId64L L"I64d"
4843
# define PRIu64L L"I64u"
4944
# define PRIx64L L"I64x"

js/public/RequiredDefines.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515

1616
/*
1717
* The c99 defining the limit macros (UINT32_MAX for example), says:
18-
* C++ implementations should define these macros only when __STDC_LIMIT_MACROS
19-
* is defined before <stdint.h> is included.
18+
*
19+
* C++ implementations should define these macros only when
20+
* __STDC_LIMIT_MACROS is defined before <stdint.h> is included.
21+
*
22+
* The same also occurs with __STDC_CONSTANT_MACROS for the constant macros
23+
* (INT8_C for example) used to specify a literal constant of the proper type,
24+
* and with __STDC_FORMAT_MACROS for the format macros (PRId32 for example) used
25+
* with the fprintf function family.
2026
*/
2127
#define __STDC_LIMIT_MACROS
28+
#define __STDC_CONSTANT_MACROS
29+
#define __STDC_FORMAT_MACROS
2230

2331
#endif /* js_RequiredDefines_h */

js/src/configure.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,16 @@ case "$host" in
13591359
;;
13601360
esac
13611361

1362+
dnl Check for using a custom <inttypes.h> implementation
1363+
dnl ========================================================
1364+
AC_MSG_CHECKING(for custom <inttypes.h> implementation)
1365+
if test "$MOZ_CUSTOM_INTTYPES_H"; then
1366+
AC_DEFINE_UNQUOTED(MOZ_CUSTOM_INTTYPES_H, "$MOZ_CUSTOM_INTTYPES_H")
1367+
AC_MSG_RESULT(using $MOZ_CUSTOM_INTTYPES_H)
1368+
else
1369+
AC_MSG_RESULT(none specified)
1370+
fi
1371+
13621372
MOZ_DOING_LTO(lto_is_enabled)
13631373

13641374
dnl ========================================================

mfbt/IntegerPrintfMacros.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
/* Implements the C99 <inttypes.h> interface, minus the SCN* format macros. */
7+
8+
#ifndef mozilla_IntegerPrintfMacros_h_
9+
#define mozilla_IntegerPrintfMacros_h_
10+
11+
/*
12+
* MSVC++ doesn't include <inttypes.h>, even in versions shipping <stdint.h>, so
13+
* we have to reimplement it there. Note: <inttypes.h> #includes <stdint.h>.
14+
*
15+
* Note that this header DOES NOT implement <inttypes.h>'s scanf macros. MSVC's
16+
* scanf doesn't have sufficient format specifier support to implement them
17+
* (specifically, to implement scanning into an 8-bit location).
18+
*
19+
* http://stackoverflow.com/questions/3036396/scanfd-char-char-as-int-format-string
20+
*
21+
* Moreover, scanf is a footgun: if the input number exceeds the bounds of the
22+
* target type, behavior is undefined (in the compiler sense: that is, this code
23+
* could overwrite your hard drive with zeroes):
24+
*
25+
* uint8_t u;
26+
* sscanf("256", "%" SCNu8, &u); // BAD
27+
*
28+
* This header will sometimes provide SCN* macros, by dint of being implemented
29+
* using <inttypes.h>. But for these reasons, *never* use them!
30+
*/
31+
32+
#if defined(MOZ_CUSTOM_INTTYPES_H)
33+
# include MOZ_CUSTOM_INTTYPES_H
34+
#elif defined(_MSC_VER)
35+
# include "mozilla/MSIntTypes.h"
36+
#else
37+
# include <inttypes.h>
38+
#endif
39+
40+
#endif /* mozilla_IntegerPrintfMacros_h_ */

mfbt/MSIntTypes.h

Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#pragma once
4141
#endif
4242

43-
#include "stdint.h"
43+
#include <stdint.h>
4444

4545
// 7.8 Format conversion of integer types
4646

@@ -151,115 +151,8 @@ typedef struct {
151151
#define PRIxPTR "Ix"
152152
#define PRIXPTR "IX"
153153

154-
// The fscanf macros for signed integers are:
155-
#define SCNd8 "d"
156-
#define SCNi8 "i"
157-
#define SCNdLEAST8 "d"
158-
#define SCNiLEAST8 "i"
159-
#define SCNdFAST8 "d"
160-
#define SCNiFAST8 "i"
161-
162-
#define SCNd16 "hd"
163-
#define SCNi16 "hi"
164-
#define SCNdLEAST16 "hd"
165-
#define SCNiLEAST16 "hi"
166-
#define SCNdFAST16 "hd"
167-
#define SCNiFAST16 "hi"
168-
169-
#define SCNd32 "ld"
170-
#define SCNi32 "li"
171-
#define SCNdLEAST32 "ld"
172-
#define SCNiLEAST32 "li"
173-
#define SCNdFAST32 "ld"
174-
#define SCNiFAST32 "li"
175-
176-
#define SCNd64 "I64d"
177-
#define SCNi64 "I64i"
178-
#define SCNdLEAST64 "I64d"
179-
#define SCNiLEAST64 "I64i"
180-
#define SCNdFAST64 "I64d"
181-
#define SCNiFAST64 "I64i"
182-
183-
#define SCNdMAX "I64d"
184-
#define SCNiMAX "I64i"
185-
186-
#ifdef _WIN64 // [
187-
# define SCNdPTR "I64d"
188-
# define SCNiPTR "I64i"
189-
#else // _WIN64 ][
190-
# define SCNdPTR "ld"
191-
# define SCNiPTR "li"
192-
#endif // _WIN64 ]
193-
194-
// The fscanf macros for unsigned integers are:
195-
#define SCNo8 "o"
196-
#define SCNu8 "u"
197-
#define SCNx8 "x"
198-
#define SCNX8 "X"
199-
#define SCNoLEAST8 "o"
200-
#define SCNuLEAST8 "u"
201-
#define SCNxLEAST8 "x"
202-
#define SCNXLEAST8 "X"
203-
#define SCNoFAST8 "o"
204-
#define SCNuFAST8 "u"
205-
#define SCNxFAST8 "x"
206-
#define SCNXFAST8 "X"
207-
208-
#define SCNo16 "ho"
209-
#define SCNu16 "hu"
210-
#define SCNx16 "hx"
211-
#define SCNX16 "hX"
212-
#define SCNoLEAST16 "ho"
213-
#define SCNuLEAST16 "hu"
214-
#define SCNxLEAST16 "hx"
215-
#define SCNXLEAST16 "hX"
216-
#define SCNoFAST16 "ho"
217-
#define SCNuFAST16 "hu"
218-
#define SCNxFAST16 "hx"
219-
#define SCNXFAST16 "hX"
220-
221-
#define SCNo32 "lo"
222-
#define SCNu32 "lu"
223-
#define SCNx32 "lx"
224-
#define SCNX32 "lX"
225-
#define SCNoLEAST32 "lo"
226-
#define SCNuLEAST32 "lu"
227-
#define SCNxLEAST32 "lx"
228-
#define SCNXLEAST32 "lX"
229-
#define SCNoFAST32 "lo"
230-
#define SCNuFAST32 "lu"
231-
#define SCNxFAST32 "lx"
232-
#define SCNXFAST32 "lX"
233-
234-
#define SCNo64 "I64o"
235-
#define SCNu64 "I64u"
236-
#define SCNx64 "I64x"
237-
#define SCNX64 "I64X"
238-
#define SCNoLEAST64 "I64o"
239-
#define SCNuLEAST64 "I64u"
240-
#define SCNxLEAST64 "I64x"
241-
#define SCNXLEAST64 "I64X"
242-
#define SCNoFAST64 "I64o"
243-
#define SCNuFAST64 "I64u"
244-
#define SCNxFAST64 "I64x"
245-
#define SCNXFAST64 "I64X"
246-
247-
#define SCNoMAX "I64o"
248-
#define SCNuMAX "I64u"
249-
#define SCNxMAX "I64x"
250-
#define SCNXMAX "I64X"
251-
252-
#ifdef _WIN64 // [
253-
# define SCNoPTR "I64o"
254-
# define SCNuPTR "I64u"
255-
# define SCNxPTR "I64x"
256-
# define SCNXPTR "I64X"
257-
#else // _WIN64 ][
258-
# define SCNoPTR "lo"
259-
# define SCNuPTR "lu"
260-
# define SCNxPTR "lx"
261-
# define SCNXPTR "lX"
262-
#endif // _WIN64 ]
154+
// DO NOT SUPPORT THE scanf MACROS! See the comment at the top of
155+
// IntegerPrintfMacros.h.
263156

264157
#endif // __STDC_FORMAT_MACROS ]
265158

mfbt/exported_headers.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ EXPORTS_mozilla += \
2828
FloatingPoint.h \
2929
GuardObjects.h \
3030
HashFunctions.h \
31+
IntegerPrintfMacros.h \
3132
Likely.h \
3233
LinkedList.h \
3334
MathAlgorithms.h \

0 commit comments

Comments
 (0)