1d5a2a073SDaniel Dunbar //===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===// 2d5a2a073SDaniel Dunbar // 3d5a2a073SDaniel Dunbar // The LLVM Compiler Infrastructure 4d5a2a073SDaniel Dunbar // 5d5a2a073SDaniel Dunbar // This file is distributed under the University of Illinois Open Source 6d5a2a073SDaniel Dunbar // License. See LICENSE.TXT for details. 7d5a2a073SDaniel Dunbar // 8d5a2a073SDaniel Dunbar //===----------------------------------------------------------------------===// 9d5a2a073SDaniel Dunbar // 10d5a2a073SDaniel Dunbar // This file implements the Clang-C Source Indexing library hooks for 11d5a2a073SDaniel Dunbar // code completion. 12d5a2a073SDaniel Dunbar // 13d5a2a073SDaniel Dunbar //===----------------------------------------------------------------------===// 14d5a2a073SDaniel Dunbar 15d5a2a073SDaniel Dunbar #include "CIndexer.h" 167df92ae9STed Kremenek #include "CXTranslationUnit.h" 174b4f3699STed Kremenek #include "CXString.h" 18d5a2a073SDaniel Dunbar #include "CIndexDiagnostic.h" 19d5a2a073SDaniel Dunbar #include "clang/Basic/SourceManager.h" 20d5a2a073SDaniel Dunbar #include "clang/Basic/FileManager.h" 218e984da8SDouglas Gregor #include "clang/Frontend/ASTUnit.h" 22d5a2a073SDaniel Dunbar #include "clang/Frontend/CompilerInstance.h" 23d5a2a073SDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 24d5a2a073SDaniel Dunbar #include "clang/Sema/CodeCompleteConsumer.h" 25028d3e4dSDouglas Gregor #include "llvm/ADT/SmallString.h" 26d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 279aeaa4d6SDouglas Gregor #include "llvm/Support/Atomic.h" 2877af1c56SDaniel Dunbar #include "llvm/Support/CrashRecoveryContext.h" 29d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 30028d3e4dSDouglas Gregor #include "llvm/Support/Timer.h" 31028d3e4dSDouglas Gregor #include "llvm/Support/raw_ostream.h" 328aaf4995SMichael J. Spencer #include "llvm/Support/Program.h" 33d6009ffbSDouglas Gregor #include <cstdlib> 34d6009ffbSDouglas Gregor #include <cstdio> 35d5a2a073SDaniel Dunbar 36028d3e4dSDouglas Gregor 37d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 38d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 39d5a2a073SDaniel Dunbar #include <arpa/inet.h> 40d5a2a073SDaniel Dunbar #include <sys/socket.h> 41d5a2a073SDaniel Dunbar #include <sys/types.h> 42d5a2a073SDaniel Dunbar #include <unistd.h> 43d5a2a073SDaniel Dunbar #endif 44d5a2a073SDaniel Dunbar 45d5a2a073SDaniel Dunbar using namespace clang; 46d5a2a073SDaniel Dunbar using namespace clang::cxstring; 47d5a2a073SDaniel Dunbar 48d5a2a073SDaniel Dunbar extern "C" { 49d5a2a073SDaniel Dunbar 50d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 51d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 52d5a2a073SDaniel Dunbar unsigned chunk_number) { 53b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 54d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 55d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 56d5a2a073SDaniel Dunbar 57d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 58d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 59d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 60d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 61d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 62d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 63d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 64d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 65d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 66d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 67d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 68d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 69d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 70d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 71d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 72d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 73d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 100d5a2a073SDaniel Dunbar } 101d5a2a073SDaniel Dunbar 102d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 103d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 104d5a2a073SDaniel Dunbar } 105d5a2a073SDaniel Dunbar 106d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 107d5a2a073SDaniel Dunbar unsigned chunk_number) { 108b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 109d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 11091554285STed Kremenek return createCXString((const char*)0); 111d5a2a073SDaniel Dunbar 112d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 113d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 114d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 115d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 116d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 117d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 118d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 119d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 120d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 121d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 122d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 123d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 124d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 125d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 126d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 127d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 128d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 129d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 13209737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 1338ed5b770SDouglas Gregor return createCXString((*CCStr)[chunk_number].Text, false); 13409737ee8SDouglas Gregor 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 136d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 137d5a2a073SDaniel Dunbar return createCXString(""); 138d5a2a073SDaniel Dunbar } 139d5a2a073SDaniel Dunbar 140d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 14191554285STed Kremenek return createCXString((const char*)0); 142d5a2a073SDaniel Dunbar } 143d5a2a073SDaniel Dunbar 144d5a2a073SDaniel Dunbar 145d5a2a073SDaniel Dunbar CXCompletionString 146d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 147d5a2a073SDaniel Dunbar unsigned chunk_number) { 148b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 149d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 150d5a2a073SDaniel Dunbar return 0; 151d5a2a073SDaniel Dunbar 152d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 153d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 154d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 155d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 156d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 157d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 158d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 159d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 160d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 161d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 162d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 163d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 164d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 165d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 166d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 167d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 168d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 169d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 170d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 171d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 172d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 173d5a2a073SDaniel Dunbar return 0; 174d5a2a073SDaniel Dunbar 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 176d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 177d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 178d5a2a073SDaniel Dunbar } 179d5a2a073SDaniel Dunbar 180d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 181d5a2a073SDaniel Dunbar return 0; 182d5a2a073SDaniel Dunbar } 183d5a2a073SDaniel Dunbar 184d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 185b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 186d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 187d5a2a073SDaniel Dunbar } 188d5a2a073SDaniel Dunbar 189a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 190b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 19147bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 192a2db793fSDouglas Gregor } 193a2db793fSDouglas Gregor 194f757a12dSDouglas Gregor enum CXAvailabilityKind 195f757a12dSDouglas Gregor clang_getCompletionAvailability(CXCompletionString completion_string) { 196b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 197b278aafbSDouglas Gregor return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability()) 198b278aafbSDouglas Gregor : CXAvailability_Available; 199f757a12dSDouglas Gregor } 200f757a12dSDouglas Gregor 201d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 202d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 203d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 204c30dcecbSAnders Carlsson AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts); 205d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 206d5a2a073SDaniel Dunbar 207d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 208d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 209d5a2a073SDaniel Dunbar 210d5a2a073SDaniel Dunbar /// \brief Diag object 2118e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 212d5a2a073SDaniel Dunbar 213d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 214d5a2a073SDaniel Dunbar LangOptions LangOpts; 215d5a2a073SDaniel Dunbar 2165159f616SChris Lattner FileSystemOptions FileSystemOpts; 2175159f616SChris Lattner 218d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 2195e14d39aSTed Kremenek llvm::IntrusiveRefCntPtr<FileManager> FileMgr; 220d5a2a073SDaniel Dunbar 22171731d6bSArgyrios Kyrtzidis /// \brief Source manager, used for diagnostics. 2225e14d39aSTed Kremenek llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr; 22371731d6bSArgyrios Kyrtzidis 224d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 225d5a2a073SDaniel Dunbar /// with the code-completion results. 226d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 227d8a5dba9SDouglas Gregor 2283f5a9efbSChris Lattner /// \brief Temporary buffers that will be deleted once we have finished with 2293f5a9efbSChris Lattner /// the code-completion results. 230d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 231b278aafbSDouglas Gregor 232162b712dSDouglas Gregor /// \brief Allocator used to store globally cached code-completion results. 233162b712dSDouglas Gregor llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> 234162b712dSDouglas Gregor CachedCompletionAllocator; 235162b712dSDouglas Gregor 236b278aafbSDouglas Gregor /// \brief Allocator used to store code completion results. 2376c7cb188SDouglas Gregor clang::CodeCompletionAllocator CodeCompletionAllocator; 238*2132584dSDouglas Gregor 239*2132584dSDouglas Gregor /// \brief Context under which completion occurred. 240*2132584dSDouglas Gregor enum clang::CodeCompletionContext::Kind ContextKind; 241*2132584dSDouglas Gregor 242*2132584dSDouglas Gregor /// \brief A bitfield representing the acceptable completions for the 243*2132584dSDouglas Gregor /// current context. 244*2132584dSDouglas Gregor unsigned long long Contexts; 245d5a2a073SDaniel Dunbar }; 246d5a2a073SDaniel Dunbar 24768dbaeadSDouglas Gregor /// \brief Tracks the number of code-completion result objects that are 24868dbaeadSDouglas Gregor /// currently active. 24968dbaeadSDouglas Gregor /// 25068dbaeadSDouglas Gregor /// Used for debugging purposes only. 2519aeaa4d6SDouglas Gregor static llvm::sys::cas_flag CodeCompletionResultObjects; 25268dbaeadSDouglas Gregor 253c30dcecbSAnders Carlsson AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( 254c30dcecbSAnders Carlsson const FileSystemOptions& FileSystemOpts) 255d0040648SArgyrios Kyrtzidis : CXCodeCompleteResults(), 256d0040648SArgyrios Kyrtzidis Diag(new Diagnostic( 257d0040648SArgyrios Kyrtzidis llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))), 258c30dcecbSAnders Carlsson FileSystemOpts(FileSystemOpts), 2595e14d39aSTed Kremenek FileMgr(new FileManager(FileSystemOpts)), 2605e14d39aSTed Kremenek SourceMgr(new SourceManager(*Diag, *FileMgr)) { 26168dbaeadSDouglas Gregor if (getenv("LIBCLANG_OBJTRACKING")) { 2629aeaa4d6SDouglas Gregor llvm::sys::AtomicIncrement(&CodeCompletionResultObjects); 26368dbaeadSDouglas Gregor fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects); 26468dbaeadSDouglas Gregor } 26568dbaeadSDouglas Gregor } 266d5a2a073SDaniel Dunbar 267d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 268d5a2a073SDaniel Dunbar delete [] Results; 269d5a2a073SDaniel Dunbar 270d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 271d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 272d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 273d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 27468dbaeadSDouglas Gregor 27568dbaeadSDouglas Gregor if (getenv("LIBCLANG_OBJTRACKING")) { 2769aeaa4d6SDouglas Gregor llvm::sys::AtomicDecrement(&CodeCompletionResultObjects); 27768dbaeadSDouglas Gregor fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects); 27868dbaeadSDouglas Gregor } 279d5a2a073SDaniel Dunbar } 280d5a2a073SDaniel Dunbar 2818e984da8SDouglas Gregor } // end extern "C" 2828e984da8SDouglas Gregor 283*2132584dSDouglas Gregor static unsigned long long getContextsForContextKind( 284*2132584dSDouglas Gregor enum CodeCompletionContext::Kind kind, 285*2132584dSDouglas Gregor Sema &S) { 286*2132584dSDouglas Gregor unsigned long long contexts = 0; 287*2132584dSDouglas Gregor switch (kind) { 288*2132584dSDouglas Gregor case CodeCompletionContext::CCC_OtherWithMacros: { 289*2132584dSDouglas Gregor //We can allow macros here, but we don't know what else is permissible 290*2132584dSDouglas Gregor //So we'll say the only thing permissible are macros 291*2132584dSDouglas Gregor contexts = CXCompletionContext_MacroName; 292*2132584dSDouglas Gregor break; 293*2132584dSDouglas Gregor } 294*2132584dSDouglas Gregor case CodeCompletionContext::CCC_TopLevel: 295*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCIvarList: 296*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ClassStructUnion: 297*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Type: { 298*2132584dSDouglas Gregor contexts = CXCompletionContext_AnyType | 299*2132584dSDouglas Gregor CXCompletionContext_ObjCInterface; 300*2132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 301*2132584dSDouglas Gregor contexts |= CXCompletionContext_EnumTag | 302*2132584dSDouglas Gregor CXCompletionContext_UnionTag | 303*2132584dSDouglas Gregor CXCompletionContext_StructTag | 304*2132584dSDouglas Gregor CXCompletionContext_ClassTag | 305*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 306*2132584dSDouglas Gregor } 307*2132584dSDouglas Gregor break; 308*2132584dSDouglas Gregor } 309*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Statement: { 310*2132584dSDouglas Gregor contexts = CXCompletionContext_AnyType | 311*2132584dSDouglas Gregor CXCompletionContext_ObjCInterface | 312*2132584dSDouglas Gregor CXCompletionContext_AnyValue; 313*2132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 314*2132584dSDouglas Gregor contexts |= CXCompletionContext_EnumTag | 315*2132584dSDouglas Gregor CXCompletionContext_UnionTag | 316*2132584dSDouglas Gregor CXCompletionContext_StructTag | 317*2132584dSDouglas Gregor CXCompletionContext_ClassTag | 318*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 319*2132584dSDouglas Gregor } 320*2132584dSDouglas Gregor break; 321*2132584dSDouglas Gregor } 322*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Expression: { 323*2132584dSDouglas Gregor contexts = CXCompletionContext_AnyValue; 324*2132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 325*2132584dSDouglas Gregor contexts |= CXCompletionContext_AnyType | 326*2132584dSDouglas Gregor CXCompletionContext_ObjCInterface | 327*2132584dSDouglas Gregor CXCompletionContext_EnumTag | 328*2132584dSDouglas Gregor CXCompletionContext_UnionTag | 329*2132584dSDouglas Gregor CXCompletionContext_StructTag | 330*2132584dSDouglas Gregor CXCompletionContext_ClassTag | 331*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 332*2132584dSDouglas Gregor } 333*2132584dSDouglas Gregor break; 334*2132584dSDouglas Gregor } 335*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCMessageReceiver: { 336*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCObjectValue | 337*2132584dSDouglas Gregor CXCompletionContext_ObjCSelectorValue | 338*2132584dSDouglas Gregor CXCompletionContext_ObjCInterface; 339*2132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 340*2132584dSDouglas Gregor contexts |= CXCompletionContext_CXXClassTypeValue | 341*2132584dSDouglas Gregor CXCompletionContext_AnyType | 342*2132584dSDouglas Gregor CXCompletionContext_EnumTag | 343*2132584dSDouglas Gregor CXCompletionContext_UnionTag | 344*2132584dSDouglas Gregor CXCompletionContext_StructTag | 345*2132584dSDouglas Gregor CXCompletionContext_ClassTag | 346*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 347*2132584dSDouglas Gregor } 348*2132584dSDouglas Gregor break; 349*2132584dSDouglas Gregor } 350*2132584dSDouglas Gregor case CodeCompletionContext::CCC_DotMemberAccess: { 351*2132584dSDouglas Gregor contexts = CXCompletionContext_DotMemberAccess; 352*2132584dSDouglas Gregor break; 353*2132584dSDouglas Gregor } 354*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ArrowMemberAccess: { 355*2132584dSDouglas Gregor contexts = CXCompletionContext_ArrowMemberAccess; 356*2132584dSDouglas Gregor break; 357*2132584dSDouglas Gregor } 358*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCPropertyAccess: { 359*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCPropertyAccess; 360*2132584dSDouglas Gregor break; 361*2132584dSDouglas Gregor } 362*2132584dSDouglas Gregor case CodeCompletionContext::CCC_EnumTag: { 363*2132584dSDouglas Gregor contexts = CXCompletionContext_EnumTag | 364*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 365*2132584dSDouglas Gregor break; 366*2132584dSDouglas Gregor } 367*2132584dSDouglas Gregor case CodeCompletionContext::CCC_UnionTag: { 368*2132584dSDouglas Gregor contexts = CXCompletionContext_UnionTag | 369*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 370*2132584dSDouglas Gregor break; 371*2132584dSDouglas Gregor } 372*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ClassOrStructTag: { 373*2132584dSDouglas Gregor contexts = CXCompletionContext_StructTag | 374*2132584dSDouglas Gregor CXCompletionContext_ClassTag | 375*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 376*2132584dSDouglas Gregor break; 377*2132584dSDouglas Gregor } 378*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCProtocolName: { 379*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCProtocol; 380*2132584dSDouglas Gregor break; 381*2132584dSDouglas Gregor } 382*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Namespace: { 383*2132584dSDouglas Gregor contexts = CXCompletionContext_Namespace; 384*2132584dSDouglas Gregor break; 385*2132584dSDouglas Gregor } 386*2132584dSDouglas Gregor case CodeCompletionContext::CCC_PotentiallyQualifiedName: { 387*2132584dSDouglas Gregor contexts = CXCompletionContext_NestedNameSpecifier; 388*2132584dSDouglas Gregor break; 389*2132584dSDouglas Gregor } 390*2132584dSDouglas Gregor case CodeCompletionContext::CCC_MacroNameUse: { 391*2132584dSDouglas Gregor contexts = CXCompletionContext_MacroName; 392*2132584dSDouglas Gregor break; 393*2132584dSDouglas Gregor } 394*2132584dSDouglas Gregor case CodeCompletionContext::CCC_NaturalLanguage: { 395*2132584dSDouglas Gregor contexts = CXCompletionContext_NaturalLanguage; 396*2132584dSDouglas Gregor break; 397*2132584dSDouglas Gregor } 398*2132584dSDouglas Gregor case CodeCompletionContext::CCC_SelectorName: { 399*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCSelectorName; 400*2132584dSDouglas Gregor break; 401*2132584dSDouglas Gregor } 402*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ParenthesizedExpression: { 403*2132584dSDouglas Gregor contexts = CXCompletionContext_AnyType | 404*2132584dSDouglas Gregor CXCompletionContext_ObjCInterface | 405*2132584dSDouglas Gregor CXCompletionContext_AnyValue; 406*2132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 407*2132584dSDouglas Gregor contexts |= CXCompletionContext_EnumTag | 408*2132584dSDouglas Gregor CXCompletionContext_UnionTag | 409*2132584dSDouglas Gregor CXCompletionContext_StructTag | 410*2132584dSDouglas Gregor CXCompletionContext_ClassTag | 411*2132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 412*2132584dSDouglas Gregor } 413*2132584dSDouglas Gregor break; 414*2132584dSDouglas Gregor } 415*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCInstanceMessage: { 416*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCInstanceMessage; 417*2132584dSDouglas Gregor break; 418*2132584dSDouglas Gregor } 419*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCClassMessage: { 420*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCClassMessage; 421*2132584dSDouglas Gregor break; 422*2132584dSDouglas Gregor } 423*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCSuperclass: { 424*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCInterface; 425*2132584dSDouglas Gregor break; 426*2132584dSDouglas Gregor } 427*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCCategoryName: { 428*2132584dSDouglas Gregor contexts = CXCompletionContext_ObjCCategory; 429*2132584dSDouglas Gregor break; 430*2132584dSDouglas Gregor } 431*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Other: 432*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCInterface: 433*2132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCImplementation: 434*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Name: 435*2132584dSDouglas Gregor case CodeCompletionContext::CCC_MacroName: 436*2132584dSDouglas Gregor case CodeCompletionContext::CCC_PreprocessorExpression: 437*2132584dSDouglas Gregor case CodeCompletionContext::CCC_PreprocessorDirective: 438*2132584dSDouglas Gregor case CodeCompletionContext::CCC_TypeQualifiers: { 439*2132584dSDouglas Gregor //Only Clang results should be accepted, so we'll set all of the other 440*2132584dSDouglas Gregor //context bits to 0 (i.e. the empty set) 441*2132584dSDouglas Gregor contexts = CXCompletionContext_Unexposed; 442*2132584dSDouglas Gregor break; 443*2132584dSDouglas Gregor } 444*2132584dSDouglas Gregor case CodeCompletionContext::CCC_Recovery: { 445*2132584dSDouglas Gregor //We don't know what the current context is, so we'll return unknown 446*2132584dSDouglas Gregor //This is the equivalent of setting all of the other context bits 447*2132584dSDouglas Gregor contexts = CXCompletionContext_Unknown; 448*2132584dSDouglas Gregor break; 449*2132584dSDouglas Gregor } 450*2132584dSDouglas Gregor } 451*2132584dSDouglas Gregor return contexts; 452*2132584dSDouglas Gregor } 453*2132584dSDouglas Gregor 4548e984da8SDouglas Gregor namespace { 4558e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 4568e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 45736e3b5c7SDouglas Gregor llvm::SmallVector<CXCompletionResult, 16> StoredResults; 4588e984da8SDouglas Gregor public: 459b278aafbSDouglas Gregor CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 46039982197SDouglas Gregor : CodeCompleteConsumer(true, false, true, false), 46139982197SDouglas Gregor AllocatedResults(Results) { } 46236e3b5c7SDouglas Gregor ~CaptureCompletionResults() { Finish(); } 4638e984da8SDouglas Gregor 46400c37ef0SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, 46500c37ef0SDouglas Gregor CodeCompletionContext Context, 466276321a9SJohn McCall CodeCompletionResult *Results, 4678e984da8SDouglas Gregor unsigned NumResults) { 46836e3b5c7SDouglas Gregor StoredResults.reserve(StoredResults.size() + NumResults); 4698e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 470b278aafbSDouglas Gregor CodeCompletionString *StoredCompletion 471b278aafbSDouglas Gregor = Results[I].CreateCodeCompletionString(S, 472b278aafbSDouglas Gregor AllocatedResults.CodeCompletionAllocator); 47336e3b5c7SDouglas Gregor 47436e3b5c7SDouglas Gregor CXCompletionResult R; 47536e3b5c7SDouglas Gregor R.CursorKind = Results[I].CursorKind; 47636e3b5c7SDouglas Gregor R.CompletionString = StoredCompletion; 47736e3b5c7SDouglas Gregor StoredResults.push_back(R); 4788e984da8SDouglas Gregor } 479*2132584dSDouglas Gregor 480*2132584dSDouglas Gregor enum CodeCompletionContext::Kind kind = Context.getKind(); 481*2132584dSDouglas Gregor 482*2132584dSDouglas Gregor AllocatedResults.ContextKind = kind; 483*2132584dSDouglas Gregor AllocatedResults.Contexts = getContextsForContextKind(kind, S); 4848e984da8SDouglas Gregor } 48549f67ce4SDouglas Gregor 48636e3b5c7SDouglas Gregor virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 48736e3b5c7SDouglas Gregor OverloadCandidate *Candidates, 48836e3b5c7SDouglas Gregor unsigned NumCandidates) { 48936e3b5c7SDouglas Gregor StoredResults.reserve(StoredResults.size() + NumCandidates); 49036e3b5c7SDouglas Gregor for (unsigned I = 0; I != NumCandidates; ++I) { 491b278aafbSDouglas Gregor CodeCompletionString *StoredCompletion 492b278aafbSDouglas Gregor = Candidates[I].CreateSignatureString(CurrentArg, S, 493b278aafbSDouglas Gregor AllocatedResults.CodeCompletionAllocator); 49436e3b5c7SDouglas Gregor 49536e3b5c7SDouglas Gregor CXCompletionResult R; 49636e3b5c7SDouglas Gregor R.CursorKind = CXCursor_NotImplemented; 49736e3b5c7SDouglas Gregor R.CompletionString = StoredCompletion; 49836e3b5c7SDouglas Gregor StoredResults.push_back(R); 49936e3b5c7SDouglas Gregor } 50036e3b5c7SDouglas Gregor } 50136e3b5c7SDouglas Gregor 502bcbf46c7SDouglas Gregor virtual CodeCompletionAllocator &getAllocator() { 503b278aafbSDouglas Gregor return AllocatedResults.CodeCompletionAllocator; 504b278aafbSDouglas Gregor } 505b278aafbSDouglas Gregor 50636e3b5c7SDouglas Gregor private: 50736e3b5c7SDouglas Gregor void Finish() { 50836e3b5c7SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; 50936e3b5c7SDouglas Gregor AllocatedResults.NumResults = StoredResults.size(); 51036e3b5c7SDouglas Gregor std::memcpy(AllocatedResults.Results, StoredResults.data(), 51136e3b5c7SDouglas Gregor StoredResults.size() * sizeof(CXCompletionResult)); 51236e3b5c7SDouglas Gregor StoredResults.clear(); 51336e3b5c7SDouglas Gregor } 5148e984da8SDouglas Gregor }; 5158e984da8SDouglas Gregor } 5168e984da8SDouglas Gregor 5178e984da8SDouglas Gregor extern "C" { 51877af1c56SDaniel Dunbar struct CodeCompleteAtInfo { 51977af1c56SDaniel Dunbar CXTranslationUnit TU; 52077af1c56SDaniel Dunbar const char *complete_filename; 52177af1c56SDaniel Dunbar unsigned complete_line; 52277af1c56SDaniel Dunbar unsigned complete_column; 52377af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files; 52477af1c56SDaniel Dunbar unsigned num_unsaved_files; 52577af1c56SDaniel Dunbar unsigned options; 52677af1c56SDaniel Dunbar CXCodeCompleteResults *result; 52777af1c56SDaniel Dunbar }; 52877af1c56SDaniel Dunbar void clang_codeCompleteAt_Impl(void *UserData) { 52977af1c56SDaniel Dunbar CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 53077af1c56SDaniel Dunbar CXTranslationUnit TU = CCAI->TU; 53177af1c56SDaniel Dunbar const char *complete_filename = CCAI->complete_filename; 53277af1c56SDaniel Dunbar unsigned complete_line = CCAI->complete_line; 53377af1c56SDaniel Dunbar unsigned complete_column = CCAI->complete_column; 53477af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 53577af1c56SDaniel Dunbar unsigned num_unsaved_files = CCAI->num_unsaved_files; 53677af1c56SDaniel Dunbar unsigned options = CCAI->options; 53777af1c56SDaniel Dunbar CCAI->result = 0; 53877af1c56SDaniel Dunbar 5398e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 5408e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 5418e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 5428e984da8SDouglas Gregor #endif 5438e984da8SDouglas Gregor #endif 5448e984da8SDouglas Gregor 5458e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 5468e984da8SDouglas Gregor 54791554285STed Kremenek ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData); 5488e984da8SDouglas Gregor if (!AST) 54977af1c56SDaniel Dunbar return; 5508e984da8SDouglas Gregor 551ca5b0539SDouglas Gregor ASTUnit::ConcurrencyCheck Check(*AST); 552ca5b0539SDouglas Gregor 5538e984da8SDouglas Gregor // Perform the remapping of source files. 5548e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 5558e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 5568e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 5578e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 5588e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 5598e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 5608e984da8SDouglas Gregor Buffer)); 5618e984da8SDouglas Gregor } 5628e984da8SDouglas Gregor 5638e984da8SDouglas Gregor if (EnableLogging) { 5648e984da8SDouglas Gregor // FIXME: Add logging. 5658e984da8SDouglas Gregor } 5668e984da8SDouglas Gregor 5678e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 568c30dcecbSAnders Carlsson AllocatedCXCodeCompleteResults *Results = 569c30dcecbSAnders Carlsson new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts()); 5708e984da8SDouglas Gregor Results->Results = 0; 5718e984da8SDouglas Gregor Results->NumResults = 0; 5728e984da8SDouglas Gregor 5738e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 5748e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 5758e984da8SDouglas Gregor 5768e984da8SDouglas Gregor // Perform completion. 5778e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 578b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 579b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 580b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 581b68bc59aSDouglas Gregor Capture, 5825e14d39aSTed Kremenek *Results->Diag, Results->LangOpts, *Results->SourceMgr, 5835e14d39aSTed Kremenek *Results->FileMgr, Results->Diagnostics, 584b97b666cSDouglas Gregor Results->TemporaryBuffers); 5858e984da8SDouglas Gregor 586162b712dSDouglas Gregor // Keep a reference to the allocator used for cached global completions, so 587162b712dSDouglas Gregor // that we can be sure that the memory used by our code completion strings 588162b712dSDouglas Gregor // doesn't get freed due to subsequent reparses (while the code completion 589162b712dSDouglas Gregor // results are still active). 590162b712dSDouglas Gregor Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator(); 591162b712dSDouglas Gregor 5928e984da8SDouglas Gregor 5938e984da8SDouglas Gregor 5948e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 5958e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 5968e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 5978e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 5988e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 5998e984da8SDouglas Gregor 6008e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 6018e984da8SDouglas Gregor const char *lang = 0; 6028e984da8SDouglas Gregor bool usesPCH = false; 6038e984da8SDouglas Gregor 6048e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 6058e984da8SDouglas Gregor I != E; ++I) { 6068e984da8SDouglas Gregor if (*I == 0) 6078e984da8SDouglas Gregor continue; 6088e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 6098e984da8SDouglas Gregor if (I + 1 != E) { 6108e984da8SDouglas Gregor lang = *(++I); 6118e984da8SDouglas Gregor continue; 6128e984da8SDouglas Gregor } 6138e984da8SDouglas Gregor } 6148e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 6158e984da8SDouglas Gregor if (I+1 != E) { 6168e984da8SDouglas Gregor const char *arg = *(++I); 6178e984da8SDouglas Gregor llvm::SmallString<512> pchName; 6188e984da8SDouglas Gregor { 6198e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 6208e984da8SDouglas Gregor os << arg << ".pth"; 6218e984da8SDouglas Gregor } 6228e984da8SDouglas Gregor pchName.push_back('\0'); 6238e984da8SDouglas Gregor struct stat stat_results; 6248e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 6258e984da8SDouglas Gregor usesPCH = true; 6268e984da8SDouglas Gregor continue; 6278e984da8SDouglas Gregor } 6288e984da8SDouglas Gregor } 6298e984da8SDouglas Gregor } 6308e984da8SDouglas Gregor 6318e984da8SDouglas Gregor os << "{ "; 6328e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 6338e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 6348e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 6358e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 6368e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 6378e984da8SDouglas Gregor const char *name = getlogin(); 6388e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 6398e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 6408e984da8SDouglas Gregor os << " }"; 6418e984da8SDouglas Gregor 6428e984da8SDouglas Gregor llvm::StringRef res = os.str(); 6438e984da8SDouglas Gregor if (res.size() > 0) { 6448e984da8SDouglas Gregor do { 6458e984da8SDouglas Gregor // Setup the UDP socket. 6468e984da8SDouglas Gregor struct sockaddr_in servaddr; 6478e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 6488e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 6498e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 6508e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 6518e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 6528e984da8SDouglas Gregor break; 6538e984da8SDouglas Gregor 6548e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 6558e984da8SDouglas Gregor if (sockfd < 0) 6568e984da8SDouglas Gregor break; 6578e984da8SDouglas Gregor 6588e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 6598e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 6608e984da8SDouglas Gregor close(sockfd); 6618e984da8SDouglas Gregor } 6628e984da8SDouglas Gregor while (false); 6638e984da8SDouglas Gregor } 6648e984da8SDouglas Gregor #endif 6658e984da8SDouglas Gregor #endif 66677af1c56SDaniel Dunbar CCAI->result = Results; 66777af1c56SDaniel Dunbar } 66877af1c56SDaniel Dunbar CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 66977af1c56SDaniel Dunbar const char *complete_filename, 67077af1c56SDaniel Dunbar unsigned complete_line, 67177af1c56SDaniel Dunbar unsigned complete_column, 67277af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 67377af1c56SDaniel Dunbar unsigned num_unsaved_files, 67477af1c56SDaniel Dunbar unsigned options) { 67577af1c56SDaniel Dunbar CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 67677af1c56SDaniel Dunbar complete_column, unsaved_files, num_unsaved_files, 67777af1c56SDaniel Dunbar options, 0 }; 67877af1c56SDaniel Dunbar llvm::CrashRecoveryContext CRC; 67977af1c56SDaniel Dunbar 680b7383e6dSDaniel Dunbar if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { 68177af1c56SDaniel Dunbar fprintf(stderr, "libclang: crash detected in code completion\n"); 68291554285STed Kremenek static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); 68377af1c56SDaniel Dunbar return 0; 684af44c781SDouglas Gregor } else if (getenv("LIBCLANG_RESOURCE_USAGE")) 685af44c781SDouglas Gregor PrintLibclangResourceUsage(TU); 68677af1c56SDaniel Dunbar 68777af1c56SDaniel Dunbar return CCAI.result; 6888e984da8SDouglas Gregor } 6898e984da8SDouglas Gregor 690b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 691b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 692b68bc59aSDouglas Gregor } 693b68bc59aSDouglas Gregor 694d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 695d5a2a073SDaniel Dunbar if (!ResultsIn) 696d5a2a073SDaniel Dunbar return; 697d5a2a073SDaniel Dunbar 698d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 699d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 700d5a2a073SDaniel Dunbar delete Results; 701d5a2a073SDaniel Dunbar } 702d5a2a073SDaniel Dunbar 703d5a2a073SDaniel Dunbar unsigned 704d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 705d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 706d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 707d5a2a073SDaniel Dunbar if (!Results) 708d5a2a073SDaniel Dunbar return 0; 709d5a2a073SDaniel Dunbar 710d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 711d5a2a073SDaniel Dunbar } 712d5a2a073SDaniel Dunbar 713d5a2a073SDaniel Dunbar CXDiagnostic 714d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 715d5a2a073SDaniel Dunbar unsigned Index) { 716d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 717d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 718d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 719d5a2a073SDaniel Dunbar return 0; 720d5a2a073SDaniel Dunbar 721d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 722d5a2a073SDaniel Dunbar } 723d5a2a073SDaniel Dunbar 724*2132584dSDouglas Gregor unsigned long long 725*2132584dSDouglas Gregor clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) { 726*2132584dSDouglas Gregor AllocatedCXCodeCompleteResults *Results 727*2132584dSDouglas Gregor = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 728*2132584dSDouglas Gregor if (!Results) 729*2132584dSDouglas Gregor return 0; 730*2132584dSDouglas Gregor 731*2132584dSDouglas Gregor return Results->Contexts; 732*2132584dSDouglas Gregor } 733d5a2a073SDaniel Dunbar 734d5a2a073SDaniel Dunbar } // end extern "C" 73549f67ce4SDouglas Gregor 7368e3e8743SDouglas Gregor /// \brief Simple utility function that appends a \p New string to the given 7378e3e8743SDouglas Gregor /// \p Old string, using the \p Buffer for storage. 7388e3e8743SDouglas Gregor /// 7398e3e8743SDouglas Gregor /// \param Old The string to which we are appending. This parameter will be 7408e3e8743SDouglas Gregor /// updated to reflect the complete string. 7418e3e8743SDouglas Gregor /// 7428e3e8743SDouglas Gregor /// 7438e3e8743SDouglas Gregor /// \param New The string to append to \p Old. 7448e3e8743SDouglas Gregor /// 7458e3e8743SDouglas Gregor /// \param Buffer A buffer that stores the actual, concatenated string. It will 7468e3e8743SDouglas Gregor /// be used if the old string is already-non-empty. 7478e3e8743SDouglas Gregor static void AppendToString(llvm::StringRef &Old, llvm::StringRef New, 7488e3e8743SDouglas Gregor llvm::SmallString<256> &Buffer) { 7498e3e8743SDouglas Gregor if (Old.empty()) { 7508e3e8743SDouglas Gregor Old = New; 7518e3e8743SDouglas Gregor return; 7528e3e8743SDouglas Gregor } 7538e3e8743SDouglas Gregor 7548e3e8743SDouglas Gregor if (Buffer.empty()) 7558e3e8743SDouglas Gregor Buffer.append(Old.begin(), Old.end()); 7568e3e8743SDouglas Gregor Buffer.append(New.begin(), New.end()); 7578e3e8743SDouglas Gregor Old = Buffer.str(); 7588e3e8743SDouglas Gregor } 7598e3e8743SDouglas Gregor 7608e3e8743SDouglas Gregor /// \brief Get the typed-text blocks from the given code-completion string 7618e3e8743SDouglas Gregor /// and return them as a single string. 7628e3e8743SDouglas Gregor /// 7638e3e8743SDouglas Gregor /// \param String The code-completion string whose typed-text blocks will be 7648e3e8743SDouglas Gregor /// concatenated. 7658e3e8743SDouglas Gregor /// 7668e3e8743SDouglas Gregor /// \param Buffer A buffer used for storage of the completed name. 7678e3e8743SDouglas Gregor static llvm::StringRef GetTypedName(CodeCompletionString *String, 7688e3e8743SDouglas Gregor llvm::SmallString<256> &Buffer) { 7698e3e8743SDouglas Gregor llvm::StringRef Result; 7708e3e8743SDouglas Gregor for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); 7718e3e8743SDouglas Gregor C != CEnd; ++C) { 7728e3e8743SDouglas Gregor if (C->Kind == CodeCompletionString::CK_TypedText) 7738e3e8743SDouglas Gregor AppendToString(Result, C->Text, Buffer); 7748e3e8743SDouglas Gregor } 7758e3e8743SDouglas Gregor 7768e3e8743SDouglas Gregor return Result; 7778e3e8743SDouglas Gregor } 7788e3e8743SDouglas Gregor 77949f67ce4SDouglas Gregor namespace { 78049f67ce4SDouglas Gregor struct OrderCompletionResults { 78149f67ce4SDouglas Gregor bool operator()(const CXCompletionResult &XR, 78249f67ce4SDouglas Gregor const CXCompletionResult &YR) const { 783b278aafbSDouglas Gregor CodeCompletionString *X 784b278aafbSDouglas Gregor = (CodeCompletionString *)XR.CompletionString; 785b278aafbSDouglas Gregor CodeCompletionString *Y 786b278aafbSDouglas Gregor = (CodeCompletionString *)YR.CompletionString; 78749f67ce4SDouglas Gregor 7888e3e8743SDouglas Gregor llvm::SmallString<256> XBuffer; 7898e3e8743SDouglas Gregor llvm::StringRef XText = GetTypedName(X, XBuffer); 7908e3e8743SDouglas Gregor llvm::SmallString<256> YBuffer; 7918e3e8743SDouglas Gregor llvm::StringRef YText = GetTypedName(Y, YBuffer); 79249f67ce4SDouglas Gregor 7938e3e8743SDouglas Gregor if (XText.empty() || YText.empty()) 7948e3e8743SDouglas Gregor return !XText.empty(); 7958e3e8743SDouglas Gregor 7968e3e8743SDouglas Gregor int result = XText.compare_lower(YText); 79749f67ce4SDouglas Gregor if (result < 0) 79849f67ce4SDouglas Gregor return true; 79949f67ce4SDouglas Gregor if (result > 0) 80049f67ce4SDouglas Gregor return false; 80149f67ce4SDouglas Gregor 8028e3e8743SDouglas Gregor result = XText.compare(YText); 803c7a7d92aSDouglas Gregor return result < 0; 80449f67ce4SDouglas Gregor } 80549f67ce4SDouglas Gregor }; 80649f67ce4SDouglas Gregor } 80749f67ce4SDouglas Gregor 80849f67ce4SDouglas Gregor extern "C" { 80949f67ce4SDouglas Gregor void clang_sortCodeCompletionResults(CXCompletionResult *Results, 81049f67ce4SDouglas Gregor unsigned NumResults) { 81149f67ce4SDouglas Gregor std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); 81249f67ce4SDouglas Gregor } 81349f67ce4SDouglas Gregor } 814