使用Frama-C分析一个简单的C ++程序

时间:2017-10-12 14:44:51

标签: c++ static-analysis clang++ frama-c

我从https://learnxinyminutes.com/docs/c++/的一个很棒的教程开始学习C ++,并希望在Frama-C中分析一个显示引用的最简单的例子:

using namespace std;
#include <iostream>
#include <string>

int main() {
    string foo = "I am foo";
    string bar = "I am bar";

    string& fooRef = foo; // This creates a reference to foo.
    fooRef += ". Hi!"; // Modifies foo through the reference
    cout << fooRef; // Prints "I am foo. Hi!"

    // Doesn't reassign "fooRef". This is the same as "foo = bar", and
    //   foo == "I am bar"
    // after this line.
    cout << &fooRef << endl; //Prints the address of foo
    fooRef = bar;
    cout << &fooRef << endl; //Still prints the address of foo
    cout << fooRef;  // Prints "I am bar"

    //The address of fooRef remains the same, i.e. it is still referring to foo.
    return 0;
}

我编译并安装了名为“Frama-Clang”的Frama-C C ++插件。 现在,当我运行frama-c时,我会在输出中收到警告和错误:

$ frama-c refs.cc 
[kernel] Parsing FRAMAC_SHARE/libc/__fc_builtin_for_normalization.i (no preprocessing)
[kernel] Parsing refs.cc (external front-end)
refs.cc:13:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
                ^
In file included from refs.cc:14:
In file included from /usr/share/frama-c/frama-clang/libc++/iostream:29:
/usr/share/frama-c/frama-clang/libc++/ostream:31:40: error: implicit instantiation of undefined template 'std::basic_ios<char, std::char_traits<char> >'
  class basic_ostream : virtual public basic_ios<charT,traits> {
                                       ^
refs.cc:23:7: note: in instantiation of template class 'std::basic_ostream<char, std::char_traits<char> >' requested here
        cout << fooRef; // Prints "I am foo. Hi!"
             ^
/usr/share/frama-c/frama-clang/libc++/iosfwd:37:68: note: template is declared here
  template <class charT, class traits = char_traits<charT> > class basic_ios;
                                                                   ^
code generation aborted due to one compilation error
[kernel] user error: Failed to parse C++ file. See Clang messages for more information
[kernel] user error: stopping on file "refs.cc" that has errors.
[kernel] Frama-C aborted: invalid user input.

有什么问题?

(Frama-C是从版本20170501 +磷+ dfsg-2的debian测试库安装的)

1 个答案:

答案 0 :(得分:4)

首先,我想指出Frama-Clang page上的警告:

  

Frama-Clang目前处于发展的早期阶段。众所周知,它是不完整的,没有任何无错误保证。

因此,如果您还不熟悉C ++,我建议立即开始使用Frama-Clang可能是一项非常大的工作。

也就是说,正如评论中提到的那样,问题是Frama-Clang中的STL支持是最小的(特别是,看起来无辜的iostream并不是最容易处理的代码。来到模板)。

使用frama-c -cxx-nostdinc refs.cc可能会更好运,它将使用您系统的标准库而不是Frama-Clang附带的库:这至少会让clang对代码进行类型检查。但是,绝对不能保证Frama-Clang本身能够理解该库提供的所有结构。