主要内容

使用模式匹配选择用于 Polyspace 分析的文件

当您使用 Polyspace® 产品运行静态分析时,分析涵盖了在您的 Polyspace 工程中指定的所有文件(或在命令行中使用 -sources 指定的所有文件)。有时,您可能希望仅在这些文件的子集中查看结果,或者可能希望对文件子集应用不同的分析行为。您可以使用文件选择模式指定文件的子集。文件选择模式(glob 模式)使用 ?* 等通配符来覆盖多个文件。

何时指定文件选择模式

当您从编译命令创建 Polyspace 工程或选项文件,或使用 Polyspace Bug Finder™ 运行静态分析时,可以选择文件的子集。

从编译命令设置 Polyspace 分析时选择文件

当您使用 polyspace-configure 创建工程时,可以包含或排除其路径与您传递给 -include-sources-exclude-sources 选项的模式相匹配的源文件。您可以在命令行中多次指定这两个选项并将其组合使用。

本文中的示例使用以下文件夹结构。

Example folder structure used for the following examples.

要尝试这些示例,请使用 polyspaceroot\polyspace\examples\doc_cxx\sources-select 中的演示文件。polyspaceroot 是 Polyspace 的安装文件夹。

运行以下命令:

polyspace-configure -allow-overwrite -include-sources "glob_pattern" \
-print-excluded-sources -print-included-sources make -B 
glob_pattern 是 glob 模式,用于匹配在工程中包含或排除的文件路径。要确保 shell 不会展开您传递到 polyspace-configure 的 glob 模式,请将它们括在双引号中。

运行 Bug Finder 分析时选择文件

在使用 Polyspace Bug Finder 分析 C/C++ 代码时,您可以在工程中定义需要在分析过程中进行特定处理的文件集。例如,您可能希望跳过第三方库中函数体的定义,或强制分析您拥有的文件中的所有函数。您可以在分类 XML 文件中枚举具有特定行为的文件集,并使用此分类文件微调 Bug Finder 分析。

在分类 XML 文件中,您可以在 file-pattern 元素(fileset > files-in-setfileset > files-not-in-set 元素的子元素)内指定文件模式。例如,以下模式选择 myproject/inc 子文件夹中的 .hpp 文件,但排除以 -generated 结尾的文件。

<fileset name="Application implementation and header files">
      <files-in-set>
           <file-pattern>myproject/inc/**/*.hpp</file-pattern>
      </files-in-set>
      <files-not-in-set>
           <file-pattern>myproject/inc/**/*-generated.hpp</file-pattern>
      </files-not-in-set>
      <behaviors>
            <!-- Specific behaviors for this file set -->
     </behaviors>
</fileset>

要在静态分析期间指定分类文件,请使用分析选项 -classification。例如,您可以使用以下命令运行 Bug Finder:

polyspace-bug-finder -options-file myOptions.txt -classification myClassification.xml
有关详细信息,请参阅:

支持的文件选择模式

下表中的示例假定 sources 是一个顶层文件夹。

Glob 模式语法示例

无特殊字符、正斜杠 ('/') 或反斜杠 ('\')。

模式将匹配相应的文件而非文件夹。

-include-sources "main.c" 匹配以下项:

/sources/app/main.c

模式包含 '*''?' 特殊字符。

'*' 匹配文件或文件夹名称中的零个或多个字符。

'?' 匹配文件或文件夹名称中的一个字符。

匹配项不包含路径分隔符。

-include-sources "b?.c" 匹配以下项:

/sources/lib/b/b1.c

/sources/lib/b/b2.c

-include-sources "app/*.c" 匹配以下项:

/sources/app/main.c

模式开头为:

  • 正斜杠 '/' (UNIX®)。

  • 驱动器号,例如 C:\ (Windows®)。

模式仅匹配绝对路径。

-include-sources "/a" 与任何项都不匹配。

-include-sources "/sources/app" 匹配以下项:

/sources/app/main.c

模式结尾为:

  • 正斜杠 (UNIX)。

  • 反斜杠 (Windows)。

  • 双星号 ('**')

模式匹配指定文件夹下的所有文件。

'**' 位于模式开头时将被忽略。

-include-sources "a/" 匹配以下项:

/sources/lib/a/a1.c

/sources/lib/a/a2.c

模式包含:

  • '/**/' (UNIX).

  • '\**\' (Windows).

模式匹配指定路径中的零个或多个文件夹。

-include-sources "lib/**/?1.c" 匹配以下项:

/sources/lib/a/a1.c

/sources/lib/b/b1.c

模式以 '.''..' 开头。

模式匹配相对于您运行命令的路径的路径。

如果您从 /sources/lib/a 启动 polyspace-configure,则

-include-sources "../lib/**/b?.c" 匹配以下项:

/sources/lib/b/b1.c

/sources/lib/b/b2.c

模式是 Windows 上的一个 UNC 路径。

如果您的文件位于服务器 myServer 上,则:

\\myServer\sources\lib\b\** 匹配以下项:

\\myServer\sources\lib\b\b1.c

\\myServer\sources\lib\b\b2.c

polyspace-configure 不支持以下 glob 模式:

  • Windows 上相对于当前驱动器的绝对路径。

    例如,\foo\bar

  • 当前文件夹的相对路径。

    例如,C:foo\bar

  • Windows 中的扩展长度路径。

    例如,\\?\foo

  • 包含 '.' 或 '..' 的路径,位于模式开头时除外。

    例如,/foo/bar/../a?.c

  • 单独的 '*' 字符。

另请参阅

|

主题