How to find struct member uses with cscope and ignore local variables?
Asked Answered
C

2

13

I'm using cscope for a large project with vim, but without the vim mappings (they froze vim for some weird reason). I'm using cscope commands from within vim, and I want to be able to find uses of structure members throughout the code.

Suppose I have something like this:

  1 typedef struct _s{
  2     
  3     int x;
  4 } S;
  5  
  6 int main(){
  7
  8     int x = 1;
  9
 10     S my_s;
 11
 12     my_s.x = 5;
 13
 14     return my_s.x;
 15 }

If I issue the command 'cs f s x' it will return both S's member variable and the local main variable. Is there a way I can only find the occurrences of S's member variable?

Cascara answered 31/5, 2011 at 17:0 Comment(2)
Can you not just do a text search for ".x" ?Nighthawk
@ChrisLutz There could be other, unrelated structs that also have a member called x.Nessim
M
4

I don't think there is any way to get cscope to differentiate between the local variable x and the structure member variable.

The way we solve this problem at my company is to use a unique naming scheme for the member variables that helps differentiate them:

typedef struct _s{    
    int s_x;
} S;

It's a little bit awkward at first, but once you get used to it, it does make it easier to navigate the code. Usually the uniquifier is only a few characters relevant to the structure, and it doesn't clutter things up too badly.

Methodist answered 31/5, 2011 at 19:31 Comment(2)
Thanks for the reply. This won't work in my case because I've just inherited a large code base where they don't follow this guideline. I'll keep that in mind, though. I asked because lots of IDEs such as Eclipse and VS can do this. Obviously vim is not an IDE, but I wondered if it could reach a bit into that functionality.Cascara
That's unfortunate. If you do find a way to do this, I'd love to hear it!Methodist
H
-1

Instead of searching for x, you can position your cursor on the structure variable "my_s" and then press the key combination "gd". This will position you on the definition of my_s and then use cscope to find the definition of S.

Humphrey answered 1/6, 2011 at 6:29 Comment(2)
I'm afraid the question reads ...struct member uses..., not definitions?Drawee
Yes, I was trying to get all the uses of member variable 'x' throughout the code, not where it was defined.Cascara

© 2022 - 2024 — McMap. All rights reserved.