Cpp.SourceLoc Class
Namespace: Cpp
Description
Cast package allows you to check various cast related issues 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
SourceLoc objects, print the string obtained by the predicate
Cpp.SourceLoc.pathStr.
| Type | Raisable | Printable |
|---|---|---|
Lang.Int
| No | Yes |
Lang.String
| No | Yes |
Path
| Yes | No |
| Predicates | Description | Example |
|---|---|---|
projectPath(Cpp.SourceLoc.Path &path)
| Retrieves all project paths for the analyzed program and stores them in
path. |
This rule reports the project path: rule projectPath = {
defect TestprojectPath =
when Cpp.SourceLoc.projectPath(&path)
and path.pathStr(&str)
raise "Project path detected: \"{str}\""
on path
} |
someSourceFile(Cpp.SourceLoc.Path &path)
| Retrieves all source files that define an object and stores them in
path. |
This rule reports the source file paths as strings in the message: rule someSourceFile = {
defect TestsomeSourceFile =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and SourceFile.pathStr(&str)
raise "Source file detected: \"{str}\""
on SourceFile
} |
pathStr(Cpp.SourceLoc.Path self, Lang.String &str)
| Retrieves the full string representation of the path self and stores it in str. | |
pathParent(Cpp.SourceLoc.Path self, Cpp.SourceLoc.Path &parent)
| Retrieves the parent directory of the path self and stores it in parent. |
This rule reports the parent paths of source files: rule pathParent = {
defect TestpathParent =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and SourceFile.pathParent(&parentPath)
and parentPath.pathStr(&str)
raise "Source file parent: \"{str}\""
on SourceFile
} |
pathFilename(Cpp.SourceLoc.Path self, Lang.String &filename)
| Retrieves the filename of the path self and stores it in
filename. |
This rule reports the file name of a source file: rule pathFilename = {
defect TestpathFilename =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and SourceFile.pathFilename(&str)
raise "Source file filename: \"{str}\""
on SourceFile
} |
pathStem(Cpp.SourceLoc.Path self, Lang.String &stem)
| Retrieves the base name without extension for self and
stores it in stem. |
This rule reports the base name of a source file without the extension: rule pathStem = {
defect TestpathStem =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and SourceFile.pathStem(&str)
raise "Source file stem: \"{str}\""
on SourceFile
} |
pathExtension(Cpp.SourceLoc.Path self, Lang.String &extension)
| Retrieves the file extension for the path self and stores it
in extension. |
This rule reports the extension of a source file: rule pathExtension = {
defect TestpathExtension =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and SourceFile.pathExtension(&str)
raise "Source file extension: \"{str}\""
on SourceFile
} |
isSameFile(Cpp.SourceLoc.Path self, Cpp.SourceLoc.Path path2)
| Retrieves path2 that are the same file as
self. |
This rule reports if two source files are the same: rule isSameFile = {
defect TestisSameFile =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and Cpp.SourceLoc.someSourceFile(&SourceFile2)
and SourceFile.isSameFile(SourceFile2)
raise "Same file"
on SourceFile
} |
isBefore(Cpp.SourceLoc.Path path1, Lang.Int line1, Lang.Int col1, Cpp.SourceLoc.Path path2, Lang.Int line2, Lang.Int col2)
| Retrieves |
This rule checks if a position in one source file is before another position in a different source file: rule isBefore = {
defect TestisBefore =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and Cpp.SourceLoc.someSourceFile(&SourceFile2)
and SourceFile != SourceFile2
and Cpp.SourceLoc.isBefore(SourceFile, 0,0, SourceFile2, 1,1)
raise "is Before"
on SourceFile
} |
isSameLine(Cpp.SourceLoc.Path path1, Lang.Int line1, Cpp.SourceLoc.Path path2, Lang.Int line2)
| Retrieves |
This rule checks if a line in one source file is in the same line as another line in a different source file: rule isSameLine = {
defect TestisSameLine =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and Cpp.SourceLoc.someSourceFile(&SourceFile2)
and SourceFile != SourceFile2
and Cpp.SourceLoc.isSameLine(SourceFile, 0, SourceFile2, 1)
raise "is sameline"
on SourceFile
} |
linesBetween(Cpp.SourceLoc.Path path1, Lang.Int line1, Cpp.SourceLoc.Path path2, Lang.Int line2, Lang.Int &nlines)
| Retrieves the number of lines between line1 in path1 and line2 in path2, for the same file, and stores it in nlines. |
This rule reports the number of lines between two lines rule linesBetween = {
defect TestlinesBetween =
when Cpp.SourceLoc.someSourceFile(&SourceFile)
and Cpp.SourceLoc.someSourceFile(&SourceFile2)
and SourceFile != SourceFile2
and Cpp.SourceLoc.linesBetween(SourceFile, 0, SourceFile2, 1, &N)
raise "Lines between: \"{N}\""
on SourceFile
} |
Examples
In a new folder
SourceLoc, 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 SourceLocExample = { #[Description("Example Section")] section ExampleSection = { #[Description("Macro definition repreated in multiple header files"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.SourceLoc.someSourceFile(&SourceFile) and SourceFile.pathExtension(&str) and str != ".cpp" raise "Source file extension: \"{str}\"" on SourceFile } } }Create the coding standard
SourceLoc.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 C++ source files that has extensions other thanpolyspace-bug-finder -sources
src.cpo-lang cpp -checkers-activation-file SourceLoc.pschk.cpp.
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)