Recognize only a whole-file #ifndef NAME / #define NAME wrapper.
(node, source: bytes)
| 5221 | |
| 5222 | |
| 5223 | def _cpp_is_header_guard(node, source: bytes) -> bool: |
| 5224 | """Recognize only a whole-file #ifndef NAME / #define NAME wrapper.""" |
| 5225 | if (node.type != "preproc_ifdef" or node.parent is None |
| 5226 | or node.parent.type != "translation_unit" or node.children[0].type != "#ifndef"): |
| 5227 | return False |
| 5228 | siblings = [n for n in node.parent.named_children if n.type != "comment"] |
| 5229 | contents = [n for n in node.named_children if n.type != "comment"] |
| 5230 | if (siblings != [node] or len(contents) < 2 or contents[1].type != "preproc_def" |
| 5231 | or any(n.type in {"preproc_else", "preproc_elif", "preproc_elifdef"} for n in contents)): |
| 5232 | return False |
| 5233 | name = node.child_by_field_name("name") |
| 5234 | define = contents[1].child_by_field_name("name") |
| 5235 | if name is None or define is None or _read_text(name, source) != _read_text(define, source): |
| 5236 | return False |
| 5237 | return not any(line.replace(" ", "").replace("\t", "").startswith("#undef") |
| 5238 | for line in _read_text(node, source).splitlines()) |
| 5239 | |
| 5240 | |
| 5241 | def _cpp_lookup_scope(node, source: bytes) -> str: |
no test coverage detected