cscan - C source scan (C::Scan alternative)
version 0.47
Jean-Damien Durand <jeandamiendurand@free.fr>
This software is copyright (c) 2013 by Jean-Damien Durand.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
cscan - C source scsan
cscan [options] [file] Startup Options: --help Brief help message. --cpprun <argument> Preprocessor run command. --cppflags <argument> Preprocessor flags. --filter <argument> File to look at after preprocessing. --get <argument,...> Dump the result of getting <argument>. --xml Print out an XML view of all declarations and definitions. --out <argument> Redirect any output to this filename. --err <argument> Redirect any error to this filename. --in <argument> Load a full XML view of all declarations and definitions from this filename. --xpath <argument> Dump the result of an xpath query on declarations and definitions. --loglevel <level> A level that has to be meaningful for Log::Log4perl, typically DEBUG, INFO, WARN, ERROR, FATAL or TRACE. --logstderr Logs to stderr or not. --enumType Type for enumerators. Aliased options: --debug Alias to --loglevel DEBUG --info Alias to --loglevel INFO --warn Alias to --loglevel WARN --error Alias to --loglevel ERROR --fatal Alias to --loglevel FATAL --trace Alias to --loglevel TRACE Advanced options: --lazy Instruct the parser to try all alternatives on typedef/enum/identifier --typedef <typedef> Comma separated list of known typedefs --enum <enums> Comma separated list of known enums
This help
cpp run command. Default is the value when perl was compiled, i.e.:
$CPPRUN (if you read this message, do not worry: this is replaced by correct value at run-time)
This option can be repeated.
cpp flags. Default is the value when perl was compiled, i.e.:
$CPPFLAGS (if you read this message, do not worry: this is replaced by correct value at run-time)
File to look at after proprocessing. Defaults to basename of file argument.
cscan is using the preprocessor. Every #include statement in your original source code is telling the preprocessor to look at another file, this is marked down by a line like:
#line ... "information on the file processed"
in the generated output. The --filter argument is used to select which processed files is/are of interest. If $filter is starting with a slash "/" it is assumed to be a full regular expression (including modifier flags). The regexp can be used to handle the case of multiple input files.
In case the file you parse already contains preprocessing information, for example a generated C source code from a source file xxx.w, then you migh want to say: --filter xxx.w, or --filter '/xxx\\.w$/'
Dump the result of getting <argument> using perl module Data::Dumper, except for the astToString. A comma "," is the separator for multiple arguments.
<argument> is exactly one of the C::Scan like methods when such one exist, i.e.:
The AST (Abstract Syntax Tree) in stringified XML format.
All declarations.
Macros with arguments.
Macros without arguments.
Declarations of functions.
Included files.
Definitions of functions.
List of macros
List of parsed functions declarations.
List of strings.
List of hash which contains known typedefs as keys.
Hash which known typedefs as keys.
List of known expansions of typedef.
List of typedefed names.
Hash of parsed extern variable declarations.
List of extern variable declarations.
List of all top-level declarations.
List of all top-level declarations in human-readable format, i.e. a-la cdecl.
Print out an XML view of all declarations and definitions.
The XML will have this structure:
<C> <decls>...</decls> <decls>...</decls> ... <defs>...</defs> <defs>...</defs> </C>
There is one <decl/> element per declaration, one <defs/> element per function definition. Both can have the following attributes:
Return type of a function.
Identifier
Full text used to get this information.
Array modifiers if any (for example: char x[2] will make mod to be: '[2]').
Type of a declarator. In case of a function, the type will contain only eventual stars '*'.
"1" value means this is an 'extern' declaration.
"1" value means this is an 'typedef' declaration.
Declarator initialization, if any. For example, with char *x = "value" init will be the string "value".
"1" means this is an function declaration.
"1" means this is a struct declaration.
"1" means this is a union declaration.
"1" means this is a struct or union declaration. If true, it is guaranteed that one of 'struct' or 'union' attributes is true.
"1" means this is a type declaration. If true, it is guaranteed that one of 'typedef' or 'structOrUnion' attribute is true, and that the 'var' attribute (see below) is false.
"1" means this is a variable declaration. If true, it is guaranteed that the 'type' attribute is false.
Filename where this parsed statement occurs. The filename is derived from the preprocessor output, with no modification.
Line number within filename where is beginning the attribute 'ft'.
The only possible child element is:
Array reference of arguments parsed declarations, which can have same attributes as listed below, and other args children.
Load a full XML view of all declarations and definitions from this filename.
Doing so will prevent any preprocessor call and analysis: cscan will assume this XML has a correct format. Such option exist because analysing preprocessing output takes time, and often, once the analysis is done, it is easier to reload the result to do xpath queries. For instance, first you create the XML:
cscan --out /tmp/file.xml --xml /tmp/file.c
then you do xpath queries on the reloaded output:
cscan --in /tmp/file.xml --xpath "//*[contains(@nm,'x')]" cscan --in /tmp/file.xml --xpath "//*[contains(@nm,'Y')]"
When you use the --in option, the --get option becomes a noop.
Dump the result of an XPath (version 1) query on the XML structure described above. Found nodes are converted to hashes for readibility and printed out. For example, to find all nodes having an identifier named "x":
cscan --xpath "//*[contains(@nm,'x')]" /tmp/file.c
To find all declared strings:
cscan --xpath "//*[starts-with(@init,'\"')]" /tmp/file.c
To find all function definitions that have at least one argument of type "double":
cscan --xpath "//*[@func=\"1\"]/args[@var=\"1\" and @ty=\"double\"]/.." /tmp/file.c
Redirect any output to this filename.
Redirect any error to this filename.
A level that has to be meaningful for Log::Log4perl, typically DEBUG, INFO, WARN, ERROR, FATAL or TRACE. Default is WARN.
Note that tracing Marpa library itself is possible, but only using environment variable MARPA_TRACE /and/ saying --loglevel TRACE.
In case of trouble, typical debugging phases are: --loglevel INFO then: --loglevel DEBUG then: --loglevel TRACE
Shortcut for --loglevel DEBUG
Shortcut for --loglevel INFO
Shortcut for --loglevel WARN
Shortcut for --loglevel ERROR
Shortcut for --loglevel FATAL
Shortcut for --loglevel TRACE
Log to stderr or not. Default is a false value.
Type for enumerators. Default is 'int'.
Instruct the parser to try all alternatives on typedef/enum/identifier. Default is a false value.
Comma separated list of known typedefs. Can be repeated. Default is an empty list.
Comma separated list of known enums. Can be repeated. Default is an empty list.
cscan --get strings /tmp/file.c cscan --get strings,macros --cppflags "-I/tmp/dir1 -DMYDEFINE" /tmp/file.c cscan --get strings,macros --cppflags -I/tmp/dir1 --cppflags -DMYDEFINE /tmp/file.c cscan --get strings --cppflags -I/tmp/dir1 --cppflags -DMYDEFINE --filter '/\.H$/i' /tmp/file.c
The parsing result for the following source code, in filename test.c:
int func1(int x1, double *x2, float *( f1)(int x11, double x12)); int func1(int x1, double *x2, float *( f1)(int x11, double x12)) { char *string = "&"; return 0; }
will be converted to xml using:
cscan --xml test.c
giving:
<C> <decls file="test.c" ft="int func1(int x1, double *x2, float *( f1)(int x11, double x12))" func="1" line="1" nm="func1" rt="int" var="1"> <args file="test.c" ft="int x1" line="1" nm="x1" ty="int" var="1" /> <args file="test.c" ft="double *x2" line="1" nm="x2" ty="double *" var="1" /> <args file="test.c" ft="float *( f1)(int x11, double x12)" func="1" line="1" nm="f1" rt="float *" var="1"> <args file="test.c" ft="int x11" line="1" nm="x11" ty="int" var="1" /> <args file="test.c" ft="double x12" line="1" nm="x12" ty="double" var="1" /> </args> </decls> <defs file="test.c" ft="int func1(int x1, double *x2, float *( f1)(int x11, double x12)) { char *string = "&"; return 0; }" func="1" line="2" nm="func1" rt="int"> <args file="test.c" ft="int x1" line="2" nm="x1" ty="int" var="1" /> <args file="test.c" ft="double *x2" line="2" nm="x2" ty="double *" var="1" /> <args file="test.c" ft="float *( f1)(int x11, double x12)" func="1" line="2" nm="f1" rt="float *" var="1"> <args file="test.c" ft="int x11" line="2" nm="x11" ty="int" var="1" /> <args file="test.c" ft="double x12" line="2" nm="x12" ty="double" var="1" /> </args> </defs> </C>
while the following source code:
struct s1_ { int x; enum {E1, E2} e; struct { long y; double z; char *s[1024][32]; } innerStructure; };
will give the following XML:
<C> <decls enum="1" nm="ANON0" ty="ANON0" type="1"> <args file="test.c" ft="E1" line="3" nm="E1" ty="int" var="1" /> </decls> <decls nm="s1_" struct="1" structOrUnion="1" ty="struct s1_" type="1"> <args file="test.c" ft="int x" line="2" nm="x" ty="int" var="1" /> <args file="test.c" ft="enum {E1, E2} e" line="3" nm="e" ty="ANON0" var="1" /> <args nm="ANON1" struct="1" structOrUnion="1" ty="struct ANON1" type="1"> <args file="test.c" ft="long y" line="5" nm="y" ty="long" var="1" /> <args file="test.c" ft="double z" line="6" nm="z" ty="double" var="1" /> <args file="test.c" ft="char *s[1024][32]" line="7" mod="[1024][32]" nm="s" ty="char *" var="1" /> </args> <args file="test.c" ft="struct { long y; double z; char *s[1024][32]; } innerStructure" line="4" nm="innerStructure" ty="struct ANON1" var="1" /> </decls> <decls file="test.c" ft="struct s1_ { int x; enum {E1, E2} e; struct { long y; double z; char *s[1024][32]; } innerStructure; };" line="1" nm="ANON2" ty="struct s1_" var="1" /> </C>
In the later example you see that anonymous types can be in an arg element. They do not have the attribute "var". Anonymous types are of two categeries: structOrUnion (divided again in struct or union), and enum. Per definition, enum types are always global, wherever and whenever they appear, i.e. they will always be a direct child of <decls/>. On contrary, structOrUnion types always stay in the scope of their declaration.
Any unknown option on the command line is passed through to --cppflags. I.e.:
cscan --get strings,macros --cppflags -I/tmp/dir1 --cppflags -DMYDEFINE /tmp/file.c
and
cscan --get strings,macros -I/tmp/dir1 -DMYDEFINE /tmp/file.c
are equivalent. A restriction is that the filename must be the last argument.
If last argument is absent or equal to '-' and if there is no '--in' option value, then STDIN is assumed.
cdecl is an alias to cscan --get cdecl. Therefore when cdecl --help is invoked, this will mention cscan instead of cdecl.
MarpaX::Languages::C::Scan
Data::Dumper
To install MarpaX::Languages::C::AST, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MarpaX::Languages::C::AST
CPAN shell
perl -MCPAN -e shell install MarpaX::Languages::C::AST
For more information on module installation, please visit the detailed CPAN module installation guide.