代码之家  ›  专栏  ›  技术社区  ›  Simone Margaritelli

有自动生成doxygen注释块的软件吗?

  •  10
  • Simone Margaritelli  · 技术社区  · 16 年前

    我正在开发一个大型C++程序,现在我决定用DOXYGEN来记录它。 有很多类、方法、函数、宏等等。因此,我正在搜索能够扫描源代码树并在每个“文档项”上插入doxygen注释块的软件,以便以后编辑它们并添加方法描述等详细信息。

    有这样的软件吗?

    我使用gnu/linux和code::blocks ide,因此不需要visual studio插件。

    6 回复  |  直到 8 年前
        1
  •  1
  •   Joris Timmermans    16 年前

    您还可以设置doxygen来提取未记录的项——这可能可以做您想要做的事情,而不需要向代码中添加任何注释块。

    之后,您可以创建模板/宏(取决于您的IDE)来为每种类型的项创建预格式化的块,同时您可以慢慢地逐个完成记录项的代码。

    [编辑] 如果您使用的是visual studio,可以对文件中的类和其他构造进行一些自省,这可能会有所帮助。或者看看 Doxycomment -可能是你想要的。

        2
  •  6
  •   Matthieu M.    16 年前

    我在这里很困惑。

    自动生成评论的目的是什么?

    评论旨在带来附加价值:

    /**
     * \brief: finds the person based on its name
     * \param: name, the name of the person
     * \result: the person
     */
    Person findPerson(Name name);
    

    只不过是代码混乱,堵塞了我宝贵的屏幕遗产。而且这几乎是可以自动生成的不幸的是…特别要注意的是,如果函数找不到那个人,我不知道会发生什么,这看起来很有可能:它会终止吗?投掷?(什么……?)返回默认构造的对象?

    另一方面:

    ///
    /// Try an exact match approach to begin with
    /// Uses the double metaphone algorithm
    ///   if none was found as we have
    ///   a western european clientele
    ///
    Person findPerson(Name name)
    {
    }
    

    更有趣!

    • 现在我知道这奇怪的收藏是什么了 if 好像在进行某种声音识别…
    • 我知道它的名字,所以我可以在互联网上查找它来检查它的实现(功能)
    • 我知道它为什么被选中,因此当我重新评估它的用途时(适合西欧客户,因此如果我们在阿拉伯市场上发展,它将需要适应…)

    不幸的是,这不会自动生成。

        3
  •  2
  •   Renato Coelho    14 年前

    好吧,这是一篇老文章,但我也遇到了同样的问题,我找到了doxymac。它与emacs很好地集成,并为函数和文件生成doxymac注释。在将E.EL文件放在Emacs路径之后,您可以添加一个钩子,以便在打开C/C++文件时添加(“添加钩子”C模式-CAMON-HOOK'doxYMACS模式)”并用C-C-D F和C-C-D文件来注释函数,还有其他注释类型可用,只需检查项目页面: http://doxymacs.sourceforge.net/

        4
  •  1
  •   parasrish    10 年前

    python中有一些c/cpp解析器,可以用于特定的目的。不过,我还没用过。

    出于类似的目的,我编写了一个python脚本,主要是在头文件中的方法中添加“doxygen头”。我使用了正则表达式,并且我有一个版本,它在方法定义的源文件中添加了“doxygen headers”(在方法查找时使用re_m_definition)。

    代码供您参考,如下所示:

    性别毒性

    #!/usr/bin/python
    
    import os
    import sys
    import re
    
    ################################################################
    
    RE_MULTI_LINE_PARAMS = ".*"
    
    # could be used in header/source files, for method-definition extraction
    RE_M_DEFINITION  = r'[A-Za-z0-9*]*\s*[A-Za-z0-9_*]+\s*[A-Za-z0-9_~:*]+\(.*\)\s*\{\s*.*?\s*\}'   #TODO:  this needs to be more generic to                                                        be able to parse for methods only
    # used in header-files in major for method declaration extraction
    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
    
    ################################################################
    
    # C/CPP CMD List
    cmdList = ["for","if","while","switch","else"];
    
    ##########################
    # exit errors enumerations
    class EErrors() :
        IncorrectUsage, FileOpenError = range(2)
    
    ###################
    # exception handler
    def handleException(e, mssg) :
        if e == EErrors.IncorrectUsage :
            print "Usage : "+mssg
            elif e == EErrors.FileOpenError :
            print "Unable to open \"" + mssg + "\" file !"
        sys.exit(2)
    
    ###############################
    # creates method doxygen header 
    def frameDoxygenHeader(param_count, paramList) :
        commentStr = "/**\n * @brief \n"    
        if param_count > 0 :
            for param in paramList:
                commentStr = commentStr + " * @param \n"
    
        # comment for return values
        commentStr = commentStr + " * @return \n */ \n"
    
        return commentStr
    
    ##############################################
    # adds the doxygen comments, on method lookup
    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)
    
            # doxygen comment header generation
            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
    
            # create file with doxygen comments
            if final_loc != -1 :
                #f_new.write(fContent[0:final_loc-1]);
                #not to miss the contents, between two methods          
                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
    
                #reset values
                final_loc = -1
            else:
                print "method not found !!"
    
        # last of the file should not be missed either
        if last_write_loc < len(fContent) :
            f_new.write(fContent[last_write_loc:]);
        f_new.close()
        f_old.close()
    
    
    #############################################
    #############################################
    # main execution of the code starts from here
    #############################################
    argc = len(sys.argv)
    if (argc == 1 or argc >2)  :
        handleException(EErrors.IncorrectUsage, "genDoxygenC.py <cpp source file>")
    else :
        # Correct Input as per USAGE.
        fname = sys.argv[1]
        out_file_name = fname+'.doxygen'
        fcontent=''
        try:
            # read file
            fh = open(fname)
            fcontent = fh.read()
        #   print fcontent
        except:
            handleException(EErrors.FileOpenError, fname)
    
        # lookup for methods in file
        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]
            #print startW
            if startW in cmdList :
                # invalid method extraction
                funcList.remove(fStr)   
    
        # process valid methods-list for doxygen header
        addDoxygenComment(fname, funcList)
        #print funcList
    

    用法: ./gendoxygenc.py文件.h

    这将产生

    文件.h.doxygen

    然后,你可以检查 添加了doxygen头文件 原始头文件 使用任何diff工具。

    例子: meld file.h file.h.doxygen文件

    注: 脚本可能会跳过构造函数,新版本的定义/声明如下;

    s():n(7){};

        5
  •  1
  •   user939719    10 年前

    gendoxygenc.py的发布有许多索引/空白错误。由于python程序流依赖于正确的索引,我担心adddoxygencomment方法的内部块可能不正确。你有可能把真实的源文件发布到这里吗?

        6
  •  0
  •   Curg    8 年前

    还有一个免费的C语言生成器,它使用的是Visual Studio 2015的宏Explorer插件,可以在这里找到: https://github.com/cppocl/visual_studio_macros