Comment Function Macro for C/C++

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

I have looked at some of the other function comment macros and unfortunately, in my opinion,
each one was lacking in some important area. Here are some of the features that I needed that
I found missing in the other macros:

  • return pointer type with spaces befor “*”;
  • templates in function parameters
  • undefined return
  • split function declaration in a few lines

One thing I will add is that my macros does not handle stdcall/cdecl and another modifiers yet.

Visual Studio Macro

Sub mcFunctionComments()
'DESCRIPTION: Function comments template
'Author : Y.Marchenko. Date : 23/09/2000

 author="Y.Marchenko"
 Delimeter="::"	 ' class delimeters
 a_char=asc("=") ' character used in adornment
 n_char=60       ' amount of adornment characters used 
 Tab3=vbTab&vbTab&vbTab	'indent 3 tabs
 Header = Trim(ActiveDocument.Selection)
 if len(Header)=0 then
  MsgBox("Select function declaration")
  Exit Sub
 end if

 ActiveDocument.Selection.StartOfLine
 ActiveDocument.Selection.NewLine
 ActiveDocument.Selection.LineUp
 if ActiveDocument.Language <>"C/C++" then
  ActiveDocument.Selection=ActiveDocument.Language
  MsgBox("Wrong file type")
 else
  'Delete all comments from function declaration
  Loc=InStr(Header,"/*")
  while Loc
   s1=Left(Header,Loc-1)
   s2=Mid(Header,InStr(Header,"*/")+2)
   Header=s1&s2
   Loc=InStr(Header,"/*")
  wend

  Loc = InStr(Header, "(")
  f_title=Trim(Left(Header,Loc-1 ))
  f_pars=Trim(Mid(Header,Loc+1,InStr(Header, ")")-Loc-1))

  ActiveDocument.Selection = "/*" + String(n_char,a_char)
                           & "*" & vbNewLine

  'Get the function return type.
  arrStr=Split(f_title,Delimeter)
  UB=UBound(arrStr)

  For i=0 To UB
   arrStr(i)=Trim(arrStr(i))
  Next

  Loc=InStrRev(arrStr(0)," " )
  Loc1=InStrRev(arrStr(0),"*" )
  if Loc1>Loc then Loc=Loc1	' pointer returned
  f_name=Mid(arrStr(0),Loc+1)
  f_return=Trim(Left(arrStr(0),Loc))

  if Len(f_return) = 0 then	' undefined return type
   f_return="int (by default)"

   if UB > 0 then
    arrStr(UB)=Replace(arrStr(UB)," ","")
    arrStr(UB)=Replace(arrStr(UB),vbTab,"")

    if arrStr(UB-1)=arrStr(UB)then
     f_return="constructor"
    elseif arrStr(UB) = "~" & arrStr(UB-1) then
     f_return="destructor"
    end if

   end if

  end if

  For i=1 To UBound(arrStr)
   f_name=f_name & Delimeter & Trim(arrStr(i))
  Next

  ActiveDocument.Selection = vbTab & "Author :"
                           & author&Tab3
                           & "Date : "
                           & Date&Tab3
                           & "version 1.0"
                           & vbNewLine

  ActiveDocument.Selection = "  "
                           & String(n_char,a_char)
                           & vbNewLine

  ActiveDocument.Selection = vbTab
                           & "Function :"
                           & Tab3
                           & f_name
                           & vbNewLine

  ActiveDocument.Selection = vbTab
                           & "Description :"
                           & vbTab
                           & vbNewLine

  ActiveDocument.Selection = vbTab
                           & "Return :"
                           & Tab3
                           & f_return
                           & vbTab
                           & "-"
                           & vbTab
                           & vbNewLine

  'Get the function parameters.
  ActiveDocument.Selection = vbTab & "Parameters :" & vbNewLine
  arrStr=Split(f_pars,",")
  bTmpl=false	' template is the current parameter

  For i=0 To UBound(arrStr)
   arrStr(i)=Trim(arrStr(i))
   Loc = InStr(arrStr(i), "<")
   if Loc then bTmpl = true
   if bTmpl then
    if Loc = 0 then
     arrStr(i)=arrStr(i-1) & "," & arrStr(i)
     Loc = InStr(arrStr(i), ">")
     if Loc then ' template end
      bTmpl=false

      ActiveDocument.Selection = Tab3&Replace(arrStr(i),vbNewLine,"")
                               & vbTab
                               & "-"
                               & vbTab&vbNewLine
     end if
    end if
   else
    ActiveDocument.Selection = Tab3&Replace(arrStr(i),vbNewLine,"")
                             & vbTab
                             & "-"
                             & vbTab&vbNewLine
   end if
  Next
  ActiveDocument.Selection = vbTab & "Note :" & vbNewLine
  ActiveDocument.Selection = "*" + String(n_char,a_char) & "*/"
 end if
End Sub

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read