format_macros.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2009 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_FORMAT_MACROS_H_
  5. #define BASE_FORMAT_MACROS_H_
  6. // This file defines the format macros for some integer types.
  7. // To print a 64-bit value in a portable way:
  8. // int64_t value;
  9. // printf("xyz:%" PRId64, value);
  10. // The "d" in the macro corresponds to %d; you can also use PRIu64 etc.
  11. //
  12. // For wide strings, prepend "Wide" to the macro:
  13. // int64_t value;
  14. // StringPrintf(L"xyz: %" WidePRId64, value);
  15. //
  16. // To print a size_t value in a portable way:
  17. // size_t size;
  18. // printf("xyz: %" PRIuS, size);
  19. // The "u" in the macro corresponds to %u, and S is for "size".
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "build/build_config.h"
  23. #if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && \
  24. (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
  25. #error "inttypes.h has already been included before this header file, but "
  26. #error "without __STDC_FORMAT_MACROS defined."
  27. #endif
  28. #if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && \
  29. !defined(__STDC_FORMAT_MACROS)
  30. #define __STDC_FORMAT_MACROS
  31. #endif
  32. #include <inttypes.h>
  33. #if BUILDFLAG(IS_WIN)
  34. #if !defined(PRId64) || !defined(PRIu64) || !defined(PRIx64)
  35. #error "inttypes.h provided by win toolchain should define these."
  36. #endif
  37. #define WidePRId64 L"I64d"
  38. #define WidePRIu64 L"I64u"
  39. #define WidePRIx64 L"I64x"
  40. #if !defined(PRIuS)
  41. #define PRIuS "Iu"
  42. #endif
  43. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  44. // GCC will concatenate wide and narrow strings correctly, so nothing needs to
  45. // be done here.
  46. #define WidePRId64 PRId64
  47. #define WidePRIu64 PRIu64
  48. #define WidePRIx64 PRIx64
  49. #if !defined(PRIuS)
  50. #define PRIuS "zu"
  51. #endif
  52. #endif // BUILDFLAG(IS_WIN)
  53. // The size of NSInteger and NSUInteger varies between 32-bit and 64-bit
  54. // architectures and Apple does not provides standard format macros and
  55. // recommends casting. This has many drawbacks, so instead define macros
  56. // for formatting those types.
  57. #if BUILDFLAG(IS_APPLE)
  58. #if defined(ARCH_CPU_64_BITS)
  59. #if !defined(PRIdNS)
  60. #define PRIdNS "ld"
  61. #endif
  62. #if !defined(PRIuNS)
  63. #define PRIuNS "lu"
  64. #endif
  65. #if !defined(PRIxNS)
  66. #define PRIxNS "lx"
  67. #endif
  68. #else // defined(ARCH_CPU_64_BITS)
  69. #if !defined(PRIdNS)
  70. #define PRIdNS "d"
  71. #endif
  72. #if !defined(PRIuNS)
  73. #define PRIuNS "u"
  74. #endif
  75. #if !defined(PRIxNS)
  76. #define PRIxNS "x"
  77. #endif
  78. #endif
  79. #endif // BUILDFLAG(IS_APPLE)
  80. #endif // BASE_FORMAT_MACROS_H_