1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : clang_driver.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#if HAS_LIBCLANG
#include "clang_driver.h"
#include "clang_code_completion.h"
#include "compilation_database.h"
#include "pluginmanager.h"
#include "clang_macro_handler.h"
#include <wx/regex.h>
#include "code_completion_box.h"
#include "clangpch_cache.h"
#include "asyncprocess.h"
#include "frame.h"
#include "processreaderthread.h"
#include "navigationmanager.h"
#include "compiler_command_line_parser.h"
#include "file_logger.h"
#include "pluginmanager.h"
#include "macromanager.h"
#include "includepathlocator.h"
#include "frame.h"
#include "macromanager.h"
#include <memory>
#include "environmentconfig.h"
#include "tags_options_data.h"
#include "ctags_manager.h"
#include <wx/tokenzr.h>
#include "processreaderthread.h"
#include "manager.h"
#include "project.h"
#include "configuration_mapping.h"
#include "procutils.h"
#include "localworkspace.h"
#include "fileextmanager.h"
#include "globals.h"
#include <set>
#include "event_notifier.h"
#include "plugin.h"
#include "browse_record.h"
#include "mainbook.h"
static bool wxIsWhitespace(wxChar ch)
{
return ch == wxT(' ') || ch == wxT('\t') || ch == wxT('\r') || ch == wxT('\n');
}
#define cstr(x) x.mb_str(wxConvUTF8).data()
#define CHECK_CLANG_ENABLED() \
if(!(TagsManagerST::Get()->GetCtagsOptions().GetClangOptions() & CC_CLANG_ENABLED))\
return;\
ClangDriver::ClangDriver()
: m_isBusy(false)
, m_activeEditor(NULL)
, m_position(wxNOT_FOUND)
{
m_index = clang_createIndex(0, 0);
m_pchMakerThread.SetSleepInterval(30);
m_pchMakerThread.Start();
#ifdef __WXMSW__
m_clangCleanerThread.Start();
#endif
EventNotifier::Get()->Connect(wxEVT_CLANG_PCH_CACHE_ENDED, wxCommandEventHandler(ClangDriver::OnPrepareTUEnded), NULL, this);
EventNotifier::Get()->Connect(wxEVT_CLANG_PCH_CACHE_CLEARED, wxCommandEventHandler(ClangDriver::OnCacheCleared), NULL, this);
EventNotifier::Get()->Connect(wxEVT_CLANG_TU_CREATE_ERROR, wxCommandEventHandler(ClangDriver::OnTUCreateError), NULL, this);
EventNotifier::Get()->Connect(wxEVT_CMD_CLANG_MACRO_HADNLER_DELETE, wxCommandEventHandler(ClangDriver::OnDeletMacroHandler), NULL, this);
EventNotifier::Get()->Connect(wxEVT_WORKSPACE_LOADED, wxCommandEventHandler(ClangDriver::OnWorkspaceLoaded), NULL, this);
}
ClangDriver::~ClangDriver()
{
// Disconnect all events before we perform anything elase
EventNotifier::Get()->Disconnect(wxEVT_CLANG_PCH_CACHE_ENDED, wxCommandEventHandler(ClangDriver::OnPrepareTUEnded), NULL, this);
EventNotifier::Get()->Disconnect(wxEVT_CLANG_PCH_CACHE_CLEARED, wxCommandEventHandler(ClangDriver::OnCacheCleared), NULL, this);
EventNotifier::Get()->Disconnect(wxEVT_CLANG_TU_CREATE_ERROR, wxCommandEventHandler(ClangDriver::OnTUCreateError), NULL, this);
EventNotifier::Get()->Disconnect(wxEVT_CMD_CLANG_MACRO_HADNLER_DELETE, wxCommandEventHandler(ClangDriver::OnDeletMacroHandler), NULL, this);
EventNotifier::Get()->Disconnect(wxEVT_WORKSPACE_LOADED, wxCommandEventHandler(ClangDriver::OnWorkspaceLoaded), NULL, this);
m_pchMakerThread.Stop();
m_pchMakerThread.ClearCache(); // clear cache and dispose all translation units
#ifdef __WXMSW__
m_clangCleanerThread.Stop();
#endif
clang_disposeIndex(m_index);
}
ClangThreadRequest* ClangDriver::DoMakeClangThreadRequest(IEditor* editor, WorkingContext context)
{
/////////////////////////////////////////////////////////////////
// Prepare all the buffers required by the thread
wxString fileName = editor->GetFileName().GetFullPath();
wxString currentBuffer = editor->GetTextRange(0, editor->GetLength());
wxString filterWord;
// Move backward until we found our -> or :: or .
m_position = editor->GetCurrentPosition();
wxString tmpBuffer = editor->GetTextRange(0, editor->GetCurrentPosition());
while ( !tmpBuffer.IsEmpty() ) {
// Context word complete and we found a whitespace - break the search
if((context == CTX_WordCompletion || context == CTX_Calltip) && wxIsWhitespace(tmpBuffer.Last())) {
break;
}
if(context == CTX_WordCompletion && !IsValidCppIndetifier(tmpBuffer.Last())) {
break;
}
if(tmpBuffer.EndsWith(wxT("->")) || tmpBuffer.EndsWith(wxT(".")) || tmpBuffer.EndsWith(wxT("::"))) {
break;
} else {
filterWord.Prepend(tmpBuffer.Last());
tmpBuffer.RemoveLast();
}
}
// Get the current line's starting pos
int lineStartPos = editor->PosFromLine( editor->GetCurrentLine() );
int column = editor->GetCurrentPosition() - lineStartPos + 1;
int lineNumber = editor->GetCurrentLine() + 1;
if(context == CTX_GotoDecl || context == CTX_GotoImpl) {
wxString sel = editor->GetSelection();
if(sel.IsEmpty()) {
filterWord = editor->GetWordAtCaret();
} else {
filterWord = sel;
column = editor->GetSelectionStart() - lineStartPos + 1;
}
} else {
column -= (int) filterWord.Length();
}
// Column can not be lower than 1
switch(context) {
case CTX_Calltip:
case CTX_WordCompletion:
case CTX_CodeCompletion:
if(column < 1) {
CL_DEBUG(wxT("Clang: column can not be lower than 1"));
m_isBusy = false;
return NULL;
}
break;
default:
break;
}
wxString projectPath;
wxString pchFile;
FileTypeCmpArgs_t compileFlags = DoPrepareCompilationArgs(editor->GetProjectName(), fileName, projectPath, pchFile);
#if 0
//{
// Prepare a copy of the file but with as special prefix CODELITE_CLANG_FILE_PREFIX
// We do this because on Windows, libclang locks the file so
// the user can not save into the file until it is released by libclang (which may take a while...)
// we overcome this by letting clang compile a copy of the file
// The temporary file that was created for this purpose is later deleted by a special "cleaner" thread
// which removes all kind of libclang leftovers (preamble*.pch files under %TMP% and these files)
wxFileName source_file(fileName);
source_file.SetFullName( CODELITE_CLANG_FILE_PREFIX + source_file.GetFullName() );
{
wxLogNull nl;
::wxCopyFile( fileName, source_file.GetFullPath() );
}
//}
#else
wxFileName source_file(fileName);
#endif
ClangThreadRequest* request = new ClangThreadRequest(m_index,
fileName,
currentBuffer,
compileFlags,
filterWord,
context,
lineNumber,
column, DoCreateListOfModifiedBuffers(editor));
request->SetFileName( source_file.GetFullPath() );
return request;
}
void ClangDriver::CodeCompletion(IEditor* editor)
{
if(m_isBusy) {
if(editor) {
CodeCompletionBox::Get().CancelTip();
CodeCompletionBox::Get().ShowTip( wxT("<b>clang: </b>Code Completion Message:<hr>A lengthy operation is in progress..."), dynamic_cast<LEditor*>(m_activeEditor));
}
return;
}
CL_DEBUG(wxT(" ==========> ClangDriver::CodeCompletion() started <=============="));
if(!editor) {
CL_WARNING(wxT("ClangDriver::CodeCompletion() called with NULL editor!"));
return;
}
m_activeEditor = editor;
m_isBusy = true;
ClangThreadRequest * request = DoMakeClangThreadRequest(m_activeEditor, GetContext());
// Failed to prepare request?
if(request == NULL) {
m_isBusy = false;
m_activeEditor = NULL;
return;
}
/////////////////////////////////////////////////////////////////
// Put a request on the parsing thread
//
m_pchMakerThread.Add( request );
}
void ClangDriver::Abort()
{
DoCleanup();
}
FileTypeCmpArgs_t ClangDriver::DoPrepareCompilationArgs(const wxString& projectName, const wxString& sourceFile, wxString& projectPath, wxString& pchfile)
{
FileTypeCmpArgs_t cmpArgs;
cmpArgs.insert(std::make_pair(FileExtManager::TypeSourceC, wxArrayString()));
cmpArgs.insert(std::make_pair(FileExtManager::TypeSourceCpp, wxArrayString()));
wxArrayString args;
wxString errMsg;
wxArrayString &cppCompileArgs = cmpArgs[FileExtManager::TypeSourceCpp];
wxArrayString &cCompileArgs = cmpArgs[FileExtManager::TypeSourceC];
// Build the TU file name
wxFileName fnSourceFile(sourceFile);
pchfile << WorkspaceST::Get()->GetWorkspaceFileName().GetPath() << wxFileName::GetPathSeparator() << wxT(".clang");
{
wxLogNull nl;
wxMkdir(pchfile);
}
pchfile << wxFileName::GetPathSeparator() << fnSourceFile.GetFullName() << wxT(".TU");
CompilationDatabase cdb;
static bool once = false;
if ( !cdb.IsOk() && !once ) {
once = true;
wxString msg;
msg << _("Could not locate compilation database or database version is not up-to-date: ")
<< cdb.GetFileName().GetFullPath() << wxT("\n\n")
<< _("This file should be created automatically for you.\nIf you don't have it, please run a full rebuild of your workspace\n\n")
<< _("If this is a custom build project (i.e. project that uses a custom makefile),\nplease set the CXX and CC environment variables like this:\n")
<< _("CXX=codelite-cc g++\n")
<< _("CC=codelite-cc gcc\n\n");
clMainFrame::Get()->GetMainBook()->ShowMessage( msg,
true,
PluginManager::Get()->GetStdIcons()->LoadBitmap(wxT("messages/48/tip")),
ButtonDetails(),
ButtonDetails(),
ButtonDetails(),
CheckboxDetails(wxT("CodeCompletionMissingCompilationDB")));
} else {
cdb.Open();
if( cdb.IsOpened() ) {
CL_DEBUG(wxT("Loading compilation flags for file: %s"), fnSourceFile.GetFullPath().c_str());
wxString compilationLine, cwd;
cdb.CompilationLine(fnSourceFile.GetFullPath(), compilationLine, cwd);
cdb.Close();
CompilerCommandLineParser cclp(compilationLine, cwd);
cclp.MakeAbsolute(cwd);
CL_DEBUG(wxT("Loaded compilation flags: %s"), compilationLine.c_str());
args.insert(args.end(), cclp.GetIncludesWithPrefix().begin(), cclp.GetIncludesWithPrefix().end());
args.insert(args.end(), cclp.GetMacrosWithPrefix().begin(), cclp.GetMacrosWithPrefix().end());
args.Add(cclp.GetStandardWithPrefix());
}
}
const TagsOptionsData& options = TagsManagerST::Get()->GetCtagsOptions();
///////////////////////////////////////////////////////////////////////
// add global clang include paths
wxString strGlobalIncludes = options.GetClangSearchPaths();
wxArrayString globalIncludes = wxStringTokenize(strGlobalIncludes, wxT("\n\r"), wxTOKEN_STRTOK);
for(size_t i=0; i<globalIncludes.GetCount(); i++) {
wxFileName fn(globalIncludes.Item(i).Trim().Trim(false), wxT(""));
fn.MakeAbsolute(projectPath);
cppCompileArgs.Add(wxString::Format(wxT("-I%s"), fn.GetPath().c_str()));
cCompileArgs.Add(wxString::Format(wxT("-I%s"), fn.GetPath().c_str()));
}
///////////////////////////////////////////////////////////////////////
// Workspace setting additional flags
///////////////////////////////////////////////////////////////////////
// Include paths
wxArrayString workspaceIncls, dummy;
LocalWorkspaceST::Get()->GetParserPaths(workspaceIncls, dummy);
for(size_t i=0; i<workspaceIncls.GetCount(); i++) {
wxFileName fn(workspaceIncls.Item(i).Trim().Trim(false), wxT(""));
fn.MakeAbsolute(WorkspaceST::Get()->GetWorkspaceFileName().GetPath());
cppCompileArgs.Add(wxString::Format(wxT("-I%s"), fn.GetPath().c_str()));
cCompileArgs.Add(wxString::Format(wxT("-I%s"), fn.GetPath().c_str()));
}
// Macros
wxString strWorkspaceMacros;
LocalWorkspaceST::Get()->GetParserMacros(strWorkspaceMacros);
wxArrayString workspaceMacros = wxStringTokenize(strWorkspaceMacros, wxT("\n\r"), wxTOKEN_STRTOK);
for(size_t i=0; i<workspaceMacros.GetCount(); i++) {
cppCompileArgs.Add(wxString::Format(wxT("-D%s"), workspaceMacros.Item(i).Trim().Trim(false).c_str()));
cCompileArgs.Add(wxString::Format(wxT("-D%s"), workspaceMacros.Item(i).Trim().Trim(false).c_str()));
}
// C++ 11
size_t workspaceFlags = LocalWorkspaceST::Get()->GetParserFlags();
if ( workspaceFlags & LocalWorkspace::EnableCpp11 ) {
cppCompileArgs.Add(wxT("-std=c++11"));
cCompileArgs.Add(wxT("-std=c++11"));
}
///////////////////////////////////////////////////////////////////////
// Project setting additional flags
///////////////////////////////////////////////////////////////////////
BuildConfigPtr buildConf = ManagerST::Get()->GetCurrentBuildConf();
if(buildConf) {
wxString projSearchPaths = buildConf->GetCcSearchPaths();
wxArrayString projectIncludePaths = wxStringTokenize(projSearchPaths, wxT("\r\n"), wxTOKEN_STRTOK);
for(size_t i=0; i<projectIncludePaths.GetCount(); i++) {
wxFileName fn(MacroManager::Instance()->Expand(projectIncludePaths.Item(i),
PluginManager::Get(), ManagerST::Get()->GetActiveProjectName()), wxT(""));
fn.MakeAbsolute(WorkspaceST::Get()->GetWorkspaceFileName().GetPath());
cppCompileArgs.Add(wxString::Format(wxT("-I%s"), fn.GetPath().c_str()));
cCompileArgs.Add(wxString::Format(wxT("-I%s"), fn.GetPath().c_str()));
}
wxString strProjectMacros = buildConf->GetClangPPFlags();
wxArrayString projectMacros = wxStringTokenize(strProjectMacros, wxT("\n\r"), wxTOKEN_STRTOK);
for(size_t i=0; i<projectMacros.GetCount(); i++) {
cppCompileArgs.Add(wxString::Format(wxT("-D%s"), projectMacros.Item(i).Trim().Trim(false).c_str()));
cCompileArgs.Add(wxString::Format(wxT("-D%s"), projectMacros.Item(i).Trim().Trim(false).c_str()));
}
if (buildConf->IsClangC11()) {
cppCompileArgs.Add(wxT("-std=c++11"));
cCompileArgs.Add(wxT("-std=c++11"));
}
}
cppCompileArgs.insert(cppCompileArgs.end(), args.begin(), args.end());
cCompileArgs.insert(cCompileArgs.end(), args.begin(), args.end());
// Remove some of the flags which are known to cause problems to clang
int where = wxNOT_FOUND;
where = cppCompileArgs.Index(wxT("-fno-strict-aliasing"));
if(where != wxNOT_FOUND) cppCompileArgs.RemoveAt(where);
where = cppCompileArgs.Index(wxT("-mthreads"));
if(where != wxNOT_FOUND) cppCompileArgs.RemoveAt(where);
where = cppCompileArgs.Index(wxT("-pipe"));
if(where != wxNOT_FOUND) cppCompileArgs.RemoveAt(where);
where = cppCompileArgs.Index(wxT("-fmessage-length=0"));
if(where != wxNOT_FOUND) cppCompileArgs.RemoveAt(where);
where = cppCompileArgs.Index(wxT("-fPIC"));
if(where != wxNOT_FOUND) cppCompileArgs.RemoveAt(where);
// Now do the same for the "C" arguments
where = cCompileArgs.Index(wxT("-fno-strict-aliasing"));
if(where != wxNOT_FOUND) cCompileArgs.RemoveAt(where);
where = cCompileArgs.Index(wxT("-mthreads"));
if(where != wxNOT_FOUND) cCompileArgs.RemoveAt(where);
where = cCompileArgs.Index(wxT("-pipe"));
if(where != wxNOT_FOUND) cCompileArgs.RemoveAt(where);
where = cCompileArgs.Index(wxT("-fmessage-length=0"));
if(where != wxNOT_FOUND) cCompileArgs.RemoveAt(where);
where = cCompileArgs.Index(wxT("-fPIC"));
if(where != wxNOT_FOUND) cCompileArgs.RemoveAt(where);
return cmpArgs;
}
void ClangDriver::ClearCache()
{
m_pchMakerThread.ClearCache();
}
bool ClangDriver::IsCacheEmpty()
{
return m_pchMakerThread.IsCacheEmpty();
}
void ClangDriver::DoCleanup()
{
m_isBusy = false;
m_activeEditor = NULL;
}
void ClangDriver::DoParseCompletionString(CXCompletionString str, int depth, wxString &entryName, wxString &signature, wxString &completeString, wxString &returnValue)
{
bool collectingSignature = false;
int numOfChunks = clang_getNumCompletionChunks(str);
for (int j=0 ; j<numOfChunks; j++) {
CXString chunkText = clang_getCompletionChunkText(str, j);
CXCompletionChunkKind chunkKind = clang_getCompletionChunkKind(str, j);
switch(chunkKind) {
case CXCompletionChunk_TypedText:
entryName = wxString(clang_getCString(chunkText), wxConvUTF8);
completeString += entryName;
break;
case CXCompletionChunk_ResultType:
completeString += wxString(clang_getCString(chunkText), wxConvUTF8);
completeString += wxT(" ");
returnValue = wxString(clang_getCString(chunkText), wxConvUTF8);
break;
case CXCompletionChunk_Optional: {
// Optional argument
CXCompletionString optStr = clang_getCompletionChunkCompletionString(str, j);
wxString optionalString;
wxString dummy;
// Once we hit the 'Optional Chunk' only the 'completeString' is matter
DoParseCompletionString(optStr, depth + 1, dummy, dummy, optionalString, dummy);
if(collectingSignature) {
signature += optionalString;
}
completeString += optionalString;
}
break;
case CXCompletionChunk_LeftParen:
collectingSignature = true;
signature += wxT("(");
completeString += wxT("(");
break;
case CXCompletionChunk_RightParen:
collectingSignature = true;
signature += wxT(")");
completeString += wxT(")");
break;
default:
if(collectingSignature) {
signature += wxString(clang_getCString(chunkText), wxConvUTF8);
}
completeString += wxString(clang_getCString(chunkText), wxConvUTF8);
break;
}
clang_disposeString(chunkText);
}
// To make this tag compatible with ctags one, we need to place
// a /^ and $/ in the pattern string (we add this only to the top level completionString)
if(depth == 0) {
completeString.Prepend(wxT("/^ "));
completeString.Append(wxT(" $/"));
}
}
void ClangDriver::OnPrepareTUEnded(wxCommandEvent& e)
{
// Our thread is done
m_isBusy = false;
// Sanity
ClangThreadReply* reply = (ClangThreadReply*) e.GetClientData();
if( !reply ) {
return;
}
// Make sure we delete the reply at the end...
std::auto_ptr<ClangThreadReply> ap(reply);
// Delete the fake file...
DoDeleteTempFile( reply->filename );
// Just a notification without real info?
if ( reply->context == CTX_None ) {
return;
}
if(reply->context == ::CTX_CachePCH || reply->context == ::CTX_ReparseTU) {
return; // Nothing more to be done
}
if(reply->context == CTX_GotoDecl || reply->context == CTX_GotoImpl) {
// Unlike other context's the 'filename' specified here
// does not belong to an editor (it could, but it is not necessarily true)
DoGotoDefinition(reply);
return;
}
// Adjust the activeEditor to fit the filename
IEditor *editor = clMainFrame::Get()->GetMainBook()->FindEditor(reply->filename);
if ( !editor ) {
CL_DEBUG(wxT("Could not find an editor for file %s"), reply->filename.c_str());
return;
}
m_activeEditor = editor;
// What should we do with the TU?
switch(reply->context) {
case CTX_CachePCH:
// Nothing more to be done
return;
default:
break;
}
if(!reply->results) {
// Display an error message if needed, but not if the code-completion box
// is visible
if(reply->errorMessage.IsEmpty() == false && !m_activeEditor->IsCompletionBoxShown()) {
// CodeCompletionBox::Get().CancelTip();
// CodeCompletionBox::Get().ShowTip(reply->errorMessage, dynamic_cast<LEditor*>(m_activeEditor));
LEditor* pEditor = dynamic_cast<LEditor*>(m_activeEditor);
if ( pEditor ) {
wxWindow* pParent = ::wxGetTopLevelParent( pEditor );
wxFrame* pFrame = dynamic_cast<wxFrame*>( pParent );
if ( pFrame ) {
pFrame->SetStatusText( reply->errorMessage );
}
}
}
return;
}
if(m_activeEditor->GetCurrentPosition() < m_position) {
CL_DEBUG(wxT("Current position is lower than the starting position, ignoring completion"));
clang_disposeCodeCompleteResults(reply->results);
return;
}
wxString typedString;
if(m_activeEditor->GetCurrentPosition() > m_position) {
// User kept on typing while the completion thread was working
typedString = m_activeEditor->GetTextRange(m_position, m_activeEditor->GetCurrentPosition());
if(typedString.find_first_not_of(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")) != wxString::npos) {
// User typed some non valid identifier char, cancel code completion
CL_DEBUG(wxT("User typed: %s since the completion thread started working until it ended, ignoring completion"), typedString.c_str());
clang_disposeCodeCompleteResults(reply->results);
return;
}
}
// update the filter word
reply->filterWord.Append(typedString);
CL_DEBUG(wxT("clang completion: filter word is %s"), reply->filterWord.c_str());
// For the Calltip, remove the opening brace from the filter string
wxString filterWord = reply->filterWord;
if(GetContext() == CTX_Calltip && filterWord.EndsWith(wxT("(")))
filterWord.RemoveLast();
wxString lowerCaseFilter = filterWord;
lowerCaseFilter.MakeLower();
unsigned numResults = reply->results->NumResults;
clang_sortCodeCompletionResults(reply->results->Results, reply->results->NumResults);
std::vector<TagEntryPtr> tags;
for(unsigned i=0; i<numResults; i++) {
CXCompletionResult result = reply->results->Results[i];
CXCompletionString str = result.CompletionString;
CXCursorKind kind = result.CursorKind;
if(kind == CXCursor_NotImplemented)
continue;
wxString entryName, entrySignature, entryPattern, entryReturnValue;
DoParseCompletionString(str, 0, entryName, entrySignature, entryPattern, entryReturnValue);
wxString lowerCaseName = entryName;
lowerCaseName.MakeLower();
if(!lowerCaseFilter.IsEmpty() && !lowerCaseName.StartsWith(lowerCaseFilter))
continue;
if (clang_getCompletionAvailability(str) != CXAvailability_Available)
continue;
TagEntry *t = new TagEntry();
TagEntryPtr tag(t);
tag->SetIsClangTag(true);
tag->SetName (entryName);
tag->SetPattern (entryPattern);
tag->SetSignature (entrySignature);
// Add support for clang comment parsing
#ifndef __FreeBSD__
CXString BriefComment = clang_getCompletionBriefComment(str);
const char* comment = clang_getCString(BriefComment);
if( comment && comment[0] != '\0' ) {
tag->SetComment(wxString(comment, wxConvUTF8));
}
clang_disposeString(BriefComment);
#endif
switch(kind) {
case CXCursor_EnumConstantDecl:
tag->SetKind(wxT("enumerator"));
break;
case CXCursor_EnumDecl:
tag->SetKind(wxT("enum"));
break;
case CXCursor_CXXMethod:
case CXCursor_Constructor:
case CXCursor_Destructor:
case CXCursor_FunctionDecl:
case CXCursor_FunctionTemplate:
tag->SetKind(wxT("prototype"));
break;
case CXCursor_MacroDefinition:
tag->SetKind(wxT("macro"));
break;
case CXCursor_Namespace:
tag->SetKind(wxT("namespace"));
break;
case CXCursor_ClassDecl:
case CXCursor_ClassTemplate:
case CXCursor_ClassTemplatePartialSpecialization:
tag->SetKind(wxT("class"));
break;
case CXCursor_StructDecl:
tag->SetKind(wxT("struct"));
break;
case CXCursor_TypeRef:
case CXCursor_TypedefDecl:
tag->SetKind(wxT("typedef"));
tag->SetKind(wxT("typedef"));
break;
default:
tag->SetKind(wxT("variable"));
break;
}
tags.push_back(tag);
}
clang_disposeCodeCompleteResults(reply->results);
CL_DEBUG(wxT("Building completion results... done "));
if(GetContext() == CTX_Calltip) {
std::vector<TagEntryPtr> tips;
TagsManagerST::Get()->GetFunctionTipFromTags(tags, filterWord, tips);
m_activeEditor->ShowCalltip(new clCallTip(tips));
} else {
m_activeEditor->ShowCompletionBox(tags, filterWord, true, NULL);
}
}
void ClangDriver::QueueRequest(IEditor *editor, WorkingContext context)
{
if(!editor)
return;
switch(context) {
case CTX_CachePCH:
break;
default:
CL_DEBUG(wxT("Context %d id not allowed to be queued"), (int)context);
return;
}
m_pchMakerThread.Add( DoMakeClangThreadRequest(editor, context) );
}
void ClangDriver::ReparseFile(const wxString& filename)
{
//CHECK_CLANG_ENABLED();
//
//ClangThreadRequest *req = new ClangThreadRequest(m_index, filename, wxT(""), wxArrayString(), wxT(""), ::CTX_ReparseTU, 0, 0);
//m_pchMakerThread.Add( req );
//CL_DEBUG(wxT("Queued request to re-parse file: %s"), filename.c_str());
}
void ClangDriver::OnCacheCleared(wxCommandEvent& e)
{
e.Skip();
/*
CHECK_CLANG_ENABLED();
// Reparse the current file
IEditor *editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
if(editor) {
CL_DEBUG( "Preparing clang thread request..." );
wxString outputProjectPath, pchFile;
ClangThreadRequest *req = new ClangThreadRequest(m_index,
editor->GetFileName().GetFullPath(),
editor->GetEditorText(),
DoPrepareCompilationArgs(editor->GetProjectName(), editor->GetFileName().GetFullPath(), outputProjectPath, pchFile),
wxT(""),
::CTX_CachePCH, 0, 0, DoCreateListOfModifiedBuffers(editor));
req->SetPchFile(pchFile);
m_pchMakerThread.Add( req );
CL_DEBUG( "Preparing clang thread request... done" );
CL_DEBUG(wxT("OnCacheCleared:: Queued request to build TU for file: %s"), editor->GetFileName().GetFullPath().c_str());
}
*/
}
void ClangDriver::DoGotoDefinition(ClangThreadReply* reply)
{
CHECK_CLANG_ENABLED();
LEditor *editor = clMainFrame::Get()->GetMainBook()->OpenFile(reply->filename, wxEmptyString, reply->line);
if(editor) {
int pos = editor->PositionFromLine(reply->line - 1);
editor->FindAndSelectV(reply->filterWord,
reply->filterWord,
pos,
NULL);
}
}
void ClangDriver::OnTUCreateError(wxCommandEvent& e)
{
e.Skip();
ClangThreadReply* reply = reinterpret_cast<ClangThreadReply*>( e.GetClientData() );
if ( reply ) {
DoDeleteTempFile( reply->filename );
wxDELETE(reply);
}
DoCleanup();
}
void ClangDriver::GetMacros(IEditor *editor)
{
if( !editor )
return;
if(editor->GetProjectName().IsEmpty()) {
// This file is not part of the workspace
// do not attemp to color its preprocessors
editor->GetSTC()->SetProperty(wxT("lexer.cpp.track.preprocessor"), wxT("0"));
editor->GetSTC()->SetProperty(wxT("lexer.cpp.update.preprocessor"), wxT("0"));
editor->GetSTC()->SetKeyWords(4, wxT(""));
editor->GetSTC()->Colourise(0, wxSTC_INVALID_POSITION);
return;
}
wxString projectPath;
wxString pchFile;
FileTypeCmpArgs_t compileFlags = DoPrepareCompilationArgs(editor->GetProjectName(), editor->GetFileName().GetFullPath(), projectPath, pchFile);
wxString cmd;
wxFileName exePath( clStandardPaths::Get().GetBinaryFullPath("codelite-clang") );
wxFileName outputFolder(pchFile);
// Select the compilation args
FileExtManager::FileType type = FileExtManager::TypeSourceCpp; // Default is C++
switch(FileExtManager::GetType(editor->GetFileName().GetFullName())) {
case FileExtManager::TypeSourceC:
type = FileExtManager::TypeSourceC;
break;
default:
// Use the default
break;
}
const wxArrayString& compilerSwitches = compileFlags[type];
cmd << wxT("\"") << exePath.GetFullPath() << wxT("\" parse-macros \"") << editor->GetFileName().GetFullPath() << wxT("\" \"") << outputFolder.GetPath() << wxT("\" ");
for(size_t i=0; i<compilerSwitches.GetCount(); i++) {
cmd << compilerSwitches.Item(i) << wxT(" ");
}
ClangMacroHandler *handler = new ClangMacroHandler();
CL_DEBUG(wxT("Executing command: %s"), cmd.c_str());
handler->SetProcessAndEditor( ::CreateAsyncProcess(handler, cmd), editor );
if(handler->GetProcess() == NULL) {
delete handler;
return;
}
}
void ClangDriver::OnDeletMacroHandler(wxCommandEvent& e)
{
ClangMacroHandler *h = reinterpret_cast<ClangMacroHandler*>(e.GetClientData());
if( h ) {
delete h;
}
}
void ClangDriver::OnWorkspaceLoaded(wxCommandEvent& event)
{
event.Skip();
wxLogNull nolog;
wxString cachePath;
cachePath << WorkspaceST::Get()->GetWorkspaceFileName().GetPath() << wxFileName::GetPathSeparator() << wxT(".clang");
wxMkdir(cachePath);
ClangTUCache::DeleteDirectoryContent(cachePath);
}
ClangThreadRequest::List_t ClangDriver::DoCreateListOfModifiedBuffers(IEditor* excludeEditor)
{
// Collect all modified buffers and pass them to clang as well
ClangThreadRequest::List_t modifiedBuffers;
std::vector<LEditor*> editors;
clMainFrame::Get()->GetMainBook()->GetAllEditors(editors, MainBook::kGetAll_IncludeDetached);
for(size_t i=0; i<editors.size(); i++) {
if( editors.at(i) == excludeEditor || !editors.at(i)->IsModified())
continue;
modifiedBuffers.push_back( std::make_pair( editors.at(i)->GetFileName().GetFullPath(), editors.at(i)->GetText() ) );
}
return modifiedBuffers;
}
void ClangDriver::DoDeleteTempFile(const wxString& fileName)
{
wxUnusedVar(fileName);
/*
if ( fileName.IsEmpty() ) {
return;
}
wxFileName sourceFile( fileName );
sourceFile.SetFullName( CODELITE_CLANG_FILE_PREFIX + sourceFile.GetFullName() );
{
wxLogNull nl;
m_clangCleanerThread.AddFileName( sourceFile.GetFullPath() );
}*/
}
#endif // HAS_LIBCLANG
|