blob: e6837c879c1661c8110ce5e4b00d8ef0f5e06db1 [file]
#!/usr/bin/env python3
# Copyright 2026 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit tests for cpp_lexer.py and cpp_parser.py."""
import os
import sys
import unittest
sys.path.insert(
0, os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir))
)
import cpp_lexer
import cpp_parser
import models
_PATH = 'feature_list.cc'
def _views(text, is_desktop_android=False):
"""Returns the text and its code and structure views without the code that
is not compiled, which the cpp_parser functions expect, like
java_flag_generator._FileCache.read().
"""
code, structure = cpp_lexer.lex(text)
return cpp_lexer.blank_inactive_branches(
text, code, structure, is_desktop_android
)
def _parse_source(text, is_desktop_android=False):
"""Calls cpp_parser.parse_source() with the views that it expects."""
return cpp_parser.parse_source(_PATH, *_views(text, is_desktop_android))
def _parse_feature_definitions(text, is_desktop_android=False):
"""Calls cpp_parser.parse_feature_definitions() with the views that it
expects.
"""
_, code, structure = _views(text, is_desktop_android)
return cpp_parser.parse_feature_definitions(
_PATH, code, structure, is_desktop_android
)
def _parse_param_definitions(text, is_desktop_android=False):
"""Calls cpp_parser.parse_param_definitions() with the views that it
expects.
"""
_, code, structure = _views(text, is_desktop_android)
return cpp_parser.parse_param_definitions(
_PATH, code, structure, is_desktop_android
)
class CppLexerTest(unittest.TestCase):
def _blanked(self, text):
"""Returns the expected lexer output, where '~' marks a blanked char."""
return text.replace('~', ' ')
def _assert_lexed(self, text, code, structure=None):
"""Checks both views returned by lex(). `structure` defaults to `code`.
In `code` and `structure`, '~' marks a character replaced by a space.
"""
actual_code, actual_structure = cpp_lexer.lex(text)
if structure is None:
structure = code
self.assertEqual(self._blanked(code), actual_code)
self.assertEqual(self._blanked(structure), actual_structure)
def testLineComment(self):
self._assert_lexed('a; // b, (c\nd;', 'a; ~~~~~~~~\nd;')
def testBlockComment(self):
self._assert_lexed('a /* b,\n(c */ d', 'a ~~~~~\n~~~~~ d')
def testUnterminatedBlockComment(self):
self._assert_lexed('a /* b\nc', 'a ~~~~\n~')
def testQuotesInComments(self):
self._assert_lexed("// it's\n/* \" */ f();", '~~~~~~~\n~~~~~~~ f();')
def testStringLiterals(self):
self._assert_lexed(
'f("a(b,c)", "/* d */", "// e");',
# Code:
'f("a(b,c)", "/* d */", "// e");',
# Structure:
'f("~~~~~~", "~~~~~~~", "~~~~");',
)
def testCharLiterals(self):
self._assert_lexed(
"f('(', '\"', ',');",
# Code:
"f('(', '\"', ',');",
# Structure:
"f('~', '~', '~');",
)
def testEscapedQuotes(self):
self._assert_lexed(
r'f("a\")", b);',
# Code:
r'f("a\")", b);',
# Structure:
r'f("~~~~", b);',
)
self._assert_lexed(
r"g('\'', c);",
# Code:
r"g('\'', c);",
# Structure:
r"g('~~', c);",
)
def testUnterminatedStringEndsAtNewline(self):
self._assert_lexed(
'"a(b\nf(c)',
# Code:
'"a(b\nf(c)',
# Structure:
'"~~~\nf(c)',
)
def testDigitSeparators(self):
self._assert_lexed(
"x = 1'000'000 + 0x1'FF + 'a';",
# Code:
"x = 1'000'000 + 0x1'FF + 'a';",
# Structure:
"x = 1'000'000 + 0x1'FF + '~';",
)
def testRawStrings(self):
self._assert_lexed(
'R"(a "b" (c)";',
# Code:
'R"(a "b" (c)";',
# Structure:
'R"(~~~~~~~~)";',
)
# The delimiter allows )" within the string.
self._assert_lexed(
'R"x()")x";',
# Code:
'R"x()")x";',
# Structure:
'R"x(~~)x";',
)
self._assert_lexed(
'u8R"(a)" LR"(b)" uR"(c)" UR"(d)"',
# Code:
'u8R"(a)" LR"(b)" uR"(c)" UR"(d)"',
# Structure:
'u8R"(~)" LR"(~)" uR"(~)" UR"(~)"',
)
def testIdentifierEndingInRIsNotRawStringPrefix(self):
self._assert_lexed(
'xR"(a)"',
# Code:
'xR"(a)"',
# Structure:
'xR"~~~"',
)
def testFindMatching(self):
structure = 'f(a[b{c}], (d))'
self.assertEqual(14, cpp_lexer.find_matching(structure, 1))
self.assertEqual(8, cpp_lexer.find_matching(structure, 3))
self.assertEqual(7, cpp_lexer.find_matching(structure, 5))
self.assertEqual(13, cpp_lexer.find_matching(structure, 11))
def testFindMatchingErrors(self):
with self.assertRaisesRegex(ValueError, 'Unbalanced "]"'):
cpp_lexer.find_matching('(a]', 0)
with self.assertRaisesRegex(ValueError, 'Unterminated bracket'):
cpp_lexer.find_matching('(a', 0)
def testSplitTopLevel(self):
structure = 'a, f(b, c), {d, e}, [f, g],'
ranges = cpp_lexer.split_top_level(structure, 0, len(structure))
self.assertEqual(
['a', ' f(b, c)', ' {d, e}', ' [f, g]', ''],
[structure[start:end] for start, end in ranges],
)
def testSplitTopLevelWithinRange(self):
structure = 'f(x, g(y, z))'
ranges = cpp_lexer.split_top_level(structure, 2, 12)
self.assertEqual(
['x', ' g(y, z)'], [structure[start:end] for start, end in ranges]
)
class BlankInactiveBranchesTest(unittest.TestCase):
def _assert_blanked(self, annotated, is_desktop_android=False):
"""Checks which lines blank_inactive_branches() blanks in the text, code
and structure views. Each line of `annotated` starts with '-' if it is
blanked, or with ' ' if it is kept.
"""
lines = annotated.split('\n')
text = '\n'.join(line[1:] for line in lines)
blanked = [line.startswith('-') for line in lines]
def blank(view):
return '\n'.join(
' ' * len(line) if line_blanked else line
for line, line_blanked in zip(view.split('\n'), blanked)
)
code, structure = cpp_lexer.lex(text)
actual = cpp_lexer.blank_inactive_branches(
text, code, structure, is_desktop_android
)
for name, view, actual_view in zip(
('text', 'code', 'structure'), (text, code, structure), actual
):
self.assertEqual(blank(view), actual_view, f'{name} view')
def testOsBuildflags(self):
text = """\
-#if BUILDFLAG(IS_ANDROID)
android
-#else
-not_android
-#endif
-#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_IOS)
-win_or_ios
-#endif
-#if !BUILDFLAG(IS_MAC) && BUILDFLAG(IS_POSIX)
posix_but_not_mac
-#endif
"""
self._assert_blanked(text)
def testDesktopAndroid(self):
desktop = """\
-#if BUILDFLAG(IS_DESKTOP_ANDROID)
desktop
-#else
-not_desktop
-#endif
"""
not_desktop = """\
-#if BUILDFLAG(IS_DESKTOP_ANDROID)
-desktop
-#else
not_desktop
-#endif
"""
self._assert_blanked(desktop, is_desktop_android=True)
self._assert_blanked(not_desktop, is_desktop_android=False)
def testUnknownBuildflagsAreTrue(self):
# Even when they are negated.
text = """\
-#if BUILDFLAG(ENABLE_FOO)
foo
-#else
-not_foo
-#endif
-#if !BUILDFLAG(ENABLE_FOO)
not_foo
-#else
-foo
-#endif
"""
self._assert_blanked(text)
def testThreeValuedLogic(self):
# Known values only decide a condition if the unknown ones can't change
# it. Otherwise, the condition is unknown, and assumed to be true.
text = """\
-#if BUILDFLAG(IS_WIN) && BUILDFLAG(ENABLE_FOO)
-win_and_foo
-#endif
-#if defined(FOO) && BUILDFLAG(IS_WIN)
-foo_and_win
-#endif
-#if !(BUILDFLAG(IS_ANDROID) || defined(FOO))
-neither_android_nor_foo
-#endif
-#if BUILDFLAG(IS_WIN) || BUILDFLAG(ENABLE_FOO)
win_or_foo
-#endif
-#if !BUILDFLAG(IS_ANDROID) || BUILDFLAG(ENABLE_FOO)
not_android_or_foo
-#endif
-#if !(BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_32_BITS))
not_32_bit_android
-#endif
-#if BUILDFLAG(IS_ANDROID) && !BUILDFLAG(ENABLE_FOO)
android_and_not_foo
-#endif
"""
self._assert_blanked(text)
def testElif(self):
text = """\
-#if BUILDFLAG(IS_IOS)
-ios
-#elif BUILDFLAG(IS_ANDROID)
android
-#elif BUILDFLAG(IS_POSIX)
-other_posix
-#else
-other
-#endif
-#if BUILDFLAG(IS_WIN)
-win
-#elif BUILDFLAG(IS_MAC)
-mac
-#else
other
-#endif
"""
self._assert_blanked(text)
def testNestedBlocks(self):
text = """\
-#if BUILDFLAG(IS_WIN)
-#if BUILDFLAG(IS_ANDROID)
-win_and_android
-#else
-win_only
-#endif
-#ifdef FOO
-win_and_foo
-#else
-win_and_not_foo
-#endif
-win
-#else
not_win
-#if BUILDFLAG(IS_DESKTOP_ANDROID)
-desktop
-#else
not_desktop
-#endif
-#endif
"""
self._assert_blanked(text)
def testOtherConditionsAreTrue(self):
text = """\
-#if DCHECK_IS_ON()
dcheck
-#else
-no_dcheck
-#endif
-#if !defined(FOO)
not_foo
-#endif
-#ifdef FOO
foo
-#elif BUILDFLAG(IS_ANDROID)
-android
-#endif
-#ifndef FOO
not_foo
-#endif
-#if BUILDFLAG(IS_WIN)
-win
-#elifdef FOO
foo
-#else
-other
-#endif
"""
self._assert_blanked(text)
def testUnparsableConditionsAreTrue(self):
text = """\
-#if BUILDFLAG(IS_WIN) == 1
compared
-#endif
-#if BUILDFLAG(IS_WIN) ? 1 : 0
ternary
-#endif
-#if BUILDFLAG(IS_WIN) &&
incomplete
-#endif
-#if BUILDFLAG(IS_WIN
unterminated
-#endif
"""
self._assert_blanked(text)
def testFormatting(self):
text = """\
- # if BUILDFLAG(IS_WIN) // Windows only.
-win
- # endif // BUILDFLAG(IS_WIN)
-#if BUILDFLAG(IS_WIN) /* Windows */ || \\
- BUILDFLAG(IS_MAC)
-win_or_mac
-#endif
-#if(BUILDFLAG(IS_ANDROID)&&!BUILDFLAG(IS_WIN))
android
-#endif
"""
self._assert_blanked(text)
def testOtherDirectives(self):
text = """\
#include "foo.h"
#define FOO(x) \\
(x)
#pragma once
-#if BUILDFLAG(IS_WIN)
-#include "win.h"
-#define BAR(x) \\
- (x)
-#endif
"""
self._assert_blanked(text)
def testDirectivesInCommentsAndStrings(self):
text = """\
/*
#if BUILDFLAG(IS_WIN)
*/
not_in_comment
const char kText[] = R"(
#if BUILDFLAG(IS_WIN)
)";
not_in_string
"""
self._assert_blanked(text)
def testUnbalancedDirectives(self):
# An unterminated #if ends at the end of the file.
text = """\
#endif
#else
stray
-#if BUILDFLAG(IS_WIN)
-unterminated
"""
self._assert_blanked(text)
class EvaluateConditionTest(unittest.TestCase):
def testEvaluateCondition(self):
for condition, is_desktop_android, expected in (
('BUILDFLAG(IS_ANDROID)', False, True),
('BUILDFLAG(IS_WIN)', False, False),
('BUILDFLAG(IS_DESKTOP_ANDROID)', False, False),
('BUILDFLAG(IS_DESKTOP_ANDROID)', True, True),
('!BUILDFLAG(IS_DESKTOP_ANDROID)', True, False),
('BUILDFLAG(IS_WIN) && BUILDFLAG(ENABLE_FOO)', False, False),
('BUILDFLAG(IS_ANDROID) || BUILDFLAG(ENABLE_FOO)', False, True),
# Unlike the conditions of #if, unknown conditions are not assumed
# to be true.
('BUILDFLAG(ENABLE_FOO)', False, None),
('!BUILDFLAG(ENABLE_FOO)', False, None),
('is_android', False, None),
('BUILDFLAG(IS_WIN) == 0', False, None),
):
with self.subTest(
condition=condition, is_desktop_android=is_desktop_android
):
self.assertEqual(
expected,
cpp_lexer.evaluate_condition(condition, is_desktop_android),
)
class ParseSourceTest(unittest.TestCase):
def _entries(self, source):
return [
(entry.line, entry.written)
for entry in source.feature_array.entries
]
def _assert_error(self, expected_messages, text):
with self.assertRaises(models.GeneratorError) as cm:
_parse_source(text)
self.assertEqual(expected_messages, cm.exception.messages)
def testParseSource(self):
text = """\
#include "chrome/flags/feature_list.h"
#include <string>
#include "base/feature_list.h"
// #include "commented/out.h"
// FEATURE_DEFINITION_FILE: //third_party/foo/features.cc
//FEATURE_DEFINITION_FILE://third_party/bar/features.cc
Foo(); // FEATURE_DEFINITION_FILE: //not/on/its/own/line.cc
namespace chrome {
namespace android {
namespace {
const base::Feature* const kFeaturesExposedToJava[] = {
&kLocal,
&features::kQualified,
&::kAbsolute,
};
} // namespace
static int64_t JNI_ChromeFeatureMap_GetNativeMap(JNIEnv* env) {
return reinterpret_cast<int64_t>(GetFeatureMap());
}
} // namespace android
} // namespace chrome
"""
self.assertEqual(
models.ParsedSource(
path=_PATH,
includes=('chrome/flags/feature_list.h', 'base/feature_list.h'),
directives=(
models.DefinitionDirective(
_PATH, 8, '//third_party/foo/features.cc'
),
models.DefinitionDirective(
_PATH, 9, '//third_party/bar/features.cc'
),
),
feature_map_names=('ChromeFeatureMap',),
feature_array=models.Array(
param_type=None,
namespace=('chrome', 'android'),
entries=(
models.ArrayEntry(_PATH, 17, '', 'kLocal'),
models.ArrayEntry(
_PATH, 18, 'features::', 'kQualified'
),
models.ArrayEntry(_PATH, 19, '::', 'kAbsolute'),
),
),
),
_parse_source(text),
)
def testArrayDeclarations(self):
for declaration in (
'const base::Feature* const kFeaturesExposedToJava[] = {',
'const base::Feature* const kFeaturesExposedToJava[2] = {',
'const base::Feature* const kFeaturesExposedToJava [ ] = {',
'const std::array<const base::Feature*, 2> '
'kFeaturesExposedToJava = {',
):
with self.subTest(declaration=declaration):
source = _parse_source(
declaration + '\n &kFoo,\n &kBar,\n};\n'
)
self.assertEqual(
[(2, 'kFoo'), (3, 'kBar')], self._entries(source)
)
def testIgnoresArrayNameInCommentsAndStrings(self):
text = """\
// const base::Feature* const kFeaturesExposedToJava[] = {&kInComment};
/* kFeaturesExposedToJava[] = {&kInBlockComment}; */
const char kText[] = "kFeaturesExposedToJava[] = {&kInString}";
const base::Feature* const kFeaturesExposedToJava[] = {&kReal};
"""
self.assertEqual([(4, 'kReal')], self._entries(_parse_source(text)))
def testEntryFormatting(self):
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {
// Comments, even with commas or &kInComment, are ignored.
&kFirst, // Trailing comment.
& ns :: kSpaced,
&ns::
kWrapped,
/* Block comment */ &kAfterBlockComment,
&kLast,
};
"""
self.assertEqual(
[
(3, 'kFirst'),
(4, 'ns::kSpaced'),
(5, 'ns::kWrapped'),
(7, 'kAfterBlockComment'),
(8, 'kLast'),
],
self._entries(_parse_source(text)),
)
def testEmptyArray(self):
text = """\
const std::array<const base::Feature*, 0> kFeaturesExposedToJava = {};
"""
self.assertEqual((), _parse_source(text).feature_array.entries)
def testIncludes(self):
text = """\
#include "a/b.h"
# include "c.h"
#include <vector>
// #include "commented_out.h"
/* #include "block_commented_out.h" */
#include "d.inc"
#if BUILDFLAG(IS_WIN)
#include "not_compiled.h"
#endif
const char kText[] = "#include \\"in_string.h\\"";
const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};
"""
self.assertEqual(
('a/b.h', 'c.h', 'd.inc'), _parse_source(text).includes
)
def testDirectivesInInactiveBranches(self):
text = """\
// FEATURE_DEFINITION_FILE: //always.cc
#if BUILDFLAG(IS_DESKTOP_ANDROID)
// FEATURE_DEFINITION_FILE: //desktop.cc
#else
// FEATURE_DEFINITION_FILE: //not_desktop.cc
#endif
#if BUILDFLAG(IS_WIN)
// FEATURE_DEFINITION_FILE: not/a/valid/path.cc
#endif
const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};
"""
for is_desktop_android, expected in (
(True, [(1, '//always.cc'), (3, '//desktop.cc')]),
(False, [(1, '//always.cc'), (5, '//not_desktop.cc')]),
):
with self.subTest(is_desktop_android=is_desktop_android):
source = _parse_source(text, is_desktop_android)
self.assertEqual(
expected,
[(d.line, d.written_path) for d in source.directives],
)
def testArrayNamespace(self):
array = 'const base::Feature* const kFeaturesExposedToJava[] = {};'
for prefix, expected in (
('', ()),
('namespace a {\nnamespace b {\n', ('a', 'b')),
('namespace a::b {\n', ('a', 'b')),
('namespace a {\nnamespace {\n', ('a',)),
('namespace a {\ninline namespace v1 {\n', ('a',)),
('namespace a {}\nnamespace b {\n', ('b',)),
('namespace a {\nstruct S {\n void F() {}\n};\n', ('a',)),
('namespace a {\nnamespace b = c;\n', ('a',)),
('namespace a {\n// }\nconst char kText[] = "}";\n', ('a',)),
):
with self.subTest(prefix=prefix):
source = _parse_source(prefix + array)
self.assertEqual(expected, source.feature_array.namespace)
def testFeatureMapNames(self):
text = """\
// JNI_InComment_GetNativeMap(
const char kText[] = "JNI_InString_GetNativeMap(";
static int64_t JNI_SecondFeatureMap_GetNativeMap(JNIEnv* env);
static int64_t JNI_FirstFeatureMap_GetNativeMap(JNIEnv* env) {
return reinterpret_cast<int64_t>(GetFirstFeatureMap());
}
static int64_t JNI_SecondFeatureMap_GetNativeMap (JNIEnv* env) {
return reinterpret_cast<int64_t>(GetSecondFeatureMap());
}
const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};
"""
self.assertEqual(
('FirstFeatureMap', 'SecondFeatureMap'),
_parse_source(text).feature_map_names,
)
def testParamArrays(self):
text = """\
namespace chrome {
const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};
namespace android {
const base::FeatureParam<std::string>* const kStringParamsExposedToJava[] = {
&kStringParam,
};
} // namespace android
const std::array<const base::FeatureParam<double>*, 1>
kDoubleParamsExposedToJava = {&kDoubleParam};
const base::FeatureParam<int>* const kIntParamsExposedToJava[] = {
&::kAbsoluteParam,
};
const base::FeatureParam<bool>* const kBooleanParamsExposedToJava[] = {
&kBoolParam,
&params::kQualifiedParam,
};
// Uses of the arrays are not definitions.
base::android::FeatureMap* GetFeatureMap() {
static base::NoDestructor<base::android::FeatureMap> kFeatureMap(
kFeaturesExposedToJava, kBooleanParamsExposedToJava,
kIntParamsExposedToJava, kDoubleParamsExposedToJava,
kStringParamsExposedToJava);
return kFeatureMap.get();
}
} // namespace chrome
"""
boolean_type, int_type, double_type, string_type = models.PARAM_TYPES
# The arrays are in PARAM_TYPES order, regardless of their order in the
# file.
self.assertEqual(
(
models.Array(
param_type=boolean_type,
namespace=('chrome',),
entries=(
models.ArrayEntry(_PATH, 17, '', 'kBoolParam'),
models.ArrayEntry(
_PATH, 18, 'params::', 'kQualifiedParam'
),
),
),
models.Array(
param_type=int_type,
namespace=('chrome',),
entries=(
models.ArrayEntry(_PATH, 14, '::', 'kAbsoluteParam'),
),
),
models.Array(
param_type=double_type,
namespace=('chrome',),
entries=(models.ArrayEntry(_PATH, 12, '', 'kDoubleParam'),),
),
models.Array(
param_type=string_type,
namespace=('chrome', 'android'),
entries=(models.ArrayEntry(_PATH, 7, '', 'kStringParam'),),
),
),
_parse_source(text).param_arrays,
)
def testParamArraysAreOptional(self):
text = 'const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};'
self.assertEqual((), _parse_source(text).param_arrays)
def testErrorNoArray(self):
text = """\
BASE_FEATURE(kFoo, base::FEATURE_DISABLED_BY_DEFAULT);
// const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};
"""
self._assert_error(
[
f'{_PATH}: error: Cannot find the definition of '
'kFeaturesExposedToJava.'
],
text,
)
def testErrorUnbalancedArray(self):
for body, reason in (
('{&kFoo,\n', 'Unterminated bracket'),
('{&kFoo(];\n', 'Unbalanced "]"'),
):
with self.subTest(reason=reason):
self._assert_error(
[
f'{_PATH}:1: error: Cannot parse kFeaturesExposedToJava: '
f'{reason}.'
],
'const base::Feature* const kFeaturesExposedToJava[] = '
+ body,
)
def testErrorUnsupportedEntries(self):
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {
kNotAPointer,
&kfoo,
&GetFeature(),
&features::kFoo.member,
&kValid,
};
"""
def message(line, entry):
return (
f'{_PATH}:{line}: error: Unsupported entry "{entry}" in '
'kFeaturesExposedToJava. Expected the form '
'"&namespace::kSomeName".'
)
self._assert_error(
[
message(2, 'kNotAPointer'),
message(3, '&kfoo'),
message(4, '&GetFeature()'),
message(5, '&features::kFoo.member'),
],
text,
)
def testErrorEmptyEntries(self):
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {
,
&kFoo,,
&kBar,
};
"""
message = 'error: Empty entry in kFeaturesExposedToJava.'
self._assert_error(
[f'{_PATH}:2: {message}', f'{_PATH}:3: {message}'], text
)
def testErrorDuplicateEntries(self):
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {
&ns::kFoo,
&kBar,
& ns::kFoo,
&kBar,
};
"""
self._assert_error(
[
f'{_PATH}:4: error: Duplicate entry "ns::kFoo" (first listed on '
'line 2).',
f'{_PATH}:5: error: Duplicate entry "kBar" (first listed on line '
'3).',
],
text,
)
def testConditionsInArrays(self):
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {
&kFoo,
#if BUILDFLAG(IS_DESKTOP_ANDROID)
&kDesktopOnly,
#else
&kNotDesktopOnly,
#endif
#if BUILDFLAG(IS_WIN)
&kWinOnly,
#endif
#ifdef BAR
&kBar,
#else
&kNotBar,
#endif
};
"""
for is_desktop_android, expected in (
(True, [(2, 'kFoo'), (4, 'kDesktopOnly'), (12, 'kBar')]),
(False, [(2, 'kFoo'), (6, 'kNotDesktopOnly'), (12, 'kBar')]),
):
with self.subTest(is_desktop_android=is_desktop_android):
self.assertEqual(
expected,
self._entries(_parse_source(text, is_desktop_android)),
)
def testErrorPreprocessorDirectivesInArray(self):
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {
&kFoo,
#define BAR_FEATURE &kBar
#include "more_features.inc"
};
"""
message = (
'error: Unsupported preprocessor directive inside '
'kFeaturesExposedToJava. Only #if, #ifdef, #ifndef, #elif, #else '
'and #endif are supported.'
)
self._assert_error(
[f'{_PATH}:3: {message}', f'{_PATH}:4: {message}'], text
)
def testErrorInvalidDirectives(self):
text = """\
// FEATURE_DEFINITION_FILE: third_party/foo/features.cc
// FEATURE_DEFINITION_FILE:
// FEATURE_DEFINITION_FILE: //third_party/foo/features.cc, //other.cc
// FEATURE_DEFINITION_FILE: //third_party/foo/valid.cc
const base::Feature* const kFeaturesExposedToJava[] = {&kFoo};
"""
def message(line, found):
return (
f'{_PATH}:{line}: error: FEATURE_DEFINITION_FILE must be '
'followed by a source-absolute path such as '
f'"//path/to/file.cc", found "{found}".'
)
self._assert_error(
[
message(1, 'third_party/foo/features.cc'),
message(2, ''),
message(3, '//third_party/foo/features.cc, //other.cc'),
],
text,
)
def testErrorsInParamArrays(self):
# The errors in all arrays are reported at once.
text = """\
const base::Feature* const kFeaturesExposedToJava[] = {kNotAPointer};
const base::FeatureParam<bool>* const kBooleanParamsExposedToJava[] = {
&kFoo,
&kFoo,
};
const base::FeatureParam<int>* const kIntParamsExposedToJava[] = {
kNotAPointer,
};
const base::FeatureParam<double>* const kDoubleParamsExposedToJava[] = {
#include "double_params.inc"
};
const base::FeatureParam<std::string>* const kStringParamsExposedToJava[] = {};
const base::FeatureParam<std::string>* const kStringParamsExposedToJava[] = {};
"""
def unsupported_entry_message(line, array_name):
return (
f'{_PATH}:{line}: error: Unsupported entry "kNotAPointer" '
f'in {array_name}. Expected the form '
'"&namespace::kSomeName".'
)
self._assert_error(
[
unsupported_entry_message(1, 'kFeaturesExposedToJava'),
f'{_PATH}:4: error: Duplicate entry "kFoo" (first listed on '
'line 3).',
unsupported_entry_message(7, 'kIntParamsExposedToJava'),
f'{_PATH}:10: error: Unsupported preprocessor directive inside '
'kDoubleParamsExposedToJava. Only #if, #ifdef, #ifndef, #elif, '
'#else and #endif are supported.',
f'{_PATH}: error: Found multiple definitions of '
'kStringParamsExposedToJava (lines 12, 13).',
],
text,
)
class ParseFeatureDefinitionsTest(unittest.TestCase):
def _definition(
self, line, namespace, identifier, macro, name, default_value=None
):
return models.FeatureDefinition(
_PATH,
line,
namespace,
identifier,
macro,
name,
default_value=default_value,
)
def testParseDefinitions(self):
text = """\
namespace foo {
BASE_FEATURE(kDerivedName, base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kCustomName, "CustomFeatureName",
base::FEATURE_ENABLED_BY_DEFAULT);
BASE_RUNTIME_MUTABLE_FEATURE(kRuntimeMutable,
base::FEATURE_DISABLED_BY_DEFAULT);
BASE_RUNTIME_MUTABLE_FEATURE(kRuntimeMutableCustomName,
"RuntimeMutableName",
base::FEATURE_DISABLED_BY_DEFAULT);
namespace bar {
BASE_FEATURE(kNested, base::FEATURE_DISABLED_BY_DEFAULT);
} // namespace bar
} // namespace foo
BASE_FEATURE(kGlobal, base::FEATURE_DISABLED_BY_DEFAULT);
"""
self.assertEqual(
[
self._definition(
3,
('foo',),
'kDerivedName',
'BASE_FEATURE',
'DerivedName',
default_value=False,
),
self._definition(
4,
('foo',),
'kCustomName',
'BASE_FEATURE',
'CustomFeatureName',
default_value=True,
),
self._definition(
6,
('foo',),
'kRuntimeMutable',
'BASE_RUNTIME_MUTABLE_FEATURE',
'RuntimeMutable',
default_value=False,
),
self._definition(
8,
('foo',),
'kRuntimeMutableCustomName',
'BASE_RUNTIME_MUTABLE_FEATURE',
'RuntimeMutableName',
default_value=False,
),
self._definition(
13,
('foo', 'bar'),
'kNested',
'BASE_FEATURE',
'Nested',
default_value=False,
),
self._definition(
18,
(),
'kGlobal',
'BASE_FEATURE',
'Global',
default_value=False,
),
],
_parse_feature_definitions(text),
)
def testOtherFeatureMacros(self):
text = """\
#define DEFINE_SYNC_AUTOFILL_AI_FEATURE(feature_name) \\
BASE_FEATURE(feature_name, base::FEATURE_ENABLED_BY_DEFAULT)
BASE_FEATURE_WITH_COUNTRY_RESTRICTIONS(kCountryRestricted,
base::FEATURE_DISABLED_FOR_COUNTRIES,
"us", "ca");
DEFINE_SYNC_AUTOFILL_AI_FEATURE(kSyncAutofillAi);
"""
self.assertEqual(
[
self._definition(
4,
(),
'kCountryRestricted',
'BASE_FEATURE_WITH_COUNTRY_RESTRICTIONS',
'CountryRestricted',
default_value=True,
),
self._definition(
7,
(),
'kSyncAutofillAi',
'DEFINE_SYNC_AUTOFILL_AI_FEATURE',
'SyncAutofillAi',
default_value=None,
),
],
_parse_feature_definitions(text),
)
def testIgnoredMacroCalls(self):
text = """\
#define BASE_FEATURE(feature, ...) Define(feature)
BASE_DECLARE_FEATURE(kDeclared);
BASE_FEATURE_PARAM(int, kParam, &kFeature, "param", 1);
BASE_FEATURE_ENUM_PARAM(Mode, kEnumParam, &kFeature, "mode", Mode::kA,
&kModeOptions);
EXPECT_SINGLE_FEATURE(features::kFeature);
// BASE_FEATURE(kInLineComment, base::FEATURE_DISABLED_BY_DEFAULT);
/* BASE_FEATURE(kInBlockComment, base::FEATURE_DISABLED_BY_DEFAULT); */
const char kText[] = "BASE_FEATURE(kInString, FEATURE_DISABLED_BY_DEFAULT)";
bool enabled = base::FeatureList::IsEnabled(kFeature);
"""
self.assertEqual([], _parse_feature_definitions(text))
def testPreprocessorLinesBetweenArguments(self):
text = """\
BASE_FEATURE(kPlatformDependent,
#if BUILDFLAG(IS_ANDROID)
base::FEATURE_ENABLED_BY_DEFAULT);
#else
base::FEATURE_DISABLED_BY_DEFAULT);
#endif
"""
self.assertEqual(
[
self._definition(
1,
(),
'kPlatformDependent',
'BASE_FEATURE',
'PlatformDependent',
default_value=True,
),
],
_parse_feature_definitions(text),
)
def testSpecialCharactersInFeatureName(self):
text = """\
BASE_FEATURE(kComma, "Comma,Name", base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kParen, "Paren)Name", base::FEATURE_DISABLED_BY_DEFAULT);
"""
self.assertEqual(
[('kComma', 'Comma,Name'), ('kParen', 'Paren)Name')],
[(d.identifier, d.name) for d in _parse_feature_definitions(text)],
)
def testInvalidFeatureNames(self):
text = """\
#define DEFINE_CUSTOM_FEATURE(feature, name) Define(feature, name)
BASE_FEATURE(kNotALiteral, kName, base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kConcatenated, "Foo" "Bar", base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kTooManyArgs, "Name", base::FEATURE_DISABLED_BY_DEFAULT, 1);
DEFINE_CUSTOM_FEATURE(kCustomMacro, "CustomName");
"""
definitions = _parse_feature_definitions(text)
self.assertEqual(
[None, None, None, None], [d.name for d in definitions]
)
self.assertEqual(
[None, None, None, None], [d.default_value for d in definitions]
)
self.assertEqual(
[
f'{_PATH}:2: The feature name of kNotALiteral must be a plain '
'string literal, found "kName".',
f'{_PATH}:3: The feature name of kConcatenated must be a plain '
'string literal, found ""Foo" "Bar"".',
f'{_PATH}:4: BASE_FEATURE(kTooManyArgs, ...) has 4 arguments; '
'expected 2 or 3.',
f'{_PATH}:5: Cannot determine the feature name of kCustomMacro '
'from macro DEFINE_CUSTOM_FEATURE, because it is passed a string '
'literal.',
],
[d.error for d in definitions],
)
def testSkipsUnbalancedMacroCalls(self):
text = """\
BASE_FEATURE(kUnterminated,
BASE_FEATURE(kMismatched, ];
BASE_FEATURE(kValid, base::FEATURE_DISABLED_BY_DEFAULT);
"""
self.assertEqual(
[
self._definition(
3,
(),
'kValid',
'BASE_FEATURE',
'Valid',
default_value=False,
),
],
_parse_feature_definitions(text),
)
def testDefaultStates(self):
text = """\
BASE_FEATURE(kEnabled, base::FEATURE_ENABLED_BY_DEFAULT);
BASE_FEATURE(kDisabled, "DisabledName", base::FEATURE_DISABLED_BY_DEFAULT);
BASE_RUNTIME_MUTABLE_FEATURE(kGlobalBase, ::base::FEATURE_ENABLED_BY_DEFAULT);
BASE_FEATURE(kFormatted, /* state= */ base :: FEATURE_ENABLED_BY_DEFAULT);
BASE_FEATURE(kEnumQualified, base::FeatureState::FEATURE_DISABLED_BY_DEFAULT);
// Country-restricted features have their state for an unknown country.
BASE_FEATURE_WITH_COUNTRY_RESTRICTIONS(kEnabledForCountries,
base::FEATURE_ENABLED_FOR_COUNTRIES,
"us");
BASE_FEATURE_WITH_COUNTRY_RESTRICTIONS(kDisabledForCountries,
base::FEATURE_DISABLED_FOR_COUNTRIES,
"us", "ca");
namespace base {
BASE_FEATURE(kUnqualified, FEATURE_ENABLED_BY_DEFAULT);
} // namespace base
// Expands to a country-restricted feature with
// base::FEATURE_DISABLED_FOR_COUNTRIES on Android.
DEFINE_FEATURE_WITH_MOBILE_COUNTRY_RESTRICTION(kMobileCountryRestricted);
"""
self.assertEqual(
[
('kEnabled', True),
('kDisabled', False),
('kGlobalBase', True),
('kFormatted', True),
('kEnumQualified', False),
('kEnabledForCountries', False),
('kDisabledForCountries', True),
('kUnqualified', True),
('kMobileCountryRestricted', True),
],
[
(d.identifier, d.default_value)
for d in _parse_feature_definitions(text)
],
)
def testConditionalDefaultStates(self):
text = """\
BASE_FEATURE(kDesktop,
BUILDFLAG(IS_DESKTOP_ANDROID) ? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kNotDesktop,
!BUILDFLAG(IS_DESKTOP_ANDROID)
? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kOs,
BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
// Known values decide a condition if unknown ones can't change it.
BASE_FEATURE(kThreeValuedLogic,
(BUILDFLAG(IS_WIN) && BUILDFLAG(ENABLE_FOO))
? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
"""
for is_desktop_android in (False, True):
with self.subTest(is_desktop_android=is_desktop_android):
definitions = _parse_feature_definitions(
text, is_desktop_android
)
self.assertEqual(
[
('kDesktop', is_desktop_android),
('kNotDesktop', not is_desktop_android),
('kOs', True),
('kThreeValuedLogic', False),
],
[(d.identifier, d.default_value) for d in definitions],
)
def testUnknownDefaultStates(self):
text = """\
BASE_FEATURE(kNamedConstant, kDefaultState);
BASE_FEATURE(kFunctionCall, enable_if(IS_ANDROID));
BASE_FEATURE(kNotAState, base::FEATURE_STATE_THAT_DOES_NOT_EXIST);
BASE_FEATURE(kParenthesized, (base::FEATURE_ENABLED_BY_DEFAULT));
BASE_FEATURE(kUnknownCondition,
BUILDFLAG(ENABLE_FOO) ? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kConstantCondition,
is_android ? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kNoColon,
BUILDFLAG(IS_ANDROID) ? base::FEATURE_ENABLED_BY_DEFAULT);
// Conditional expressions can't be nested, and their branches can't be in
// parentheses.
BASE_FEATURE(kNested,
BUILDFLAG(IS_IOS) ? base::FEATURE_DISABLED_BY_DEFAULT
: BUILDFLAG(IS_ANDROID) ? base::FEATURE_ENABLED_BY_DEFAULT
: base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kParenthesizedBranches,
BUILDFLAG(IS_ANDROID) ? (base::FEATURE_ENABLED_BY_DEFAULT)
: (base::FEATURE_DISABLED_BY_DEFAULT));
DEFINE_CUSTOM_FEATURE(kCustomMacro);
"""
definitions = _parse_feature_definitions(text)
self.assertEqual(10, len(definitions))
for definition in definitions:
self.assertIsNone(definition.default_value, definition.identifier)
class ParseParamDefinitionsTest(unittest.TestCase):
def _definition(
self,
line,
namespace,
identifier,
name,
feature_qualifier,
feature_identifier,
default_value=None,
):
return models.ParamDefinition(
_PATH,
line,
namespace,
identifier,
name,
models.ArrayEntry(
_PATH, line, feature_qualifier, feature_identifier
),
default_value=default_value,
)
def _default_value(self, cpp_type, written, is_desktop_android=False):
"""Returns the default value of the param definition
`BASE_FEATURE_PARAM(cpp_type, kParam, &kFeature, "param", written)`.
"""
[definition] = _parse_param_definitions(
f'BASE_FEATURE_PARAM({cpp_type}, kParam, &kFeature, "param",\n'
f' {written});\n',
is_desktop_android,
)
return definition.default_value
def _assert_default_values(self, cpp_type, cases):
"""Checks the default value of a param of `cpp_type` for each
(written, expected) in `cases`. `expected` is None when the default
value can't be determined.
"""
for written, expected in cases:
with self.subTest(written=written):
value = self._default_value(cpp_type, written)
self.assertEqual(expected, value)
# E.g. 1 == 1.0 and 1 == True, but they are different values.
self.assertIs(type(expected), type(value))
def testParseParamDefinitions(self):
text = """\
namespace foo {
BASE_FEATURE_PARAM(bool, kDerivedName, &kFeature, false);
BASE_FEATURE_PARAM(int,
kCustomName,
&kFeature,
"custom_name",
0);
BASE_FEATURE_PARAM(double, kQualifiedFeature, &features::kFeature, 0.5);
BASE_FEATURE_PARAM(std::string, kGlobalFeature, & :: kGlobal, "global", "");
namespace bar {
BASE_FEATURE_PARAM(base::TimeDelta, kNested, &kFeature, base::Seconds(1));
} // namespace bar
} // namespace foo
"""
self.assertEqual(
[
self._definition(
3,
('foo',),
'kDerivedName',
'DerivedName',
'',
'kFeature',
default_value=False,
),
self._definition(
4,
('foo',),
'kCustomName',
'custom_name',
'',
'kFeature',
default_value=0,
),
self._definition(
9,
('foo',),
'kQualifiedFeature',
'QualifiedFeature',
'features::',
'kFeature',
default_value=0.5,
),
self._definition(
10,
('foo',),
'kGlobalFeature',
'global',
'::',
'kGlobal',
default_value='',
),
self._definition(
13,
('foo', 'bar'),
'kNested',
'Nested',
'',
'kFeature',
default_value=None,
),
],
_parse_param_definitions(text),
)
def testIgnoredMacroCalls(self):
text = """\
#define BASE_FEATURE_PARAM(T, name, ...) Define(name)
BASE_DECLARE_FEATURE_PARAM(int, kDeclared);
BASE_FEATURE(kFeature, base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE_ENUM_PARAM(Mode, kEnumParam, &kFeature, "mode", Mode::kA,
&kModeOptions);
constexpr base::FeatureParam<int> kNotAMacro{&kFeature, "not_a_macro", 0};
// BASE_FEATURE_PARAM(int, kInLineComment, &kFeature, 0);
/* BASE_FEATURE_PARAM(int, kInBlockComment, &kFeature, 0); */
const char kText[] = "BASE_FEATURE_PARAM(int, kInString, &kFeature, 0)";
"""
self.assertEqual([], _parse_param_definitions(text))
def testPreprocessorLinesBetweenArguments(self):
text = """\
BASE_FEATURE_PARAM(int, kPlatformDependent, &kFeature, "size",
#if BUILDFLAG(IS_DESKTOP_ANDROID)
2);
#else
1);
#endif
"""
self.assertEqual(
[
self._definition(
1,
(),
'kPlatformDependent',
'size',
'',
'kFeature',
default_value=1,
),
],
_parse_param_definitions(text),
)
def testSpecialCharactersInParamName(self):
text = """\
BASE_FEATURE_PARAM(int, kComma, &kFeature, "comma,name", 0);
BASE_FEATURE_PARAM(int, kParen, &kFeature, "paren)name", 0);
"""
self.assertEqual(
[('kComma', 'comma,name'), ('kParen', 'paren)name')],
[(d.identifier, d.name) for d in _parse_param_definitions(text)],
)
def testInvalidParamDefinitions(self):
text = """\
BASE_FEATURE_PARAM(int, kNotALiteral, &kFeature, kName, 0);
BASE_FEATURE_PARAM(int, kConcatenated, &kFeature, "foo" "bar", 0);
BASE_FEATURE_PARAM(int, kTooFewArgs, &kFeature);
BASE_FEATURE_PARAM(int, kTooManyArgs, &kFeature, "name", 0, 1);
BASE_FEATURE_PARAM(int, kNotAPointer, kFeature, 0);
BASE_FEATURE_PARAM(int, kFunctionCall, &GetFeature(), 0);
"""
definitions = _parse_param_definitions(text)
self.assertEqual(
[(None, None)] * 6, [(d.name, d.feature) for d in definitions]
)
self.assertEqual([None] * 6, [d.default_value for d in definitions])
self.assertEqual(
[
f'{_PATH}:1: The param name of kNotALiteral must be a plain '
'string literal, found "kName".',
f'{_PATH}:2: The param name of kConcatenated must be a plain '
'string literal, found ""foo" "bar"".',
f'{_PATH}:3: BASE_FEATURE_PARAM(int, kTooFewArgs, ...) has 3 '
'arguments; expected 4 or 5.',
f'{_PATH}:4: BASE_FEATURE_PARAM(int, kTooManyArgs, ...) has 6 '
'arguments; expected 4 or 5.',
f'{_PATH}:5: The feature of kNotAPointer must have the form '
'"&namespace::kFeatureName", found "kFeature".',
f'{_PATH}:6: The feature of kFunctionCall must have the form '
'"&namespace::kFeatureName", found "&GetFeature()".',
],
[d.error for d in definitions],
)
def testSkipsMacroCallsThatAreNotDefinitions(self):
text = """\
BASE_FEATURE_PARAM(int, notAKIdentifier, &kFeature, 0);
BASE_FEATURE_PARAM(kOnlyOneArgument);
BASE_FEATURE_PARAM(int, kUnterminated, &kFeature,
BASE_FEATURE_PARAM(int, kMismatched, &kFeature, ];
BASE_FEATURE_PARAM(int, kValid, &kFeature, 0);
"""
self.assertEqual(
[
self._definition(
5, (), 'kValid', 'Valid', '', 'kFeature', default_value=0
),
],
_parse_param_definitions(text),
)
def testBoolDefaultValues(self):
self._assert_default_values(
'bool',
(
('true', True),
('false', False),
('kDefault', None),
('!false', None),
('1', None),
),
)
def testIntDefaultValues(self):
self._assert_default_values(
'int',
(
('0', 0),
('42', 42),
('-1', -1),
('+1', 1),
('kDefault', None),
('static_cast<int>(Mode::kA)', None),
('0x1F', None),
("1'000", None),
('10u', None),
('1.5', None),
('32 * 1024', None),
),
)
def testDoubleDefaultValues(self):
self._assert_default_values(
'double',
(
('0.5', 0.5),
('-0.25', -0.25),
('.5', 0.5),
('1e-3', 0.001),
('1', 1.0),
('kRatio', None),
('0.5f', None),
('1.0 / 3', None),
),
)
def testStringDefaultValues(self):
self._assert_default_values(
'std::string',
(
('""', ''),
('"foo"', 'foo'),
# "?" and ":" in strings are not conditional expressions.
('"https://example.com/?a=b:c"', 'https://example.com/?a=b:c'),
# Like names, values are kept as written.
('"a\\"b"', 'a\\"b'),
('kDefault', None),
('"a" "b"', None),
('std::string("foo")', None),
('R"(foo)"', None),
),
)
def testConditionalDefaultValues(self):
for cpp_type, written, if_desktop, if_not_desktop in (
(
'bool',
'BUILDFLAG(IS_DESKTOP_ANDROID) ? true : false',
True,
False,
),
('int', 'BUILDFLAG(IS_DESKTOP_ANDROID)\n ? 2\n : 1', 2, 1),
('double', '!BUILDFLAG(IS_DESKTOP_ANDROID) ? 0.5 : 1.5', 1.5, 0.5),
(
'std::string',
'BUILDFLAG(IS_DESKTOP_ANDROID) ? "a" : "b:c"',
'a',
'b:c',
),
('int', 'BUILDFLAG(ENABLE_FOO) ? 1 : 2', None, None),
('int', 'BUILDFLAG(IS_ANDROID) ? kDefault : 2', None, None),
('int', 'BUILDFLAG(IS_ANDROID) ? 1', None, None),
# Conditional expressions can't be nested, and their branches can't
# be in parentheses.
(
'int',
'BUILDFLAG(IS_IOS) ? 1 : BUILDFLAG(IS_ANDROID) ? 2 : 3',
None,
None,
),
('int', 'BUILDFLAG(IS_ANDROID) ? (1) : (2)', None, None),
):
with self.subTest(written=written):
self.assertEqual(
if_desktop,
self._default_value(
cpp_type, written, is_desktop_android=True
),
)
self.assertEqual(
if_not_desktop,
self._default_value(
cpp_type, written, is_desktop_android=False
),
)
def testUnsupportedParamTypes(self):
text = """\
BASE_FEATURE_PARAM(base::TimeDelta, kDelay, &kFeature, base::Seconds(1));
BASE_FEATURE_PARAM(size_t, kSize, &kFeature, "size", 10);
"""
self.assertEqual(
[None, None],
[d.default_value for d in _parse_param_definitions(text)],
)
if __name__ == '__main__':
unittest.main()