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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)