Author Topic: Strange Thing: a cpp file will be parsed twice?  (Read 7699 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Strange Thing: a cpp file will be parsed twice?
« on: February 22, 2009, 07:58:00 am »
Hi, I have add the DebugLog to the CodeCompletion plug-in source to see it's output.
Here is my code of bool ParserThread::Parse()
Code
bool ParserThread::Parse()
{

    if (!InitTokenizer())
        return false;

    Manager::Get()->GetLogManager()->DebugLog(_("parsing file ")+ m_Filename);
    bool result = false;
    m_ParsingTypedef = false;

    do
    {
        if (!m_pTokens || !m_Tokenizer.IsOK())
            break;

        if(!m_Options.useBuffer) // Parse a file
        {
            s_MutexProtection.Enter();
            m_File = m_pTokens->ReserveFileForParsing(m_Filename);
            s_MutexProtection.Leave();
            if(!m_File)
                break;
        }
        Manager::Get()->GetLogManager()->DebugLog(_("Entering DoParse"));
        DoParse();
        Manager::Get()->GetLogManager()->DebugLog(_("Leave DoParse"));
        if(!m_Options.useBuffer) // Parsing a file
        {
            s_MutexProtection.Enter();
            m_pTokens->FlagFileAsParsed(m_Filename);
            s_MutexProtection.Leave();
        }
        result = true;
    }while(false);

    return result;
}

Then here is my simple test code
Code
int b;
int c;


int main()
{
    int a;
    c=2;
    a=1;
    return 0;
}


Then the output is below:
Code
Removed testParser from all deps
Updating class browser...
Class browser updated.
Loading project file...
Parsing project file...
Loading target Debug
Loading project files...
1 files loaded
Done loading project in 0ms
Project's base path: D:\temp\testParser\
Project's common toplevel path: D:\temp\testParser\
Warning: Using user specified encoding as fallback!
Encoding fallback is: Unicode 8 bit (UTF-8) (ID: 41)
Final encoding detected: Unicode 8 bit (UTF-8) (ID: 41)
project data set for D:\temp\testParser\main.cpp
Top Editor: D:\temp\testParser\main.cpp
Add project testParser in parsing queue
Passing list of files to parse
parsing file                                    *************************Note Here
Entering DoParse
m_Str=[] token=[int]
m_Str=[int ] token=[b]
Added/updated token [b] (0), type 'int', actual 'int'. Parent is [] (-1)
m_Str=[int] token=[;]
m_Str=[] token=[int]
m_Str=[int ] token=[c]
Added/updated token [c] (1), type 'int', actual 'int'. Parent is [] (-1)
m_Str=[int] token=[;]
m_Str=[] token=[int]
m_Str=[int ] token=[main]
Leave DoParse
Starting batch parsing
parsing file D:\temp\testParser\main.cpp         *************************Note Here
Entering DoParse
m_Str=[] token=[int]
m_Str=[int ] token=[b]
Added/updated token [b] (0), type 'int', actual 'int'. Parent is [] (-1)
m_Str=[int] token=[;]
m_Str=[] token=[int]
m_Str=[int ] token=[c]
Added/updated token [c] (1), type 'int', actual 'int'. Parent is [] (-1)
m_Str=[int] token=[;]
m_Str=[] token=[int]
m_Str=[int ] token=[main]
Leave DoParse
Parsing stage done (1 total parsed files, 3 tokens in 0 minute(s), 0.0 seconds).
Updating class browser...
Class browser updated.



It seems the the "main.cpp" was parsed twice when I open this project.

The first time, see the "*****Note", even the "m_Filename" is an empty string, this file still be parsed.
In the second time, the m_Filename contains the true file name, then this file parsed again.

Is there something wrong?

Thanks
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Strange Thing: a cpp file will be parsed twice?
« Reply #1 on: October 05, 2009, 10:53:44 am »
After several months, Let me answer my own question:

It is true, the file will be parsed twice (excactly, one phase is parsing the source file and one phase is parsing cbEditor's buffer).

One is that the parser try to grab all the tokens from every source files.

The other is triggered by nativeparser, you can see the code in this function:

Code
int NativeParser::FindCurrentFunctionStart

......
    Parser parser(this);
    parser.ParseBufferForFunctions(control->GetTextRange(0, pos));

    wxArrayString funcs;
    TokensTree* tmptree = parser.GetTempTokens();
......


« Last Edit: October 05, 2009, 10:56:42 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.