1 //===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the Clang-C Source Indexing library hooks for 11 // code completion. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "CIndexer.h" 16 #include "CIndexDiagnostic.h" 17 #include "CLog.h" 18 #include "CXCursor.h" 19 #include "CXString.h" 20 #include "CXTranslationUnit.h" 21 #include "clang/AST/Decl.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/Type.h" 24 #include "clang/Basic/FileManager.h" 25 #include "clang/Basic/SourceManager.h" 26 #include "clang/Frontend/ASTUnit.h" 27 #include "clang/Frontend/CompilerInstance.h" 28 #include "clang/Frontend/FrontendDiagnostic.h" 29 #include "clang/Sema/CodeCompleteConsumer.h" 30 #include "llvm/ADT/SmallString.h" 31 #include "llvm/ADT/StringExtras.h" 32 #include "llvm/Support/Atomic.h" 33 #include "llvm/Support/CrashRecoveryContext.h" 34 #include "llvm/Support/MemoryBuffer.h" 35 #include "llvm/Support/PathV1.h" 36 #include "llvm/Support/Program.h" 37 #include "llvm/Support/Timer.h" 38 #include "llvm/Support/raw_ostream.h" 39 #include <cstdio> 40 #include <cstdlib> 41 #include <string> 42 43 44 #ifdef UDP_CODE_COMPLETION_LOGGER 45 #include "clang/Basic/Version.h" 46 #include <arpa/inet.h> 47 #include <sys/socket.h> 48 #include <sys/types.h> 49 #include <unistd.h> 50 #endif 51 52 using namespace clang; 53 using namespace clang::cxindex; 54 55 extern "C" { 56 57 enum CXCompletionChunkKind 58 clang_getCompletionChunkKind(CXCompletionString completion_string, 59 unsigned chunk_number) { 60 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 61 if (!CCStr || chunk_number >= CCStr->size()) 62 return CXCompletionChunk_Text; 63 64 switch ((*CCStr)[chunk_number].Kind) { 65 case CodeCompletionString::CK_TypedText: 66 return CXCompletionChunk_TypedText; 67 case CodeCompletionString::CK_Text: 68 return CXCompletionChunk_Text; 69 case CodeCompletionString::CK_Optional: 70 return CXCompletionChunk_Optional; 71 case CodeCompletionString::CK_Placeholder: 72 return CXCompletionChunk_Placeholder; 73 case CodeCompletionString::CK_Informative: 74 return CXCompletionChunk_Informative; 75 case CodeCompletionString::CK_ResultType: 76 return CXCompletionChunk_ResultType; 77 case CodeCompletionString::CK_CurrentParameter: 78 return CXCompletionChunk_CurrentParameter; 79 case CodeCompletionString::CK_LeftParen: 80 return CXCompletionChunk_LeftParen; 81 case CodeCompletionString::CK_RightParen: 82 return CXCompletionChunk_RightParen; 83 case CodeCompletionString::CK_LeftBracket: 84 return CXCompletionChunk_LeftBracket; 85 case CodeCompletionString::CK_RightBracket: 86 return CXCompletionChunk_RightBracket; 87 case CodeCompletionString::CK_LeftBrace: 88 return CXCompletionChunk_LeftBrace; 89 case CodeCompletionString::CK_RightBrace: 90 return CXCompletionChunk_RightBrace; 91 case CodeCompletionString::CK_LeftAngle: 92 return CXCompletionChunk_LeftAngle; 93 case CodeCompletionString::CK_RightAngle: 94 return CXCompletionChunk_RightAngle; 95 case CodeCompletionString::CK_Comma: 96 return CXCompletionChunk_Comma; 97 case CodeCompletionString::CK_Colon: 98 return CXCompletionChunk_Colon; 99 case CodeCompletionString::CK_SemiColon: 100 return CXCompletionChunk_SemiColon; 101 case CodeCompletionString::CK_Equal: 102 return CXCompletionChunk_Equal; 103 case CodeCompletionString::CK_HorizontalSpace: 104 return CXCompletionChunk_HorizontalSpace; 105 case CodeCompletionString::CK_VerticalSpace: 106 return CXCompletionChunk_VerticalSpace; 107 } 108 109 llvm_unreachable("Invalid CompletionKind!"); 110 } 111 112 CXString clang_getCompletionChunkText(CXCompletionString completion_string, 113 unsigned chunk_number) { 114 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 115 if (!CCStr || chunk_number >= CCStr->size()) 116 return cxstring::createNull(); 117 118 switch ((*CCStr)[chunk_number].Kind) { 119 case CodeCompletionString::CK_TypedText: 120 case CodeCompletionString::CK_Text: 121 case CodeCompletionString::CK_Placeholder: 122 case CodeCompletionString::CK_CurrentParameter: 123 case CodeCompletionString::CK_Informative: 124 case CodeCompletionString::CK_LeftParen: 125 case CodeCompletionString::CK_RightParen: 126 case CodeCompletionString::CK_LeftBracket: 127 case CodeCompletionString::CK_RightBracket: 128 case CodeCompletionString::CK_LeftBrace: 129 case CodeCompletionString::CK_RightBrace: 130 case CodeCompletionString::CK_LeftAngle: 131 case CodeCompletionString::CK_RightAngle: 132 case CodeCompletionString::CK_Comma: 133 case CodeCompletionString::CK_ResultType: 134 case CodeCompletionString::CK_Colon: 135 case CodeCompletionString::CK_SemiColon: 136 case CodeCompletionString::CK_Equal: 137 case CodeCompletionString::CK_HorizontalSpace: 138 case CodeCompletionString::CK_VerticalSpace: 139 return cxstring::createRef((*CCStr)[chunk_number].Text); 140 141 case CodeCompletionString::CK_Optional: 142 // Note: treated as an empty text block. 143 return cxstring::createEmpty(); 144 } 145 146 llvm_unreachable("Invalid CodeCompletionString Kind!"); 147 } 148 149 150 CXCompletionString 151 clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 152 unsigned chunk_number) { 153 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 154 if (!CCStr || chunk_number >= CCStr->size()) 155 return 0; 156 157 switch ((*CCStr)[chunk_number].Kind) { 158 case CodeCompletionString::CK_TypedText: 159 case CodeCompletionString::CK_Text: 160 case CodeCompletionString::CK_Placeholder: 161 case CodeCompletionString::CK_CurrentParameter: 162 case CodeCompletionString::CK_Informative: 163 case CodeCompletionString::CK_LeftParen: 164 case CodeCompletionString::CK_RightParen: 165 case CodeCompletionString::CK_LeftBracket: 166 case CodeCompletionString::CK_RightBracket: 167 case CodeCompletionString::CK_LeftBrace: 168 case CodeCompletionString::CK_RightBrace: 169 case CodeCompletionString::CK_LeftAngle: 170 case CodeCompletionString::CK_RightAngle: 171 case CodeCompletionString::CK_Comma: 172 case CodeCompletionString::CK_ResultType: 173 case CodeCompletionString::CK_Colon: 174 case CodeCompletionString::CK_SemiColon: 175 case CodeCompletionString::CK_Equal: 176 case CodeCompletionString::CK_HorizontalSpace: 177 case CodeCompletionString::CK_VerticalSpace: 178 return 0; 179 180 case CodeCompletionString::CK_Optional: 181 // Note: treated as an empty text block. 182 return (*CCStr)[chunk_number].Optional; 183 } 184 185 llvm_unreachable("Invalid CompletionKind!"); 186 } 187 188 unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 189 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 190 return CCStr? CCStr->size() : 0; 191 } 192 193 unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 194 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 195 return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 196 } 197 198 enum CXAvailabilityKind 199 clang_getCompletionAvailability(CXCompletionString completion_string) { 200 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 201 return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability()) 202 : CXAvailability_Available; 203 } 204 205 unsigned clang_getCompletionNumAnnotations(CXCompletionString completion_string) 206 { 207 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 208 return CCStr ? CCStr->getAnnotationCount() : 0; 209 } 210 211 CXString clang_getCompletionAnnotation(CXCompletionString completion_string, 212 unsigned annotation_number) { 213 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 214 return CCStr ? cxstring::createRef(CCStr->getAnnotation(annotation_number)) 215 : cxstring::createNull(); 216 } 217 218 CXString 219 clang_getCompletionParent(CXCompletionString completion_string, 220 CXCursorKind *kind) { 221 if (kind) 222 *kind = CXCursor_NotImplemented; 223 224 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 225 if (!CCStr) 226 return cxstring::createNull(); 227 228 return cxstring::createRef(CCStr->getParentContextName()); 229 } 230 231 CXString 232 clang_getCompletionBriefComment(CXCompletionString completion_string) { 233 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 234 235 if (!CCStr) 236 return cxstring::createNull(); 237 238 return cxstring::createRef(CCStr->getBriefComment()); 239 } 240 241 namespace { 242 243 /// \brief The CXCodeCompleteResults structure we allocate internally; 244 /// the client only sees the initial CXCodeCompleteResults structure. 245 /// 246 /// Normally, clients of CXString shouldn't care whether or not a CXString is 247 /// managed by a pool or by explicitly malloc'ed memory. But 248 /// AllocatedCXCodeCompleteResults outlives the CXTranslationUnit, so we can 249 /// not rely on the StringPool in the TU. 250 struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 251 AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts); 252 ~AllocatedCXCodeCompleteResults(); 253 254 /// \brief Diagnostics produced while performing code completion. 255 SmallVector<StoredDiagnostic, 8> Diagnostics; 256 257 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; 258 259 /// \brief Diag object 260 IntrusiveRefCntPtr<DiagnosticsEngine> Diag; 261 262 /// \brief Language options used to adjust source locations. 263 LangOptions LangOpts; 264 265 FileSystemOptions FileSystemOpts; 266 267 /// \brief File manager, used for diagnostics. 268 IntrusiveRefCntPtr<FileManager> FileMgr; 269 270 /// \brief Source manager, used for diagnostics. 271 IntrusiveRefCntPtr<SourceManager> SourceMgr; 272 273 /// \brief Temporary files that should be removed once we have finished 274 /// with the code-completion results. 275 std::vector<llvm::sys::Path> TemporaryFiles; 276 277 /// \brief Temporary buffers that will be deleted once we have finished with 278 /// the code-completion results. 279 SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 280 281 /// \brief Allocator used to store globally cached code-completion results. 282 IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> 283 CachedCompletionAllocator; 284 285 /// \brief Allocator used to store code completion results. 286 IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> 287 CodeCompletionAllocator; 288 289 /// \brief Context under which completion occurred. 290 enum clang::CodeCompletionContext::Kind ContextKind; 291 292 /// \brief A bitfield representing the acceptable completions for the 293 /// current context. 294 unsigned long long Contexts; 295 296 /// \brief The kind of the container for the current context for completions. 297 enum CXCursorKind ContainerKind; 298 299 /// \brief The USR of the container for the current context for completions. 300 std::string ContainerUSR; 301 302 /// \brief a boolean value indicating whether there is complete information 303 /// about the container 304 unsigned ContainerIsIncomplete; 305 306 /// \brief A string containing the Objective-C selector entered thus far for a 307 /// message send. 308 std::string Selector; 309 }; 310 311 } // end anonymous namespace 312 313 /// \brief Tracks the number of code-completion result objects that are 314 /// currently active. 315 /// 316 /// Used for debugging purposes only. 317 static llvm::sys::cas_flag CodeCompletionResultObjects; 318 319 AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( 320 const FileSystemOptions& FileSystemOpts) 321 : CXCodeCompleteResults(), 322 DiagOpts(new DiagnosticOptions), 323 Diag(new DiagnosticsEngine( 324 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 325 &*DiagOpts)), 326 FileSystemOpts(FileSystemOpts), 327 FileMgr(new FileManager(FileSystemOpts)), 328 SourceMgr(new SourceManager(*Diag, *FileMgr)), 329 CodeCompletionAllocator(new clang::GlobalCodeCompletionAllocator), 330 Contexts(CXCompletionContext_Unknown), 331 ContainerKind(CXCursor_InvalidCode), 332 ContainerIsIncomplete(1) 333 { 334 if (getenv("LIBCLANG_OBJTRACKING")) { 335 llvm::sys::AtomicIncrement(&CodeCompletionResultObjects); 336 fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects); 337 } 338 } 339 340 AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 341 delete [] Results; 342 343 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 344 TemporaryFiles[I].eraseFromDisk(); 345 for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 346 delete TemporaryBuffers[I]; 347 348 if (getenv("LIBCLANG_OBJTRACKING")) { 349 llvm::sys::AtomicDecrement(&CodeCompletionResultObjects); 350 fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects); 351 } 352 } 353 354 } // end extern "C" 355 356 static unsigned long long getContextsForContextKind( 357 enum CodeCompletionContext::Kind kind, 358 Sema &S) { 359 unsigned long long contexts = 0; 360 switch (kind) { 361 case CodeCompletionContext::CCC_OtherWithMacros: { 362 //We can allow macros here, but we don't know what else is permissible 363 //So we'll say the only thing permissible are macros 364 contexts = CXCompletionContext_MacroName; 365 break; 366 } 367 case CodeCompletionContext::CCC_TopLevel: 368 case CodeCompletionContext::CCC_ObjCIvarList: 369 case CodeCompletionContext::CCC_ClassStructUnion: 370 case CodeCompletionContext::CCC_Type: { 371 contexts = CXCompletionContext_AnyType | 372 CXCompletionContext_ObjCInterface; 373 if (S.getLangOpts().CPlusPlus) { 374 contexts |= CXCompletionContext_EnumTag | 375 CXCompletionContext_UnionTag | 376 CXCompletionContext_StructTag | 377 CXCompletionContext_ClassTag | 378 CXCompletionContext_NestedNameSpecifier; 379 } 380 break; 381 } 382 case CodeCompletionContext::CCC_Statement: { 383 contexts = CXCompletionContext_AnyType | 384 CXCompletionContext_ObjCInterface | 385 CXCompletionContext_AnyValue; 386 if (S.getLangOpts().CPlusPlus) { 387 contexts |= CXCompletionContext_EnumTag | 388 CXCompletionContext_UnionTag | 389 CXCompletionContext_StructTag | 390 CXCompletionContext_ClassTag | 391 CXCompletionContext_NestedNameSpecifier; 392 } 393 break; 394 } 395 case CodeCompletionContext::CCC_Expression: { 396 contexts = CXCompletionContext_AnyValue; 397 if (S.getLangOpts().CPlusPlus) { 398 contexts |= CXCompletionContext_AnyType | 399 CXCompletionContext_ObjCInterface | 400 CXCompletionContext_EnumTag | 401 CXCompletionContext_UnionTag | 402 CXCompletionContext_StructTag | 403 CXCompletionContext_ClassTag | 404 CXCompletionContext_NestedNameSpecifier; 405 } 406 break; 407 } 408 case CodeCompletionContext::CCC_ObjCMessageReceiver: { 409 contexts = CXCompletionContext_ObjCObjectValue | 410 CXCompletionContext_ObjCSelectorValue | 411 CXCompletionContext_ObjCInterface; 412 if (S.getLangOpts().CPlusPlus) { 413 contexts |= CXCompletionContext_CXXClassTypeValue | 414 CXCompletionContext_AnyType | 415 CXCompletionContext_EnumTag | 416 CXCompletionContext_UnionTag | 417 CXCompletionContext_StructTag | 418 CXCompletionContext_ClassTag | 419 CXCompletionContext_NestedNameSpecifier; 420 } 421 break; 422 } 423 case CodeCompletionContext::CCC_DotMemberAccess: { 424 contexts = CXCompletionContext_DotMemberAccess; 425 break; 426 } 427 case CodeCompletionContext::CCC_ArrowMemberAccess: { 428 contexts = CXCompletionContext_ArrowMemberAccess; 429 break; 430 } 431 case CodeCompletionContext::CCC_ObjCPropertyAccess: { 432 contexts = CXCompletionContext_ObjCPropertyAccess; 433 break; 434 } 435 case CodeCompletionContext::CCC_EnumTag: { 436 contexts = CXCompletionContext_EnumTag | 437 CXCompletionContext_NestedNameSpecifier; 438 break; 439 } 440 case CodeCompletionContext::CCC_UnionTag: { 441 contexts = CXCompletionContext_UnionTag | 442 CXCompletionContext_NestedNameSpecifier; 443 break; 444 } 445 case CodeCompletionContext::CCC_ClassOrStructTag: { 446 contexts = CXCompletionContext_StructTag | 447 CXCompletionContext_ClassTag | 448 CXCompletionContext_NestedNameSpecifier; 449 break; 450 } 451 case CodeCompletionContext::CCC_ObjCProtocolName: { 452 contexts = CXCompletionContext_ObjCProtocol; 453 break; 454 } 455 case CodeCompletionContext::CCC_Namespace: { 456 contexts = CXCompletionContext_Namespace; 457 break; 458 } 459 case CodeCompletionContext::CCC_PotentiallyQualifiedName: { 460 contexts = CXCompletionContext_NestedNameSpecifier; 461 break; 462 } 463 case CodeCompletionContext::CCC_MacroNameUse: { 464 contexts = CXCompletionContext_MacroName; 465 break; 466 } 467 case CodeCompletionContext::CCC_NaturalLanguage: { 468 contexts = CXCompletionContext_NaturalLanguage; 469 break; 470 } 471 case CodeCompletionContext::CCC_SelectorName: { 472 contexts = CXCompletionContext_ObjCSelectorName; 473 break; 474 } 475 case CodeCompletionContext::CCC_ParenthesizedExpression: { 476 contexts = CXCompletionContext_AnyType | 477 CXCompletionContext_ObjCInterface | 478 CXCompletionContext_AnyValue; 479 if (S.getLangOpts().CPlusPlus) { 480 contexts |= CXCompletionContext_EnumTag | 481 CXCompletionContext_UnionTag | 482 CXCompletionContext_StructTag | 483 CXCompletionContext_ClassTag | 484 CXCompletionContext_NestedNameSpecifier; 485 } 486 break; 487 } 488 case CodeCompletionContext::CCC_ObjCInstanceMessage: { 489 contexts = CXCompletionContext_ObjCInstanceMessage; 490 break; 491 } 492 case CodeCompletionContext::CCC_ObjCClassMessage: { 493 contexts = CXCompletionContext_ObjCClassMessage; 494 break; 495 } 496 case CodeCompletionContext::CCC_ObjCInterfaceName: { 497 contexts = CXCompletionContext_ObjCInterface; 498 break; 499 } 500 case CodeCompletionContext::CCC_ObjCCategoryName: { 501 contexts = CXCompletionContext_ObjCCategory; 502 break; 503 } 504 case CodeCompletionContext::CCC_Other: 505 case CodeCompletionContext::CCC_ObjCInterface: 506 case CodeCompletionContext::CCC_ObjCImplementation: 507 case CodeCompletionContext::CCC_Name: 508 case CodeCompletionContext::CCC_MacroName: 509 case CodeCompletionContext::CCC_PreprocessorExpression: 510 case CodeCompletionContext::CCC_PreprocessorDirective: 511 case CodeCompletionContext::CCC_TypeQualifiers: { 512 //Only Clang results should be accepted, so we'll set all of the other 513 //context bits to 0 (i.e. the empty set) 514 contexts = CXCompletionContext_Unexposed; 515 break; 516 } 517 case CodeCompletionContext::CCC_Recovery: { 518 //We don't know what the current context is, so we'll return unknown 519 //This is the equivalent of setting all of the other context bits 520 contexts = CXCompletionContext_Unknown; 521 break; 522 } 523 } 524 return contexts; 525 } 526 527 namespace { 528 class CaptureCompletionResults : public CodeCompleteConsumer { 529 AllocatedCXCodeCompleteResults &AllocatedResults; 530 CodeCompletionTUInfo CCTUInfo; 531 SmallVector<CXCompletionResult, 16> StoredResults; 532 CXTranslationUnit *TU; 533 public: 534 CaptureCompletionResults(const CodeCompleteOptions &Opts, 535 AllocatedCXCodeCompleteResults &Results, 536 CXTranslationUnit *TranslationUnit) 537 : CodeCompleteConsumer(Opts, false), 538 AllocatedResults(Results), CCTUInfo(Results.CodeCompletionAllocator), 539 TU(TranslationUnit) { } 540 ~CaptureCompletionResults() { Finish(); } 541 542 virtual void ProcessCodeCompleteResults(Sema &S, 543 CodeCompletionContext Context, 544 CodeCompletionResult *Results, 545 unsigned NumResults) { 546 StoredResults.reserve(StoredResults.size() + NumResults); 547 for (unsigned I = 0; I != NumResults; ++I) { 548 CodeCompletionString *StoredCompletion 549 = Results[I].CreateCodeCompletionString(S, getAllocator(), 550 getCodeCompletionTUInfo(), 551 includeBriefComments()); 552 553 CXCompletionResult R; 554 R.CursorKind = Results[I].CursorKind; 555 R.CompletionString = StoredCompletion; 556 StoredResults.push_back(R); 557 } 558 559 enum CodeCompletionContext::Kind contextKind = Context.getKind(); 560 561 AllocatedResults.ContextKind = contextKind; 562 AllocatedResults.Contexts = getContextsForContextKind(contextKind, S); 563 564 AllocatedResults.Selector = ""; 565 if (Context.getNumSelIdents() > 0) { 566 for (unsigned i = 0; i < Context.getNumSelIdents(); i++) { 567 IdentifierInfo *selIdent = Context.getSelIdents()[i]; 568 if (selIdent != NULL) { 569 StringRef selectorString = Context.getSelIdents()[i]->getName(); 570 AllocatedResults.Selector += selectorString; 571 } 572 AllocatedResults.Selector += ":"; 573 } 574 } 575 576 QualType baseType = Context.getBaseType(); 577 NamedDecl *D = NULL; 578 579 if (!baseType.isNull()) { 580 // Get the declaration for a class/struct/union/enum type 581 if (const TagType *Tag = baseType->getAs<TagType>()) 582 D = Tag->getDecl(); 583 // Get the @interface declaration for a (possibly-qualified) Objective-C 584 // object pointer type, e.g., NSString* 585 else if (const ObjCObjectPointerType *ObjPtr = 586 baseType->getAs<ObjCObjectPointerType>()) 587 D = ObjPtr->getInterfaceDecl(); 588 // Get the @interface declaration for an Objective-C object type 589 else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>()) 590 D = Obj->getInterface(); 591 // Get the class for a C++ injected-class-name 592 else if (const InjectedClassNameType *Injected = 593 baseType->getAs<InjectedClassNameType>()) 594 D = Injected->getDecl(); 595 } 596 597 if (D != NULL) { 598 CXCursor cursor = cxcursor::MakeCXCursor(D, *TU); 599 600 AllocatedResults.ContainerKind = clang_getCursorKind(cursor); 601 602 CXString CursorUSR = clang_getCursorUSR(cursor); 603 AllocatedResults.ContainerUSR = clang_getCString(CursorUSR); 604 clang_disposeString(CursorUSR); 605 606 const Type *type = baseType.getTypePtrOrNull(); 607 if (type != NULL) { 608 AllocatedResults.ContainerIsIncomplete = type->isIncompleteType(); 609 } 610 else { 611 AllocatedResults.ContainerIsIncomplete = 1; 612 } 613 } 614 else { 615 AllocatedResults.ContainerKind = CXCursor_InvalidCode; 616 AllocatedResults.ContainerUSR.clear(); 617 AllocatedResults.ContainerIsIncomplete = 1; 618 } 619 } 620 621 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 622 OverloadCandidate *Candidates, 623 unsigned NumCandidates) { 624 StoredResults.reserve(StoredResults.size() + NumCandidates); 625 for (unsigned I = 0; I != NumCandidates; ++I) { 626 CodeCompletionString *StoredCompletion 627 = Candidates[I].CreateSignatureString(CurrentArg, S, getAllocator(), 628 getCodeCompletionTUInfo()); 629 630 CXCompletionResult R; 631 R.CursorKind = CXCursor_NotImplemented; 632 R.CompletionString = StoredCompletion; 633 StoredResults.push_back(R); 634 } 635 } 636 637 virtual CodeCompletionAllocator &getAllocator() { 638 return *AllocatedResults.CodeCompletionAllocator; 639 } 640 641 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { return CCTUInfo; } 642 643 private: 644 void Finish() { 645 AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; 646 AllocatedResults.NumResults = StoredResults.size(); 647 std::memcpy(AllocatedResults.Results, StoredResults.data(), 648 StoredResults.size() * sizeof(CXCompletionResult)); 649 StoredResults.clear(); 650 } 651 }; 652 } 653 654 extern "C" { 655 struct CodeCompleteAtInfo { 656 CXTranslationUnit TU; 657 const char *complete_filename; 658 unsigned complete_line; 659 unsigned complete_column; 660 struct CXUnsavedFile *unsaved_files; 661 unsigned num_unsaved_files; 662 unsigned options; 663 CXCodeCompleteResults *result; 664 }; 665 void clang_codeCompleteAt_Impl(void *UserData) { 666 CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 667 CXTranslationUnit TU = CCAI->TU; 668 const char *complete_filename = CCAI->complete_filename; 669 unsigned complete_line = CCAI->complete_line; 670 unsigned complete_column = CCAI->complete_column; 671 struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 672 unsigned num_unsaved_files = CCAI->num_unsaved_files; 673 unsigned options = CCAI->options; 674 bool IncludeBriefComments = options & CXCodeComplete_IncludeBriefComments; 675 CCAI->result = 0; 676 677 #ifdef UDP_CODE_COMPLETION_LOGGER 678 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 679 const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 680 #endif 681 #endif 682 683 bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 684 685 ASTUnit *AST = cxtu::getASTUnit(TU); 686 if (!AST) 687 return; 688 689 CIndexer *CXXIdx = TU->CIdx; 690 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) 691 setThreadBackgroundPriority(); 692 693 ASTUnit::ConcurrencyCheck Check(*AST); 694 695 // Perform the remapping of source files. 696 SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 697 for (unsigned I = 0; I != num_unsaved_files; ++I) { 698 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 699 const llvm::MemoryBuffer *Buffer 700 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 701 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 702 Buffer)); 703 } 704 705 if (EnableLogging) { 706 // FIXME: Add logging. 707 } 708 709 // Parse the resulting source file to find code-completion results. 710 AllocatedCXCodeCompleteResults *Results = 711 new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts()); 712 Results->Results = 0; 713 Results->NumResults = 0; 714 715 // Create a code-completion consumer to capture the results. 716 CodeCompleteOptions Opts; 717 Opts.IncludeBriefComments = IncludeBriefComments; 718 CaptureCompletionResults Capture(Opts, *Results, &TU); 719 720 // Perform completion. 721 AST->CodeComplete(complete_filename, complete_line, complete_column, 722 RemappedFiles.data(), RemappedFiles.size(), 723 (options & CXCodeComplete_IncludeMacros), 724 (options & CXCodeComplete_IncludeCodePatterns), 725 IncludeBriefComments, 726 Capture, 727 *Results->Diag, Results->LangOpts, *Results->SourceMgr, 728 *Results->FileMgr, Results->Diagnostics, 729 Results->TemporaryBuffers); 730 731 // Keep a reference to the allocator used for cached global completions, so 732 // that we can be sure that the memory used by our code completion strings 733 // doesn't get freed due to subsequent reparses (while the code completion 734 // results are still active). 735 Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator(); 736 737 738 739 #ifdef UDP_CODE_COMPLETION_LOGGER 740 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 741 const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 742 SmallString<256> LogResult; 743 llvm::raw_svector_ostream os(LogResult); 744 745 // Figure out the language and whether or not it uses PCH. 746 const char *lang = 0; 747 bool usesPCH = false; 748 749 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 750 I != E; ++I) { 751 if (*I == 0) 752 continue; 753 if (strcmp(*I, "-x") == 0) { 754 if (I + 1 != E) { 755 lang = *(++I); 756 continue; 757 } 758 } 759 else if (strcmp(*I, "-include") == 0) { 760 if (I+1 != E) { 761 const char *arg = *(++I); 762 SmallString<512> pchName; 763 { 764 llvm::raw_svector_ostream os(pchName); 765 os << arg << ".pth"; 766 } 767 pchName.push_back('\0'); 768 struct stat stat_results; 769 if (stat(pchName.str().c_str(), &stat_results) == 0) 770 usesPCH = true; 771 continue; 772 } 773 } 774 } 775 776 os << "{ "; 777 os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 778 os << ", \"numRes\": " << Results->NumResults; 779 os << ", \"diags\": " << Results->Diagnostics.size(); 780 os << ", \"pch\": " << (usesPCH ? "true" : "false"); 781 os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 782 const char *name = getlogin(); 783 os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 784 os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 785 os << " }"; 786 787 StringRef res = os.str(); 788 if (res.size() > 0) { 789 do { 790 // Setup the UDP socket. 791 struct sockaddr_in servaddr; 792 bzero(&servaddr, sizeof(servaddr)); 793 servaddr.sin_family = AF_INET; 794 servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 795 if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 796 &servaddr.sin_addr) <= 0) 797 break; 798 799 int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 800 if (sockfd < 0) 801 break; 802 803 sendto(sockfd, res.data(), res.size(), 0, 804 (struct sockaddr *)&servaddr, sizeof(servaddr)); 805 close(sockfd); 806 } 807 while (false); 808 } 809 #endif 810 #endif 811 CCAI->result = Results; 812 } 813 CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 814 const char *complete_filename, 815 unsigned complete_line, 816 unsigned complete_column, 817 struct CXUnsavedFile *unsaved_files, 818 unsigned num_unsaved_files, 819 unsigned options) { 820 LOG_FUNC_SECTION { 821 *Log << TU << ' ' 822 << complete_filename << ':' << complete_line << ':' << complete_column; 823 } 824 825 CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 826 complete_column, unsaved_files, num_unsaved_files, 827 options, 0 }; 828 829 if (getenv("LIBCLANG_NOTHREADS")) { 830 clang_codeCompleteAt_Impl(&CCAI); 831 return CCAI.result; 832 } 833 834 llvm::CrashRecoveryContext CRC; 835 836 if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { 837 fprintf(stderr, "libclang: crash detected in code completion\n"); 838 cxtu::getASTUnit(TU)->setUnsafeToFree(true); 839 return 0; 840 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) 841 PrintLibclangResourceUsage(TU); 842 843 return CCAI.result; 844 } 845 846 unsigned clang_defaultCodeCompleteOptions(void) { 847 return CXCodeComplete_IncludeMacros; 848 } 849 850 void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 851 if (!ResultsIn) 852 return; 853 854 AllocatedCXCodeCompleteResults *Results 855 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 856 delete Results; 857 } 858 859 unsigned 860 clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 861 AllocatedCXCodeCompleteResults *Results 862 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 863 if (!Results) 864 return 0; 865 866 return Results->Diagnostics.size(); 867 } 868 869 CXDiagnostic 870 clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 871 unsigned Index) { 872 AllocatedCXCodeCompleteResults *Results 873 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 874 if (!Results || Index >= Results->Diagnostics.size()) 875 return 0; 876 877 return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 878 } 879 880 unsigned long long 881 clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) { 882 AllocatedCXCodeCompleteResults *Results 883 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 884 if (!Results) 885 return 0; 886 887 return Results->Contexts; 888 } 889 890 enum CXCursorKind clang_codeCompleteGetContainerKind( 891 CXCodeCompleteResults *ResultsIn, 892 unsigned *IsIncomplete) { 893 AllocatedCXCodeCompleteResults *Results = 894 static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); 895 if (!Results) 896 return CXCursor_InvalidCode; 897 898 if (IsIncomplete != NULL) { 899 *IsIncomplete = Results->ContainerIsIncomplete; 900 } 901 902 return Results->ContainerKind; 903 } 904 905 CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) { 906 AllocatedCXCodeCompleteResults *Results = 907 static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); 908 if (!Results) 909 return cxstring::createEmpty(); 910 911 return cxstring::createRef(Results->ContainerUSR.c_str()); 912 } 913 914 915 CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) { 916 AllocatedCXCodeCompleteResults *Results = 917 static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); 918 if (!Results) 919 return cxstring::createEmpty(); 920 921 return cxstring::createDup(Results->Selector); 922 } 923 924 } // end extern "C" 925 926 /// \brief Simple utility function that appends a \p New string to the given 927 /// \p Old string, using the \p Buffer for storage. 928 /// 929 /// \param Old The string to which we are appending. This parameter will be 930 /// updated to reflect the complete string. 931 /// 932 /// 933 /// \param New The string to append to \p Old. 934 /// 935 /// \param Buffer A buffer that stores the actual, concatenated string. It will 936 /// be used if the old string is already-non-empty. 937 static void AppendToString(StringRef &Old, StringRef New, 938 SmallString<256> &Buffer) { 939 if (Old.empty()) { 940 Old = New; 941 return; 942 } 943 944 if (Buffer.empty()) 945 Buffer.append(Old.begin(), Old.end()); 946 Buffer.append(New.begin(), New.end()); 947 Old = Buffer.str(); 948 } 949 950 /// \brief Get the typed-text blocks from the given code-completion string 951 /// and return them as a single string. 952 /// 953 /// \param String The code-completion string whose typed-text blocks will be 954 /// concatenated. 955 /// 956 /// \param Buffer A buffer used for storage of the completed name. 957 static StringRef GetTypedName(CodeCompletionString *String, 958 SmallString<256> &Buffer) { 959 StringRef Result; 960 for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); 961 C != CEnd; ++C) { 962 if (C->Kind == CodeCompletionString::CK_TypedText) 963 AppendToString(Result, C->Text, Buffer); 964 } 965 966 return Result; 967 } 968 969 namespace { 970 struct OrderCompletionResults { 971 bool operator()(const CXCompletionResult &XR, 972 const CXCompletionResult &YR) const { 973 CodeCompletionString *X 974 = (CodeCompletionString *)XR.CompletionString; 975 CodeCompletionString *Y 976 = (CodeCompletionString *)YR.CompletionString; 977 978 SmallString<256> XBuffer; 979 StringRef XText = GetTypedName(X, XBuffer); 980 SmallString<256> YBuffer; 981 StringRef YText = GetTypedName(Y, YBuffer); 982 983 if (XText.empty() || YText.empty()) 984 return !XText.empty(); 985 986 int result = XText.compare_lower(YText); 987 if (result < 0) 988 return true; 989 if (result > 0) 990 return false; 991 992 result = XText.compare(YText); 993 return result < 0; 994 } 995 }; 996 } 997 998 extern "C" { 999 void clang_sortCodeCompletionResults(CXCompletionResult *Results, 1000 unsigned NumResults) { 1001 std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); 1002 } 1003 } 1004