Author Topic: Patch for handle preprocessor - V2 - beta 3  (Read 24666 times)

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Patch for handle preprocessor - V2 - beta 3
« on: April 17, 2010, 07:42:26 pm »
Hi, all, with the help of ollydbg, I have enhanced the CC by solving the #if XXX expression value.
As of now, the parser in CC only parse all the #if XXX branch, and ignore all the #else branch. That's not so satisfied for me, So, my implementation of the new CC is that I have resolved all the expression after the #if . Here is the test code:

Test code one:

Code

#include <iostream>

using namespace std;

//#define TEST_


#ifdef TEST_  //Here, you can change the #ifdef to ifndef, if defined or if !defined. all should works.


int iIfdef1;
int iIfdef2;
int iIfdef3;
int iIfdef4;
int iIfdef5;
int iIfdef6;
#else
int iElse1;
int iElse2;
int iElse3;
int iElse4;
int iElse5;
int iElse6;
#endif

int main()
{
    return 0;
}


 
Test code two:
Code
#include <windows.h> // I have tested under MinGW

int main()
{
    CreateFile // Well, this function can be showned in autocompletion list, (you need the add #include <winbase.h> before.
    return 0;
}


A belief description of parsing windows.h by now:  

code snippet in windows.h

Code
#ifdef RC_INVOKED
/* winresrc.h includes the necessary headers */
#include <winresrc.h>
#else

#include <stdarg.h>
#include <windef.h>
#include <wincon.h>
#include <winbase.h>

My implementation of CC is that #ifdef RC_INVOKED get false, then, the #else branch was parsed, then winbase.h  file was automatically included.


Test code three:

Code
#if 0
    #define Expression_1
#elif 0
    #define Expression_2
#elif 0
    #define Expression_3
#elif 0
    #define Expression_4
#if 0
    #define Expression_4_if
#else
    #define Expression_4_else
#endif
#elif 0
    #define Expression_5
#else
    #define Expression_1_else
#if 0
    #define Expression_1_else_if
#elif 0
    #define Expression_1_else_elif
#else
    #define Expression_1_else_else
#if 0
    #define Expression_1_else_else_if
#elif 0
    #define Expression_1_else_else_elif_1
#elif 0
    #define Expression_1_else_else_elif_2
#else
    #define Expression_1_else_else_else
#if 1
    #define Expression_1_else_else_else_if
#endif
#if 1
    #define Expression_1_else_if2
#else
    #define Expression_1_else_else2
#endif
#endif
#endif
#endif

int main()
{
    Exp
    return 0;
}


My implemenatation of CC can correctly parse arbitrary level of  the predefined conditional derective. You can tested by your self.

Comments are welcome. thanks.

By the way, in rare cases, when the expression resolving precession failed, I have a "fall back" method, which is still parse the #if branch by default( the same way as the current CC's parser) :D

Beta 2 changed:
Rewrote ParserThread::SkipToNextPreprocessorBlock() and ParserThread::SkipToEndPreprocessorBlock(), make it clear.
AND FIX a bug!

Version 2 Change Log:
1. Move condition preprocessor handle from ParserThread to Tokenizer class
2. Support parse like this demo:
Code
#define AAA

void test(int testInt1 // some comment
#ifdef AAA // test
, int testInt2)
#else
)
#endif
{
    tes|
};
3. Rewrote SkipXXX functions.
4. Make argument support default value, like (int i, bool b = true)
5. Fixed some bugs.

Another test demo:
Code
class A
{
public:
    void AFunc()
    {

    }
};

class B
{
public:
    void BFunc()
    {

    }
};

class C
#ifndef AAA
    : public A
#else
    : public B
#endif
{
public:
    void CFunc()
    {

    }
};

C c;
c.

V2 - Beta 2 Change Log:
1. Fixed some bugs.
2. Add #undef handle
3. Refactoring Expression

V2 - Beta 3 Change Log:
1. Replace from 'GetArgument' to 'ReadBlock'.
2. Fix bugs.

NOTE: Once officially accepted this patch, all attachment will be deleted.
Welcome for test!

[attachment deleted by admin]
« Last Edit: May 01, 2010, 07:53:21 pm by Loaden »

