Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Unified Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 2637393002: Make mojom-generated C++ string constants really constant. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_declaration.tmpl ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/tools/bindings/generators/mojom_cpp_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
index 7ca141e468e553a59eb78d3cbccd2fe60d5aa42c..be70d2f8fc577f657a072c35fb7525c68797568e 100644
--- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
@@ -216,10 +216,16 @@ def GetNativeTypeName(typemapped_kind):
return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"]
def GetCppPodType(kind):
- if mojom.IsStringKind(kind):
- return "char*"
return _kind_to_cpp_type[kind]
+def FormatConstantDeclaration(constant, nested=False):
+ if mojom.IsStringKind(constant.kind):
+ if nested:
+ return "const char %s[]" % constant.name
+ return "extern const char %s[]" % constant.name
+ return "constexpr %s %s = %s" % (GetCppPodType(constant.kind), constant.name,
+ ConstantValue(constant))
+
def GetCppWrapperType(kind, add_same_module_namespaces=False):
def _AddOptional(type_name):
pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>"
@@ -417,13 +423,18 @@ def TranslateConstants(token, kind):
flatten_nested_kind=True)
if isinstance(token, mojom.BuiltinValue):
- if token.value == "double.INFINITY" or token.value == "float.INFINITY":
- return "INFINITY";
- if token.value == "double.NEGATIVE_INFINITY" or \
- token.value == "float.NEGATIVE_INFINITY":
- return "-INFINITY";
- if token.value == "double.NAN" or token.value == "float.NAN":
- return "NAN";
+ if token.value == "double.INFINITY":
+ return "std::numeric_limits<double>::infinity()"
+ if token.value == "float.INFINITY":
+ return "std::numeric_limits<float>::infinity()"
+ if token.value == "double.NEGATIVE_INFINITY":
+ return "-std::numeric_limits<double>::infinity()"
+ if token.value == "float.NEGATIVE_INFINITY":
+ return "-std::numeric_limits<float>::infinity()"
+ if token.value == "double.NAN":
+ return "std::numeric_limits<double>::quiet_NaN()"
+ if token.value == "float.NAN":
+ return "std::numeric_limits<float>::quiet_NaN()"
if (kind is not None and mojom.IsFloatKind(kind)):
return token if token.isdigit() else token + "f";
@@ -535,6 +546,7 @@ class Generator(generator.Generator):
"cpp_wrapper_type": GetCppWrapperType,
"default_value": DefaultValue,
"expression_to_text": ExpressionToText,
+ "format_constant_declaration": FormatConstantDeclaration,
"get_container_validate_params_ctor_args":
GetContainerValidateParamsCtorArgs,
"get_name_for_kind": GetNameForKind,
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_declaration.tmpl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698