Commit 911ec1cf authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

Reland "bind-gen: Implement CodeGenContext.idl_location_and_name"

This is a reland of 5a2bf1a6

Fixed the build breakage on Windows by applying s/os.path/posixpath/

Original change's description:
> bind-gen: Implement CodeGenContext.idl_location_and_name
>
> Implements CodeGenContext.idl_location_and_name in order to pretty-
> print a path to an IDL file and IDL definition name being processed.
>
> Bug: 839389
> Change-Id: I2699f735fd5897206e25364b2b29932bd0177330
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1909049
> Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
> Reviewed-by: Hitoshi Yoshida <peria@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#714152}

Bug: 839389
Change-Id: Iad7c1c65f196d45bce0f4290bd25d3d67896e29e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1909678Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714176}
parent 374c4fd6
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import copy import copy
from . import name_style from . import name_style
from .path_manager import PathManager
class CodeGenContext(object): class CodeGenContext(object):
...@@ -60,6 +61,9 @@ class CodeGenContext(object): ...@@ -60,6 +61,9 @@ class CodeGenContext(object):
"class_like", "class_like",
"function_like", "function_like",
"idl_definition", "idl_definition",
"idl_location",
"idl_location_and_name",
"idl_name",
"is_return_by_argument", "is_return_by_argument",
"may_throw_exception", "may_throw_exception",
"member_like", "member_like",
...@@ -154,6 +158,31 @@ class CodeGenContext(object): ...@@ -154,6 +158,31 @@ class CodeGenContext(object):
or self.dictionary or self.enumeration or self.interface or self.dictionary or self.enumeration or self.interface
or self.namespace or self.typedef or self.union) or self.namespace or self.typedef or self.union)
@property
def idl_location(self):
idl_def = self.member_like or self.idl_definition
if idl_def:
location = idl_def.debug_info.location
text = PathManager.relpath_to_project_root(location.filepath)
if location.line_number is not None:
text += ":{}".format(location.line_number)
return text
return "<<unknown path>>"
@property
def idl_location_and_name(self):
return "{}: {}".format(self.idl_location, self.idl_name)
@property
def idl_name(self):
member = self.member_like or self.property_
if member:
return "{}.{}".format(self.class_like.identifier,
member.identifier)
if self.idl_definition:
return self.idl_definition.identifier
return "<<unknown name>>"
@property @property
def is_return_by_argument(self): def is_return_by_argument(self):
if self.return_type is None: if self.return_type is None:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import os.path import posixpath
import web_idl import web_idl
...@@ -34,16 +34,24 @@ class PathManager(object): ...@@ -34,16 +34,24 @@ class PathManager(object):
assert not cls._is_initialized assert not cls._is_initialized
assert isinstance(output_dirs, dict) assert isinstance(output_dirs, dict)
cls._output_dirs = output_dirs cls._output_dirs = output_dirs
cls._blink_path_prefix = os.path.sep + os.path.join( cls._blink_path_prefix = posixpath.sep + posixpath.join(
"third_party", "blink", "renderer", "") "third_party", "blink", "renderer", "")
cls._is_initialized = True cls._is_initialized = True
@classmethod
def relpath_to_project_root(cls, path):
index = path.find(cls._blink_path_prefix)
if index < 0:
assert path.startswith(cls._blink_path_prefix[1:])
return path
return path[index + 1:]
def __init__(self, idl_definition): def __init__(self, idl_definition):
assert self._is_initialized, self._REQUIRE_INIT_MESSAGE assert self._is_initialized, self._REQUIRE_INIT_MESSAGE
idl_path = idl_definition.debug_info.location.filepath idl_path = idl_definition.debug_info.location.filepath
self._idl_basepath, _ = os.path.splitext(idl_path) self._idl_basepath, _ = posixpath.splitext(idl_path)
self._idl_dir, self._idl_basename = os.path.split(self._idl_basepath) self._idl_dir, self._idl_basename = posixpath.split(self._idl_basepath)
components = sorted(idl_definition.components) components = sorted(idl_definition.components)
...@@ -82,7 +90,7 @@ class PathManager(object): ...@@ -82,7 +90,7 @@ class PathManager(object):
Returns a path to a Blink implementation file relative to the project Returns a path to a Blink implementation file relative to the project
root directory, e.g. "third_party/blink/renderer/..." root directory, e.g. "third_party/blink/renderer/..."
""" """
return self._rel_to_root( return self.relpath_to_project_root(
self._join( self._join(
dirpath=self._blink_dir, dirpath=self._blink_dir,
filename=(filename or self._blink_basename), filename=(filename or self._blink_basename),
...@@ -120,14 +128,8 @@ class PathManager(object): ...@@ -120,14 +128,8 @@ class PathManager(object):
filename=(filename or self._out_basename), filename=(filename or self._out_basename),
ext=ext) ext=ext)
def _rel_to_root(self, path): @staticmethod
index = path.find(self._blink_path_prefix) def _join(dirpath, filename, ext=None):
if index < 0:
assert path.startswith(self._blink_path_prefix[1:])
return path
return path[index + 1:]
def _join(self, dirpath, filename, ext=None):
if ext is not None: if ext is not None:
filename = os.path.extsep.join([filename, ext]) filename = posixpath.extsep.join([filename, ext])
return os.path.join(dirpath, filename) return posixpath.join(dirpath, filename)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment