Skip to content

Commit

Permalink
catalog: add Cpp struct inheritance example
Browse files Browse the repository at this point in the history
fix #675
  • Loading branch information
HerringtonDarkholme committed Feb 17, 2025
1 parent a141039 commit b382cfe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions website/catalog/cpp/find-struct-inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Find Struct Inheritance

* [Playground Link](/playground.html#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoiY3BwIiwicXVlcnkiOiJzdHJ1Y3QgJFNPTUVUSElORzogICRJTkhFUklUU19GUk9NIHsgJCQkQk9EWTsgfSIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoic21hcnQiLCJzZWxlY3RvciI6IiIsImNvbmZpZyI6IiIsInNvdXJjZSI6InN0cnVjdCBGb286IEJhciB7fTtcblxuc3RydWN0IEJhcjogQmF6IHtcbiAgaW50IGEsIGI7XG59In0=)

### Description

ast-grep's pattern is AST based. A code snippet like `struct $SOMETHING: $INHERITS` will not work because it does not have a correct AST structure. The correct pattern should spell out the full syntax like `struct $SOMETHING: $INHERITS { $$$BODY; }`.

Compare the ast structure below to see the difference, especially the `ERROR` node. You can also use the playground's pattern panel to debug.

:::code-group
```shell [Wrong Pattern]
ERROR
$SOMETHING
base_class_clause
$INHERITS
```

```shell [Correct Pattern]
struct_specifier
$SOMETHING
base_class_clause
$INHERITS
field_declaration_list
field_declaration
$$$BODY
```
:::

If it is not possible to write a full pattern, [YAML rule](/guide/rule-config.html) is a better choice.


### Pattern
```shell
ast-grep --lang cpp --pattern '
struct $SOMETHING: $INHERITS { $$$BODY; }'
```

### Example

<!-- highlight matched code in curly-brace {lineNum} -->
```cpp {1-3}
struct Bar: Baz {
int a, b;
}
```
### Contributed by
Inspired by this [tweet](https://x.com/techno_bog/status/1885421768384331871)
1 change: 1 addition & 0 deletions website/catalog/cpp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Cpp is a superset of C, so you can reuse Cpp rules with C code. The [`languageGl
:::

<!--@include: ./fix-format-vuln.md-->
<!--@include: ./find-struct-inheritance.md-->

0 comments on commit b382cfe

Please sign in to comment.