可以告诉 clang 不要分析某些文件吗?

发布于 2024-07-21 10:19:55 字数 153 浏览 2 评论 0原文

我正在尝试使用 clang 来分析我正在开发的项目。 该项目包含一个相当大的静态库,它作为依赖项包含在 Xcode 中。

我真的希望 clang 不分析依赖项的文件,因为它似乎会使 clang 失败。 这可能吗? 我一直在阅读 clang 文档,但没有找到它。

I'm trying to use clang to profile a project I'm working on. The project includes a rather large static library that is included in Xcode as a dependency.

I would really like clang to not analyze the dependencies' files, as it seems to make clang fail. Is this possible? I've been reading the clang documentation, and I haven't found it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我家小可爱 2024-07-28 10:19:55

作为最后的手段,还有一个暴力选择。

将其添加到文件的开头:

// Omit from static analysis.
#ifndef __clang_analyzer__

将其添加到末尾:

#endif // not __clang_analyzer__

并且 clang --analyze 将看不到文件的内容。

参考:控制静态分析器诊断

As a last resort, there is a brute force option.

Add this to the beginning of a file:

// Omit from static analysis.
#ifndef __clang_analyzer__

Add this to the end:

#endif // not __clang_analyzer__

and clang --analyze won't see the contents of the file.

reference: Controlling Static Analyzer Diagnostics

╰沐子 2024-07-28 10:19:55

所以,这并不是一个真正的答案,但它效果很好。

我最终做的是提前构建静态库,然后使用 scan-build 构建项目。 由于已经有静态库的最新版本,因此没有重建它,因此也没有扫描。

不过,我仍然希望得到一个真正的答案。

So, this isn't really an answer, but it worked well enough.

What I ended up doing was building the static library ahead of time, and then building the project using scan-build. Since there was already an up-to-date build of the static library, it wasn't rebuilt and thus wasn't scanned.

I'd still love to have a real answer for this, though.

灵芸 2024-07-28 10:19:55

最终,该选项于 2018 年得到实施。

使用 --exclude [1] [2] 选项

--排除

不要针对此目录中找到的文件运行静态分析器
(您可以多次指定此选项)。 可能有用的时候
项目包含第 3 方库。

Finally, in 2018 the option was implemented.

Use --exclude <path> [1] [2] option

--exclude

Do not run static analyzer against files found in this directory
(You can specify this option multiple times). Could be useful when
project contains 3rd party libraries.

南烟 2024-07-28 10:19:55

我不使用 XCode,但在 linux 中使用 scan-build 以下内容对我有用。 就我而言,我想对所有第一方非生成的代码运行静态分析。 但是,我想避免在第三方代码和生成的代码上运行它。

在命令行上,当 scan-build 将 CC 和 CXX 环境变量设置为 ccc-analyzer 和 c++-analyzer 位置时,clang-analyzer 就会挂接到构建中。 我编写了两个名为 ccc-analyzer.py 和 c++-analyzer.py 的简单脚本,并将它们连接到编译中以代替默认脚本。 在这些包装器脚本中,我只是查看正在编译的文件的路径,然后直接运行原始编译器(如果我希望避免静态分析)或 c*-analyzer(如果我希望进行静态分析)。 我的脚本是用 python 编写的,并与我的特定构建系统相关联,但作为一个需要修改的示例:

import subprocess
import sys

def main(argv):
  is_third_party_code = False
  for i in range(len(argv)):
    arg = argv[i]
    if arg == '-c':
      file_to_compile = argv[i + 1]
      if '/third_party/' in file_to_compile or \
            file_to_compile.startswith('gen/'):
        is_third_party_code = True
      break
  if is_third_party_code:
    argv[0] = '/samegoal/bin/clang++'
  else:
    argv[0] = '/samegoal/scan-build/c++-analyzer'
  return subprocess.call(argv)

if __name__ == '__main__':
  sys.exit(main(sys.argv))

I don't use XCode, but using scan-build in linux the following works for me. I my case, I want to run the static analysis on all first party, non-generated code. However, I want to avoid running it on third_party code and generated code.

On the command line, clang-analyzer is hooked into the build when scan-build sets CC and CXX environment variables to ccc-analyzer and c++-analyzer locations. I wrote two simple scripts called ccc-analyzer.py and c++-analyzer.py and hooked them in to the compile in place of the default. In these wrapper scripts, I simply looked at the path of the file being compiled and then run either the raw compiler directly (if I wish to avoid static analysis) or the c*-analyzer (if I wish for static analysis to occur). My script is in python and tied to my specific build system, but as an example that needs modification:

import subprocess
import sys

def main(argv):
  is_third_party_code = False
  for i in range(len(argv)):
    arg = argv[i]
    if arg == '-c':
      file_to_compile = argv[i + 1]
      if '/third_party/' in file_to_compile or \
            file_to_compile.startswith('gen/'):
        is_third_party_code = True
      break
  if is_third_party_code:
    argv[0] = '/samegoal/bin/clang++'
  else:
    argv[0] = '/samegoal/scan-build/c++-analyzer'
  return subprocess.call(argv)

if __name__ == '__main__':
  sys.exit(main(sys.argv))
一个人的旅程 2024-07-28 10:19:55

对于 Xcode 用户,您可以通过在 Target -> 中添加以下标志来从静态分析器中排除单个文件。 构建阶段 -> 编译源部分:-Xanalyzer -analyzer-disable-all-checks

For Xcode users, you can exclude individual files from static analyzer by adding the following flags in the Target -> Build Phases -> Compile Sources section: -Xanalyzer -analyzer-disable-all-checks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文