2010-03-26 42 views
10

我正在開發一個大型的C++程序,現在我決定用Doxygen來記錄它。 有很多類,方法,函數,宏等等。因此,我正在尋找可掃描我的源代碼樹並在每個「可記錄項目」頂部插入Doxygen註釋塊的軟件,以便稍後對其進行編輯並添加方法描述等詳細信息。任何軟件自動生成doxygen註釋塊?

有沒有這樣的軟件存在?

我在GNU/Linux下使用Code :: Blocks IDE,因此不需要Visual Studio插件。

回答

1

您可以設置Doxygen以提取未記錄的項目 - 這可能會做你想要的,但不添加任何評論塊的代碼。

之後,您可以創建模板/宏(取決於您的IDE)爲每種類型的項目創建預格式化的塊,因爲您可以逐個逐步處理代碼記錄項目。

如果您使用的是Visual Studio,那麼可以對文件中的類和其他構造提供一些內省,這可能會有所幫助。或者看一看Doxycomment - 它可能是你想要的一些東西。

+1

我知道那個功能,但我更喜歡在源代碼中有這些註釋。 – 2010-03-26 16:55:59

+0

那麼您可以輕鬆地爲要添加的塊創建*空*模板。自動獲取一些數據到這些模板可能會更棘手 - 但請檢查Doxycomment。 – 2010-03-26 17:01:08

+0

我在GNU/Linux下使用Code :: Blocks IDE,因此不需要VisualStudio插件。 – 2010-03-26 17:01:28

5

我很困惑這裏。

自動生成評論的目標是什麼?

評論都意味着帶來更多的價值:

/** 
* \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集合,這似乎是在執行某種聲音識別的...
  • 我知道它的名字,所以我可以看看它在互聯網上,以檢查其執行情況(功能)
  • 我知道它爲什麼被選中,因此,當我應該重新評估其使用(符合西歐的客戶,所以如果我們發展阿拉伯市場上它會需要一個適應...)

不幸的是,這是不會自動生成。

+0

我認爲這個問題很有價值。在一個好的項目中,有很多評論都是這樣,因爲大多數例程可以預期是不平凡的。對於要學習或維護代碼的人來說,每條單(智能)線都很有價值。 如果你有30,000行幾乎沒有文檔記錄的代碼,那麼作者要求的這樣一個小工具可以是一個幸福,如果你想閱讀代碼並自己創建一個文檔。 – shuhalo 2010-08-08 16:49:35

+0

你忘記了那些寫得不盡人意的廣告,而是純粹,清醒,乾燥的合同作品,其中有人!=你已經設置了編碼風格。你可以嘗試與不可避免的對抗,或者你只是儘量以最少的努力來適應。 – Vroomfondel 2017-05-19 12:21:06

+0

@Vroomfondel:有一個關於一位新經理人的着名(可能是補充)的故事,並且要求從現在開始每週末開發人員向他報告他們添加到代碼庫的行數。本週末,最高級開發人員的報告出現在:-16,000行。如果你發現自己處於這種狀況,那麼可能是時候尋找更好的工作;浪費每個人掌握權力的時間永遠不會好。 – 2017-05-19 12:34:01

2

好吧,所以這是一箇舊帖子,但我只是有同樣的問題,我找到了doxymacs。它很好地集成了emacs併爲您的函數和文件生成doxymacs註釋。放完了之後。el文件在你的emacs路徑中,你可以添加一個鉤子以使它在你打開一個C/C++文件時可用「(add-hook'c-mode-common-hook'doxymacs-mode)」並且用Cc df和有Cc di的文件,還有其他註釋類型可用,只需檢查項目頁面:http://doxymacs.sourceforge.net/

1

python中有一些c/cpp解析器,可能會用於您的特定目的。但是,我從來沒有使用過這些。

爲了類似的目的,我編寫了一個python腳本,主要爲頭文件中的方法添加「doxygen-headers」。我使用了正則表達式,並且我有一個版本在方法定義的源文件中添加了「doxygen頭文件」(在方法查找中使用RE_M_DEFINITION)。

代碼,供大家參考,如下:

genDoxygenC.py

#!/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 file.h

這會產生

file.h.doxygen

,然後,你可以使用任何差異可能檢查doxygen的報頭添加文件,與原頭文件 -工具。

例子:MELD file.h file.h.doxygen

注::腳本可能會跳過構造,用新版本的定義/聲明一樣;

S():n(7)){};

+0

你可以給文件本身,因爲從這裏複製只是破壞了代碼。 – joaopauloribeiro 2016-05-17 20:30:45

+1

請參考樣本https://github.com/parasrish/selfDoxygenH – parasrish 2016-05-19 18:54:16

1

genDoxygenC.py的發佈有許多索引/空白錯誤。由於Python程序流程依賴於正確的索引,我擔心內部塊addDoxygenComment方法可能不正確。有沒有機會將實際的源文件發佈到這裏?