diff options
| author | Luca Burelli <l.burelli@arduino.cc> | 2025-09-18 11:58:09 +0200 |
|---|---|---|
| committer | Torsten Tejlmand Rasmussen <torsten.rasmussen@nordicsemi.no> | 2025-10-20 12:34:58 +0200 |
| commit | 2472ba7ebb045150cfeed1b4f6539a14c2248afb (patch) | |
| tree | 844fd884c6646632e90f409ab1c925067e29f5a9 /kconfiglib.py | |
| parent | 228d0b91f3527af1adcafa1a4df06415509edd8d (diff) | |
kconfiglib: node: add 'loc' attribute
Replace 'filename' and 'linenr' usages with a single 'loc' attribute,
which is a (filename, linenr) tuple. This simplifies code dealing with
locations, and makes it explicitly constant. The original attributes are
now provided via properties for compatibility.
Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index a50312e..0c1ca67 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -503,9 +503,9 @@ expected by the function (excluding the implicit 'name' argument). If <max.args> is None, there is no upper limit to the number of arguments. Passing an invalid number of arguments will generate a KconfigError exception. -Functions can access the current parsing location as kconf.filename/linenr. -Accessing other fields of the Kconfig object is not safe. See the warning -below. +Functions can access the current parsing location as kconf.loc, or individually +as kconf.filename/linenr. Accessing other fields of the Kconfig object is not +safe. See the warning below. Keep in mind that for a variable defined like 'foo = $(fn)', 'fn' will be called only when 'foo' is expanded. If 'fn' uses the parsing location and the @@ -1051,8 +1051,7 @@ class Kconfig(object): self.top_node.prompt = ("Main menu", self.y) self.top_node.parent = None self.top_node.dep = self.y - self.top_node.filename = filename - self.top_node.linenr = 1 + self.top_node.loc = (filename, 1) self.top_node.include_path = () # Parse the Kconfig files @@ -2932,8 +2931,7 @@ class Kconfig(object): node.is_menuconfig = (t0 is _T_MENUCONFIG) node.prompt = node.help = node.list = None node.parent = parent - node.filename = self.filename - node.linenr = self.linenr + node.loc = self.loc node.include_path = self._include_path sym.nodes.append(node) @@ -3018,8 +3016,7 @@ class Kconfig(object): node.prompt = (self._expect_str_and_eol(), self.y) node.visibility = self.y node.parent = parent - node.filename = self.filename - node.linenr = self.linenr + node.loc = self.loc node.include_path = self._include_path self.menus.append(node) @@ -3038,8 +3035,7 @@ class Kconfig(object): node.prompt = (self._expect_str_and_eol(), self.y) node.list = None node.parent = parent - node.filename = self.filename - node.linenr = self.linenr + node.loc = self.loc node.include_path = self._include_path self.comments.append(node) @@ -3070,8 +3066,7 @@ class Kconfig(object): node.is_menuconfig = True node.prompt = node.help = None node.parent = parent - node.filename = self.filename - node.linenr = self.linenr + node.loc = self.loc node.include_path = self._include_path choice.nodes.append(node) @@ -3963,7 +3958,7 @@ class Kconfig(object): for node in self.node_iter(): if sym in node.referenced: msg += "\n\n- Referenced at {}:{}:\n\n{}" \ - .format(node.filename, node.linenr, node) + .format(node.loc[0], node.loc[1], node) self._warn(msg) def _warn(self, msg, filename=None, linenr=None): @@ -4735,7 +4730,7 @@ class Symbol(object): if self.nodes: for node in self.nodes: - add("{}:{}".format(node.filename, node.linenr)) + add("{}:{}".format(*node.loc)) else: add("constant" if self.is_constant else "undefined") @@ -5341,7 +5336,7 @@ class Choice(object): add("optional") for node in self.nodes: - add("{}:{}".format(node.filename, node.linenr)) + add("{}:{}".format(*node.loc)) return "<{}>".format(", ".join(fields)) @@ -5591,10 +5586,11 @@ class MenuNode(object): 'is_menuconfig' is just a hint on how to display the menu node. It's ignored internally by Kconfiglib, except when printing symbols. - filename/linenr: - The location where the menu node appears. The filename is relative to - $srctree (or to the current directory if $srctree isn't set), except - absolute paths are used for paths outside $srctree. + loc/filename/linenr: + The location where the menu node appears, as a (filename, linenr) tuple + or as individual properties. The filename is relative to $srctree (or to + the current directory if $srctree isn't set), except absolute paths are + used for paths outside $srctree. include_path: A tuple of (filename, linenr) tuples, giving the locations of the @@ -5610,14 +5606,13 @@ class MenuNode(object): """ __slots__ = ( "dep", - "filename", "help", "include_path", "is_menuconfig", "item", "kconfig", - "linenr", "list", + "loc", "next", "parent", "prompt", @@ -5640,6 +5635,20 @@ class MenuNode(object): self.ranges = [] @property + def filename(self): + """ + See the class documentation. + """ + return self.loc[0] + + @property + def linenr(self): + """ + See the class documentation. + """ + return self.loc[1] + + @property def orig_prompt(self): """ See the class documentation. @@ -5758,7 +5767,7 @@ class MenuNode(object): if self.next: add("has next") - add("{}:{}".format(self.filename, self.linenr)) + add("{}:{}".format(*self.loc)) return "<{}>".format(", ".join(fields)) @@ -6427,7 +6436,7 @@ def _locs(sc): if sc.nodes: return "(defined at {})".format( - ", ".join("{0.filename}:{0.linenr}".format(node) + ", ".join("{}:{}".format(*node.loc) for node in sc.nodes)) return "(undefined)" |
