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