reference Rule options

cpp_parsing_pref

Value
as_vardef_with_ctor or as_fundef
Default
as_vardef_with_ctor

In C++, foo bar(x); can declare a function bar that takes an x and returns a foo, or define a variable bar of type foo built with the argument x. In a function body, Opengrep reads it as a variable; elsewhere, as in a class body, as a declaration. A pattern has no such context, and this option chooses how a pattern like foo $X(...); is read:

Examples

One pattern, two readings

ctor.yaml
rules:  - id: foo-ctor    pattern: foo $X(...);    message: foo $X    languages: [cpp]    severity: INFO    options:      cpp_parsing_pref: as_vardef_with_ctor  - id: foo-fundef    pattern: foo $X(...);    message: foo $X    languages: [cpp]    severity: INFO    options:      cpp_parsing_pref: as_fundef
ctor.cpp
class Widget {  // ruleid: foo-fundef  foo bar(x);  void build() {    // ruleid: foo-ctor    foo obj(1);  }};
Command and result
$ opengrep test .
2/2: ✓ All tests passed
No tests for fixes found.