Closed ainne26 closed 5 years ago
Frama-Clang is used for C++. If you only need C, you don't need Frama-Clang.
Frama-C builds its internal AST after parsing a C program, which can be accessed using visitors. It can be pretty-printed in C syntax with -print
(but then it is no longer an AST), or it can be manipulated using OCaml code by writing visitors (there is a section about visitors in the Plug-In Development Guide).
Or, if you just want a "pretty-printed AST" in raw mode, that is, an OCaml-like print, you can try to use Cil_types_debug
, but the result is non-parsable and too large for any practical use other than very specific debugging. For instance, the expression 1 + 2
becomes:
{eid=4;
enode=
BinOp(PlusA,
{eid=2;
enode=Const(CInt64(Integer(1),IInt,Some(1)));
eloc=<omitted>
},
{eid=3;
enode=Const(CInt64(Integer(2),IInt,Some(2)));
eloc=<omitted>
},
TInt(IInt,[])
);
eloc=<omitted>}
So, for any reasonable usage, you need to define your own visitor.
For C++, you need Frama-Clang to convert your C++ code into a Frama-C AST, and then everything can be treated as if you had C (with a few extra nodes, such as constructors, exceptions, etc).
Sorry for inconvenience.. Sir, I am a PG student and doing my research on code clone detection. For that purpose I have to generate normalized ASTs of C code as well as C++ code . You said for C++ code frama-clang is required to converst C++ code into Frama-C AST. My question is which commands are needed to convert in frama-clang to convert C++ code into frama-c AST and pretty print them. My OS is ubuntu-18.04.2 Please give me some suggestions regarding this.
frama-clang
is a front-end for Frama-C. When frama-clang is installed, you simply give it a cpp file and Frama-C will recognize the extension, call the Frama-Clang plug-in for it to parse the file, and it will construct an AST based on Frama-C's C AST (with a few extensions). If you pretty-print the result, you'll get something that looks like C.
So, to sum up, if you have Frama-C with the Frama-Clang plugin, all you do is:
frama-c file.cpp
And if everything goes well, Frama-C will print something like:
[kernel] Parsing file.cpp (with preprocessing)
That's all. Note however that many parts of the STL are still unsupported in Frama-Clang, so you may end up having errors when trying to parse a small program, if it uses them.
Otherwise, if it succeeds, you can write your plug-in to visit the Frama-C AST and do some normalizations or something to try to do your code clone detection.
I'm not sure I understand your question. Once you have managed to parse C and/or C++ file(s) with Frama-C, the AST has been generated. And as mentioned by @maroneze above, Frama-C's API offers many possibilities to interact with it. However, there are currently only two textual representations of the AST that Frama-C can output: either a pretty-printed form which gives you a compilable C file, or the debug format which is roughly equivalent to the OCaml internal representation. The latter might be closer to what you have in mind when speaking of "generating normalized AST", but keep in mind that, as its name suggests, it is mainly meant for debugging purposes.
That said, if you want to use a normalized version of the code to compute some kind of distance between two pieces of code, the pretty-printed C might be sufficient.
float Type1a_Krawitz(int n)
{
float __retres;
int p = -1;
int sum = 0;
p = 0;
while (p < n) {
/*@ assert Value: signed_overflow: sum + p ≤ 2147483647; */
sum += p;
p ++;
}
if (n == 0) {
__retres = (float)sum;
goto return_label;
}
else {
__retres = (float)(sum / n);
goto return_label;
}
return_label: return __retres;
}
/*@ assigns \result;
assigns \result \from *(x_0 + (0 ..)), x_1; */
extern int ( /* missing proto */ printf)(char const *x_0, double x_1);
void main(void)
{
float tmp;
tmp = Type1a_Krawitz(4);
printf("Type1a_Krawitz: %lf \n",(double)tmp);
return;
}
Is the above given code is AST of C code? If yes then how can I generate its dot file and view it graphically
as already said several times above, this is the representation of Frama-C AST as a C (+ACSL) file. The AST itself is simply a data structure within Frama-C internal state, and if the output as C code does not suit your needs, you'll need to write OCaml code to access it. In particular, the already mentioned plugin development guide features in its section 2.4 an small example of a script that will output a very crude version of the control-flow graph for each function in the development in dot
format. This might be useful as a starting point.
Okay thank you so much.
I am a beginner in using frama-c. My operating system is Ubuntu. I have to generate Abstract syntax trees (ASTs) of C code using frama-clang. I do not know which commands have to used for generating ASTs from C source code. If anyone know about that please help me.