Author Topic: Clang CC  (Read 251219 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang CC
« Reply #120 on: February 23, 2016, 08:08:58 am »
@yves:
I think the 20 ms lock time can be removed, at least I see a time delay in the ccmanager, see below:
Code
void CCManager::OnEditorHook(cbEditor* ed, wxScintillaEvent& event)
{
    wxEventType evtType = event.GetEventType();
    if (evtType == wxEVT_SCI_CHARADDED)
    {
        const wxChar ch = event.GetKey();
        // get the char set the active ccplugin is interest to trigger the call tip
        CCPluginCharMap::const_iterator ctChars = m_CallTipChars.find(GetProviderFor(ed));
        if (ctChars == m_CallTipChars.end())
            ctChars = m_CallTipChars.find(nullptr); // default char set

        // Are there any characters which could trigger the call tip?
        if (ctChars->second.find(ch) != ctChars->second.end())
        {
            int tooltipMode = Manager::Get()->GetConfigManager(wxT("ccmanager"))->ReadInt(wxT("/tooltip_mode"), 1);
            if (   tooltipMode != 3 // keybound only
                || m_CallTipActive != wxSCI_INVALID_POSITION )
            {
                wxCommandEvent pendingShow(cbEVT_DEFERRED_CALLTIP_SHOW);
                AddPendingEvent(pendingShow);
            }
        }
        else
        {
            cbStyledTextCtrl* stc = ed->GetControl();
            const int pos = stc->GetCurrentPos();
            const int wordStartPos = stc->WordStartPosition(pos, true);
            // get the char set the active ccplugin is interest to trigger the suggestion list
            CCPluginCharMap::const_iterator alChars = m_AutoLaunchChars.find(GetProviderFor(ed));
            if (alChars == m_AutoLaunchChars.end())
                alChars = m_AutoLaunchChars.find(nullptr); // default char set

            // auto suggest list can be triggered either:
            // 1, some number of chars are entered
            // 2, an interested char belong to alChars is entered
            int autolaunchCt = Manager::Get()->GetConfigManager(wxT("ccmanager"))->ReadInt(wxT("/auto_launch_count"), 3);
            /* TODO (ollydbg#1#02/22/16): What does the magic number 4 means? */
            if (   (pos - wordStartPos >= autolaunchCt && !stc->AutoCompActive())
                || pos - wordStartPos == autolaunchCt + 4 )
            {
                CodeBlocksEvent evt(cbEVT_COMPLETE_CODE);
                Manager::Get()->ProcessEvent(evt);
            }
            else if (alChars->second.find(ch) != alChars->second.end())
            {
            /* TODO (ollydbg#1#02/22/16): What does the 10 ms used for? Can we just ask plugin to start a code completion session? */
                m_AutoLaunchTimer.Start(10, wxTIMER_ONE_SHOT);
                m_AutocompPosition = pos; // remember the current caret position
            }
        }
    }
...

You can see, there are 10 ms there.

Quote
On the other hand, I do think the 'GetAutocomplist'-call in the cc-plugin framework looks like an afterthought, something with a request event and a response event would probably have been much nicer, but I'm unaware of the lowlevel choices and scintilla if that would even be possible.
I think this is a good method, and we can go with this direction.
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 yvesdm3000

  • Almost regular
  • **
  • Posts: 225
Re: Clang CC
« Reply #121 on: February 23, 2016, 08:39:08 am »
@yves:
I think the 20 ms lock time can be removed, at least I see a time delay in the ccmanager, see below:

You can see, there are 10 ms there.
This is not the same timeout. The 20 ms comes after the 10 ms you identified and gives Clang 20 ms to calculate the CC results. If it can do it in 20ms, the code-completion results are returned immediately, otherwise the clang-plugin will post a new code-completion request when the results have arrived.

Quote
Quote
On the other hand, I do think the 'GetAutocomplist'-call in the cc-plugin framework looks like an afterthought, something with a request event and a response event would probably have been much nicer, but I'm unaware of the lowlevel choices and scintilla if that would even be possible.
I think this is a good method, and we can go with this direction.
Beware that with the current code-completion support in C::B, it is very complex where a code-completion is requested, handled in a thread, the editor is then modified and then when the operation is finished, the plugin needs to figure out if the code-completion still applies because another code completion might have come in, it might be identical, it might not be, the same request might be pending, and then you do another one in our own callback handler etc... The amount of cases where it can do something else than the user expects is very high.
Nevertheless it's there, in 16.01 so we'll live with it.

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Offline yvesdm3000

  • Almost regular
  • **
  • Posts: 225
Re: Clang CC
« Reply #122 on: February 23, 2016, 08:40:20 am »
Oh, and I pushed some doxygen documentation to the 'staging' branch. It's not everything, but it's a start.

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang CC
« Reply #123 on: February 23, 2016, 08:48:23 am »
@yves:
I think the 20 ms lock time can be removed, at least I see a time delay in the ccmanager, see below:

You can see, there are 10 ms there.
This is not the same timeout. The 20 ms comes after the 10 ms you identified and gives Clang 20 ms to calculate the CC results. If it can do it in 20ms, the code-completion results are returned immediately, otherwise the clang-plugin will post a new code-completion request when the results have arrived.
You are right, the 20 ms lock is from clangcc, but 10 ms delay is from ccmanager, I mean we do not need the 10ms delay(note that under windows 10ms could be 16ms, because the system time tick), and we just start the clangcc, thus we can save a lot of time.

Oh, and I pushed some doxygen documentation to the 'staging' branch. It's not everything, but it's a start.

Yves
Great job!
I think I will add some doxygen style document to ccmanager class either. :)
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: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang CC
« Reply #124 on: February 23, 2016, 09:06:16 am »
Beware that with the current code-completion support in C::B, it is very complex where a code-completion is requested, handled in a thread, the editor is then modified and then when the operation is finished, the plugin needs to figure out if the code-completion still applies because another code completion might have come in, it might be identical, it might not be, the same request might be pending, and then you do another one in our own callback handler etc... The amount of cases where it can do something else than the user expects is very high.
Nevertheless it's there, in 16.01 so we'll live with it.
The current way when code-completion is requested
1, ccmanager call a function in ccplugin, thus a request is send to ccplugin
2, ccplugin directly return some token list back to ccmanager, thus ccmanager show the results.


EDIT: the asynchronized request is just my idea.
An asynchronized request:
1, ccmanager call a function in ccplugin, thus a request is send to ccplugin
2, ccplugin return a special value(maybe a number), and ccmanager know it will return the results later.
3, ccplugin can do the dirty work in a worker thread
4, when result is out, ccplugin can send a message or call a sdk function from ccplugin
5, the result is shown.

If there are another request in between those steps, we can cancel some steps and do it again, a request can have a number, so they don't conflict.


« Last Edit: February 23, 2016, 12:03:35 pm 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.

Offline yvesdm3000

  • Almost regular
  • **
  • Posts: 225
Re: Clang CC
« Reply #125 on: February 23, 2016, 11:26:20 am »
Can you show me where that number is? I tried setting a number on the event but that doesn't get used and doesn't come back in the event that is a result of the ccplugin indicating it has some results.

For what it looks like now, I think I managed to make it work correctly by saving the line/column position where code-completion is requested and when ccplugin is ready, it checks if that position is still valid, if not, drops/cancels the request.

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang CC
« Reply #126 on: February 23, 2016, 12:06:26 pm »
Can you show me where that number is? I tried setting a number on the event but that doesn't get used and doesn't come back in the event that is a result of the ccplugin indicating it has some results.
Oh, sorry, this is my idea, not implemented yet, I just put there for discussion. Not sure it is good or bad. :)