Offline JGM

  • Lives here!
  • ****
  • Posts: 518
  • Got to practice :)
Re: Patch for handle preprocessor - beta 1
« Reply #1 on: April 18, 2010, 02:17:06 am »
you are amazing dude   :)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Patch for handle preprocessor - beta 1
« Reply #2 on: April 18, 2010, 05:08:26 am »
This is a great step to enhance the CodeCompletion plugin. Nice work!!!!
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 Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch for handle preprocessor - beta 1
« Reply #3 on: April 18, 2010, 12:55:56 pm »
Test it on linux, only this code, seems to works well.

Code
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{
    std::string s;
    std::vector<int> v;
    std::map<int, std::string> m;
    m.insert(std::make_pair(1, "ok"));
    std::set<int> set;
    std::list<float> l;
    strcpy()

    return 0;
}
Quote
Reparsing while typing for editor /home/loaden/Projects/dfsffs/main.cpp
Reparsing saved files...
Starting batch parsing...
Parsing stage done (114 total parsed files, 5060 tokens in 0 minute(s), 0.465 seconds).
Updating class browser...
Class browser updated.
« Last Edit: April 24, 2010, 05:19:36 pm by Loaden »

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: Patch for handle preprocessor - beta 1
« Reply #4 on: April 19, 2010, 03:47:55 pm »
Wow!!! Beta version, cool !!! I was willing to get any version, even an alpha !!!! I'll try it right now.... :-)

Thank you !!!!!!

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: Patch for handle preprocessor - beta 1
« Reply #5 on: April 21, 2010, 11:17:58 am »

 I am using the handle preprocessor patch and have one question. Here's what I have:

- 2 projects in one workspace
- each project has its own define, for example (-DPROJECT_ONE and -DPROJECT_TWO)
- some source files (.cpp and .h) are shared between both projects, but have some different code for each project using
Code
#ifdef PROJECT_ONE
    // code for project one
#endif
or
Code
#ifdef PROJECT_TWO
    // code for project two
#endif

My question is now:

- since both PROJECT_ONE and PROJECT_TWO are defined within the workspace, the parser is still parsing both #ifdef's regardless of the active project, right? And I have no advantage in using this patch. Is that correct?

- If so, can it be changed, so only the active project #defines (in the project options) are used??

Thanks a lot...

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch for handle preprocessor - beta 1
« Reply #6 on: April 21, 2010, 04:31:06 pm »
- since both PROJECT_ONE and PROJECT_TWO are defined within the workspace, the parser is still parsing both #ifdef's regardless of the active project, right? And I have no advantage in using this patch. Is that correct?
Not! You can define the macros in project's option, but not in header files.


- If so, can it be changed, so only the active project #defines (in the project options) are used??
You can try it.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch for handle preprocessor - beta 2
« Reply #7 on: April 21, 2010, 04:36:40 pm »
Beta 2 changed:
Rewrote ParserThread::SkipToNextPreprocessorBlock() and ParserThread::SkipToEndPreprocessorBlock(), make it clear.
AND FIX a bug!
Welcome for test!

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: Patch for handle preprocessor - beta 1
« Reply #8 on: April 23, 2010, 09:00:15 am »
- since both PROJECT_ONE and PROJECT_TWO are defined within the workspace, the parser is still parsing both #ifdef's regardless of the active project, right? And I have no advantage in using this patch. Is that correct?
Not! You can define the macros in project's option, but not in header files.
I have them defined in each project's options. In "Project One" I have -DPROJECT_ONE and in "Project Two" I have -DPROJECT_TWO.

Just to clarify, what I'd like to have is the parser to only use the defines from the *active* project. I have lots of project dependent code parts, for example
Code
#ifdef PROJECT_ONE
  #define SOME_VALUE 1
#endif
#ifdef PROJECT_TWO
  #define SOME_VALUE 2
#endif
I'd like that the CC shows SOME_VALUE as 1 (in the tip and in the symbols browser) when "Project One" is the active one and 2 when "Project Two" is the active one.

