Cpp.Function Class
Namespace: Cpp
Superclasses: ObjectWithPosition
Description
Function class represents the various types of functions in your code.
This class inherits from the class ObjectWithPosition. You can use the predicates associated with this class and
the base class with objects of this object.
Predicates
You can report a defect on Raisable types. If a type is
Printable, it can be reported in the message. For
Function objects, print the string obtained by the predicate
Cpp.Function.name.
| Type | Raisable | Printable |
|---|---|---|
Function
| Yes | No |
Lang.Int
| No | Yes |
Lang.String
| No | Yes |
This class defines these predicates that act on the Function
objects. In addition, objects of Function class can access the
predicates defined by the base class ObjectWithPosition. An object of Function class is an
object of ObjectWithPosition class.
| Predicates | Description | Example |
|---|---|---|
is(Function &function)
| Retrieves all Function objects in your C/C++ code and
stores them in function. |
This rule flags all rule is = {
defect Testis =
when Cpp.Function.is(&Function)
raise "Function detected"
on Function
} |
name(Function self, Lang.String &name)
| Retrieves the name of the function self. The
namespace qualifier and trailing parentheses are not included in the name.
The retrieved name is stored in name. |
This rule flags all rule name = {
defect Testname =
when Cpp.Function.is(&Function)
and Function.name(&fname)
raise "Function detected: \"{fname}\""
on Function
} |
qualifiedName(Function self, Lang.String
&name)
| Retrieves the name of the function self including the
namespace. The retrieved qualified name is stored in
name. |
This rule flags all rule qualifiedName = {
defect TestqualifiedName =
when Cpp.Function.is(&Function)
and Function.qualifiedName(&qname)
raise "Function detected: \"{qname}\""
on Function
} |
isMain(Function self)
| Retrieves the function with the name main. |
This rule flags the rule isMain = {
defect TestisMain =
when Cpp.Function.is(&Function)
and Function.isMain()
raise "Main Function detected"
on Function
} |
isInline(Function self)
| Retrieves the functions that are specified as
inline |
This rule flags the inlined functions: rule isInline = {
defect TestisInline =
when Cpp.Function.is(&Function)
and Function.isInline()
and Function.name(&fname)
raise "Function detected: \"{fname}\" is marked as inline"
on Function
} |
isFileStatic(Function self)
| Retrieves the function that are specified as file static using the
specifier static or declared in anonymous
namespace. |
This rule flags the functions that are in anonymous namespaces or declared as file static: rule isFileStatic = {
defect TestisFileStatic =
when Cpp.Function.is(&Function)
and Function.isFileStatic()
and Function.name(&fname)
raise "File Static Function detected: \"{fname}\""
on Function
} |
isCompilerGenerated(Function self)
| Retrieves compiler generated functions such as special member function of classes and structures. Explicitly defaulted functions are not considered compiler generated. |
This rule flags the compiler generated funcstions rule isCompilerGenerated = {
defect TestisCompilerGenerated =
when Cpp.Function.is(&Function)
and Function.isCompilerGenerated()
and Function.name(&fname)
raise "Compiler Generated Function detected: \"{fname}\""
on Function
} |
isDefaulted(Function self)
| Retrieves the functions that are set to
=default. |
This rule flags the rule isDefaulted = {
defect TestisDefaulted =
when Cpp.Function.is(&Function)
and Function.isDefaulted()
raise "Defaulted Function detected"
on Function
} |
isMemberFunction(Function self, Cpp.Type.Type
&class)
| Retrieves nonstatic functions that are member of
class |
This rule flags the functions that are members of a class: rule isMemberFunction = {
defect TestisMemberFunction =
when Cpp.Function.is(&Function)
and Cpp.Type.is(&Type)
and Type.isClass()
and Function.isMemberFunction(&Type)
and Function.name(&fname)
raise "Member Function detected: \"{fname}\""
on Function
} |
isStaticMemberFunction(Function self, Cpp.Type.Type
&class)
| Retrieves static functions that are member of
class |
This rule flags the functions that are static members of a class: rule isStaticMemberFunction = {
defect TestisStaticMemberFunction =
when Cpp.Function.is(&Function)
and Cpp.Type.is(&Type)
and Type.isClass()
and Function.isStaticMemberFunction(&Type)
raise "Static Member Function detected"
on Function
} |
isVirtual(Function self)
| Retrieves virtual functions. |
This rule flags the virtual function in your code: rule isVirtual = {
defect TestisVirtual =
when Cpp.Function.is(&Function)
and Function.isVirtual()
raise "Virtual Function detected"
on Function
} |
type(Function self, Cpp.Type.Type &type)
| Retrieves function that has the type type. |
This rule flags the functions in your code and reports their types in the message: rule type = {
defect Testtype =
when Cpp.Function.is(&Function)
and Function.type(&ftype)
and ftype.toString(&ftypestr)
raise "Function detected with type: \"{ftypestr}\""
on Function
} |
returnType(Function self, Cpp.Type.Type
&type)
| Retrieves the functions with the return type
type |
This rule flags functions that return an
rule returnType = {
defect TestreturnType =
when Cpp.Function.is(&Function)
and Function.returnType(&rtype)
and rtype.toString(&rtypestr)
and rtypestr == "int"
raise "Function with int return type detected"
on Function
} |
declaredParameter(Function self, Cpp.Variable.Variable
¶m, Lang.Int &pos)
| For a function, Retrieves the parameters and stores them
inparam. The declaration orders of the parameters in
the function signature is also retrieved in stored in
pos. |
This rule flags the functions that has an integer as its first declared parameter: rule declaredParameter = {
defect TestdeclaredParameter =
when Cpp.Function.is(&Function)
and Function.declaredParameter(¶m, &pos)
and param.type(&ptype)
and ptype.toString(&ptypestr)
and ptypestr=="int"
and pos==0
and param.name(&pname)
raise "First parmater is integer \"{pname}\""
on Function
} |
numDeclaredParameters(Function self, Lang.Int
&num)
| Retrieves the number of parameter of a function and stores the value in
num. |
This rule flags functions with more than 2 parameters: rule numDeclaredParameters = {
defect TestnumDeclaredParameters =
when Cpp.Function.is(&Function)
and Function.numDeclaredParameters(&num)
and num>2
raise "Function with greater than 2 parameters detected"
on Function
} |
localVariable(Function self, Cpp.Variable.Variable
&variable)
| Retrieves the local variables of a function and stores them in
variable |
This rule flags virtual functions that declare a
rule localVariable = {
defect TestlocalVariable =
when Cpp.Function.is(&Function)
and Function.isVirtual()
and Function.localVariable(&var)
and var.type(&vartype)
and vartype.toString(&vts)
and vts=="double"
raise "Virtual function declares double type local variable"
on Function
} |
isDefined(Function self)
| Retrieves functions that are defined. |
This rule flags all functions that are not defined in the code: rule isDefined = {
defect TestisDefined =
when Cpp.Function.is(&Function)
and not Function.isDefined()
raise "Undefined Function detected"
on Function
} |
isExternC(Function self)
| Retrieves functions that are specified as extern
"C" |
This rule flags all rule isExternC = {
defect TestisExternC =
when Cpp.Function.is(&Function)
and Function.isExternC()
raise "Extern C Function detected"
on Function
} |
isVariadic(Function self)
| Retrieves the variadic functions in your code. |
This rule flags all variadic functions: rule isVariadic = {
defect TestisVariadic =
when Cpp.Function.is(&Function)
and Function.isVariadic()
raise "Variadic Function detected"
on Function
} |
isNoExcept(Function self)
| Retrieves the function that are specified as noexcept
or noexcept , where
is an expression
that evaluates to true at compile time. |
This rule flags all functions that are explicitly or implicitly
rule isNoExcept = {
defect TestisNoExcept =
when Cpp.Function.is(&Function)
and Function.isNoExcept()
raise "Noexcept Function detected"
on Function
} |
isNoExceptFalse(Function self)
| Retrieves the function that are specified as noexcept
falseor noexcept
, where
is an expression
that evaluates to false at compile time.. |
This rule flags all functions that are explicitly or implicitly
rule isNoExceptFalse = {
defect TestisNoExceptFalse =
when Cpp.Function.is(&Function)
and Function.isNoExceptFalse()
raise "Exception throwing Function detected"
on Function
} |
overloads(Function self, Function
&overload)
| Retrieves the overloads of the function self and
stores the overloads in overload. |
This rule flags function that has an overloads with a different number of parameters: rule overloads = {
defect Testoverloads =
when Cpp.Function.is(&Function)
and Function.overloads(&ovf)
and Function.numDeclaredParameters(&fnum)
and ovf.numDeclaredParameters(&ovfnum)
and fnum != ovfnum
raise "Function has overloads with a different number of params"
on Function
} |
hasNoPrototype(Function self)
| Retrieves functions that are declared without parameter type information. |
This rule flags functions that has no prototype: rule hasNoPrototype = {
defect TesthasNoPrototype =
when Cpp.Function.is(&Function)
and Function.hasNoPrototype()
and Function.name(&fname)
raise "Function \"{fname}\" has no prototype"
on Function
} |
isImplicitlyDeclared(Function self)
| Retrieves functions that are implicitly declared. That is, these function are used without being declared. |
This rule flags functions that are implicitly declared: rule isImplicitlyDeclared = {
defect TestisImplicitlyDeclared =
when Cpp.Function.is(&Function)
and Function.isImplicitlyDeclared()
and Function.name(&fname)
raise "Function detected: \"{fname}\" is implicitly declared"
on Function
} |
namespace(Function self, Cpp.Namespace.Namespace
&namespace)
| Retrieves the namespace for the function self and
stores the value in namespace |
This rule flags the functions in your code and reports the namespace in which they are declared. rule namespace = {
defect Testnamespace =
when Cpp.Function.is(&Function)
and Function.namespace(&ns)
and ns.name(&nsname)
raise "Function detected in namespace : \"{nsname}\""
on Function
} |
Examples
In a new folder
Function, initialize a new coding standard. At the command line, enter:polyspace-query-language init
In the file
main.pql, enter this content:package main // Main PQL file defines the catalog of your PQL project. // The catalog is a collection of sections. catalog FunctionExample = { #[Description("Example Section")] section ExampleSection = { #[Description("Noninlined Function Defined in header"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.Function.is(&func) and func.isDefined() and not func.isInline() and func.extension(&ext) and ext == ".h" raise "Noninline function defined in header file" on func } } }Create the coding standard
Function.pschk using this command at the command line:polyspace-query-language package
Using the generated coding standard, run a Bug Finder analysis on your source file. Foe example, at the command line, enter:
The analysis reports a defect on the nonlined functionpolyspace-bug-finder -sources src.h -lang cpp -checkers-activation-file Function.pschk
add()in the header file.
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)