llvm版本5.0.0
我磨损此代码,并使用clang/llvm构建。但是,我不明白为什么将此代码转换为无限循环。
此代码是我用于构建的C 代码。
#include <stdio.h>
int foo()
{
for (int j= 0; j < 23; j++)
putchar('a');
}
int main()
{
foo();
}
我使用了以下命令行。
clang -O0 a.cpp // a.out not working
clang -O1 a.cpp
-O2 -O3 ... also
我也可以在llvm-ir中找到错误。
clang -S -O1 -emit-llvm a.cpp
clang -S -O1 -mllvm -disable-llvm-optzns -emit-llvm a.cpp
+ opt -S -O1 a.ll
define i32 @_Z3foov() local_unnamed_addr #0 {
entry:
br label %for.cond
for.cond: ; preds = %for.cond, %entry
%call = tail call i32 @putchar(i32 97)
br label %for.cond
}
但是此代码效果很好。
int main()
{
for (int j= 0; j < 23; j++)
putchar('a');
}
您缺少函数上的返回语句int foo()
和int main()
。这很可能导致ISO C 标准第6.6.3节中指定的未定义行为:
流出函数末尾 价值;这导致价值退还的行为不确定 功能。
您应该在clang -O0 a.cpp
a.cpp:7:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
这是一个应该适合您的版本:
#include <stdio.h>
#include <stdlib.h>
int foo()
{
for (int j= 0; j < 23; j++)
putchar('a');
return 0;
}
int main()
{
foo();
return EXIT_SUCCESS;
}