class project¶
A CodeSonar or CodeSurfer project.
The project class corresponds to the SDG abstraction.
Get the currently-loaded project with project.current().
project Members¶
project Details¶
-
class
cs.project¶ A CodeSonar or CodeSurfer project.
-
static
current()¶ Get the currently-loaded project.
Return type: projectReturns: The project.Raises: result.ERROR_SDG_NOT_PRESENTif no project is currently loaded.>>> project.current() apitest
-
static
is_loaded()¶ Check: is a project currently loaded?
Return type: bool Returns: Trueif a project is currently loaded,Falseotherwise.>>> project.is_loaded() True
-
static
lookup_project_handle(project_handle)¶ Get a
projectfrom a handle.Parameters: project_handle (str) – The project’s handle, as returned by project.handle().Return type: projectReturns: The projectcorresponding toproject_handle.Raises: result.ELEMENT_NOT_PRESENTif the specifiedproject_handleis not the handle of the current project.Use this function to retrieve a
projectbased on a handle, a string generated byproject.handle()that uniquely identifies theproject. The handle can be stored externally and this method can be used to retrieve theprojectwhenever needed.>>> p = project.current() >>> ph = p.handle() >>> p2 = project.current().lookup_project_handle(ph) >>> p2.name() == p.name() True
-
__cmp__(other)¶ Comparison function for
project, with respect to a stable overall ordering.Parameters: other ( project) – Theprojectobject to compare against.Return type: int Returns: An integer N such that: - N==0 if the two objects compare equal
- N<0 if
self< other - N>0 if
self> other
>>> myproj = project.current() >>> myproj.__cmp__(project.current()) 0
-
__eq__(b)¶ Equality operator for
project.Parameters: b ( project) – Theprojectto compare against.Return type: bool Returns: Trueifselfandbcompare equal,Falseotherwise.>>> myproj = project.current() >>> myproj == project.current() True
-
__ge__(b)¶ Greater-than-or-equal operator for
project.Parameters: b ( project) – Theprojectto compare against.Return type: bool Returns: Trueifself>=b,Falseotherwise.>>> myproj = project.current() >>> myproj >= project.current() True
-
__gt__(b)¶ Greater-than operator for
project.Parameters: b ( project) – Theprojectto compare against.Return type: bool Returns: Trueifself>b,Falseotherwise.>>> myproj = project.current() >>> myproj > project.current() False
-
__hash__()¶ Hash function for
project.Return type: int Returns: The hash value. >>> myproj = project.current() >>> hash(myproj) 42
-
__le__(b)¶ Less-than-or-equal operator for
project.Parameters: b ( project) – Theprojectto compare against.Return type: bool Returns: Trueifself<=b,Falseotherwise.>>> myproj = project.current() >>> myproj <= project.current() True
-
__lt__(b)¶ Less-than operator for
project.Parameters: b ( project) – Theprojectto compare against.Return type: bool Returns: Trueifself<b,Falseotherwise.>>> myproj = project.current() >>> myproj < project.current() False
-
__ne__(b)¶ Inequality operator for
project.Parameters: b ( project) – Theprojectto compare against.Return type: bool Returns: Falseifselfandbcompare equal,Trueotherwise.>>> myproj = project.current() >>> myproj != project.current() False
-
__repr__()¶ Get a representation of a
projectobject that includes information useful for debugging.Return type: str Returns: The string representation. >>> v0 = project.current() >>> repr(v0) 'apitest'
-
__str__()¶ Get a simple string representation of a
projectobject.Return type: str Returns: The string representation. >>> v0 = project.current() >>> str(v0) 'apitest'
-
compunits()¶ Get an iterator over all the compilation units (
compunit) in a project.Return type: project_compunits_iteratorReturns: The initialized project_compunits_iterator.>>> for cu in project.current().compunits(): # iteration managed by project_compunits_iterator.__iter__() ... # and project_compunits_iterator.__next__() ... if cu.is_user(): ... print(cu) ... C:\cygwin\home\alex\testmicros\API\apitest.cpp
-
compunits_vector()¶ Get the compilation units contributing to a project.
Return type: [ compunit]Returns: A list of the compilation units ( compunit) contributing to the project.>>> v0 = project.current() >>> v0.compunits_vector() (<cs.compunit System Initialization, Indirect & Undefined>, <cs.compunit /mys0/alex/trunk/cso-tests/regression/functional/apitest.cpp>)
-
error_count([including_ignored = false])¶ Get the number of errors encountered in compiling the files in a project.
Parameters: including_ignored (bool) – (optional)
Trueto include errors in compilations that are dropped from the build,Falseto exclude those errors.Return type: int
Returns: The total number of errors (summing over all compilation units, subject to
including_ignored).Raises: result.ERROR_COUNT_UNKNOWN_BUT_POSITIVEif there is at least one compiler error but the total number is not known.result.ERROR_COUNT_UNKNOWNif the number of compiler errors is completely unknown.
>>> v0 = project.current() >>> v0.error_count(True) 0
>>> v0 = project.current() >>> v0.error_count() 0
-
file_is_newer(fn)¶ Check: is a file newer than the rest of the project?
Parameters: fn (str) – The pathname of the file to check. Return type: bool Returns: Trueif the file atfnis newer than the rest of the project,Falseotherwise.Raises: result.ERROR_IO_STATiffncannot be found.>>> v0 = project.current() >>> v0.file_is_newer('/mys0/alex/trunk/cso-tests/regression') False
-
find_compunit(id)¶ Retrieve the compilation unit with the specified unique ID.
Parameters: id (int) – The compilation unit ID.
Return type: Returns: The
compunitfound whose unique ID isid.Raises: result.ERROR_UID_NOT_FOUNDif no such compilation unit exists.OverflowError
To get a compilation unit’s ID, use
compunit.get_id().>>> cu = next(c for c in project.current().compunits() if c.is_user()) >>> cu.name() 'C:\\cygwin64\\home\\alex\\apitest.cpp' >>> cuid = cu.get_id() >>> cuid 268435457 >>> cu2 = project.current().find_compunit(cuid) >>> cu == cu2 True
-
find_procedure(procname)¶ Retrieve the
procedurewith the specified verbose name.Parameters: procname (str) – The procedure’s verbose name as returned by procedure.verbose_name().Return type: procedureReturns: The first procedurefound whose verbose name isprocname.Raises: result.PDG_NOT_FOUNDif no such procedure exists.It is possible for a project to have multiple procedures whose name is
procname. For example, two compilation units may each have a static function calledprocname. In this case,project.find_procedure()will return the first one it encounters.>>> proc = project.current().find_procedure('mymalloc(int)') >>> for pt in proc.callers(): ... fl = pt.file_line() ... print(fl[0].normalized_name().split('/')[-1], fl[1]) ... apitest.cpp 37 apitest.cpp 15
-
find_procedure(id)¶ Retrieve the procedure with the specified unique ID.
Parameters: id (int) – The procedure ID. Return type: procedureReturns: The procedurefound whose unique ID isid.Raises: result.NO_SUCH_PDGif no such procedure exists.To get a procedure’s ID, use
procedure.get_id().>>> v0 = project.current() >>> v0.find_procedure(-5) <cs.procedure whoknows(int)>
-
handle()¶ Get a handle for this
project.Return type: str Returns: The project’s handle. Use this function to retrieve a handle to the
projectwhich can be stored externally and used withproject.lookup_project_handle()to retrieve theprojectwhen needed. This handle is valid only for the analysis it was generated from. If you rebuild the project, the handle will no longer be valid.A handle is a string consisting of letters (upper and lower case( and numbers, and could also include the following characters: “+”, “=”, and “_”.
>>> project.current().handle() ''
-
has_unnormalized_c_asts()¶ Check: was the project built to retain unnormalized C/C++ ASTs?
Return type: bool Returns: Trueif the project was built to retain unnormalized C/C++ ASTs,Falseotherwise.Unnormalized C/C++ ASTs are retained if the project is built with RETAIN_UNNORMALIZED_C_AST=Yes. If this returns
False, the following methods will not be able to retrieve ASTs (ast) whose family isast_family.C_UNNORMALIZED.>>> v0 = project.current() >>> v0.has_unnormalized_c_asts() False
-
ignored_compunits()¶ Get a list of the compilation units that are dropped from the build.
Return type: () Returns: A list of the compilation units ( compunit) that are dropped from the build.>>> v0 = project.current() >>> v0.ignored_compunits() ()
-
incremental_capable()¶ Check: was INCREMENTAL_BUILD=Yes specified in the configuration file?
Return type: bool Returns: TrueifINCREMENTAL_BUILD=Yes,Falseotherwise.>>> v0 = project.current() >>> v0.incremental_capable() True
-
is_read_only()¶ Check: is the project read-only?
Return type: bool Returns: Trueif the project is read-only,Falseotherwise.>>> v0 = project.current() >>> v0.is_read_only() True
-
lookup_compunit_handle(compunit_handle)¶ Get a
compunitfrom a handle.Parameters: compunit_handle (str) – The compunit’s handle, as returned by
compunit.handle().Return type: Returns: The
compunitcorresponding tocompunit_handle.Raises: result.ERROR_INVALID_ARGUMENTifcompunit_handleis not a valid handle for a compilation unit in the project.result.ERROR_IO_READ_PAST_EOFif the handle parameter is too short to be a valid handle.
Use this function to retrieve a
compunitbased on a handle: a string generated bycompunit.handle()that uniquely identifies thecompunit. The handle can be stored externally and this method can be used to retrieve thecompunitwhenever needed.>>> cu = next(c for c in project.current().compunits() if c.name().endswith('apitest.cpp')) >>> ch = cu.handle() >>> ch 'goCAgAI=' >>> cu2 = project.current().lookup_compunit_handle(ch) >>> cu == cu2 True
-
lookup_point_handle(point_handle)¶ Get a
pointfrom a handle.Parameters: point_handle (str) – The point’s handle, as returned by
point.handle().Return type: Returns: The
pointcorresponding topoint_handle.Raises: result.ERROR_INVALID_ARGUMENTifpoint_handleis not a valid handle for a point in the project.result.ERROR_IO_READ_PAST_EOFif the handle parameter is too short to be a valid handle.result.ERROR_SDG_NOT_PRESENTif there is no current System Dependence Graph.result.ELEMENT_NOT_PRESENTif there is nopointmatching thepoint_handle.
Use this function to retrieve a
pointbased on a handle: a string generated bypoint.handle()that uniquely identifies thepoint. The handle can be stored externally and this method can be used to retrieve thepointwhenever needed.>>> barfn = next(p for p in project.current().procedures() if p.name()=='bar') >>> pt = next(iter(barfn.points())) >>> pth = pt.handle() >>> pth 'goCAgAILAg==' >>> pt2 = project.current().lookup_point_handle(pth) >>> pt == pt2 True
-
lookup_procedure_handle(proc_handle)¶ Get a
procedurefrom a handle.Parameters: proc_handle (str) – The procedure’s handle, as returned by
procedure.handle().Return type: Returns: The
procedurecorresponding toproc_handle.Raises: result.ERROR_INVALID_ARGUMENTifprocedure_handleis not a valid handle for a procedure in the project.result.ERROR_IO_READ_PAST_EOFif the handle parameter is too short to be a valid handle.result.ERROR_SDG_NOT_PRESENTif there is no current System Dependence Graph.result.ELEMENT_NOT_PRESENTif there is noprocedurematchingproc_handle.
Use this function to retrieve a
procedurebased on a handle: a string generated byprocedure.handle()that uniquely identifies theprocedure. The handle can be stored externally and this method can be used to retrieve theprocedurewhenever needed.>>> barfn = next(p for p in project.current().procedures() if p.name()=='bar') >>> ph = barfn.handle() >>> p2 = project.current().lookup_procedure_handle(ph) >>> p2 == barfn True
-
lookup_sfile_handle(sfile_handle)¶ Get a
sfilefrom a handle.Parameters: sfile_handle (str) – The sfile’s handle, as returned by
sfile.handle().Return type: Returns: The
sfilecorresponding tosfile_handle.Raises: result.ERROR_INVALID_ARGUMENTifsfile_handleis not a valid handle for a source file in the project.result.ERROR_IO_READ_PAST_EOFif the handle parameter is too short to be a valid handle.
Use this function to retrieve a
sfilebased on a handle: a string generated bysfile.handle()that uniquely identifies thesfile. The handle can be stored externally and this method can be used to retrieve thesfilewhenever needed.>>> sf = next(project.current().sfiles()) >>> sh = sf.handle() >>> sf2 = project.current().lookup_sfile_handle(sh) >>> sf == sf2 True
-
lookup_sfileinst_handle(sfileinst_handle)¶ Get a
sfileinstfrom a handle.Parameters: sfileinst_handle (str) – The sfileinst’s handle, as returned by
sfileinst.handle().Return type: Returns: The
sfileinstcorresponding tosfileinst_handle.Raises: result.ERROR_INVALID_ARGUMENTifsfileinst_handleis not a valid handle for a source file instance in the project.result.ERROR_IO_READ_PAST_EOFif the handle parameter is too short to be a valid handle.result.ERROR_SDG_NOT_PRESENTif there is no current System Dependence Graph.result.ELEMENT_NOT_PRESENTif there is nosfileinstmatchingsfileinst_handle.
Use this function to retrieve an
sfileinstbased on a handle: a string generated bysfileinst.handle()that uniquely identifies thesfileinst. The handle can be stored externally and this method can be used to retrieve thesfileinstwhenever needed.>>> sfi = next(project.current().sfiles()).arbitrary_instance() >>> sfh = sfi.handle() >>> sfi2 = project.current().lookup_sfileinst_handle(sfh) >>> sfi == sfi2 True
-
lookup_symbol(symname)¶ Retrieve a
symbolby verbose name.Parameters: symname (str) – The symbol’s verbose name, as returned by symbol.verbose_name().Return type: symbolReturns: The symbol.Raises: result.ELEMENT_NOT_PRESENTif there is no symbol associated withsymname.>>> v0 = project.current() >>> v0.lookup_symbol('foo') <cs.symbol foo>
-
lookup_symbol_handle(symbol_handle)¶ Get a
symbolfrom a handle.Parameters: symbol_handle (str) – The symbol’s handle, as returned by
symbol.handle().Return type: Returns: The
symbolcorresponding tosymbol_handle.Raises: result.ERROR_INVALID_ARGUMENTifsymbol_handleis not a valid handle for a symbol in the project.result.ERROR_IO_READ_PAST_EOFif the handle parameter is too short to be a valid handle.result.ERROR_SDG_NOT_PRESENTif there is no current System Dependence Graph.result.ELEMENT_NOT_PRESENTif the search for theprocedurecontaining thissymbolfails.
Use this function to retrieve a
symbolbased on a handle: a string generated bysymbol.handle()that uniquely identifies thesymbol. The handle can be stored externally and this method can be used to retrieve thesymbolwhenever needed.>>> s = project.current().lookup_symbol('foo') >>> sh = s.handle() >>> s2 = project.current().lookup_symbol_handle(sh) >>> s == s2
-
name()¶ Get the name of a project.
Return type: str Returns: The project name (a string). >>> v0 = project.current() >>> v0.name() 'apitest'
-
param_symbol(num)¶ Retrieve
$param_NUM: thesymbolrepresenting the NUM’th formal parameter.Parameters: num (int) – The number of the parameter to be retrieved (counting from the left, starting from 1).
Return type: Returns: The
$param_numsymbol.Raises: result.ELEMENT_NOT_PRESENTif $param_NUM does not exist.OverflowError
The
$param_nsymbols have global scope, no type, and do not belong to any specific function.>>> v0 = project.current() >>> v0.param_symbol(2) <cs.symbol $param_2>
-
prj_files_directory()¶ Get the absolute file system path for the project analysis directory (pfilesname.prj_files) for a project.
Return type: str Returns: The file system path of the .prj_files directory (a string). If called from within a slave process of a distributed analysis, this function can return a file system path that only exists on the computer running the master process (analysis master or daemon master). Even if the two processes are on the same machine, the slave may be running as a different user and may not have permission to access the directory.
>>> v0 = project.current() >>> v0.prj_files_directory() '/mys0/alex/trunk/cso-tests/regression/functional/apitest.test/apitest.prj_files'
-
procedures()¶ Get an iterator over all the procedures (
procedure) in a project.Return type: project_procedures_iteratorReturns: The initialized project_procedures_iterator.>>> for proc in project.current().procedures(): # iteration managed by project_procedures_iterator.__iter__() ... # and project_procedures_iterator.__next_() ... if proc.callers_count()>0 and proc.get_kind() == procedure_kind.USER_DEFINED: ... print(proc, proc.callers_count()) ... mymalloc 2 bar 4
-
procedures_vector()¶ Get the procedures defined in a project.
Return type: [ procedure]Returns: A list of the procedures ( procedure) defined in the project.>>> v0 = project.current() >>> v0.procedures_vector() (<cs.procedure #System_Initialization>, <cs.procedure whoknows(int)>, <cs.procedure #File_Initialization>, <cs.procedure mymalloc>, <cs.procedure bar>, <cs.procedure foo>)
-
root_directories()¶ Get an iterator over the set of root directories (
directory) deduced by inspecting all source file paths in the project.Return type: project_root_directories_iteratorReturns: The initialized project_root_directories_iterator.>>> for rd in project.current().root_directories(): # iteration managed by project_root_directories_iterator.__iter__() ... # and project__root_directories_iterator.__next__() ... print(rd) ... C:\cygwin\home\alex\ C:
-
root_directory_count()¶ Get the number of root directories in the project.
Return type: int Returns: The number of root directories in the project. >>> v0 = project.current() >>> v0.root_directory_count() 2
-
sfiles()¶ Get an iterator over all the source files (
sfile) in a project.Return type: project_sfiles_iteratorReturns: The initialized project_sfiles_iterator.>>> for sf in project.current().sfiles(): # iteration managed by project_sfiles_iterator.__iter__() ... # and project_sfiles_iterator.__next__() ... if 'apitest' in sf.name(): ... print(sf) ... C:\cygwin\home\alex\testmicros\API\apitest.cpp C:\cygwin\home\alex\testmicros\API\apitest.h
-
symbols()¶ Get an iterator over the symbols (
symbol) in a project.Return type: symbol_universe_iteratorReturns: The initialized symbol_universe_iterator.>>> for sym in project.current().symbols(): # iteration managed by symbol_universe_iterator.__iter__() ... # and symbol_universe_iterator.__next__() ... if sym.is_string() and sym.get_compunit().is_user(): ... print(sym, sym.represented_string()) ... #string1 bar #string0 foo
-
token_search(q)¶ Execute the specified cross-reference query over the tokens in a project.
Parameters: q ( xr_query) – Anxr_querythat fully specifies the search conditions.Return type: xr_query_iteratorReturns: An xr_query_iteratorover the query results (xr_tuple).>>> q = xr_query() >>> q.add_term_filter('whoknows') >>> for t in project.current().token_search(q): # iteration managed by xr_query_iterator.__iter__() ... # and xr_query_iterator.__next__() ... ... print(t.get_file().normalized_name().split('/')[-1], ... t.get_line(), t.get_kind_role()) ... apitest.cpp 5 func_definition apitest.cpp 6 func_call
-
warning_count([including_ignored = false])¶ Get the number of compiler warnings encountered in compiling the files in a project.
Parameters: including_ignored (bool) – (optional)
Trueto include warnings in compilations that are dropped from the build,Falseto exclude those warnings.Return type: int
Returns: The total number of warnings (summing over all compilation units, subject to
including_ignored).Raises: result.ERROR_COUNT_UNKNOWN_BUT_POSITIVEif there is at least one compiler warning but the total number is not known.result.ERROR_COUNT_UNKNOWNif the number of compiler warnings is completely unknown.
Note that this method retrieves the number of compiler warnings: it is not related to CodeSonar warnings.
>>> v0 = project.current() >>> v0.warning_count(True) 0
>>> v0 = project.current() >>> v0.warning_count() 0
-
static