Clang工具在使用-Xclang运行时出现文件无法识别的错误。

huangapple go评论81阅读模式
英文:

Clang tool gives file not recognized error when run using -Xclang

问题

我创建了一个基本的clang工具,并使用FrontendPluginRegistry进行了注册:

static FrontendPluginRegistry::Add<MyActionClass>
    X("my-parser", "my-clang-tool-parser")

我能够成功地使用-cc1选项在源文件上运行此工具:

clang -cc1 -load my-tool.so -plugin my-parser source_file.cpp

上述命令按预期提供了正确的结果,没有任何问题。然后,我使用以下命令使用Xclang选项运行工具:

clang -Xclang -load -Xclang my-tool.so -Xclang -plugin -Xclang my-parser source_file.cpp

在这种情况下,首先我得到了所有正确的输出行(与cc1选项一样),但在输出之后,我多了2行额外的内容:

/tmp/test1-856817.o文件无法识别:文件被截断
clang:错误链接器命令失败,退出码为1(使用-v查看调用)

尽管我的工具是一个庞大项目的一部分,但我猜代码是正确的,因为它能够成功构建,并且在通过cc1和Xclang选项运行时都提供了正确的输出。只有在使用Xclang选项时,我无法理解输出之后的2行额外内容。

我试图使用Xclang运行工具,因为我想要传递-I选项以包含一些在source_file.cpp中的头文件,而clang文档不鼓励在这种情况下使用cc1。

据我所知,工具应该在这两种情况下提供相同的输出。

英文:

I created a basic clang tool and registered it using FrontendPluginRegistry:

static FrontendPluginRegistry::Add&lt;MyActionClass&gt;
    X(&quot;my-parser&quot;, &quot;my-clang-tool-parser&quot;)

I am able to successfully run this tool on a source file using the -cc1 option:

clang -cc1 -load my-tool.so -plugin my-parser source_file.cpp

The above command gives the right result as expected without any issues. Then I ran the tool using Xclang option using the following command:

clang -Xclang -load -Xclang my-tool.so -Xclang -plugin -Xclang my-parser source_file.cpp

In this case, first I get all the correct output lines (same as I got with cc1 option), but after the output, I get 2 extra lines:

/tmp/test1-856817.o file not recognized: File truncated
clang:error linker command failed with exit code 1 (use -v to see invocation)

Though my tool is a part of a huge project, I am guessing the code is correct, since it is able to build successfully and also gives the correct output when run throught both cc1 and Xclang options. It's only the 2 extra lines after output in case of Xclang that I am not able to understand.

I am trying to run the tool using Xclang since I want to pass -I option to include some header files which are in the source_file.cpp, and clang documentation discourages using cc1 for this case.

As far as I know, the tool should give the same output in both the cases.

答案1

得分: 1

TL;DR: 不使用 -cc1 时,只需传递 -fsyntax-only

但为什么呢? -cc1 是一个_非常_特殊的标志。要理解这一点,我们需要看一下编译器 clang 扮演的角色,即 gcc。当您运行 gcc 时,它不仅执行编译,还可能执行一些其他任务,包括编译。如果您运行 "gcc foo.c -c -o foo.o",那么它会进行编译,是的,但您也可以运行 "gcc foo.c math.f -o program",然后它会编译您的 C 代码,运行 gfortran 编译您的 Fortran 代码,然后运行 collect2,它又会运行 ld 将这两个生成的 .o 文件链接成一个单独的可执行文件。如果您运行 gcc foo.c,那么它将尝试将其链接到名为 a.out 的程序中。我们将这个最外层的 gcc 称为“驱动程序”,因为它的全部工作就是运行其他程序。GNU 编译器集合的一部分,实际上编译 C 代码的程序被称为 cc1

Clang 遵循了 gcc 的设计。clang 是一个驱动程序,它分叉并运行多个程序。然而,没有 clang-cc1 的等价物,而是再次使用 clang 二进制文件,但第一个标志是 -cc1 标志。请注意,这意味着 clang 表现得好像它是一个完全不同的二进制文件,支持完全不同的标志(不兼容 gcc -cc1 标志,我要补充的是)。-Xclang 的目的是告诉 clang 驱动程序,请将以下标志传递给 clang -cc1 调用,而不是将其解析为 clang 驱动程序的标志。这在精神上类似于 -Wl,-linker-flag-here-Wa,-assembler-flag-here

您可以通过传递 -fsyntax-only 来告诉驱动程序不要汇编或链接。如果您想调试驱动程序,您可以要求它使用 -### 作为您的第一个标志以获取它将要运行的信息。

英文:

TL;DR: when not using -cc1 just pass -fsyntax-only.

But why? -cc1 is a very special flag. To understand we need to look at the compiler clang is impersonating, gcc. When you run gcc, it doesn't merely perform a compilation, it may do a number of tasks including compiling. If you run "gcc foo.c -c -o foo.o" then it compiles, yes, but you may also run "gcc foo.c math.f -o program" and then it compiles your C, runs gfortran to compile your fortran, runs collect2 which runs ld to link the two produced .o files into a single executable. If you run gcc foo.c then it will attempt to link it into a program named a.out. We call this outermost gcc a "driver" because all it does is run other programs. The program that is part of the GNU Compiler Collection which actually compiles C code is called cc1.

Clang follows gcc's design. clang is a driver which forks and runs multiple programs. However, there's no clang-cc1 equivalent, instead it's just clang binary again, but where the first flag is the -cc1 flag. Note that this means that clang behaves as if it were an entirely different binary with entirely different supported flags (and not compatible with the gcc -cc1 flags I might add). The purpose of -Xclang is to tell the clang driver to please pass the following flag along to the clang -cc1 invocation instead of parsing it as a flag to the clang driver. This is similar in spirit to -Wl,-linker-flag-here or -Wa,-assembler-flag-here.

You can tell the driver to not assemble or link by passing -fsyntax-only. If you want to debug the driver, you can ask it for a dump of what it would run with -### as your first flag.

huangapple
  • 本文由 发表于 2023年7月13日 16:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定