- If so, can it be changed, so only the active project #defines (in the project options) are used??
You can try it.
What I see by enabling the debug log in nativeparser.cpp is that the parser does a batch parsing of all projects in the workspace. In my case, I have messages like
Code
Add project Project One in parsing queue
...
Add project and current buildtarget defined preprocessor macros:
#define PROJECT_ONE
...
Add project Project Two in parsing queue
...
Add project and current buildtarget defined preprocessor macros:
#define PROJECT_TWO
...
Passing list of files to batch-parser.
Batch-parsing 184 file(s)...
Starting batch parsing...
Parsing stage done (268 total parsed files, 11936 tokens in 0 minute(s), 2.913 seconds).
Updating class browser...
Class browser updated.


Is it possible to change the CC, so that one the current project is parsed? (maybe that's the right question here)
Since I'm new to the CC, I'm a little bit lost about where to start looking for it...

I know I could have only one project in the workspace at a time and that would solve my problem, but it's so convenient to have both at the same time...

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Patch for handle preprocessor - beta 2
« Reply #9 on: April 23, 2010, 04:01:13 pm »
@daniloz
Quote
Is it possible to change the CC, so that one the current project is parsed? (maybe that's the right question here)
Since I'm new to the CC, I'm a little bit lost about where to start looking for it...

I found a code snippet in the nativeparser.cpp, line 511

I just do a search of "in parsing queue", and found that it was here:
Code
void NativeParser::AddParser(cbProject* project, bool useCache)
{
    if (!project)
        return;

    Manager::Get()->GetLogManager()->DebugLog(F(_T("Add project %s in parsing queue"), project->GetTitle().wx_str()));

    ReparseProject(project);
}

So, this function only parse files in a single project.

But strange that in the function below: nativeparser.cpp line 606.

Code
// NOTE: it actually forces reparsing of workspace
void NativeParser::ForceReparseActiveProject()
{
    m_Parser.Clear();
    UpdateClassBrowser();

    ProjectsArray* projects = Manager::Get()->GetProjectManager()->GetProjects();
    for (size_t i = 0; i < projects->GetCount(); ++i)
    {
        AddParser(projects->Item(i), false);
    }
}

Note: the comment:
/ /NOTE: it actually forces reparsing of workspace

I'm grad you have interests to debug and test CC. I would like to do my best to help  :D



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 hakim

  • Single posting newcomer
  • *
  • Posts: 9
Re: Patch for handle preprocessor - beta 2
« Reply #10 on: April 27, 2010, 02:39:19 pm »
In beta2 patch ".cpp" is missing in line 45.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Patch for handle preprocessor - beta 2
« Reply #11 on: April 27, 2010, 02:53:06 pm »
In beta2 patch ".cpp" is missing in line 45.
??Can you enplane more? I don't catch your idea.
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 hakim

  • Single posting newcomer
  • *
  • Posts: 9
Re: Patch for handle preprocessor - beta 2
« Reply #12 on: April 27, 2010, 04:21:02 pm »
In beta2 patch ".cpp" is missing in line 45.
??Can you enplane more? I don't catch your idea.
Just take a look at line 45 in "cc_handle_preprocessor_beta_2.patch" and you'll understand...

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Patch for handle preprocessor - beta 2
« Reply #13 on: April 27, 2010, 04:31:47 pm »
In beta2 patch ".cpp" is missing in line 45.
??Can you enplane more? I don't catch your idea.
Just take a look at line 45 in "cc_handle_preprocessor_beta_2.patch" and you'll understand...

Thanks. I see, see below:

Code
 			parser/parserthread.cpp \
  parser/token.cpp \
  parser/tokenizer.cpp \
- parser/searchtree.cpp
+ parser/searchtree.cpp \
+ parser/expression
 
 noinst_HEADERS = ccdebuginfo.h \
  ccoptionsdlg.h \
@@ -40,4 +41,5 @@

So, it should be: expression.cpp
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 Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch for handle preprocessor - beta 2
« Reply #14 on: April 27, 2010, 04:34:00 pm »
In beta2 patch ".cpp" is missing in line 45.
??Can you enplane more? I don't catch your idea.
Just take a look at line 45 in "cc_handle_preprocessor_beta_2.patch" and you'll understand...
Thanks!