gecko-dev/mfbt/Char16.h
Ehsan Akhgari 2824b29025 Bug 895322 - Part 1: Replace the usages of MOZ_STATIC_ASSERT with C++11 static_assert; r=Waldo
This patch was mostly generated by running the following scripts on the codebase, with some
manual changes made afterwards:

# static_assert.sh
#!/bin/bash
# Command to convert an NSPR integer type to the equivalent standard integer type

function convert() {
echo "Converting $1 to $2..."
find . ! -wholename "*nsprpub*" \
       ! -wholename "*security/nss*" \
       ! -wholename "*/.hg*" \
       ! -wholename "obj-ff-dbg*" \
       ! -name nsXPCOMCID.h \
       ! -name prtypes.h \
         -type f \
      \( -iname "*.cpp" \
         -o -iname "*.h" \
         -o -iname "*.cc" \
         -o -iname "*.mm" \) | \
    xargs -n 1 `dirname $0`/assert_replacer.py #sed -i -e "s/\b$1\b/$2/g"
}

convert MOZ_STATIC_ASSERT static_assert
hg rev --no-backup mfbt/Assertions.h \
                   media/webrtc/signaling/src/sipcc/core/includes/ccapi.h \
                   modules/libmar/src/mar_private.h \
                   modules/libmar/src/mar.h


# assert_replacer.py
#!/usr/bin/python

import sys
import re

pattern = re.compile(r"\bMOZ_STATIC_ASSERT\b")

def replaceInPlace(fname):
  print fname
  f = open(fname, "rw+")
  lines = f.readlines()
  for i in range(0, len(lines)):
    while True:
      index = re.search(pattern, lines[i])
      if index != None:
        index = index.start()
        lines[i] = lines[i][0:index] + "static_assert" + lines[i][index+len("MOZ_STATIC_ASSERT"):]
        for j in range(i + 1, len(lines)):
          if lines[j].find("                 ", index) == index:
            lines[j] = lines[j][0:index] + lines[j][index+4:]
          else:
            break
      else:
        break
  f.seek(0, 0)
  f.truncate()
  f.write("".join(lines))
  f.close()

argc = len(sys.argv)
for i in range(1, argc):
  replaceInPlace(sys.argv[i])

--HG--
extra : rebase_source : 4b4a4047d82f2c205b9fad8d56dfc3f1afc0b045
2013-07-18 13:59:53 -04:00

58 lines
2.3 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Implements a UTF-16 character type. */
#ifndef mozilla_Char16_h
#define mozilla_Char16_h
#include "mozilla/Assertions.h"
/*
* C11 and C++11 introduce a char16_t type and support for UTF-16 string and
* character literals. C++11's char16_t is a distinct builtin type. C11's
* char16_t is a typedef for uint_least16_t. Technically, char16_t is a 16-bit
* code unit of a Unicode code point, not a "character".
*
* For now, Char16.h only supports C++ because we don't want mix different C
* and C++ definitions of char16_t in the same code base.
*/
#ifdef _MSC_VER
/*
* C++11 says char16_t is a distinct builtin type, but Windows's yvals.h
* typedefs char16_t as an unsigned short. We would like to alias char16_t
* to Windows's 16-bit wchar_t so we can declare UTF-16 literals as constant
* expressions (and pass char16_t pointers to Windows APIs). We #define our
* char16_t as a macro to override yval.h's typedef of the same name.
*/
# define MOZ_UTF16_HELPER(s) L##s
# include <yvals.h>
# define char16_t wchar_t
#elif defined(__cplusplus) && \
(__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
/* C++11 has a builtin char16_t type. */
# define MOZ_UTF16_HELPER(s) u##s
#else
# error "Char16.h requires C++11 (or something like it) for UTF-16 support."
#endif
/*
* Macro arguments used in concatenation or stringification won't be expanded.
* Therefore, in order for |MOZ_UTF16(FOO)| to work as expected (which is to
* expand |FOO| before doing whatever |MOZ_UTF16| needs to do to it) a helper
* macro, |MOZ_UTF16_HELPER| needs to be inserted in between to allow the macro
* argument to expand. See "3.10.6 Separate Expansion of Macro Arguments" of the
* CPP manual for a more accurate and precise explanation.
*/
#define MOZ_UTF16(s) MOZ_UTF16_HELPER(s)
static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?");
static_assert(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?");
static_assert(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?");
#endif /* mozilla_Char16_h */