<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hello,</p>
<p><span id="result_box" class="" lang="en"><span class="">My goal
is to design a location checker of functions and methods in C
++ source code.</span></span></p>
<p><span id="result_box" class="" lang="en"><span class=""><span
id="result_box" class="" lang="en"><span class="">In a
second step, I want to be able to modify the source code.</span></span></span></span></p>
<p><span id="result_box" class="" lang="en"><span class=""><span
id="result_box" class="" lang="en"><span class=""></span></span>My
question is not whether it is good or not to do that.</span> <span>I
want to do it.</span></span></p>
<p><span id="result_box" class="" lang="en"><span class="">I want to
write this program in Python with the module clang.</span></span></p>
<p><span id="result_box" class="" lang="en"><span class="">I managed
to recover for each function and method, its placement </span></span><span
id="result_box" class="" lang="en"><span class=""><span
id="result_box" class="short_text" lang="en"><span class="">(Start
offset and end offset in source file)</span></span>.</span></span></p>
<p><span id="result_box" class="" lang="en"><span class="">But the
associated comment </span></span><span id="result_box"
class="" lang="en"><span class=""><span id="result_box" class=""
lang="en"><span class="">(raw_comment) </span></span>is not
taken into account.</span></span></p>
<p><span id="result_box" class="short_text" lang="en"><span class="">*
Case 1:</span></span></p>
<p><span id="result_box" class="short_text" lang="en"><span class="">Actually:<br>
</span></span></p>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">/*! \brief function1
is good<br>
</span></span></font> <font face="Courier New, Courier,
monospace"><span id="result_box" class="short_text" lang="en"><span
class=""> \param a First param</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""> \param b Second
param</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">*/</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">[START_OFFSET]bool
X::function1(int a, int b)</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">{</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""> ...</span></span></font><br>
<span id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace">}[END_OFFSET]</font><br>
<br>
It should:<br>
</span></span><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">
<font face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class="">[START_OFFSET]</span></span></font>/*! \brief
function1 is good<br>
</span></span></font> <font face="Courier New, Courier,
monospace"><span id="result_box" class="short_text" lang="en"><span
class=""> \param a First param</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""> \param b Second
param</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">*/</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">bool
X::function1(int a, int b)</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class="">{</span></span></font><br>
<font face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""> ...</span></span></font><br>
<span id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace">}[END_OFFSET]<br>
<br>
* Case 2:<br>
<br>
Actually:<br>
<br>
</font></span></span><span id="result_box" class="short_text"
lang="en"><span class=""><font face="Courier New, Courier,
monospace"><font face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class="">[START_OFFSET]</span></span></font></span></span></font>int
_y</font></span></span><span id="result_box"
class="short_text" lang="en"><span class=""><font face="Courier
New, Courier, monospace">;</font></span></span><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace">[END_OFFSET]</font></span></span></font></span></span>
//!< Brief description after the member<br>
<br>
</font></span></span><span id="result_box" class="short_text"
lang="en"><span class="">It should:<br>
<br>
</span></span><span id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier, monospace"><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class="">[START_OFFSET]</span></span></font></span></span></font>int
_y</font></span></span><span id="result_box"
class="short_text" lang="en"><span class=""><font face="Courier
New, Courier, monospace">;</font></span></span><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""></span></span></font></span></span> //!<
Brief description after the member</font></span></span><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace">[END_OFFSET]<br>
<br>
<br>
<br>
To calculate the full location, I would need
"node.extent.raw_comment.start.offset" and
"node.extent.raw_comment.end.offset".<br>
</font></span></span></font></span></span></font></span></span><br>
<span id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace"><span id="result_box"
class="short_text" lang="en"><span class="">Or,
more simple, I would need </span></span></font></span></span></font></span></span></font></span></span><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace"><span id="result_box"
class="short_text" lang="en"><span
class=""><font face="Courier New,
Courier, monospace"><span
id="result_box"
class="short_text" lang="en"><span
class=""><font face="Courier
New, Courier, monospace">"node.full_extent.start.offset</font></span></span></font></span></span></font></span></span>"
and </span></span></font></span></span></font></span></span></font></span></span><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace"><span id="result_box"
class="short_text" lang="en"><span
class=""><font face="Courier New,
Courier, monospace"><span
id="result_box"
class="short_text" lang="en"><span
class=""><font face="Courier
New, Courier, monospace"><span
id="result_box"
class="short_text"
lang="en"><span class=""><span
id="result_box"
class="short_text"
lang="en"><span
class=""><font
face="Courier New,
Courier,
monospace"><span
id="result_box"
class="short_text" lang="en"><span class=""><font face="Courier New,
Courier,
monospace"><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier
New, Courier,
monospace">"node.full_extent.end.offset"</font></span></span></font></span></span></font></span></span></span></span></font></span></span></font></span></span></font></span></span><br>
</span></span>NB: START_OFFSET = </font></span></span></font></span></span></font></span></span><span
id="result_box" class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text" lang="en"><span
class=""><font face="Courier New, Courier,
monospace"><span id="result_box"
class="short_text" lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text"
lang="en"><span class=""><font
face="Courier New, Courier, monospace"><span
id="result_box" class="short_text"
lang="en"><span class=""><font
face="Courier New, Courier,
monospace">node.extent.start.offset<br>
<br>
How to do? Thanks.<br>
<br>
Minitchoup<br>
<br>
<br>
<br>
My Python script:<br>
<br>
[BEGIN]<br>
# -*- coding: utf-8 -*-<br>
import os<br>
import os.path<br>
import clang.cindex<br>
from clang.cindex import
CursorKind as CK<br>
<br>
PRJ_DIR =
os.path.abspath("test")<br>
FILENAME = "test1.cpp"<br>
<br>
def filename(node):<br>
if hasattr(node, 'location')
and hasattr(node.location,
'file') and
hasattr(node.location.file,
'name'): <br>
return
node.location.file.name<br>
return None<br>
<br>
def parse(node, lvl):<br>
""" Find all references to
the type named 'typename'<br>
"""<br>
fn = str(filename(node))<br>
if fn != "None" and
os.path.commonprefix((os.path.abspath(fn),
PRJ_DIR)) != PRJ_DIR:<br>
return<br>
if True or node.kind in
(CK.CLASS_DECL, CK.CONSTRUCTOR,
CK.DESTRUCTOR, CK.CXX_METHOD,
CK.FIELD_DECL,
CK.FUNCTION_DECL):<br>
comment =
node.raw_comment<br>
print '%s Found %s [%s
<%d-%d> kind=%s,
filename=%s, spelling=%s,
line=%s, col=%s]%s' % (' ' *
(lvl * 4), <br>
node.displayname, <br>
code.is_definition(), <br>
node.extent.start.offset, <br>
node.extent.end.offset, <br>
node.kind,<br>
fn, <br>
node.location.line, <br>
node.location.column, <br>
node.location.offset, <br>
comment)<br>
# Recurse for children of
this node<br>
for c in
node.get_children():<br>
parse(c, lvl + 1)<br>
<br>
OLD_DIR = os.getcwd()<br>
os.chdir(PRJ_DIR)<br>
index =
clang.cindex.Index.create()<br>
tu = index.parse(FILENAME)<br>
print 'Translation unit:',
tu.spelling<br>
parse(tu.cursor, 0)<br>
os.chdir(OLD_DIR)<br>
[END]<br>
<br>
</font></span></span></font></span></span></font></span></span></font></span></span></font></span></span></font></span></span>
</body>
</html>