1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0 3# Copyright(c) 2025: Mauro Carvalho Chehab <mchehab@kernel.org>. 4# 5# pylint: disable=C0301,C0302,R0904,R0912,R0913,R0914,R0915,R0917,R1702 6 7""" 8Classes and functions related to reading a C language source or header FILE 9and extract embedded documentation comments from it. 10""" 11 12import sys 13import re 14from pprint import pformat 15 16from kdoc.c_lex import CTokenizer, tokenizer_set_log 17from kdoc.kdoc_re import KernRe 18from kdoc.kdoc_item import KdocItem 19 20# 21# Regular expressions used to parse kernel-doc markups at KernelDoc class. 22# 23# Let's declare them in lowercase outside any class to make it easier to 24# convert from the Perl script. 25# 26# As those are evaluated at the beginning, no need to cache them 27# 28 29# Allow whitespace at end of comment start. 30doc_start = KernRe(r'^/\*\*\s*$', cache=False) 31 32doc_end = KernRe(r'\*/', cache=False) 33doc_com = KernRe(r'\s*\*\s*', cache=False) 34doc_com_body = KernRe(r'\s*\* ?', cache=False) 35doc_decl = doc_com + KernRe(r'(\w+)', cache=False) 36 37# @params and a strictly limited set of supported section names 38# Specifically: 39# Match @word: 40# @...: 41# @{section-name}: 42# while trying to not match literal block starts like "example::" 43# 44known_section_names = 'description|context|returns?|notes?|examples?' 45known_sections = KernRe(known_section_names, flags = re.I) 46doc_sect = doc_com + \ 47 KernRe(r'\s*(@[.\w]+|@\.\.\.|' + known_section_names + r')\s*:([^:].*)?$', 48 flags=re.I, cache=False) 49 50doc_content = doc_com_body + KernRe(r'(.*)', cache=False) 51doc_inline_start = KernRe(r'^\s*/\*\*\s*$', cache=False) 52doc_inline_sect = KernRe(r'\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)', cache=False) 53doc_inline_end = KernRe(r'^\s*\*/\s*$', cache=False) 54doc_inline_oneline = KernRe(r'^\s*/\*\*\s*(@\s*[\w][\w\.]*\s*):\s*(.*)\s*\*/\s*$', cache=False) 55 56export_symbol = KernRe(r'^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*', cache=False) 57export_symbol_ns = KernRe(r'^\s*EXPORT_SYMBOL_NS(_GPL)?\s*\(\s*(\w+)\s*,\s*"\S+"\)\s*', cache=False) 58 59type_param = KernRe(r"@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)", cache=False) 60 61# 62# Tests for the beginning of a kerneldoc block in its various forms. 63# 64doc_block = doc_com + KernRe(r'DOC:\s*(.*)?', cache=False) 65doc_begin_data = KernRe(r"^\s*\*?\s*(struct|union|enum|typedef|var)\b\s*(\w*)", cache = False) 66doc_begin_func = KernRe(str(doc_com) + # initial " * ' 67 r"(?:\w+\s*\*\s*)?" + # type (not captured) 68 r'(?:define\s+)?' + # possible "define" (not captured) 69 r'(\w+)\s*(?:\(\w*\))?\s*' + # name and optional "(...)" 70 r'(?:[-:].*)?$', # description (not captured) 71 cache = False) 72 73# 74# Ancillary functions 75# 76 77multi_space = KernRe(r'\s\s+') 78def trim_whitespace(s): 79 """ 80 A little helper to get rid of excess white space. 81 """ 82 return multi_space.sub(' ', s.strip()) 83 84def trim_private_members(text): 85 """ 86 Remove ``struct``/``enum`` members that have been marked "private". 87 """ 88 89 tokens = CTokenizer(text) 90 return str(tokens) 91 92class state: 93 """ 94 States used by the parser's state machine. 95 """ 96 97 # Parser states 98 NORMAL = 0 #: Normal code. 99 NAME = 1 #: Looking for function name. 100 DECLARATION = 2 #: We have seen a declaration which might not be done. 101 BODY = 3 #: The body of the comment. 102 SPECIAL_SECTION = 4 #: Doc section ending with a blank line. 103 PROTO = 5 #: Scanning prototype. 104 DOCBLOCK = 6 #: Documentation block. 105 INLINE_NAME = 7 #: Gathering doc outside main block. 106 INLINE_TEXT = 8 #: Reading the body of inline docs. 107 108 #: Names for each parser state. 109 name = [ 110 "NORMAL", 111 "NAME", 112 "DECLARATION", 113 "BODY", 114 "SPECIAL_SECTION", 115 "PROTO", 116 "DOCBLOCK", 117 "INLINE_NAME", 118 "INLINE_TEXT", 119 ] 120 121 122SECTION_DEFAULT = "Description" #: Default section. 123 124class KernelEntry: 125 """ 126 Encapsulates a Kernel documentation entry. 127 """ 128 129 def __init__(self, config, fname, ln): 130 self.config = config 131 self.fname = fname 132 133 self._contents = [] 134 self.prototype = "" 135 136 self.warnings = [] 137 138 self.parameterlist = [] 139 self.parameterdescs = {} 140 self.parametertypes = {} 141 self.parameterdesc_start_lines = {} 142 143 self.section_start_lines = {} 144 self.sections = {} 145 146 self.anon_struct_union = False 147 148 self.leading_space = None 149 150 self.fname = fname 151 152 # State flags 153 self.brcount = 0 154 self.declaration_start_line = ln + 1 155 156 # 157 # Management of section contents 158 # 159 def add_text(self, text): 160 """Add a new text to the entry contents list.""" 161 self._contents.append(text) 162 163 def contents(self): 164 """Returns a string with all content texts that were added.""" 165 return '\n'.join(self._contents) + '\n' 166 167 # TODO: rename to emit_message after removal of kernel-doc.pl 168 def emit_msg(self, ln, msg, *, warning=True): 169 """Emit a message.""" 170 171 log_msg = f"{self.fname}:{ln} {msg}" 172 173 if not warning: 174 self.config.log.info(log_msg) 175 return 176 177 # Delegate warning output to output logic, as this way it 178 # will report warnings/info only for symbols that are output 179 180 self.warnings.append(log_msg) 181 return 182 183 def begin_section(self, line_no, title = SECTION_DEFAULT, dump = False): 184 """ 185 Begin a new section. 186 """ 187 if dump: 188 self.dump_section(start_new = True) 189 self.section = title 190 self.new_start_line = line_no 191 192 def dump_section(self, start_new=True): 193 """ 194 Dumps section contents to arrays/hashes intended for that purpose. 195 """ 196 # 197 # If we have accumulated no contents in the default ("description") 198 # section, don't bother. 199 # 200 if self.section == SECTION_DEFAULT and not self._contents: 201 return 202 name = self.section 203 contents = self.contents() 204 205 if type_param.match(name): 206 name = type_param.group(1) 207 208 self.parameterdescs[name] = contents 209 self.parameterdesc_start_lines[name] = self.new_start_line 210 211 self.new_start_line = 0 212 213 else: 214 if name in self.sections and self.sections[name] != "": 215 # Only warn on user-specified duplicate section names 216 if name != SECTION_DEFAULT: 217 self.emit_msg(self.new_start_line, 218 f"duplicate section name '{name}'") 219 # Treat as a new paragraph - add a blank line 220 self.sections[name] += '\n' + contents 221 else: 222 self.sections[name] = contents 223 self.section_start_lines[name] = self.new_start_line 224 self.new_start_line = 0 225 226# self.config.log.debug("Section: %s : %s", name, pformat(vars(self))) 227 228 if start_new: 229 self.section = SECTION_DEFAULT 230 self._contents = [] 231 232python_warning = False 233 234class KernelDoc: 235 """ 236 Read a C language source or header FILE and extract embedded 237 documentation comments. 238 """ 239 240 #: Name of context section. 241 section_context = "Context" 242 243 #: Name of return section. 244 section_return = "Return" 245 246 #: String to write when a parameter is not described. 247 undescribed = "-- undescribed --" 248 249 def __init__(self, config, fname, xforms): 250 """Initialize internal variables""" 251 252 self.fname = fname 253 self.config = config 254 self.xforms = xforms 255 256 tokenizer_set_log(self.config.log, f"{self.fname}: CMatch: ") 257 258 # Initial state for the state machines 259 self.state = state.NORMAL 260 261 # Store entry currently being processed 262 self.entry = None 263 264 # Place all potential outputs into an array 265 self.entries = [] 266 267 # 268 # We need Python 3.7 for its "dicts remember the insertion 269 # order" guarantee 270 # 271 global python_warning 272 if (not python_warning and 273 sys.version_info.major == 3 and sys.version_info.minor < 7): 274 275 self.emit_msg(0, 276 'Python 3.7 or later is required for correct results') 277 python_warning = True 278 279 def emit_msg(self, ln, msg, *, warning=True): 280 """Emit a message""" 281 282 if self.entry: 283 self.entry.emit_msg(ln, msg, warning=warning) 284 return 285 286 log_msg = f"{self.fname}:{ln} {msg}" 287 288 if warning: 289 self.config.log.warning(log_msg) 290 else: 291 self.config.log.info(log_msg) 292 293 def dump_section(self, start_new=True): 294 """ 295 Dump section contents to arrays/hashes intended for that purpose. 296 """ 297 298 if self.entry: 299 self.entry.dump_section(start_new) 300 301 # TODO: rename it to store_declaration after removal of kernel-doc.pl 302 def output_declaration(self, dtype, name, **args): 303 """ 304 Store the entry into an entry array. 305 306 The actual output and output filters will be handled elsewhere. 307 """ 308 309 item = KdocItem(name, self.fname, dtype, 310 self.entry.declaration_start_line, **args) 311 item.warnings = self.entry.warnings 312 313 # Drop empty sections 314 # TODO: improve empty sections logic to emit warnings 315 sections = self.entry.sections 316 for section in ["Description", "Return"]: 317 if section in sections and not sections[section].rstrip(): 318 del sections[section] 319 item.set_sections(sections, self.entry.section_start_lines) 320 item.set_params(self.entry.parameterlist, self.entry.parameterdescs, 321 self.entry.parametertypes, 322 self.entry.parameterdesc_start_lines) 323 self.entries.append(item) 324 325 self.config.log.debug("Output: %s:%s = %s", dtype, name, pformat(args)) 326 327 def emit_unused_warnings(self): 328 """ 329 When the parser fails to produce a valid entry, it places some 330 warnings under `entry.warnings` that will be discarded when resetting 331 the state. 332 333 Ensure that those warnings are not lost. 334 335 .. note:: 336 337 Because we are calling `config.warning()` here, those 338 warnings are not filtered by the `-W` parameters: they will all 339 be produced even when `-Wreturn`, `-Wshort-desc`, and/or 340 `-Wcontents-before-sections` are used. 341 342 Allowing those warnings to be filtered is complex, because it 343 would require storing them in a buffer and then filtering them 344 during the output step of the code, depending on the 345 selected symbols. 346 """ 347 if self.entry and self.entry not in self.entries: 348 for log_msg in self.entry.warnings: 349 self.config.warning(log_msg) 350 351 def reset_state(self, ln): 352 """ 353 Ancillary routine to create a new entry. It initializes all 354 variables used by the state machine. 355 """ 356 357 self.emit_unused_warnings() 358 359 self.entry = KernelEntry(self.config, self.fname, ln) 360 361 # State flags 362 self.state = state.NORMAL 363 364 def push_parameter(self, ln, decl_type, param, dtype, 365 org_arg, declaration_name): 366 """ 367 Store parameters and their descriptions at self.entry. 368 """ 369 370 if self.entry.anon_struct_union and dtype == "" and param == "}": 371 return # Ignore the ending }; from anonymous struct/union 372 373 self.entry.anon_struct_union = False 374 375 param = KernRe(r'[\[\)].*').sub('', param, count=1) 376 377 # 378 # Look at various "anonymous type" cases. 379 # 380 if dtype == '': 381 if param.endswith("..."): 382 if len(param) > 3: # there is a name provided, use that 383 param = param[:-3] 384 if not self.entry.parameterdescs.get(param): 385 self.entry.parameterdescs[param] = "variable arguments" 386 387 elif (not param) or param == "void": 388 param = "void" 389 self.entry.parameterdescs[param] = "no arguments" 390 391 elif param in ["struct", "union"]: 392 # Handle unnamed (anonymous) union or struct 393 dtype = param 394 param = "{unnamed_" + param + "}" 395 self.entry.parameterdescs[param] = "anonymous\n" 396 self.entry.anon_struct_union = True 397 398 # Warn if parameter has no description 399 # (but ignore ones starting with # as these are not parameters 400 # but inline preprocessor statements) 401 if param not in self.entry.parameterdescs and not param.startswith("#"): 402 self.entry.parameterdescs[param] = self.undescribed 403 404 if "." not in param: 405 if decl_type == 'function': 406 dname = f"{decl_type} parameter" 407 else: 408 dname = f"{decl_type} member" 409 410 self.emit_msg(ln, 411 f"{dname} '{param}' not described in '{declaration_name}'") 412 413 # Strip spaces from param so that it is one continuous string on 414 # parameterlist. This fixes a problem where check_sections() 415 # cannot find a parameter like "addr[6 + 2]" because it actually 416 # appears as "addr[6", "+", "2]" on the parameter list. 417 # However, it's better to maintain the param string unchanged for 418 # output, so just weaken the string compare in check_sections() 419 # to ignore "[blah" in a parameter string. 420 421 self.entry.parameterlist.append(param) 422 org_arg = KernRe(r'\s\s+').sub(' ', org_arg) 423 self.entry.parametertypes[param] = org_arg 424 425 426 def create_parameter_list(self, ln, decl_type, args, 427 splitter, declaration_name): 428 """ 429 Creates a list of parameters, storing them at self.entry. 430 """ 431 432 # temporarily replace all commas inside function pointer definition 433 arg_expr = KernRe(r'(\([^\),]+),') 434 while arg_expr.search(args): 435 args = arg_expr.sub(r"\1#", args) 436 437 for arg in args.split(splitter): 438 # Ignore argument attributes 439 arg = KernRe(r'\sPOS0?\s').sub(' ', arg) 440 441 # Strip leading/trailing spaces 442 arg = arg.strip() 443 arg = KernRe(r'\s+').sub(' ', arg, count=1) 444 445 if arg.startswith('#'): 446 # Treat preprocessor directive as a typeless variable just to fill 447 # corresponding data structures "correctly". Catch it later in 448 # output_* subs. 449 450 # Treat preprocessor directive as a typeless variable 451 self.push_parameter(ln, decl_type, arg, "", 452 "", declaration_name) 453 # 454 # The pointer-to-function case. 455 # 456 elif KernRe(r'\(.+\)\s*\(').search(arg): 457 arg = arg.replace('#', ',') 458 r = KernRe(r'[^\(]+\(\*?\s*' # Everything up to "(*" 459 r'([\w\[\].]*)' # Capture the name and possible [array] 460 r'\s*\)') # Make sure the trailing ")" is there 461 if r.match(arg): 462 param = r.group(1) 463 else: 464 self.emit_msg(ln, f"Invalid param: {arg}") 465 param = arg 466 dtype = arg.replace(param, '') 467 self.push_parameter(ln, decl_type, param, dtype, arg, declaration_name) 468 # 469 # The array-of-pointers case. Dig the parameter name out from the middle 470 # of the declaration. 471 # 472 elif KernRe(r'\(.+\)\s*\[').search(arg): 473 r = KernRe(r'[^\(]+\(\s*\*\s*' # Up to "(" and maybe "*" 474 r'([\w.]*?)' # The actual pointer name 475 r'\s*(\[\s*\w+\s*\]\s*)*\)') # The [array portion] 476 if r.match(arg): 477 param = r.group(1) 478 else: 479 self.emit_msg(ln, f"Invalid param: {arg}") 480 param = arg 481 dtype = arg.replace(param, '') 482 self.push_parameter(ln, decl_type, param, dtype, arg, declaration_name) 483 elif arg: 484 # 485 # Clean up extraneous spaces and split the string at commas; the first 486 # element of the resulting list will also include the type information. 487 # 488 arg = KernRe(r'\s*:\s*').sub(":", arg) 489 arg = KernRe(r'\s*\[').sub('[', arg) 490 args = KernRe(r'\s*,\s*').split(arg) 491 args[0] = re.sub(r'(\*+)\s*', r' \1', args[0]) 492 # 493 # args[0] has a string of "type a". If "a" includes an [array] 494 # declaration, we want to not be fooled by any white space inside 495 # the brackets, so detect and handle that case specially. 496 # 497 r = KernRe(r'^([^[\]]*\s+)(.*)$') 498 if r.match(args[0]): 499 args[0] = r.group(2) 500 dtype = r.group(1) 501 else: 502 # No space in args[0]; this seems wrong but preserves previous behavior 503 dtype = '' 504 505 bitfield_re = KernRe(r'(.*?):(\w+)') 506 for param in args: 507 # 508 # For pointers, shift the star(s) from the variable name to the 509 # type declaration. 510 # 511 r = KernRe(r'^(\*+)\s*(.*)') 512 if r.match(param): 513 self.push_parameter(ln, decl_type, r.group(2), 514 f"{dtype} {r.group(1)}", 515 arg, declaration_name) 516 # 517 # Perform a similar shift for bitfields. 518 # 519 elif bitfield_re.search(param): 520 if dtype != "": # Skip unnamed bit-fields 521 self.push_parameter(ln, decl_type, bitfield_re.group(1), 522 f"{dtype}:{bitfield_re.group(2)}", 523 arg, declaration_name) 524 else: 525 self.push_parameter(ln, decl_type, param, dtype, 526 arg, declaration_name) 527 528 def check_sections(self, ln, decl_name, decl_type): 529 """ 530 Check for errors inside sections, emitting warnings if not found 531 parameters are described. 532 """ 533 for section in self.entry.sections: 534 if section not in self.entry.parameterlist and \ 535 not known_sections.search(section): 536 if decl_type == 'function': 537 dname = f"{decl_type} parameter" 538 else: 539 dname = f"{decl_type} member" 540 self.emit_msg(ln, 541 f"Excess {dname} '{section}' description in '{decl_name}'") 542 543 def check_return_section(self, ln, declaration_name, return_type): 544 """ 545 If the function doesn't return void, warns about the lack of a 546 return description. 547 """ 548 549 if not self.config.wreturn: 550 return 551 552 # Ignore an empty return type (It's a macro) 553 # Ignore functions with a "void" return type (but not "void *") 554 if not return_type or KernRe(r'void\s*\w*\s*$').search(return_type): 555 return 556 557 if not self.entry.sections.get("Return", None): 558 self.emit_msg(ln, 559 f"No description found for return value of '{declaration_name}'") 560 561 def split_struct_proto(self, proto): 562 """ 563 Split apart a structure prototype; returns (struct|union, name, 564 members) or ``None``. 565 """ 566 567 type_pattern = r'(struct|union)' 568 qualifiers = [ 569 "__attribute__", 570 "__packed", 571 "__aligned", 572 "____cacheline_aligned_in_smp", 573 "____cacheline_aligned", 574 ] 575 definition_body = r'\{(.*)\}\s*' + "(?:" + '|'.join(qualifiers) + ")?" 576 577 r = KernRe(type_pattern + r'\s+(\w+)\s*' + definition_body) 578 if r.search(proto): 579 return (r.group(1), r.group(2), r.group(3)) 580 else: 581 r = KernRe(r'typedef\s+' + type_pattern + r'\s*' + definition_body + r'\s*(\w+)\s*;') 582 if r.search(proto): 583 return (r.group(1), r.group(3), r.group(2)) 584 return None 585 586 def rewrite_struct_members(self, members): 587 """ 588 Process ``struct``/``union`` members from the most deeply nested 589 outward. 590 591 Rewrite the members of a ``struct`` or ``union`` for easier formatting 592 later on. Among other things, this function will turn a member like:: 593 594 struct { inner_members; } foo; 595 596 into:: 597 598 struct foo; inner_members; 599 """ 600 601 # 602 # The trick is in the ``^{`` below - it prevents a match of an outer 603 # ``struct``/``union`` until the inner one has been munged 604 # (removing the ``{`` in the process). 605 # 606 struct_members = KernRe(r'(struct|union)' # 0: declaration type 607 r'([^\{\};]+)' # 1: possible name 608 r'(\{)' 609 r'([^\{\}]*)' # 3: Contents of declaration 610 r'(\})' 611 r'([^\{\};]*)(;)') # 5: Remaining stuff after declaration 612 tuples = struct_members.findall(members) 613 while tuples: 614 for t in tuples: 615 newmember = "" 616 oldmember = "".join(t) # Reconstruct the original formatting 617 dtype, name, lbr, content, rbr, rest, semi = t 618 # 619 # Pass through each field name, normalizing the form and formatting. 620 # 621 for s_id in rest.split(','): 622 s_id = s_id.strip() 623 newmember += f"{dtype} {s_id}; " 624 # 625 # Remove bitfield/array/pointer info, getting the bare name. 626 # 627 s_id = KernRe(r'[:\[].*').sub('', s_id) 628 s_id = KernRe(r'^\s*\**(\S+)\s*').sub(r'\1', s_id) 629 # 630 # Pass through the members of this inner structure/union. 631 # 632 for arg in content.split(';'): 633 arg = arg.strip() 634 # 635 # Look for (type)(*name)(args) - pointer to function 636 # 637 r = KernRe(r'^([^\(]+\(\*?\s*)([\w.]*)(\s*\).*)') 638 if r.match(arg): 639 dtype, name, extra = r.group(1), r.group(2), r.group(3) 640 # Pointer-to-function 641 if not s_id: 642 # Anonymous struct/union 643 newmember += f"{dtype}{name}{extra}; " 644 else: 645 newmember += f"{dtype}{s_id}.{name}{extra}; " 646 # 647 # Otherwise a non-function member. 648 # 649 else: 650 # 651 # Remove bitmap and array portions and spaces around commas 652 # 653 arg = KernRe(r':\s*\d+\s*').sub('', arg) 654 arg = KernRe(r'\[.*\]').sub('', arg) 655 arg = KernRe(r'\s*,\s*').sub(',', arg) 656 # 657 # Look for a normal decl - "type name[,name...]" 658 # 659 r = KernRe(r'(.*)\s+([\S+,]+)') 660 if r.search(arg): 661 for name in r.group(2).split(','): 662 name = KernRe(r'^\s*\**(\S+)\s*').sub(r'\1', name) 663 if not s_id: 664 # Anonymous struct/union 665 newmember += f"{r.group(1)} {name}; " 666 else: 667 newmember += f"{r.group(1)} {s_id}.{name}; " 668 else: 669 newmember += f"{arg}; " 670 # 671 # At the end of the s_id loop, replace the original declaration with 672 # the munged version. 673 # 674 members = members.replace(oldmember, newmember) 675 # 676 # End of the tuple loop - search again and see if there are outer members 677 # that now turn up. 678 # 679 tuples = struct_members.findall(members) 680 return members 681 682 def format_struct_decl(self, declaration): 683 """ 684 Format the ``struct`` declaration into a standard form for inclusion 685 in the resulting docs. 686 """ 687 688 # 689 # Insert newlines, get rid of extra spaces. 690 # 691 declaration = KernRe(r'([\{;])').sub(r'\1\n', declaration) 692 declaration = KernRe(r'\}\s+;').sub('};', declaration) 693 # 694 # Format inline enums with each member on its own line. 695 # 696 r = KernRe(r'(enum\s+\{[^\}]+),([^\n])') 697 while r.search(declaration): 698 declaration = r.sub(r'\1,\n\2', declaration) 699 # 700 # Now go through and supply the right number of tabs 701 # for each line. 702 # 703 def_args = declaration.split('\n') 704 level = 1 705 declaration = "" 706 for clause in def_args: 707 clause = KernRe(r'\s+').sub(' ', clause.strip(), count=1) 708 if clause: 709 if '}' in clause and level > 1: 710 level -= 1 711 if not clause.startswith('#'): 712 declaration += "\t" * level 713 declaration += "\t" + clause + "\n" 714 if "{" in clause and "}" not in clause: 715 level += 1 716 return declaration 717 718 719 def dump_struct(self, ln, proto): 720 """ 721 Store an entry for a ``struct`` or ``union`` 722 """ 723 # 724 # Do the basic parse to get the pieces of the declaration. 725 # 726 struct_parts = self.split_struct_proto(proto) 727 if not struct_parts: 728 self.emit_msg(ln, f"{proto} error: Cannot parse struct or union!") 729 return 730 decl_type, declaration_name, members = struct_parts 731 732 if self.entry.identifier != declaration_name: 733 self.emit_msg(ln, f"expecting prototype for {decl_type} {self.entry.identifier}. " 734 f"Prototype was for {decl_type} {declaration_name} instead\n") 735 return 736 # 737 # Go through the list of members applying all of our transformations. 738 # 739 members = trim_private_members(members) 740 members = self.xforms.apply("struct", members) 741 742 # 743 # Deal with embedded struct and union members, and drop enums entirely. 744 # 745 declaration = members 746 members = self.rewrite_struct_members(members) 747 members = re.sub(r'(\{[^\{\}]*\})', '', members) 748 # 749 # Output the result and we are done. 750 # 751 self.create_parameter_list(ln, decl_type, members, ';', 752 declaration_name) 753 self.check_sections(ln, declaration_name, decl_type) 754 self.output_declaration(decl_type, declaration_name, 755 definition=self.format_struct_decl(declaration), 756 purpose=self.entry.declaration_purpose) 757 758 def dump_enum(self, ln, proto): 759 """ 760 Store an ``enum`` inside self.entries array. 761 """ 762 # 763 # Strip preprocessor directives. Note that this depends on the 764 # trailing semicolon we added in process_proto_type(). 765 # 766 proto = KernRe(r'#\s*((define|ifdef|if)\s+|endif)[^;]*;', flags=re.S).sub('', proto) 767 # 768 # Parse out the name and members of the enum. Typedef form first. 769 # 770 r = KernRe(r'typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;') 771 if r.search(proto): 772 declaration_name = r.group(2) 773 members = trim_private_members(r.group(1)) 774 # 775 # Failing that, look for a straight enum 776 # 777 else: 778 r = KernRe(r'enum\s+(\w*)\s*\{(.*)\}') 779 if r.match(proto): 780 declaration_name = r.group(1) 781 members = trim_private_members(r.group(2)) 782 # 783 # OK, this isn't going to work. 784 # 785 else: 786 self.emit_msg(ln, f"{proto}: error: Cannot parse enum!") 787 return 788 # 789 # Make sure we found what we were expecting. 790 # 791 if self.entry.identifier != declaration_name: 792 if self.entry.identifier == "": 793 self.emit_msg(ln, 794 f"{proto}: wrong kernel-doc identifier on prototype") 795 else: 796 self.emit_msg(ln, 797 f"expecting prototype for enum {self.entry.identifier}. " 798 f"Prototype was for enum {declaration_name} instead") 799 return 800 801 if not declaration_name: 802 declaration_name = "(anonymous)" 803 # 804 # Parse out the name of each enum member, and verify that we 805 # have a description for it. 806 # 807 member_set = set() 808 members = KernRe(r'\([^;)]*\)').sub('', members) 809 for arg in members.split(','): 810 arg = KernRe(r'^\s*(\w+).*').sub(r'\1', arg) 811 if not arg.strip(): 812 continue 813 814 self.entry.parameterlist.append(arg) 815 if arg not in self.entry.parameterdescs: 816 self.entry.parameterdescs[arg] = self.undescribed 817 self.emit_msg(ln, 818 f"Enum value '{arg}' not described in enum '{declaration_name}'") 819 member_set.add(arg) 820 # 821 # Ensure that every described member actually exists in the enum. 822 # 823 for k in self.entry.parameterdescs: 824 if k not in member_set: 825 self.emit_msg(ln, 826 f"Excess enum value '@{k}' description in '{declaration_name}'") 827 828 self.output_declaration('enum', declaration_name, 829 purpose=self.entry.declaration_purpose) 830 831 def dump_var(self, ln, proto): 832 """ 833 Store variables that are part of kAPI. 834 """ 835 VAR_ATTRIBS = [ 836 "extern", 837 "const", 838 ] 839 OPTIONAL_VAR_ATTR = r"^(?:\b(?:" +"|".join(VAR_ATTRIBS) +r")\b\s*)*" 840 841 # 842 # Store the full prototype before modifying it 843 # 844 full_proto = proto 845 declaration_name = None 846 847 # 848 # Handle macro definitions 849 # 850 macro_prefixes = [ 851 KernRe(r"DEFINE_[\w_]+\s*\(([\w_]+)\)"), 852 ] 853 854 for r in macro_prefixes: 855 match = r.search(proto) 856 if match: 857 declaration_name = match.group(1) 858 break 859 860 # 861 # Drop comments and macros to have a pure C prototype 862 # 863 if not declaration_name: 864 proto = self.xforms.apply("var", proto) 865 866 proto = proto.rstrip() 867 868 # 869 # Variable name is at the end of the declaration 870 # 871 872 default_val = None 873 874 r= KernRe(OPTIONAL_VAR_ATTR + r"\s*[\w_\s]*\s+(?:\*+)?([\w_]+)\s*[\d\]\[]*\s*(=.*)?") 875 if r.match(proto): 876 if not declaration_name: 877 declaration_name = r.group(1) 878 879 default_val = r.group(2) 880 else: 881 r= KernRe(OPTIONAL_VAR_ATTR + r"(?:[\w_\s]*)?\s+(?:\*+)?(?:[\w_]+)\s*[\d\]\[]*\s*(=.*)?") 882 883 if r.match(proto): 884 default_val = r.group(1) 885 if not declaration_name: 886 self.emit_msg(ln,f"{proto}: can't parse variable") 887 return 888 889 if default_val: 890 default_val = default_val.lstrip("=").strip() 891 892 self.output_declaration("var", declaration_name, 893 full_proto=full_proto, 894 default_val=default_val, 895 purpose=self.entry.declaration_purpose) 896 897 def dump_declaration(self, ln, prototype): 898 """ 899 Store a data declaration inside self.entries array. 900 """ 901 902 if self.entry.decl_type == "enum": 903 self.dump_enum(ln, prototype) 904 elif self.entry.decl_type == "typedef": 905 self.dump_typedef(ln, prototype) 906 elif self.entry.decl_type in ["union", "struct"]: 907 self.dump_struct(ln, prototype) 908 elif self.entry.decl_type == "var": 909 self.dump_var(ln, prototype) 910 else: 911 # This would be a bug 912 self.emit_message(ln, f'Unknown declaration type: {self.entry.decl_type}') 913 914 def dump_function(self, ln, prototype): 915 """ 916 Store a function or function macro inside self.entries array. 917 """ 918 919 found = func_macro = False 920 return_type = '' 921 decl_type = 'function' 922 923 # 924 # If we have a macro, remove the "#define" at the front. 925 # 926 new_proto = KernRe(r"^#\s*define\s+").sub("", prototype) 927 if new_proto != prototype: 928 prototype = new_proto 929 # 930 # Dispense with the simple "#define A B" case here; the key 931 # is the space after the name of the symbol being defined. 932 # NOTE that the seemingly misnamed "func_macro" indicates a 933 # macro *without* arguments. 934 # 935 r = KernRe(r'^(\w+)\s+') 936 if r.search(prototype): 937 return_type = '' 938 declaration_name = r.group(1) 939 func_macro = True 940 found = True 941 else: 942 # 943 # Apply the initial transformations. 944 # 945 prototype = self.xforms.apply("func", prototype) 946 947 # Yes, this truly is vile. We are looking for: 948 # 1. Return type (may be nothing if we're looking at a macro) 949 # 2. Function name 950 # 3. Function parameters. 951 # 952 # All the while we have to watch out for function pointer parameters 953 # (which IIRC is what the two sections are for), C types (these 954 # regexps don't even start to express all the possibilities), and 955 # so on. 956 # 957 # If you mess with these regexps, it's a good idea to check that 958 # the following functions' documentation still comes out right: 959 # - parport_register_device (function pointer parameters) 960 # - atomic_set (macro) 961 # - pci_match_device, __copy_to_user (long return type) 962 963 name = r'\w+' 964 type1 = r'(?:[\w\s]+)?' 965 type2 = r'(?:[\w\s]+\*+)+' 966 # 967 # Attempt to match first on (args) with no internal parentheses; this 968 # lets us easily filter out __acquires() and other post-args stuff. If 969 # that fails, just grab the rest of the line to the last closing 970 # parenthesis. 971 # 972 proto_args = r'\(([^\(]*|.*)\)' 973 # 974 # (Except for the simple macro case) attempt to split up the prototype 975 # in the various ways we understand. 976 # 977 if not found: 978 patterns = [ 979 rf'^()({name})\s*{proto_args}', 980 rf'^({type1})\s+({name})\s*{proto_args}', 981 rf'^({type2})\s*({name})\s*{proto_args}', 982 ] 983 984 for p in patterns: 985 r = KernRe(p) 986 if r.match(prototype): 987 return_type = r.group(1) 988 declaration_name = r.group(2) 989 args = r.group(3) 990 self.create_parameter_list(ln, decl_type, args, ',', 991 declaration_name) 992 found = True 993 break 994 # 995 # Parsing done; make sure that things are as we expect. 996 # 997 if not found: 998 self.emit_msg(ln, 999 f"cannot understand function prototype: '{prototype}'") 1000 return 1001 if self.entry.identifier != declaration_name: 1002 self.emit_msg(ln, f"expecting prototype for {self.entry.identifier}(). " 1003 f"Prototype was for {declaration_name}() instead") 1004 return 1005 self.check_sections(ln, declaration_name, "function") 1006 self.check_return_section(ln, declaration_name, return_type) 1007 # 1008 # Store the result. 1009 # 1010 self.output_declaration(decl_type, declaration_name, 1011 typedef=('typedef' in return_type), 1012 functiontype=return_type, 1013 purpose=self.entry.declaration_purpose, 1014 func_macro=func_macro) 1015 1016 1017 def dump_typedef(self, ln, proto): 1018 """ 1019 Store a ``typedef`` inside self.entries array. 1020 """ 1021 # 1022 # We start by looking for function typedefs. 1023 # 1024 typedef_type = r'typedef((?:\s+[\w*]+\b){0,7}\s+(?:\w+\b|\*+))\s*' 1025 typedef_ident = r'\*?\s*(\w\S+)\s*' 1026 typedef_args = r'\s*\((.*)\);' 1027 1028 typedef1 = KernRe(typedef_type + r'\(' + typedef_ident + r'\)' + typedef_args) 1029 typedef2 = KernRe(typedef_type + typedef_ident + typedef_args) 1030 1031 # Parse function typedef prototypes 1032 for r in [typedef1, typedef2]: 1033 if not r.match(proto): 1034 continue 1035 1036 return_type = r.group(1).strip() 1037 declaration_name = r.group(2) 1038 args = r.group(3) 1039 1040 if self.entry.identifier != declaration_name: 1041 self.emit_msg(ln, 1042 f"expecting prototype for typedef {self.entry.identifier}. Prototype was for typedef {declaration_name} instead\n") 1043 return 1044 1045 self.create_parameter_list(ln, 'function', args, ',', declaration_name) 1046 1047 self.output_declaration('function', declaration_name, 1048 typedef=True, 1049 functiontype=return_type, 1050 purpose=self.entry.declaration_purpose) 1051 return 1052 # 1053 # Not a function, try to parse a simple typedef. 1054 # 1055 r = KernRe(r'typedef.*\s+(\w+)\s*;') 1056 if r.match(proto): 1057 declaration_name = r.group(1) 1058 1059 if self.entry.identifier != declaration_name: 1060 self.emit_msg(ln, 1061 f"expecting prototype for typedef {self.entry.identifier}. Prototype was for typedef {declaration_name} instead\n") 1062 return 1063 1064 self.output_declaration('typedef', declaration_name, 1065 purpose=self.entry.declaration_purpose) 1066 return 1067 1068 self.emit_msg(ln, "error: Cannot parse typedef!") 1069 1070 @staticmethod 1071 def process_export(function_set, line): 1072 """ 1073 process ``EXPORT_SYMBOL*`` tags 1074 1075 This method doesn't use any variable from the class, so declare it 1076 with a staticmethod decorator. 1077 """ 1078 1079 # We support documenting some exported symbols with different 1080 # names. A horrible hack. 1081 suffixes = [ '_noprof' ] 1082 1083 # Note: it accepts only one EXPORT_SYMBOL* per line, as having 1084 # multiple export lines would violate Kernel coding style. 1085 1086 if export_symbol.search(line): 1087 symbol = export_symbol.group(2) 1088 elif export_symbol_ns.search(line): 1089 symbol = export_symbol_ns.group(2) 1090 else: 1091 return False 1092 # 1093 # Found an export, trim out any special suffixes 1094 # 1095 for suffix in suffixes: 1096 # Be backward compatible with Python < 3.9 1097 if symbol.endswith(suffix): 1098 symbol = symbol[:-len(suffix)] 1099 function_set.add(symbol) 1100 return True 1101 1102 def process_normal(self, ln, line): 1103 """ 1104 STATE_NORMAL: looking for the ``/**`` to begin everything. 1105 """ 1106 1107 if not doc_start.match(line): 1108 return 1109 1110 # start a new entry 1111 self.reset_state(ln) 1112 1113 # next line is always the function name 1114 self.state = state.NAME 1115 1116 def process_name(self, ln, line): 1117 """ 1118 STATE_NAME: Looking for the "name - description" line 1119 """ 1120 # 1121 # Check for a DOC: block and handle them specially. 1122 # 1123 if doc_block.search(line): 1124 1125 if not doc_block.group(1): 1126 self.entry.begin_section(ln, "Introduction") 1127 else: 1128 self.entry.begin_section(ln, doc_block.group(1)) 1129 1130 self.entry.identifier = self.entry.section 1131 self.state = state.DOCBLOCK 1132 # 1133 # Otherwise we're looking for a normal kerneldoc declaration line. 1134 # 1135 elif doc_decl.search(line): 1136 self.entry.identifier = doc_decl.group(1) 1137 1138 # Test for data declaration 1139 if doc_begin_data.search(line): 1140 self.entry.decl_type = doc_begin_data.group(1) 1141 self.entry.identifier = doc_begin_data.group(2) 1142 # 1143 # Look for a function description 1144 # 1145 elif doc_begin_func.search(line): 1146 self.entry.identifier = doc_begin_func.group(1) 1147 self.entry.decl_type = "function" 1148 # 1149 # We struck out. 1150 # 1151 else: 1152 self.emit_msg(ln, 1153 f"This comment starts with '/**', but isn't a kernel-doc comment. Refer to Documentation/doc-guide/kernel-doc.rst\n{line}") 1154 self.state = state.NORMAL 1155 return 1156 # 1157 # OK, set up for a new kerneldoc entry. 1158 # 1159 self.state = state.BODY 1160 self.entry.identifier = self.entry.identifier.strip(" ") 1161 # if there's no @param blocks need to set up default section here 1162 self.entry.begin_section(ln + 1) 1163 # 1164 # Find the description portion, which *should* be there but 1165 # isn't always. 1166 # (We should be able to capture this from the previous parsing - someday) 1167 # 1168 r = KernRe("[-:](.*)") 1169 if r.search(line): 1170 self.entry.declaration_purpose = trim_whitespace(r.group(1)) 1171 self.state = state.DECLARATION 1172 else: 1173 self.entry.declaration_purpose = "" 1174 1175 if not self.entry.declaration_purpose and self.config.wshort_desc: 1176 self.emit_msg(ln, 1177 f"missing initial short description on line:\n{line}") 1178 1179 if not self.entry.identifier and self.entry.decl_type != "enum": 1180 self.emit_msg(ln, 1181 f"wrong kernel-doc identifier on line:\n{line}") 1182 self.state = state.NORMAL 1183 1184 if self.config.verbose: 1185 self.emit_msg(ln, 1186 f"Scanning doc for {self.entry.decl_type} {self.entry.identifier}", 1187 warning=False) 1188 # 1189 # Failed to find an identifier. Emit a warning 1190 # 1191 else: 1192 self.emit_msg(ln, f"Cannot find identifier on line:\n{line}") 1193 1194 def is_new_section(self, ln, line): 1195 """ 1196 Helper function to determine if a new section is being started. 1197 """ 1198 if doc_sect.search(line): 1199 self.state = state.BODY 1200 # 1201 # Pick out the name of our new section, tweaking it if need be. 1202 # 1203 newsection = doc_sect.group(1) 1204 if newsection.lower() == 'description': 1205 newsection = 'Description' 1206 elif newsection.lower() == 'context': 1207 newsection = 'Context' 1208 self.state = state.SPECIAL_SECTION 1209 elif newsection.lower() in ["@return", "@returns", 1210 "return", "returns"]: 1211 newsection = "Return" 1212 self.state = state.SPECIAL_SECTION 1213 elif newsection[0] == '@': 1214 self.state = state.SPECIAL_SECTION 1215 # 1216 # Initialize the contents, and get the new section going. 1217 # 1218 newcontents = doc_sect.group(2) 1219 if not newcontents: 1220 newcontents = "" 1221 self.dump_section() 1222 self.entry.begin_section(ln, newsection) 1223 self.entry.leading_space = None 1224 1225 self.entry.add_text(newcontents.lstrip()) 1226 return True 1227 return False 1228 1229 def is_comment_end(self, ln, line): 1230 """ 1231 Helper function to detect (and effect) the end of a kerneldoc comment. 1232 """ 1233 if doc_end.search(line): 1234 self.dump_section() 1235 1236 # Look for doc_com + <text> + doc_end: 1237 r = KernRe(r'\s*\*\s*[a-zA-Z_0-9:.]+\*/') 1238 if r.match(line): 1239 self.emit_msg(ln, f"suspicious ending line: {line}") 1240 1241 self.entry.prototype = "" 1242 self.entry.new_start_line = ln + 1 1243 1244 self.state = state.PROTO 1245 return True 1246 return False 1247 1248 1249 def process_decl(self, ln, line): 1250 """ 1251 STATE_DECLARATION: We've seen the beginning of a declaration. 1252 """ 1253 if self.is_new_section(ln, line) or self.is_comment_end(ln, line): 1254 return 1255 # 1256 # Look for anything with the " * " line beginning. 1257 # 1258 if doc_content.search(line): 1259 cont = doc_content.group(1) 1260 # 1261 # A blank line means that we have moved out of the declaration 1262 # part of the comment (without any "special section" parameter 1263 # descriptions). 1264 # 1265 if cont == "": 1266 self.state = state.BODY 1267 # 1268 # Otherwise we have more of the declaration section to soak up. 1269 # 1270 else: 1271 self.entry.declaration_purpose = \ 1272 trim_whitespace(self.entry.declaration_purpose + ' ' + cont) 1273 else: 1274 # Unknown line, ignore 1275 self.emit_msg(ln, f"bad line: {line}") 1276 1277 1278 def process_special(self, ln, line): 1279 """ 1280 STATE_SPECIAL_SECTION: a section ending with a blank line. 1281 """ 1282 # 1283 # If we have hit a blank line (only the " * " marker), then this 1284 # section is done. 1285 # 1286 if KernRe(r"\s*\*\s*$").match(line): 1287 self.entry.begin_section(ln, dump = True) 1288 self.state = state.BODY 1289 return 1290 # 1291 # Not a blank line, look for the other ways to end the section. 1292 # 1293 if self.is_new_section(ln, line) or self.is_comment_end(ln, line): 1294 return 1295 # 1296 # OK, we should have a continuation of the text for this section. 1297 # 1298 if doc_content.search(line): 1299 cont = doc_content.group(1) 1300 # 1301 # If the lines of text after the first in a special section have 1302 # leading white space, we need to trim it out or Sphinx will get 1303 # confused. For the second line (the None case), see what we 1304 # find there and remember it. 1305 # 1306 if self.entry.leading_space is None: 1307 r = KernRe(r'^(\s+)') 1308 if r.match(cont): 1309 self.entry.leading_space = len(r.group(1)) 1310 else: 1311 self.entry.leading_space = 0 1312 # 1313 # Otherwise, before trimming any leading chars, be *sure* 1314 # that they are white space. We should maybe warn if this 1315 # isn't the case. 1316 # 1317 for i in range(0, self.entry.leading_space): 1318 if cont[i] != " ": 1319 self.entry.leading_space = i 1320 break 1321 # 1322 # Add the trimmed result to the section and we're done. 1323 # 1324 self.entry.add_text(cont[self.entry.leading_space:]) 1325 else: 1326 # Unknown line, ignore 1327 self.emit_msg(ln, f"bad line: {line}") 1328 1329 def process_body(self, ln, line): 1330 """ 1331 STATE_BODY: the bulk of a kerneldoc comment. 1332 """ 1333 if self.is_new_section(ln, line) or self.is_comment_end(ln, line): 1334 return 1335 1336 if doc_content.search(line): 1337 cont = doc_content.group(1) 1338 self.entry.add_text(cont) 1339 else: 1340 # Unknown line, ignore 1341 self.emit_msg(ln, f"bad line: {line}") 1342 1343 def process_inline_name(self, ln, line): 1344 """STATE_INLINE_NAME: beginning of docbook comments within a prototype.""" 1345 1346 if doc_inline_sect.search(line): 1347 self.entry.begin_section(ln, doc_inline_sect.group(1)) 1348 self.entry.add_text(doc_inline_sect.group(2).lstrip()) 1349 self.state = state.INLINE_TEXT 1350 elif doc_inline_end.search(line): 1351 self.dump_section() 1352 self.state = state.PROTO 1353 elif doc_content.search(line): 1354 self.emit_msg(ln, f"Incorrect use of kernel-doc format: {line}") 1355 self.state = state.PROTO 1356 1357 # 1358 # Don't let it add partial comments at the code, as breaks the 1359 # logic meant to remove comments from prototypes. 1360 # 1361 self.process_proto_type(ln, "/**\n" + line) 1362 # else ... ?? 1363 1364 def process_inline_text(self, ln, line): 1365 """STATE_INLINE_TEXT: docbook comments within a prototype.""" 1366 1367 if doc_inline_end.search(line): 1368 self.dump_section() 1369 self.state = state.PROTO 1370 elif doc_content.search(line): 1371 self.entry.add_text(doc_content.group(1)) 1372 # else ... ?? 1373 1374 def syscall_munge(self, ln, proto): # pylint: disable=W0613 1375 """ 1376 Handle syscall definitions. 1377 """ 1378 1379 is_void = False 1380 1381 # Strip newlines/CR's 1382 proto = re.sub(r'[\r\n]+', ' ', proto) 1383 1384 # Check if it's a SYSCALL_DEFINE0 1385 if 'SYSCALL_DEFINE0' in proto: 1386 is_void = True 1387 1388 # Replace SYSCALL_DEFINE with correct return type & function name 1389 proto = KernRe(r'SYSCALL_DEFINE.*\(').sub('long sys_', proto) 1390 1391 r = KernRe(r'long\s+(sys_.*?),') 1392 if r.search(proto): 1393 proto = KernRe(',').sub('(', proto, count=1) 1394 elif is_void: 1395 proto = KernRe(r'\)').sub('(void)', proto, count=1) 1396 1397 # Now delete all of the odd-numbered commas in the proto 1398 # so that argument types & names don't have a comma between them 1399 count = 0 1400 length = len(proto) 1401 1402 if is_void: 1403 length = 0 # skip the loop if is_void 1404 1405 for ix in range(length): 1406 if proto[ix] == ',': 1407 count += 1 1408 if count % 2 == 1: 1409 proto = proto[:ix] + ' ' + proto[ix + 1:] 1410 1411 return proto 1412 1413 def tracepoint_munge(self, ln, proto): 1414 """ 1415 Handle tracepoint definitions. 1416 """ 1417 1418 tracepointname = None 1419 tracepointargs = None 1420 1421 # Match tracepoint name based on different patterns 1422 r = KernRe(r'TRACE_EVENT\((.*?),') 1423 if r.search(proto): 1424 tracepointname = r.group(1) 1425 1426 r = KernRe(r'DEFINE_SINGLE_EVENT\((.*?),') 1427 if r.search(proto): 1428 tracepointname = r.group(1) 1429 1430 r = KernRe(r'DEFINE_EVENT\((.*?),(.*?),') 1431 if r.search(proto): 1432 tracepointname = r.group(2) 1433 1434 if tracepointname: 1435 tracepointname = tracepointname.lstrip() 1436 1437 r = KernRe(r'TP_PROTO\((.*?)\)') 1438 if r.search(proto): 1439 tracepointargs = r.group(1) 1440 1441 if not tracepointname or not tracepointargs: 1442 self.emit_msg(ln, 1443 f"Unrecognized tracepoint format:\n{proto}\n") 1444 else: 1445 proto = f"static inline void trace_{tracepointname}({tracepointargs})" 1446 self.entry.identifier = f"trace_{self.entry.identifier}" 1447 1448 return proto 1449 1450 def process_proto_function(self, ln, line): 1451 """Ancillary routine to process a function prototype.""" 1452 1453 # strip C99-style comments to end of line 1454 line = KernRe(r"//.*$", re.S).sub('', line) 1455 # 1456 # Soak up the line's worth of prototype text, stopping at { or ; if present. 1457 # 1458 if KernRe(r'\s*#\s*define').match(line): 1459 self.entry.prototype = line 1460 elif not line.startswith('#'): # skip other preprocessor stuff 1461 r = KernRe(r'([^\{]*)') 1462 if r.match(line): 1463 self.entry.prototype += r.group(1) + " " 1464 # 1465 # If we now have the whole prototype, clean it up and declare victory. 1466 # 1467 if '{' in line or ';' in line or KernRe(r'\s*#\s*define').match(line): 1468 # strip comments and surrounding spaces 1469 self.entry.prototype = KernRe(r'/\*.*\*/').sub('', self.entry.prototype).strip() 1470 # 1471 # Handle self.entry.prototypes for function pointers like: 1472 # int (*pcs_config)(struct foo) 1473 # by turning it into 1474 # int pcs_config(struct foo) 1475 # 1476 r = KernRe(r'^(\S+\s+)\(\s*\*(\S+)\)') 1477 self.entry.prototype = r.sub(r'\1\2', self.entry.prototype) 1478 # 1479 # Handle special declaration syntaxes 1480 # 1481 if 'SYSCALL_DEFINE' in self.entry.prototype: 1482 self.entry.prototype = self.syscall_munge(ln, 1483 self.entry.prototype) 1484 else: 1485 r = KernRe(r'TRACE_EVENT|DEFINE_EVENT|DEFINE_SINGLE_EVENT') 1486 if r.search(self.entry.prototype): 1487 self.entry.prototype = self.tracepoint_munge(ln, 1488 self.entry.prototype) 1489 # 1490 # ... and we're done 1491 # 1492 self.dump_function(ln, self.entry.prototype) 1493 self.reset_state(ln) 1494 1495 def process_proto_type(self, ln, line): 1496 """ 1497 Ancillary routine to process a type. 1498 """ 1499 1500 # Strip C99-style comments and surrounding whitespace 1501 line = KernRe(r"//.*$", re.S).sub('', line).strip() 1502 if not line: 1503 return # nothing to see here 1504 1505 # To distinguish preprocessor directive from regular declaration later. 1506 if line.startswith('#'): 1507 line += ";" 1508 # 1509 # Split the declaration on any of { } or ;, and accumulate pieces 1510 # until we hit a semicolon while not inside {brackets} 1511 # 1512 r = KernRe(r'(.*?)([{};])') 1513 for chunk in r.split(line): 1514 if chunk: # Ignore empty matches 1515 self.entry.prototype += chunk 1516 # 1517 # This cries out for a match statement ... someday after we can 1518 # drop Python 3.9 ... 1519 # 1520 if chunk == '{': 1521 self.entry.brcount += 1 1522 elif chunk == '}': 1523 self.entry.brcount -= 1 1524 elif chunk == ';' and self.entry.brcount <= 0: 1525 self.dump_declaration(ln, self.entry.prototype) 1526 self.reset_state(ln) 1527 return 1528 # 1529 # We hit the end of the line while still in the declaration; put 1530 # in a space to represent the newline. 1531 # 1532 self.entry.prototype += ' ' 1533 1534 def process_proto(self, ln, line): 1535 """STATE_PROTO: reading a function/whatever prototype.""" 1536 1537 if doc_inline_oneline.search(line): 1538 self.entry.begin_section(ln, doc_inline_oneline.group(1)) 1539 self.entry.add_text(doc_inline_oneline.group(2)) 1540 self.dump_section() 1541 1542 elif doc_inline_start.search(line): 1543 self.state = state.INLINE_NAME 1544 1545 elif self.entry.decl_type == 'function': 1546 self.process_proto_function(ln, line) 1547 1548 else: 1549 self.process_proto_type(ln, line) 1550 1551 def process_docblock(self, ln, line): 1552 """STATE_DOCBLOCK: within a ``DOC:`` block.""" 1553 1554 if doc_end.search(line): 1555 self.dump_section() 1556 self.output_declaration("doc", self.entry.identifier) 1557 self.reset_state(ln) 1558 1559 elif doc_content.search(line): 1560 self.entry.add_text(doc_content.group(1)) 1561 1562 def parse_export(self): 1563 """ 1564 Parses ``EXPORT_SYMBOL*`` macros from a single Kernel source file. 1565 """ 1566 1567 export_table = set() 1568 1569 try: 1570 with open(self.fname, "r", encoding="utf8", 1571 errors="backslashreplace") as fp: 1572 1573 for line in fp: 1574 self.process_export(export_table, line) 1575 1576 except IOError: 1577 return None 1578 1579 return export_table 1580 1581 #: The state/action table telling us which function to invoke in each state. 1582 state_actions = { 1583 state.NORMAL: process_normal, 1584 state.NAME: process_name, 1585 state.BODY: process_body, 1586 state.DECLARATION: process_decl, 1587 state.SPECIAL_SECTION: process_special, 1588 state.INLINE_NAME: process_inline_name, 1589 state.INLINE_TEXT: process_inline_text, 1590 state.PROTO: process_proto, 1591 state.DOCBLOCK: process_docblock, 1592 } 1593 1594 def parse_kdoc(self): 1595 """ 1596 Open and process each line of a C source file. 1597 The parsing is controlled via a state machine, and the line is passed 1598 to a different process function depending on the state. The process 1599 function may update the state as needed. 1600 1601 Besides parsing kernel-doc tags, it also parses export symbols. 1602 """ 1603 1604 prev = "" 1605 prev_ln = None 1606 export_table = set() 1607 1608 try: 1609 with open(self.fname, "r", encoding="utf8", 1610 errors="backslashreplace") as fp: 1611 for ln, line in enumerate(fp): 1612 1613 line = line.expandtabs().strip("\n") 1614 1615 # Group continuation lines on prototypes 1616 if self.state == state.PROTO: 1617 if line.endswith("\\"): 1618 prev += line.rstrip("\\") 1619 if not prev_ln: 1620 prev_ln = ln 1621 continue 1622 1623 if prev: 1624 ln = prev_ln 1625 line = prev + line 1626 prev = "" 1627 prev_ln = None 1628 1629 self.config.log.debug("%d %s: %s", 1630 ln, state.name[self.state], 1631 line) 1632 1633 # This is an optimization over the original script. 1634 # There, when export_file was used for the same file, 1635 # it was read twice. Here, we use the already-existing 1636 # loop to parse exported symbols as well. 1637 # 1638 if (self.state != state.NORMAL) or \ 1639 not self.process_export(export_table, line): 1640 # Hand this line to the appropriate state handler 1641 self.state_actions[self.state](self, ln, line) 1642 1643 self.emit_unused_warnings() 1644 1645 except OSError: 1646 self.config.log.error(f"Error: Cannot open file {self.fname}") 1647 1648 return export_table, self.entries 1649