[cfe-users] How to recover the full extend of a node (full = with his raw_comment)
minitchoup@cegetel.net via cfe-users
cfe-users at lists.llvm.org
Thu Aug 17 12:46:43 PDT 2017
Hello,
My goal is to design a location checker of functions and methods in C ++
source code.
In a second step, I want to be able to modify the source code.
My question is not whether it is good or not to do that. I want to do it.
I want to write this program in Python with the module clang.
I managed to recover for each function and method, its placement (Start
offset and end offset in source file).
But the associated comment (raw_comment) is not taken into account.
* Case 1:
Actually:
/*! \brief function1 is good
\param a First param
\param b Second param
*/
[START_OFFSET]bool X::function1(int a, int b)
{
...
}[END_OFFSET]
It should:
[START_OFFSET]/*! \brief function1 is good
\param a First param
\param b Second param
*/
bool X::function1(int a, int b)
{
...
}[END_OFFSET]
* Case 2:
Actually:
[START_OFFSET]int _y;[END_OFFSET] //!< Brief description after the member
It should:
[START_OFFSET]int _y;//!< Brief description after the member[END_OFFSET]
To calculate the full location, I would need
"node.extent.raw_comment.start.offset" and
"node.extent.raw_comment.end.offset".
Or, more simple, I would need "node.full_extent.start.offset" and
"node.full_extent.end.offset"
NB: START_OFFSET = node.extent.start.offset
How to do? Thanks.
Minitchoup
My Python script:
[BEGIN]
# -*- coding: utf-8 -*-
import os
import os.path
import clang.cindex
from clang.cindex import CursorKind as CK
PRJ_DIR = os.path.abspath("test")
FILENAME = "test1.cpp"
def filename(node):
if hasattr(node, 'location') and hasattr(node.location, 'file') and
hasattr(node.location.file, 'name'):
return node.location.file.name
return None
def parse(node, lvl):
""" Find all references to the type named 'typename'
"""
fn = str(filename(node))
if fn != "None" and os.path.commonprefix((os.path.abspath(fn),
PRJ_DIR)) != PRJ_DIR:
return
if True or node.kind in (CK.CLASS_DECL, CK.CONSTRUCTOR,
CK.DESTRUCTOR, CK.CXX_METHOD, CK.FIELD_DECL, CK.FUNCTION_DECL):
comment = node.raw_comment
print '%s Found %s [%s <%d-%d> kind=%s, filename=%s,
spelling=%s, line=%s, col=%s]%s' % (' ' * (lvl * 4),
node.displayname,
code.is_definition(),
node.extent.start.offset,
node.extent.end.offset,
node.kind,
fn,
node.location.line,
node.location.column,
node.location.offset,
comment)
# Recurse for children of this node
for c in node.get_children():
parse(c, lvl + 1)
OLD_DIR = os.getcwd()
os.chdir(PRJ_DIR)
index = clang.cindex.Index.create()
tu = index.parse(FILENAME)
print 'Translation unit:', tu.spelling
parse(tu.cursor, 0)
os.chdir(OLD_DIR)
[END]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20170817/0fd75ca7/attachment.html>
More information about the cfe-users
mailing list