Thread: Accurately detecting function calls

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Accurately detecting function calls

    I have a small problem in a little project I'm doing that attempts to accurately detect function calls (not definitions) in an editor window. I only have access to the lines of text in the editor (Eg. Editor->GetLineText()) so I've wrote a few routines for tokenising the line, removing newlines, and concatenating until I receive a semicolon just in case a call goes over a single line.

    Now I need an accurate way of detecting what is a function call. I don't want to use a full lexical analyser, as it seems overkill, and I've no interest in understanding what the written code does, don't care about casts, etc.

    I was thinking of the following: A non-reserved name followed immediately by an opening bracket is a function call with the name being the name of the function. This should exclude stuff like "for(" "switch(" and "if(" for example, but I'm concerned that it'll fall over with function pointers:

    float (*GetPtr1(const char opCode))(float, float)

    Maybe If I check for the asterix before the name, it'll cure that.

    Any suggestions?

    Oh, and I'm posting this here because I don't care about parsing C++ code. So no need to worry about namespaces or classes. All code is standard ansi-compliant C code (apart from the parser itself, which is all C++).

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The other thing to watch for is prototypes and definitions I guess. Perhaps looking for types inside the parens, or a type immediately before then name?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sounds reasonable: "A non-reserved name followed immediately by an opening bracket is a function call with the name being the name of the function"

    Don't forget "sizeof" which is often followed by a bracket although it doesn't need to be.
    Even function pointers, when called, follow the same syntax. There wouldn't be an asterisk before the name as in your example. You've shown the prototype which shouldn't be detected anyhow.

    You need to worry about detecting function calls vs. prototype or function definitions.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonoob View Post
    Don't forget "sizeof" which is often followed by a bracket although it doesn't need to be.
    Change that to "may not need to be". You cannot use sizeof on a type specifier without parenthesis.
    Code:
    #include<stdio.h>
    int main( void )
    {
        int x;
        
        printf( "%d %d\n", sizeof x, sizeof int );
    
        return 0;
    }
    
    size.c: In function `main':
    size.c:6: error: parse error before "int"
    Quzah.
    Last edited by quzah; 05-16-2009 at 08:03 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since sizeof is a reserved word, it would not need to be handled separately from other reserved words anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    Quote Originally Posted by nonoob View Post
    Even function pointers, when called, follow the same syntax. There wouldn't be an asterisk before the name as in your example. You've shown the prototype which shouldn't be detected anyhow.
    I can detect the fact the cursor is inside a function by looking for a closing bracket followed immediately by an opening brace in the token list. Assuming I'm correct in thinking that function prototypes and definitions can not happen inside a function, that just leaves me with the definition of function pointer which can happen, as they are really just 'normal' variables. "float (" would be recognised as a reserved name, but "user_defined_type (" would be recognised as a function call, unless I parse the entire file (including #includes) looking for and storing all typedef's. That seems way overboard.

    I think the best thing to do is quickly scan the file for all function calls (should be easy enough) and then change the algorithm from "non-reserved word" to "known function name".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM