Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
CC scope
Alpha:
Does CC take scope into account? If so, the code
--- Code: ---int main()
{
{
int fooBar = 0;
}
return foo //CC suggests "fooBar" here
}
--- End code ---
says otherwise...
ollydbg:
As you said, CC does not handle the "scope" information. So, when you leave the
--- Code: --- {
int fooBar = 0;
}
--- End code ---
The variable fooBar is still recognized as a local variable in main().
I think it is not easy to take scope into account.
Any ideas?
Alpha:
One idea would be that when CC collects local variables, it stores them as a series of nodes (like xml). The root node of a function would contain the local variables that are always available. Child nodes would be created for each { } pair containing the variables declared there (child nodes would, of course, have their own child nodes for { } pairs within them).
When CC is called, it would start by looking for all the variables in the current child node, then traverse up, collecting the tokens in each parent node until it reaches the root node.
I think the hard part with this method would how to keep track of which child node the user is currently typing in.
(I am still working on reading Code::Blocks' source, so I do not yet know enough to be able to tell if implementing this method would be feasible.)
ollydbg:
--- Quote from: Alpha on October 30, 2011, 03:07:40 am ---One idea would be that when CC collects local variables, it stores them as a series of nodes (like xml). The root node of a function would contain the local variables that are always available. Child nodes would be created for each { } pair containing the variables declared there (child nodes would, of course, have their own child nodes for { } pairs within them).
When CC is called, it would start by looking for all the variables in the current child node, then traverse up, collecting the tokens in each parent node until it reaches the root node.
I think the hard part with this method would how to keep track of which child node the user is currently typing in.
(I am still working on reading Code::Blocks' source, so I do not yet know enough to be able to tell if implementing this method would be feasible.)
--- End quote ---
Yes, this is the correct way I think.
1, local variables were collected only when we are in a function body, we do not collect them when we do a batch parsing(batch parsing only collect declaration and skip all the function body)
2, we need a data structure to handle such scope information.
3, If you read CodeCompletion's source code, you can have a look at: Code Completion Design - CodeBlocks
4, If you have any question in CC's source code, you can ask me.
eranif:
--- Quote ---I think the hard part with this method would how to keep track of which child node the user is currently typing in.
--- End quote ---
Its pretty simple actually. Assuming you know the location of the current function's body you should collect locals and manage them in a stack.
A simple container like:
--- Code: ---typedef std::map<wxString, TokenInfo> ScopedLocals; // TokenInfo represents here a class of your choice that holds into about a single local variable
typedef std::list<ScopedLocals> LocalsStack;
--- End code ---
And then apply the following logic:
--- Code: ---start:
stack.push_back( new ScopedLocals );
if found '{':
stack.push_back( new ScopedLocals );
if found '}':
delete stack.back(); // No longer relevant, free the allocated memory
stack.pop_back(); // Remove it from the stack
if found local:
stack.back()->insert(std::make_pair<wxString, TokenInfo>(token.Name(), token);
--- End code ---
When you hit the end of your parsing, whatever left in the stack are your visible locals.
Special exceptions that should be handled:
- Variable defined inside 'catch()'
- Variable defined inside 'for(..)'
Eran
Navigation
[0] Message Index
[#] Next page
Go to full version