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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)