mirror of https://github.com/FRRouting/frr.git
python: improve clippy/clidef macro processing
Process macros from the current file, and warn if something is redefined (to a different value). Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
parent
0798d2760d
commit
e960eedd98
|
@ -241,24 +241,51 @@ def get_always_args(token, always_args, args=[], stack=[]):
|
||||||
|
|
||||||
|
|
||||||
class Macros(dict):
|
class Macros(dict):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self._loc = {}
|
||||||
|
|
||||||
def load(self, filename):
|
def load(self, filename):
|
||||||
filedata = clippy.parse(filename)
|
filedata = clippy.parse(filename)
|
||||||
for entry in filedata["data"]:
|
for entry in filedata["data"]:
|
||||||
if entry["type"] != "PREPROC":
|
if entry["type"] != "PREPROC":
|
||||||
continue
|
continue
|
||||||
ppdir = entry["line"].lstrip().split(None, 1)
|
self.load_preproc(filename, entry)
|
||||||
if ppdir[0] != "define" or len(ppdir) != 2:
|
|
||||||
continue
|
|
||||||
ppdef = ppdir[1].split(None, 1)
|
|
||||||
name = ppdef[0]
|
|
||||||
if "(" in name:
|
|
||||||
continue
|
|
||||||
val = ppdef[1] if len(ppdef) == 2 else ""
|
|
||||||
|
|
||||||
val = val.strip(" \t\n\\")
|
def setup(self, key, val, where="built-in"):
|
||||||
if name in self:
|
self[key] = val
|
||||||
sys.stderr.write("warning: macro %s redefined!\n" % (name))
|
self._loc[key] = (where, 0)
|
||||||
|
|
||||||
|
def load_preproc(self, filename, entry):
|
||||||
|
ppdir = entry["line"].lstrip().split(None, 1)
|
||||||
|
if ppdir[0] != "define" or len(ppdir) != 2:
|
||||||
|
return
|
||||||
|
ppdef = ppdir[1].split(None, 1)
|
||||||
|
name = ppdef[0]
|
||||||
|
if "(" in name:
|
||||||
|
return
|
||||||
|
val = ppdef[1] if len(ppdef) == 2 else ""
|
||||||
|
|
||||||
|
val = val.strip(" \t\n\\")
|
||||||
|
if self.get(name, val) != val:
|
||||||
|
sys.stderr.write(
|
||||||
|
"%s:%d: warning: macro %s redefined!\n"
|
||||||
|
% (
|
||||||
|
filename,
|
||||||
|
entry["lineno"],
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
sys.stderr.write(
|
||||||
|
"%s:%d: note: previously defined here\n"
|
||||||
|
% (
|
||||||
|
self._loc[name][0],
|
||||||
|
self._loc[name][1],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
self[name] = val
|
self[name] = val
|
||||||
|
self._loc[name] = (filename, entry["lineno"])
|
||||||
|
|
||||||
|
|
||||||
def process_file(fn, ofd, dumpfd, all_defun, macros):
|
def process_file(fn, ofd, dumpfd, all_defun, macros):
|
||||||
|
@ -283,6 +310,11 @@ def process_file(fn, ofd, dumpfd, all_defun, macros):
|
||||||
cond_stack.append(prev_line + line)
|
cond_stack.append(prev_line + line)
|
||||||
elif tokens[0] in ["endif"]:
|
elif tokens[0] in ["endif"]:
|
||||||
cond_stack.pop(-1)
|
cond_stack.pop(-1)
|
||||||
|
elif tokens[0] in ["define"]:
|
||||||
|
if not cond_stack:
|
||||||
|
macros.load_preproc(fn, entry)
|
||||||
|
elif len(cond_stack) == 1 and cond_stack[0] == "#ifdef CLIPPY\n":
|
||||||
|
macros.load_preproc(fn, entry)
|
||||||
continue
|
continue
|
||||||
if entry["type"].startswith("DEFPY") or (
|
if entry["type"].startswith("DEFPY") or (
|
||||||
all_defun and entry["type"].startswith("DEFUN")
|
all_defun and entry["type"].startswith("DEFUN")
|
||||||
|
@ -454,9 +486,9 @@ if __name__ == "__main__":
|
||||||
macros.load(os.path.join(basepath, "lib/command.h"))
|
macros.load(os.path.join(basepath, "lib/command.h"))
|
||||||
macros.load(os.path.join(basepath, "bgpd/bgp_vty.h"))
|
macros.load(os.path.join(basepath, "bgpd/bgp_vty.h"))
|
||||||
# sigh :(
|
# sigh :(
|
||||||
macros["PROTO_REDIST_STR"] = "FRR_REDIST_STR_ISISD"
|
macros.setup("PROTO_REDIST_STR", "FRR_REDIST_STR_ISISD")
|
||||||
macros["PROTO_IP_REDIST_STR"] = "FRR_IP_REDIST_STR_ISISD"
|
macros.setup("PROTO_IP_REDIST_STR", "FRR_IP_REDIST_STR_ISISD")
|
||||||
macros["PROTO_IP6_REDIST_STR"] = "FRR_IP6_REDIST_STR_ISISD"
|
macros.setup("PROTO_IP6_REDIST_STR", "FRR_IP6_REDIST_STR_ISISD")
|
||||||
|
|
||||||
errors = process_file(args.cfile, ofd, dumpfd, args.all_defun, macros)
|
errors = process_file(args.cfile, ofd, dumpfd, args.all_defun, macros)
|
||||||
if errors != 0:
|
if errors != 0:
|
||||||
|
|
Loading…
Reference in New Issue