From 176ab8038d7c1b291dcec51f29ed81f9fba52b8e Mon Sep 17 00:00:00 2001 From: "joi@chromium.org" Date: Thu, 18 Nov 2010 02:15:12 +0000 Subject: Add shared macros for stringizing and converting ANSI string constants (in particular defined by macros) to wide string constants. Convert existing locally-defined stringizing to use the shared macros. Unit tests for the shared macros. This also fixes a minor bug in ceee_module_util.cc where I accidentally quoted a string constant I only meant to convert to wide (this caused no bug, but was unintended, so the change in semantics in that file in the current change is intentional). BUG=none TEST=automated tests Review URL: http://codereview.chromium.org/5103001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66579 0039d316-1c4b-4281-b951-d872f2087c98 --- base/base.gyp | 1 + base/base.gypi | 3 +- base/stringize_macros.h | 57 +++++++++++++++++++++++++++++++++++++ base/stringize_macros_unittest.cc | 59 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 base/stringize_macros.h create mode 100644 base/stringize_macros_unittest.cc (limited to 'base') diff --git a/base/base.gyp b/base/base.gyp index 0c3918f..f68359a 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -135,6 +135,7 @@ 'string_split_unittest.cc', 'string_tokenizer_unittest.cc', 'string_util_unittest.cc', + 'stringize_macros_unittest.cc', 'stringprintf_unittest.cc', 'sys_info_unittest.cc', 'sys_string_conversions_mac_unittest.mm', diff --git a/base/base.gypi b/base/base.gypi index a90a451..8b876bd 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -143,7 +143,7 @@ 'metrics/histogram.cc', 'metrics/histogram.h', 'metrics/nacl_histogram.cc', - 'metrics/nacl_histogram.h', + 'metrics/nacl_histogram.h', 'metrics/stats_counters.cc', 'metrics/stats_counters.h', 'metrics/stats_table.cc', @@ -228,6 +228,7 @@ 'string_util.cc', 'string_util.h', 'string_util_win.h', + 'stringize_macros.h', 'stringprintf.cc', 'stringprintf.h', 'sys_info.h', diff --git a/base/stringize_macros.h b/base/stringize_macros.h new file mode 100644 index 0000000..7c3af49 --- /dev/null +++ b/base/stringize_macros.h @@ -0,0 +1,57 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// This file defines preprocessor macros for stringizing preprocessor +// symbols (or their output) and manipulating preprocessor symbols +// that define strings. + +#ifndef BASE_STRINGIZE_MACROS_H_ +#define BASE_STRINGIZE_MACROS_H_ +#pragma once + +#include "build/build_config.h" + +// This is not very useful as it does not expand defined symbols if +// called directly. Use its counterpart without the _NO_EXPANSION +// suffix, below. +#define STRINGIZE_NO_EXPANSION(x) #x + +// Use this to quote the provided parameter, first expanding it if it +// is a preprocessor symbol. +// +// For example, if: +// #define A FOO +// #define B(x) myobj->FunctionCall(x) +// +// Then: +// STRINGIZE(A) produces "FOO" +// STRINGIZE(B(y)) produces "myobj->FunctionCall(y)" +#define STRINGIZE(x) STRINGIZE_NO_EXPANSION(x) + +// The following are defined only on Windows (for use when interacting +// with Windows APIs) as wide strings are otherwise deprecated. +#if defined(OS_WIN) + +// Second-level utility macros to let us expand symbols. +#define LSTRINGIZE_NO_EXPANSION(x) L ## #x +#define TO_L_STRING_NO_EXPANSION(x) L ## x + +// L version of STRINGIZE(). For examples above, +// LSTRINGIZE(A) produces L"FOO" +// LSTRINGIZE(B(y)) produces L"myobj->FunctionCall(y)" +#define LSTRINGIZE(x) LSTRINGIZE_NO_EXPANSION(x) + +// Adds an L in front of an existing ASCII string constant (after +// expanding symbols). Does not do any quoting. +// +// For example, if: +// #define C "foo" +// +// Then: +// TO_L_STRING(C) produces L"foo" +#define TO_L_STRING(x) TO_L_STRING_NO_EXPANSION(x) + +#endif // defined(OS_WIN) + +#endif // BASE_STRINGIZE_MACROS_H_ diff --git a/base/stringize_macros_unittest.cc b/base/stringize_macros_unittest.cc new file mode 100644 index 0000000..8d92d53 --- /dev/null +++ b/base/stringize_macros_unittest.cc @@ -0,0 +1,59 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Unit tests for stringize_macros.h + +#include "base/stringize_macros.h" + +#include "testing/gtest/include/gtest/gtest.h" + +// Macros as per documentation in header file. +#define PREPROCESSOR_UTIL_UNITTEST_A FOO +#define PREPROCESSOR_UTIL_UNITTEST_B(x) myobj->FunctionCall(x) +#define PREPROCESSOR_UTIL_UNITTEST_C "foo" + +TEST(StringizeTest, Ansi) { + EXPECT_STREQ( + "PREPROCESSOR_UTIL_UNITTEST_A", + STRINGIZE_NO_EXPANSION(PREPROCESSOR_UTIL_UNITTEST_A)); + EXPECT_STREQ( + "PREPROCESSOR_UTIL_UNITTEST_B(y)", + STRINGIZE_NO_EXPANSION(PREPROCESSOR_UTIL_UNITTEST_B(y))); + EXPECT_STREQ( + "PREPROCESSOR_UTIL_UNITTEST_C", + STRINGIZE_NO_EXPANSION(PREPROCESSOR_UTIL_UNITTEST_C)); + + EXPECT_STREQ("FOO", STRINGIZE(PREPROCESSOR_UTIL_UNITTEST_A)); + EXPECT_STREQ("myobj->FunctionCall(y)", + STRINGIZE(PREPROCESSOR_UTIL_UNITTEST_B(y))); + EXPECT_STREQ("\"foo\"", STRINGIZE(PREPROCESSOR_UTIL_UNITTEST_C)); +} + +#if defined(OS_WIN) + +TEST(StringizeTest, Wide) { + EXPECT_STREQ( + L"PREPROCESSOR_UTIL_UNITTEST_A", + LSTRINGIZE_NO_EXPANSION(PREPROCESSOR_UTIL_UNITTEST_A)); + EXPECT_STREQ( + L"PREPROCESSOR_UTIL_UNITTEST_B(y)", + LSTRINGIZE_NO_EXPANSION(PREPROCESSOR_UTIL_UNITTEST_B(y))); + EXPECT_STREQ( + L"PREPROCESSOR_UTIL_UNITTEST_C", + LSTRINGIZE_NO_EXPANSION(PREPROCESSOR_UTIL_UNITTEST_C)); + + EXPECT_STREQ(L"FOO", LSTRINGIZE(PREPROCESSOR_UTIL_UNITTEST_A)); + EXPECT_STREQ(L"myobj->FunctionCall(y)", + LSTRINGIZE(PREPROCESSOR_UTIL_UNITTEST_B(y))); + EXPECT_STREQ(L"\"foo\"", LSTRINGIZE(PREPROCESSOR_UTIL_UNITTEST_C)); +} + +TEST(ToLStringTest, Main) { + EXPECT_STREQ(L"blat", TO_L_STRING_NO_EXPANSION("blat")); + + EXPECT_STREQ(L"foo", TO_L_STRING(PREPROCESSOR_UTIL_UNITTEST_C)); + EXPECT_STREQ(L"blat", TO_L_STRING("blat")); +} + +#endif // defined(OS_WIN) -- cgit v1.1