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 ++++++++++++++++++++++++++++++++++++++ ceee/ie/common/ceee_module_util.cc | 10 ++----- media/base/media_posix.cc | 10 +++---- net/base/net_errors.cc | 5 ++-- 7 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 base/stringize_macros.h create mode 100644 base/stringize_macros_unittest.cc 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) diff --git a/ceee/ie/common/ceee_module_util.cc b/ceee/ie/common/ceee_module_util.cc index 6197d30..930520f 100644 --- a/ceee/ie/common/ceee_module_util.cc +++ b/ceee/ie/common/ceee_module_util.cc @@ -10,17 +10,13 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" +#include "base/stringize_macros.h" #include "base/win/registry.h" #include "ceee/common/process_utils_win.h" #include "chrome/installer/util/google_update_constants.h" #include "version.h" // NOLINT -// TODO(joi@chromium.org): would be nice to move these (and non-L counterparts) -// to e.g. base/string_util.h -#define LSTRINGIZE2(x) L ## #x -#define LSTRINGIZE(x) LSTRINGIZE2(x) - namespace { const wchar_t* kRegistryPath = L"SOFTWARE\\Google\\CEEE"; @@ -196,7 +192,7 @@ bool NeedToInstallExtension() { base::win::RegKey hkcu(HKEY_CURRENT_USER, kRegistryPath, KEY_READ); success = hkcu.ReadValue( kRegistryValueCrxInstalledByVersion, &version_string); - return !success || version_string != LSTRINGIZE(CHROME_VERSION_STRING); + return !success || version_string != TO_L_STRING(CHROME_VERSION_STRING); } } @@ -224,7 +220,7 @@ void SetInstalledExtensionPath(const FilePath& path) { DCHECK(write_result); write_result = key.WriteValue(kRegistryValueCrxInstalledByVersion, - LSTRINGIZE(CHROME_VERSION_STRING)); + TO_L_STRING(CHROME_VERSION_STRING)); } bool IsCrxOrEmpty(const std::wstring& path) { diff --git a/media/base/media_posix.cc b/media/base/media_posix.cc index 89f8a17..272e9e6 100644 --- a/media/base/media_posix.cc +++ b/media/base/media_posix.cc @@ -12,6 +12,7 @@ #include "base/logging.h" #include "base/native_library.h" #include "base/path_service.h" +#include "base/stringize_macros.h" #include "media/ffmpeg/ffmpeg_common.h" #include "third_party/ffmpeg/ffmpeg_stubs.h" #if defined(OS_LINUX) @@ -32,12 +33,9 @@ namespace { #error FFmpeg headers not included! #endif -#define STRINGIZE(x) #x -#define STRINGIZE_MACRO(x) STRINGIZE(x) - -#define AVCODEC_VERSION STRINGIZE_MACRO(LIBAVCODEC_VERSION_MAJOR) -#define AVFORMAT_VERSION STRINGIZE_MACRO(LIBAVFORMAT_VERSION_MAJOR) -#define AVUTIL_VERSION STRINGIZE_MACRO(LIBAVUTIL_VERSION_MAJOR) +#define AVCODEC_VERSION STRINGIZE(LIBAVCODEC_VERSION_MAJOR) +#define AVFORMAT_VERSION STRINGIZE(LIBAVFORMAT_VERSION_MAJOR) +#define AVUTIL_VERSION STRINGIZE(LIBAVUTIL_VERSION_MAJOR) #if defined(OS_MACOSX) #define DSO_NAME(MODULE, VERSION) ("lib" MODULE "." VERSION ".dylib") diff --git a/net/base/net_errors.cc b/net/base/net_errors.cc index 18f6661..6c0e09c 100644 --- a/net/base/net_errors.cc +++ b/net/base/net_errors.cc @@ -5,8 +5,7 @@ #include "net/base/net_errors.h" #include "base/basictypes.h" - -#define STRINGIZE(x) #x +#include "base/stringize_macros.h" namespace net { @@ -19,7 +18,7 @@ const char* ErrorToString(int error) { switch (error) { #define NET_ERROR(label, value) \ case ERR_ ## label: \ - return "net::" STRINGIZE(ERR_ ## label); + return "net::" STRINGIZE_NO_EXPANSION(ERR_ ## label); #include "net/base/net_error_list.h" #undef NET_ERROR default: -- cgit v1.1