There are some c/cpp parsers in python, that might be used for your specific purpose. However, I have never used those yet.
For a similar objective, I wrote a python script that adds "doxygen-headers" to the methods in the header-file majorly. I have used regular expression, and I have a version that adds "doxygen headers" in the source file for the method-definitions (use RE_M_DEFINITION, at method-lookup).
Code for your reference, as below:
genDoxygenC.py
import os
import sys
import re
RE_MULTI_LINE_PARAMS = ".*"
RE_M_DEFINITION = r'[A-Za-z0-9*]*\s*[A-Za-z0-9_*]+\s*[A-Za-z0-9_~:*]+\(.*\)\s*\{\s*.*?\s*\}'
RE_M_DECLERATION = r"[A-Za-z0-9*]*\s*[A-Za-z0-9_*]+\s+[A-Za-z0-9_~*]+\s*\(%s\)\s*;"%RE_MULTI_LINE_PARAMS
cmdList = ["for","if","while","switch","else"];
class EErrors() :
IncorrectUsage, FileOpenError = range(2)
def handleException(e, mssg) :
if e == EErrors.IncorrectUsage :
print "Usage : "+mssg
elif e == EErrors.FileOpenError :
print "Unable to open \"" + mssg + "\" file !"
sys.exit(2)
def frameDoxygenHeader(param_count, paramList) :
commentStr = "/**\n * @brief \n"
if param_count > 0 :
for param in paramList:
commentStr = commentStr + " * @param \n"
commentStr = commentStr + " * @return \n */ \n"
return commentStr
def addDoxygenComment(file_name, funcList) :
try:
fh = open(file_name, 'rb')
f_old = open(file_name, 'r+')
except:
handleException(EErrors.FileOpenError, file_name)
f_new = open(out_file_name, "w")
final_loc = 0
next_split_loc = 0
last_write_loc = 0
fContent = str(f_old.read())
for func in funcList:
SEARCH_TEXT = func
print "SEARCH_TEXT "+SEARCH_TEXT
fsize = os.path.getsize(file_name)
bsize = fsize
word_len = len(SEARCH_TEXT)
fh.seek(0)
paramListStr = re.findall(r'\(.*\)', SEARCH_TEXT)
paramListStr[0] = paramListStr[0].replace('(','')
paramListStr[0] = paramListStr[0].replace(')','')
paramList = paramListStr[0].split(",")
comment_text = frameDoxygenHeader(len(paramList),paramList)
while True:
found = 0
pr = fh.read(bsize)
pf = pr.find(SEARCH_TEXT, next_split_loc)
if pf > -1:
found = 1
pos_dec = fh.tell() - (bsize - pf)
fh.seek(pos_dec + word_len)
bsize = fsize - fh.tell()
print "Case-I:"+str(fh.tell())
if fh.tell() < fsize:
seek = fh.tell() - word_len + 1
print "seek"+str(seek)
fh.seek(seek)
if 1==found:
final_loc = seek
next_split_loc = final_loc + word_len - 1
print "loc: "+str(final_loc)
print "Case-IIa:"+str(fh.tell())
else:
break
if final_loc != -1 :
if last_write_loc < final_loc :
f_new.write(fContent[last_write_loc:final_loc-1]);
f_new.write(comment_text);
f_new.write(fContent[final_loc-1:next_split_loc])
last_write_loc = next_split_loc
final_loc = -1
else:
print "method not found !!"
if last_write_loc < len(fContent) :
f_new.write(fContent[last_write_loc:]);
f_new.close()
f_old.close()
argc = len(sys.argv)
if (argc == 1 or argc >2) :
handleException(EErrors.IncorrectUsage, "genDoxygenC.py <cpp source file>")
else :
fname = sys.argv[1]
out_file_name = fname+'.doxygen'
fcontent=''
try:
fh = open(fname)
fcontent = fh.read()
except:
handleException(EErrors.FileOpenError, fname)
funcList = re.findall(RE_M_DECLERATION, fcontent, re.VERBOSE)
fh.close()
funcListCopy = funcList
for fStr in funcListCopy :
fStr = fStr.lstrip()
startW = fStr.partition(' ')[0]
startW = fStr.partition('(')[0]
if startW in cmdList :
funcList.remove(fStr)
addDoxygenComment(fname, funcList)
Usage :: ./genDoxygenC.py file.h
This will generate
file.h.doxygen
and then, you can probably check the doxygen-headers-added-file, with original-header-file using any diff-tool.
Example : meld file.h file.h.doxygen
Note:: The script might skip constructors, with new versions definitions/declarations like;
S() : n(7)) {};