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