Cpp.Namespace Class
Namespace: Cpp
Description
Namespace class represents the C++ namespaces in your code.
Predicates
You can report a defect on Raisable types. If a type is
Printable, it can be reported in the message. For
Namespace objects, print the string obtained by the predicate
Cpp.Namespace.name.
| Type | Raisable | Printable |
|---|---|---|
Lang.Int
| No | Yes |
Lang.String
| No | Yes |
Namespace
| No | No |
| Predicates | Description | Example |
|---|---|---|
is(Namespace &namespace)
| Retrieves all C++ namespaces excluding inline and inaccessible anonymous
namespaces and stores them in namespace. |
This rule detects the namespace in your code and reports the file path in which the namespace is declared. rule is = {
defect Testis =
when Cpp.Namespace.is(&ns)
and Cpp.SourceLoc.someSourceFile(&path)
and path.pathStr(&str)
raise "Namesapce detected in \"{str}\""
on path
} |
name(Namespace self, Lang.String &name)
| Retrieves the unqualified name of the namespace self
and stores the name string in name. Global and anonymous
namespaces have no names. |
This rule reports the namespace names of the variables in your code. rule name = {
defect Testname =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.name(&NSname)
raise "Namesapce detected:\"{NSname}\""
on variable
} |
fullName(Namespace self, Lang.String &name)
| Retrieves the fully qualified name of the namespace
self and stores the name string in
name. |
This rule reports the full namespace names of the variables in your code. rule fullName = {
defect TestfullName =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.fullName(&nsname)
and variable.name(&varname)
raise "Full Namespace Name of variable \"{varname}\": \"{nsname}\""
on variable
} |
parent(Namespace self, Namespace &parent)
| Retrieves the parent namespace of the namespace self
and stores it in parent. The parent of a top-level
namespace is the global namespace. |
This rule reports the names of the parent namespace for the variables in your code. rule parent = {
defect Testparent =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.parent(&parentNS)
and parentNS.name(&parent)
raise "parentNamesapce detected:\"{parent}\""
on variable
} |
ancestor(Namespace self, Namespace &ancestor)
| Retrieves all ancestor namespace of the namespace
self including itself and stores them in
ancestor. |
This rule reports the name of the ancestor namespace of the variables in your code. rule ancestor = {
defect Testancestor =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.ancestor(&ancestor)
and ancestor.name(&ancestorName)
and variable.name(&varname)
raise "Ancestor Namesapce of variable \"{varname}\":\"{ancestorName}\""
on variable
} |
depth(Namespace self, Lang.Int &depth)
| Retrieves the nesting depth of the namespace self and
stores it in depth. The global namespace has depth
0. |
This rule reports the namespace nesting depth of the variables in your code. rule depth = {
defect Testdepth =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.depth(&nL)
and variable.name(&varname)
raise "Namespace depth of variable \"{varname}\": \"{nL}\""
on variable
} |
isGlobal(Namespace self)
| Retrieves the global namespace. |
This rule flags the variables in the global namespace. rule isGlobal = {
defect TestisGlobal =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.isGlobal()
raise "Global Namesapce detected"
on variable
} |
isStd(Namespace self)
| Retrieves the std namespace. |
This rule flags the variables in the rule isStd = {
defect TestisStd =
when Cpp.Variable.is(&variable)
and variable.namespace(&ns)
and ns.isStd()
raise "std Namesapce detected"
on variable
} |
Examples
In a new folder
Namespace, 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 namespaceExample = { #[Description("Example Section")] section ExampleSection = { #[Description("variable declared in global namespace"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.Variable.is(&variable) and variable.namespace(&ns) and ns.isGlobal() and variable.name(&vname) raise "Variable global namesapce detected: \"{vname}\"" on variable } } }Create the coding standard
Namespace.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 defects on the variablepolyspace-bug-finder -sources src.cpp -lang cpp -checkers-activation-file Namespace.pschk
globalCounter.
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)