Cpp.ForRangeLoop Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the for_range_loop nodes in the syntax tree of your code
Since R2026a
Description
The PQL class Cpp.ForRangeLoop represents the node for_range_loop in the syntax tree of your code.
#include <vector>
void process(int);
void test() {
std::vector<int> xs;
for (auto x : xs) { process(x); } // for_range_loop node
for (const auto &y : xs) { } // for_range_loop node with declarator and type qualifiers
}The two for (...) : ... { } statements are
for_range_loop nodes and correspond to this class.
Predicates
| Type | Raisable | Printable |
|---|---|---|
ForRangeLoop
| Yes | No |
This class defines these predicates that act on the objects of this class. In addition, objects of this class can access the predicates defined by the base class AstNodeProperties. An object of this class is an object of AstNodeProperties class.
| Predicates | Description | Example |
|---|---|---|
is(required ForRangeLoop &forRangeLoop)
| Matches a for_range_loop node and returns it as the output
variable. Use this to start inspecting fields of a specific for-range loop. | This PQL defect checks for any defect ForRangeIs =
when
Cpp.ForRangeLoop.is(&frl)
and frl.nodeText(&txt)
raise "Found for-range: \"{txt}\""
on frlIn this C++ code the defect finds every for-range statement.
#include <vector>
void f(int);
int main() {
std::vector<int> v;
for (auto i : v) f(i);
} |
cast(Cpp.Node.Node node, required ForRangeLoop &cast)
| Checks whether a generic Node is a
for_range_loop and if so returns it as a
ForRangeLoop for further field queries. | This PQL defect checks when a node converted from another specific node
is actually a
defect ForRangeCast =
when
Cpp.Node.is(&n, &, &, &)
and Cpp.ForRangeLoop.cast(n, &frl)
and frl.nodeText(&txt)
raise "cast -> ForRangeLoop: \"{txt}\""
on frlIn this C++ code the defect finds a range based for loop.
#include <vector>
int main() {
std::vector<int> v;
for (auto x : v) { (void)x; }
} |
isa(Cpp.Node.Node node)
| Returns true if the given generic Node is a
for_range_loop. Use when you need only a boolean check. | This PQL defect checks the type of a node converted from another node to
see if it's a
defect ForRangeIsa =
when
Cpp.Node.is(&n, &, &, &)
and Cpp.ForRangeLoop.isa(n)
raise "Node is a for-range loop"
on nIn this C++ code the defect finds a range based for loop.
#include <vector>
int main() {
std::vector<int> v;
for (auto x : v) { (void)x; }
} |
body(ForRangeLoop self, Cpp.Node.Node &child)
| Finds the loop body node and returns it as child. Use to
inspect the statements executed per iteration. | This PQL defect checks for the body node of a for-range loop. defect ForRangeBody =
when
Cpp.ForRangeLoop.is(&frl)
and frl.body(&b)
and b.nodeText(&txt)
raise "for-range body: \"{txt}\""
on frlIn this C++ code the defect finds the compound statement that is the loop body.
#include <vector>
void use(int);
int main() {
std::vector<int> v;
for (auto x : v) { use(x); } // body is "{ use(x); }"
} |
declarator(ForRangeLoop self, Cpp.Node.Node &child)
| Finds loop variable declarators and returns them as
child. | This PQL defect checks the declarator (loop variable) of a for-range loop. defect ForRangeDeclarator =
when
Cpp.ForRangeLoop.is(&frl)
and frl.declarator(&d)
and d.nodeText(&txt)
raise "declarator: \"{txt}\""
on frlIn this C++ code the defect reports the loop variable
such as
#include <vector>
int main() {
std::vector<int> v;
for (const auto &elem : v) { (void)elem; }
} |
initializer(ForRangeLoop self, Cpp.Node.Node &child)
| Finds initializers of the range-based for loop and returns it as
child. | This PQL defect checks the range expression used in a for-range loop. defect ForRangeInitializer =
when
Cpp.ForRangeLoop.is(&frl)
and frl.initializer(&init)
and init.nodeText(&txt)
raise "initializer: \"{txt}\""
on frlIn this C++ code the defect finds the initializer
#include <vector>
std::vector<int> make();
int main() {
for (auto r = make(); auto x : r) {
// use x
}
} |
right(ForRangeLoop self, Cpp.Node.Node &child)
| Finds the right-hand expression of the for-range and returns as
child. | This PQL defect checks the right-hand node of the for-range header. defect ForRangeRight =
when
Cpp.ForRangeLoop.is(&frl)
and frl.right(&r)
and r.nodeText(&txt)
raise "right: \"{txt}\""
on frlIn this C++ code the defect inspects the same range
expression like
#include <vector>
int main() {
std::vector<int> arr;
for (int x : arr) { (void)x; }
} |
typeField(ForRangeLoop self, Cpp.Node.Node &child)
| Finds the type annotation appearing before the declarator and returns it as
child. | This PQL defect checks the explicit type part of the loop declarator. defect ForRangeTypeField =
when
Cpp.ForRangeLoop.is(&frl)
and frl.typeField(&t)
and t.nodeText(&txt)
raise "typeField: \"{txt}\""
on frlIn this C++ code the defect reports types like
#include <vector>
int main() {
std::vector<int> v;
for (int x : v) { (void)x; }
} |
msDeclspecModifier(ForRangeLoop self, Cpp.Node.Node &child)
| Finds Microsoft __declspec modifier applied in the loop
declaration and returns it aschild. | This PQL defect checks for defect ForRangeMsDeclspec =
when
Cpp.ForRangeLoop.is(&frl)
and frl.msDeclspecModifier(&m)
and m.nodeText(&txt)
raise "__declspec modifier: \"{txt}\""
on frlIn this C++ code the defect would find
#include <vector>
int main() {
std::vector<int> v;
for (__declspec(align(16)) int x : v) {}
} |
storageClassSpecifier(ForRangeLoop self, Cpp.Node.Node
&child)
| Finds storage-class specifier such as static or
extern applied to the loop variable, and returns as
child. | This PQL defect checks for storage class specifiers on the loop variable. defect ForRangeStorageClass =
when
Cpp.ForRangeLoop.is(&frl)
and frl.storageClassSpecifier(&s)
and s.nodeText(&txt)
raise "storage class: \"{txt}\""
on frlIn this C++ code the defect would detect
#include <vector>
int main() {
std::vector<int> v;
for (static int x : v) { (void)x; } // ill-formed but shows the node if present
} |
typeQualifier(ForRangeLoop self, Cpp.Node.Node &child)
| Finds type qualifier like const or
volatile applied to the loop variable type and returns as
child. | This PQL defect checks for type qualifiers in the for-range loop declarator. defect ForRangeTypeQualifier =
when
Cpp.ForRangeLoop.is(&frl)
and frl.typeQualifier(&tq)
and tq.nodeText(&txt)
raise "type qualifier: \"{txt}\""
on frlIn this C++ code the defect finds qualifiers like
#include <vector>
int main() {
std::vector<int> v;
for (const auto &x : v) { (void)x; }
} |
virtualField(ForRangeLoop self, Cpp.Node.Node &child)
| Finds virtual tokens if it appears in the loop header
context and returns as child. | This PQL defect checks for a defect ForRangeVirtual =
when
Cpp.ForRangeLoop.is(&frl)
and frl.virtualField(&v)
and v.nodeText(&txt)
raise "virtual token: \"{txt}\""
on frlIn normal C++ code |
attributeDeclaration(ForRangeLoop self, Cpp.Node.Node &child)
| Finds attribute declarations attached to the loop variable, like
[[deprecated]], and returns as
child. | This PQL defect checks for attribute declarations on the for-range loop variable. defect ForRangeAttributeDecl =
when
Cpp.ForRangeLoop.is(&frl)
and frl.attributeDeclaration(&ad)
and ad.nodeText(&txt)
raise "attribute decl: \"{txt}\""
on frlIn this C++ code the defect finds attributes such as
#include <vector>
int main() {
std::vector<int> v;
for ([[deprecated]] int x : v) { (void)x; }
} |
attributeSpecifier(ForRangeLoop self, Cpp.Node.Node &child)
| Finds attribute specifiers that annotate the loop variable or header, and
returns as child. | This PQL defect checks for attribute specifiers attached to the for-range loop. defect ForRangeAttrSpec =
when
Cpp.ForRangeLoop.is(&frl)
and frl.attributeSpecifier(&ats)
and ats.nodeText(&txt)
raise "attribute specifier: \"{txt}\""
on frlIn this C++ code the defect detects attributes like
#include <vector>
int main() {
std::vector<int> v;
for ([[nodiscard]] int x : v) { (void)x; }
} |
getEnclosingForRangeLoop(Cpp.Node.Node child, required ForRangeLoop
&parent)
| Finds the nearest enclosing for_range_loop ancestor of
child and returns it as parent. | This PQL defect checks for the closest for-range loop that encloses a given node converted from another node. defect GetEnclosingForRange =
when
Cpp.Identifier.is(&id)
and id.toNode(&n)
and Cpp.ForRangeLoop.getEnclosingForRangeLoop(n, &frl)
and frl.nodeText(&txt)
raise "enclosing for-range: \"{txt}\""
on frlIn this C++ code the defect finds the for-range loop that contains a given identifier inside the loop body.
#include <vector>
void use(int);
int main() {
std::vector<int> v;
for (auto x : v) { use(x); } // identifier `x` is enclosed by this for-range
} |
isEnclosedInForRangeLoop(Cpp.Node.Node child)
| Returns true when the node has any ancestor that is a
for_range_loop. Use to detect whether code lies inside a
range-based loop. | This PQL defect checks whether a converted node is located inside any for-range loop. defect IsEnclosedInForRange =
when
Cpp.Identifier.is(&id)
and id.toNode(&n)
and Cpp.ForRangeLoop.isEnclosedInForRangeLoop(n)
raise "identifier is inside a for-range loop"
on nIn this C++ code the defect detects that an identifier used inside the loop body is enclosed by a for-range loop.
#include <vector>
void use(int);
int main() {
std::vector<int> v;
for (auto x : v) { use(x); } // `x` is enclosed in the for-range loop
} |
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)