Quote
For what it looks like now, I think I managed to make it work correctly by saving the line/column position where code-completion is requested and when ccplugin is ready, it checks if that position is still valid, if not, drops/cancels the request.

Yves
Yes, it is also a good choice, if you looked at the GDB's MI interface, it use a number to synchronize a request and a response.
« Last Edit: February 23, 2016, 02:16:39 pm 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.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang CC
« Reply #127 on: February 23, 2016, 02:57:48 pm »

EDIT: the asynchronized request is just my idea.
An asynchronized request:
1, ccmanager call a function in ccplugin, thus a request is send to ccplugin
2, ccplugin return a special value(maybe a number), and ccmanager know it will return the results later.
3, ccplugin can do the dirty work in a worker thread
4, when result is out, ccplugin can send a message or call a sdk function from ccplugin
5, the result is shown.

If there are another request in between those steps, we can cancel some steps and do it again, a request can have a number, so they don't conflict.

Hi, yvesdm3000, I think you have already implement this kind of feature.
The code is here:
Code
std::vector<cbCodeCompletionPlugin::CCToken> ClangCodeCompletion::GetAutocompList(bool isAuto, cbEditor* ed, int& tknStart, int& tknEnd)
{
    // you just use the m_CCOutstandingResults to generate a returned tokens
}

