c++ - 如何使用 Frama-Clang 解析 C++ 程序
问题描述
我已经安装了 Frama-c 的插件 Frama-Clang 来解析 C++ 程序。但是,我不知道如何正确使用它。我用一个非常简单的 c++ 程序尝试了它,但失败了。这是test.cpp的代码:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
我使用命令 frama-c test.cpp 并得到以下错误:
[kernel] Parsing test.cpp (external front-end)
In file included from test.cpp:1:
In file included from /home/server3/.opam/system/share/frama-c/frama-clang/libc++/iostream:29:
/home/server3/.opam/system/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> {
^
test.cpp:5:10: note: in instantiation of template class 'std::basic_ostream<char, std::char_traits<char> >' requested here
cout << "Hello, world!" << endl;
^
/home/server3/.opam/system/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 "test.cpp" that has errors.
[kernel] Frama-C aborted: invalid user input.
有人可以告诉我如何成功解析它吗?
解决方案
您的用法是正确的:只需给它一个.cpp
文件,它就会尝试解析它。
但是,由于 STL 的大小和复杂性,使用 Hello World<iostream>
并不是最好的示例:您的程序在预处理后包含 18k 到 28k 行(取决于我使用的是g++
还是clang
)。
如 Frama-Clang 网页所示,
Frama-Clang 目前处于开发的早期阶段。众所周知,它是不完整的 (...)
处理 STL 确实是支持 C++ 的主要困难之一,目前正在开发中。
如果您尝试使用非 STL 文件,您应该会得到更好的结果。支持 STL 的一部分,但没有完整的列表来说明哪些类是哪些类是不支持的(因为这是不断发展的)。
例如,下面的玩具示例使用std::exception
、模板和类,只需运行frama-c test.cpp
.
#include <exception>
class empty_stack: public std::exception {
virtual const char* what() const throw() {
return "stack is empty!";
}
};
class full_stack: public std::exception {
virtual const char* what() const throw() {
return "stack is full!";
}
};
template <class T>
class Stack {
private:
T elems[10];
unsigned index;
public:
Stack() {
index = 0;
}
void push(T const&);
T pop();
T top() const;
bool empty() const {
return index == 0;
}
};
template <class T>
void Stack<T>::push (T const& elem) {
if (index >= 10) throw new full_stack();
elems[index++] = elem;
}
template <class T>
T Stack<T>::pop () {
if (index == 0) throw new empty_stack;
return elems[--index];
}
template <class T>
T Stack<T>::top () const {
if (index == 0) throw new empty_stack;
return elems[index-1];
}
int main() {
try {
Stack<int> intStack;
intStack.push(7);
intStack.push(42);
return intStack.pop();
} catch (char* ex) {
return -1;
}
}
推荐阅读
- django - 姜戈 | 2外键到自己的模型显示错误| 自己的一个外键就可以正常工作
- python - Pandas 循环搜索词以标记包含它们的行
- python - 根据子列表中特定元素的值从列表列表中删除子列表
- lua - 如何在不拉伸的情况下调整子对象的大小?
- c++ - 这是在多线程程序中处理 SIGFPE、SIGSEGV 等信号的符合 POSIX 的实现吗?
- php - Laravel:错误未显示
- r - 获取 R 中每一行中每个唯一值的计数
- c++ - 在 C++ 中排序时的地图会产生巨大的错误
- excel - 有没有办法用脚本字典中的值填充用户窗体列表框?
- python - 使用Olympic.csv。我试图找到“哪个国家的金牌数量最多。获得整排的结果