Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
Add shared macros for stringizing and converting ANSI string constants
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
joi@chromium.org committed Nov 18, 2010
1 parent 7d9f83e commit 8e0f745
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 17 deletions.
1 change: 1 addition & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -228,6 +228,7 @@
'string_util.cc',
'string_util.h',
'string_util_win.h',
'stringize_macros.h',
'stringprintf.cc',
'stringprintf.h',
'sys_info.h',
Expand Down
57 changes: 57 additions & 0 deletions base/stringize_macros.h
Original file line number Diff line number Diff line change
@@ -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_
59 changes: 59 additions & 0 deletions base/stringize_macros_unittest.cc
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 3 additions & 7 deletions ceee/ie/common/ceee_module_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions media/base/media_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
5 changes: 2 additions & 3 deletions net/base/net_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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:
Expand Down

0 comments on commit 8e0f745

Please sign in to comment.