And in the code:
Code
void ClangCodeCompletion::OnCodeCompleteFinished( ClangEvent& event )
{
    //CCLogger::Get()->DebugLog( wxT("OnCodeCompleteFinished") );
    if ( event.GetTranslationUnitId() != m_TranslUnitId )
    {
        return;
    }
    if (m_CCOutstanding > 0)
    {
        if ( event.GetLocation() != m_CCOutstandingLoc )
        {
            CCLogger::Get()->DebugLog( wxT("Discard old CodeCompletion request result") );
            return;
        }
        EditorManager* edMgr = Manager::Get()->GetEditorManager();
        cbEditor* ed = edMgr->GetBuiltinActiveEditor();
        if (ed)
        {
            m_CCOutstandingResults = event.GetCodeCompletionResults();
            if ( m_CCOutstandingResults.size() > 0 )
            {
                CodeBlocksEvent evt(cbEVT_COMPLETE_CODE);
                evt.SetInt(1);
                Manager::Get()->ProcessEvent(evt);
                return;
            }
        }
        m_CCOutstanding--;
    }
}
This event is send from the thread to the plugin, and you manually fire a cbEVT_COMPLETE_CODE.

Pay attachment to the value
Code
evt.SetInt(1);
I think in the ccmanager, this int value only have two meanings:
FROM_TIMER or not FROM_TIMER.
I'm not quite sure whether FROM_TIMER means, but we may add another option like "delayed call".
By reading the doxygen docs of this function:
Code
virtual std::vector<CCToken> GetAutocompList(bool isAuto, cbEditor* ed, int& tknStart, int& tknEnd) = 0;
I see that the returned value could be
1, an empty vector, in this case, no code suggestion will list
2, a non empty vector, thus a code suggestion list will prompted.
So, maybe we can add another returned value, indicates that the plugin will returned the vector later (asynchronized way).
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: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang CC
« Reply #128 on: February 23, 2016, 02:59:24 pm »
A bug report: I think not every source file(cpp or h files) were included in the clanglib.cbp.
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 yvesdm3000

  • Almost regular
  • **
  • Posts: 225
Re: Clang CC
« Reply #129 on: February 23, 2016, 04:30:27 pm »
A bug report: I think not every source file(cpp or h files) were included in the clanglib.cbp.

use clanglib-unix.cbp or clanglib-msw_64.cbp

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Offline iceant

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: Clang CC
« Reply #130 on: March 07, 2016, 01:23:30 pm »
Yvesdm3000, thanks for your great work.
I got an problem to run this plugin with latest svn version(10798). Which is wx2.8.12 32bit windows, unicode version

I build Clang CC with clanglib-msw_64.cbp, and changed some folders. Clang is downloaded from official web site(3.7.1 version 32bit)
Here is the installation steps:
1) First, build the clanglib.dll and clanglib.zip
2) copy clanglib.dll from c:\git directory to CODEBLOCKS/share/CodeBlocks/plugins
3) copy clanglib.zip to CODEBLOCKS/share/CodeBlocks/
4) copy libclang.dll to CODEBLOCKS/, the same directory with codeblocks.exe

But I can not run it success.

Here is the error message:
-------------------------------------------------------------
One or more plugins were not loaded.
This usually happens when a plugin is built for
a different version of the Code::Blocks SDK.
Check the application log for more info.

List of failed plugins:

clanglib.dll
-------------------------------------------------------------

Can you help me on this? Thanks

A bug report: I think not every source file(cpp or h files) were included in the clanglib.cbp.

use clanglib-unix.cbp or clanglib-msw_64.cbp

Yves

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: Clang CC
« Reply #131 on: March 07, 2016, 01:33:58 pm »
You have to link to the 'codeblocks.dll' in your installation folder. Check the following link for more information:
http://wiki.codeblocks.org/index.php/Linking_the_plugin_to_a_Nightly_Build

Offline iceant

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: Clang CC
« Reply #132 on: March 07, 2016, 01:51:37 pm »
scarphin, thanks a lot. It works now.

You have to link to the 'codeblocks.dll' in your installation folder. Check the following link for more information:
http://wiki.codeblocks.org/index.php/Linking_the_plugin_to_a_Nightly_Build

Offline yvesdm3000

  • Almost regular
  • **
  • Posts: 225
Re: Clang CC
« Reply #133 on: March 22, 2016, 06:48:42 pm »
I pushed new code on master that fixes the clang-based-occurrences highlighting for people that are keen on that and some other stability improvements and some very small feature enhancements available in the configuration panel like adding compile-options to clang.

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib

Offline yvesdm3000

  • Almost regular
  • **
  • Posts: 225
Re: Clang CC
« Reply #134 on: March 28, 2016, 09:20:52 am »
I pushed new functionality on the 'staging' branch that implements the 'fixit' functionality. It's not the way you suggested but I add a marker in the gutter-bar and when you click it, the fixit is applied.

I can't immediately see how I could do it with the annotations since I can't find any event that fires when you click an annotation. The marker-way is how eclipse does it.

Yves
Clang based code completion for Code::Blocks:   http://github.com/yvesdm3000/ClangLib