Find the answer to your Linux question:
Results 1 to 5 of 5
I have a C++ project , some of the source files have function calls like this: WarningMsg("Something is here"); My job is to come up with a way to find ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    1

    Find all warning messages in code

    I have a C++ project , some of the source files have function calls like this:

    WarningMsg("Something is here");

    My job is to come up with a way to find all such instances in all files. And output the entire WarningMsg("Something is here") to a text file. I tried using grep, it worked but I found out WarningMsg could be spread across more than one line, eg:


    if( ... ) WarningMsg(
    "A warning
    message");

    What is the best way to solve this problem? Please give me some tips.

  2. #2
    Just Joined!
    Join Date
    Apr 2009
    Posts
    14
    It certainly won't match every possible case of function call in C++ but if You are sure that WarinngMsg() is called only with a constant "in-place" char *, then my code should work:
    Code:
    grep -P 'WarningMsg(?s).*?\)'

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by Beju View Post
    It certainly won't match every possible case of function call in C++ but if You are sure that WarinngMsg() is called only with a constant "in-place" char *, then my code should work:
    Code:
    grep -P 'WarningMsg(?s).*?\)'
    This is one of those situations where a short C or C++ program might be a better solution, especially if you want to filter out any of these that might be buried inside comments or are only conditionally compiled into the code. It's one of those tasks that seem like they should be simple on the surface of it, but end up being more complex and difficult to accomplish than anticipated.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    I'm not sure how much use it will be, but just to throw it in there....the strings command is very useful for outputting text strings in binaries. I'm not sure how useful it'll be for the filter you need, but thought it may be useful.

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Posts
    14
    Quote Originally Posted by Rubberman View Post
    This is one of those situations where a short C or C++ program might be a better solution, especially if you want to filter out any of these that might be buried inside comments or are only conditionally compiled into the code.
    You're right. There's no way regular expressions will work in all situations, i.e. if You use another function inside of WarningMsg. Regular expressions are not powerful enough to parse a(ny) language, so to fully achieve the goal You need to construct Your own grammar. If You want, You can use a lex/yacc pair to do it easily.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...