Click here to Skip to main content
14,699,107 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I just wondered.
is it possible to find out the length of a function name?
for example i have a requirement that all function names in a project should not exceed 32 characters in length. i can. use strlen() once i know the name of the function.
but how to recognize a function in the project files?
elgaabeb 29-Nov-11 6:19am
Are you analyzing project source files?
LaxmikantYadav 29-Nov-11 6:30am
when you want to check function name's lenght i.e. compile time or run time ?
Albert Holguin 29-Nov-11 15:01pm
It would be too late to do anything about it at run time anyway... not useful for anything...
RKnGl 29-Nov-11 7:31am
hm ..I think he wants to make some kind an addition to preprocessor to check on function names during compile time ... Like some sort of Extension for Visual Studio or whatever those are named in other compilers/IDE's ...

I think he wants to optimize source code files. This can be done with a regular expression with search and replace.
RKnGl 29-Nov-11 11:50am
"how to recognize a function in the project files?" he says ... Doesn't that line excludes search/replace macro ?? ^_^
P.S. Use BIG Yellow "Have a Question or Comment?" links for questions , as You obviously do not have enough info to provide solution ...
Not sure what you're trying to achieve, but why not write a script that goes through a projects' header files and searches for any line that has "(" and just analyze that way (simple string search and trim). Scripts should offer easier ways to search through files and look through strings, hence the recommendation.

If you want to do it C (instead of script), use something like this[^] to find the special character (the '('), from that location, move backwards to find the first blank space.

If you need more help, post a more clear/concise/specific question. This is a little broad.
Use the Visual Studio MACRO(Script) Editor:
I suggest you look at the VS Macros Tool and write a small macro script to parse through the file for you. The main object you will be interested in is one called Microsoft.VisualStudio.VCCodeModel. This will allow you to use the Visual Studio IDE to parse the code and find all of the function names, then test them with a simple MACRO in the VS IDE:

I can't say this is 100% correct, but it will get you close to what you need:

When you are done, you can map the Macro TestFunctionNameLength to a keyboard command, or run the macro through the Macro Explorer in Visual Studio.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports VSLangProj2
Imports VSLangProj90
Imports System.Diagnostics
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.VCCodeModel
Imports System.Text

    'DESCRIPTION: Tests all of the functions in the current file for proper len.
    Sub TestFunctionNameLength()
        ' Walk all of the elements for the file and test with the helper delegate.
        WalkFileCodeElements(AddressOf TestFnNameLenCallBack_)

    End Sub
    'DESCRIPTION: A worker function to test the length of a function name.
    Sub TestFnNameLenCallBack_(ByRef elt As VCCodeElement)
        ' Only process if the input element is a function.
        If (elt.Kind() = vsCMElement.vsCMElementFunction) Then
            If (elt.Name.Length > 32) Then
            End If
        End If
    End Sub

    Public Delegate Sub CodeElementDelegateCallBack(ByRef elt As VCCodeElement)

    ' DESCRIPTION: Walks all of the code elements in the specified collection
    '              and uses each element in a call to the callback delegate 
    '              passed into the function.
    Sub WalkElementCodeElements(ByVal elts As VCCodeElements, _
                                ByVal callback As CodeElementDelegateCallBack)
        For Each elt As VCCodeElement In elts

    End Sub

    Sub WalkFileCodeElements(ByVal callback As CodeElementDelegateCallBack)
        ' First try to get the code model for the current file.
        Dim vcCm As VCCodeModel
            vcCm = DTE.ActiveDocument.ProjectItem.FileCodeModel
        Catch ex As Exception

        End Try

        ' That did not work.  The file is probably not part of the project.
        ' Attempt to use the active projects code model.
        If (vcCm Is Nothing) Then
            Dim dte2 As EnvDTE80.DTE2 = DTE
            Dim e As EnvDTE.Properties = dte2.Solution.Properties

            Dim project As Project = dte2.Solution.Projects.Item(1)

            vcCm = project.CodeModel

            ' Validate these results.  If they failed, drop out.
            If (vcCm Is Nothing) Then
            End If
        End If

        WalkElementCodeElements(vcCm.CodeElements, callback)
    End Sub

[Edit: Changed the name of the function call from Sub RemoveFunctionComments(), to TestFunctionNameLength(). This sample was a quick adaptation from a macro I wrote to strip the comments from a function definition.]
Albert Holguin 29-Nov-11 15:58pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900