Cpp.Macro Class
Namespace: Cpp
Description
Macro class represents the macros in your code. Use the predicates in
this class to query various macro related aspects of your code. This class is not raisable.
That is, you cannot report a defect on a Macro object.
Predicates
You can report a defect on Raisable types. If a type is
Printable, it can be reported in the message. For
Macro objects, print the string obtained by the predicate
Cpp.Macro.name.
| Type | Raisable | Printable |
|---|---|---|
Lang.Int
| No | Yes |
Lang.String
| No | Yes |
Lang.Unsigned
| No | Yes |
Macro
| No | No |
This class defines these predicates that act on the Macro objects in
your C/C++ code.
| Predicates | Description | Example |
|---|---|---|
is(Macro ¯o)
| Retrieves all Macro objects in your C/C++ code and stores
them in macro. |
This rule flags all macros in your source file and reports a defect on the file for each macro. rule is = {
defect Testis =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected"
on file
} |
name(Macro self, Lang.String &name)
| Retrieves the name of the macro self and stores it in
name. |
This rule reports a defect on the source file for each macro and reports the name of the macros in the message: rule name = {
defect Testname =
when Cpp.Macro.is(&Macro)
and Macro.name(&name)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected: name is \"{name}\""
on file
} |
sourceLocation(Macro self, Cpp.SourceLoc.Path &file,
Lang.Int &line, Lang.Int &column)
|
For a macro object, retrieves:
|
This rule reports a defect on the source file for each macro and reports the filename, line and column of the macro: rule sourceLocation = {
defect TestsourceLocation =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &line, &column)
and file.pathStem(&name)
raise "Macro detected: \"{name}\", \"{line}\",\"{column}\""
on file
} |
translationUnit(Macro self, Cpp.SourceLoc.Path
&tu)
| Retrieves the translation unit in which the macro self is
visible, and stores it in tu. |
This rule reports a defect on the source file for each macro and reports the path string of the macro's translation unit in the message. rule translationUnit = {
defect TesttranslationUnit =
when Cpp.Macro.is(&Macro)
and Macro.translationUnit(&translationUnit)
and translationUnit.pathStr(&pstr)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected \"{pstr}\""
on file
} |
isObjectLike(Macro self)
| Retrieves macros that has no parameters. |
This rule reports a defect on the source file for each macro that is object-like. rule isObjectLike = {
defect TestisObjectLike =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.isObjectLike()
raise "Object Like Macro detected"
on file
} |
isFunctionLike(Macro self)
| Retrieves macros that accept one or more parameters. |
This rule reports a defect on the source file for each macro that is function-like. rule isFunctionLike = {
defect TestisFunctionLike =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.isFunctionLike()
raise "Function like Macro detected"
on file
} |
hasEmptyDefinition(Macro self)
| Retrieves macros that contain no replacement text after the macro name or parameter list. |
This rule reports a defect on the source file for each macro with an empty definition. rule hasEmptyDefinition = {
defect TesthasEmptyDefinition =
when Cpp.Macro.is(&Macro)
and Macro.hasEmptyDefinition()
and Macro.sourceLocation(&file, &_line, &_column)
raise "Empty Macro detected"
on file
} |
definitionText(Macro self, Lang.String
&text)
| Retrieves the normalized textual definition of the macro
self and stores it in text. |
This rule reports a defect on the source file for each macro and reports the macro's definition text in the message. rule definitionText = {
defect TestdefinitionText =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.definitionText(&text)
raise "Macro detected with definition:\"{text}\" "
on file
} |
isVariadic(Macro self)
| Retrieves macros that accept a variable number of arguments. |
This rule reports a defect on the source file for each variadic macro. rule isVariadic = {
defect TestisVariadic =
when Cpp.Macro.is(&Macro)
and Macro.isVariadic()
and Macro.sourceLocation(&file, &_line, &_column)
raise "Variadic Macro detected"
on file
} |
isCommandLineDefinition(Macro self)
| Retrieves macros that are defined on the compiler command line , for example,
using the -D flag, instead of being defined in the source file
itself. |
This rule reports a defect on the source file for each macro defined by the compiler command line. rule isCommandLineDefinition = {
defect TestisCommandLineDefinition =
when Cpp.Macro.is(&Macro)
and Macro.isCommandLineDefinition()
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro defined by compiler command line detected"
on file
} |
isPredefined(Macro self)
| Retrieves macros that are predefined by the compiler. |
This rule reports a defect on the source file for each predefined macro. rule isPredefined = {
defect TestisPredefined =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.isPredefined()
raise "Predefined Macro detected"
on file
} |
integralValue(Macro self, Lang.Int
&value)
| Retrieves the signed 64-bit integer value of the macro
self, if representable, and stores it in
value. |
This rule reports a defect on the source file for each macro with an integral value and reports the value in the message. rule integralValue = {
defect TestintegralValue =
when Cpp.Macro.is(&Macro)
and Macro.integralValue(&value)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected with int value \"{value}\""
on file
} |
unsignedIntegralValue(Macro self, Lang.Unsigned
&value)
| Retrieves the unsigned 64-bit integer value of the macro
self, if representable, and stores it in
value. |
This rule reports a defect on the source file for each macro with an unsigned integral value and reports the value in the message. rule unsignedIntegralValue = {
defect TestunsignedIntegralValue =
when Cpp.Macro.is(&Macro)
and Macro.unsignedIntegralValue(&value)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected with unsigned value \"{value}\""
on file
} |
locPath(Macro self, Cpp.SourceLoc.Path
&path)
| Retrieves the file path where the macro self is
defined and stores it in path. |
This rule reports a defect on the source file for each macro and reports the macro's location path in the message: rule locPath = {
defect TestlocPath =
when Cpp.Macro.is(&Macro)
and Macro.locPath(&path)
and path.pathStr(&str)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected:\"{str}\""
on file
} |
extension(Macro self, Lang.String
&extension)
| Retrieves the file extension of the source file where the macro
self is defined and stores it in
extension. |
This rule reports a defect on the source file for each macro and reports the file extension of the macro's source file. rule extension = {
defect Testextension =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.extension(&ext)
raise "Macro detected in file: *\"{ext}\""
on file
} |
stem(Macro self, Lang.String &stem)
| Retrieves the filename (without extension) of the source file where the macro
self is defined and stores it in
stem. |
This rule reports a defect on the source file for each macro and reports the name of the macro's source file without the extension. rule stem = {
defect Teststem =
when Cpp.Macro.is(&Macro)
and Macro.stem(&stem)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected with stem \"{stem}\""
on file
} |
path(Macro self, Lang.String &path)
| Retrieves the full path as a string for the file where the macro
self is defined and stores it in
path. |
This rule reports a defect on the source file for each macro and reports the full path of the macro's source file. rule path = {
defect Testpath =
when Cpp.Macro.is(&Macro)
and Macro.path(&path)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected in path \"{path}\""
on file
} |
filename(Macro self, Lang.String
&basename)
| Retrieves the name of the file where the macro self is
defined and stores it in basename. Only the file name is
retrieved instead of the full path. |
This rule reports a defect on the source file for each macro and reports the filename of the macro's source file. rule filename = {
defect Testfilename =
when Cpp.Macro.is(&Macro)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.filename(&fn)
raise "Macro detected in file: \"{fn}\""
on file
} |
line(Macro self, Lang.Int &line)
| Retrieves the line number where the macro self is
defined and stores it in line. |
This rule reports a defect on the source file for each macro defined on line 10. rule line = {
defect Testline =
when Cpp.Macro.is(&Macro)
and Macro.line(&line)
and line == 10
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro detected on line 10"
on file
} |
column(Macro self, Lang.Int &column)
| Retrieves the column number where the macro self is
defined and stores the number in column. |
This rule reports a defect on the source file for each macro and reports the column at which the macro is defined. rule column = {
defect Testcolumn =
when Cpp.Macro.is(&Macro)
and Macro.column(&column2)
and Macro.sourceLocation(&file, &_line, &column1)
raise "Macro detected: column from source loc \"{column1}\" vs macro.column:\"{column2}\""
on file
} |
isInSameFile(Macro self, Macro macro2)
| Retrieves the macro self that are in the same file as
macro2. |
This rule reports a defect on the source file if there are two macros in the same file, one containing "Complex" and the other containing "POW" in their definition texts. rule isInSameFile = {
defect TestisInSameFile =
when Cpp.Macro.is(&Macro)
and Macro.definitionText(&Text)
and Text.contains("Complex")
and Cpp.Macro.is(&Macro2)
and Macro2.definitionText(&Text2)
and Text2.contains("POW")
and Macro.isInSameFile(Macro2)
and Macro.sourceLocation(&file, &_line, &_column)
raise "Macro Containing POW and Complex in same file"
on file
} |
isBefore(Macro self, Macro macro2)
| Retrieves the macro self that are before the macro
macro2 in the same file. |
This rule reports a defect on the source file for each macro that is defined before another macro with an integral value of 2 in the same file. rule isBefore = {
defect TestisBefore =
when Cpp.Macro.is(&Macro)
and Macro.integralValue(&val) and val ==2
and Cpp.Macro.is(&Macro2)
and Macro2.isBefore(Macro)
and Macro2.sourceLocation(&file, &_line, &_column)
raise "Macro is before another macro that has integral value 2 in file"
on file
} |
isSameLine(Macro self, Macro macro2)
| Retrieves the macro self that are defined in the same line
as the macro macro2 in the same file. |
This rule reports a defect on the source file for each pair of macros that are in the same file and reports both macros' definition texts. rule isSameLine = {
defect TestisSameLine =
when Cpp.Macro.is(&Macro)
and Cpp.Macro.is(¯o2)
and Macro.isInSameFile(macro2)
and Macro.sourceLocation(&file, &_line, &_column)
and Macro.definitionText(&Text1)
and macro2.definitionText(&Text2)
raise "Macros are in same file:\"{Text1}\", \"{Text2}\" "
on file
} |
linesBetween(Macro self, Macro macro2, Lang.Int
&nlines)
| Retrieves the number of lines between the definitions of
self and macro2 in the same file
and stores it in nlines. |
This rule reports a defect on the source file for each pair of macros that have exactly 2 lines between them and reports both macros' definition texts. rule linesBetween = {
defect TestlinesBetween =
when Cpp.Macro.is(&Macro)
and Cpp.Macro.is(&Macro2)
and Macro.linesBetween(Macro2, &nlines)
and nlines == 2
and Macro.definitionText(&Text1)
and Macro2.definitionText(&Text2)
and Macro.sourceLocation(&file, &_line, &_column)
raise "2 lines between macros :\"{Text1}\", \"{Text2}\""
on file
} |
Examples
In a new folder
Macro, 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 MacroExample = { #[Description("Example Section")] section ExampleSection = { #[Description("Macro definition repreated in multiple header files"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.Macro.is(&Macro1) and Cpp.Macro.is(&Macro2) and Macro1 != Macro2 and Macro1.name(&name1) and Macro2.name(&name2) and name1 == name2 and Macro1.locPath(&file1) and Macro2.locPath(&file2) and file1 != file2 and Macro1.extension(&ext1) and Macro2.extension(&ext2) and ext1 == ".h" and ext2 == ".h" and Macro1.sourceLocation(&fileRaise, &_line, &_column) raise "Macro \"{name1}\" is defined in multiple header files" on fileRaise } } }Create the coding standard
Macro.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 definitions ofpolyspace-bug-finder -sources foo.h,bar,h -lang cpp -checkers-activation-file Macro.pschk
BUFFER_SIZE.
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)