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" 16d5a2a073SDaniel Dunbar #include "CIndexDiagnostic.h" 17d5a2a073SDaniel Dunbar #include "clang/Basic/SourceManager.h" 18d5a2a073SDaniel Dunbar #include "clang/Basic/FileManager.h" 198e984da8SDouglas Gregor #include "clang/Frontend/ASTUnit.h" 20d5a2a073SDaniel Dunbar #include "clang/Frontend/CompilerInstance.h" 21d5a2a073SDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 22d5a2a073SDaniel Dunbar #include "clang/Sema/CodeCompleteConsumer.h" 23028d3e4dSDouglas Gregor #include "llvm/ADT/SmallString.h" 24d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 2577af1c56SDaniel Dunbar #include "llvm/Support/CrashRecoveryContext.h" 26d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 27028d3e4dSDouglas Gregor #include "llvm/Support/Timer.h" 28028d3e4dSDouglas Gregor #include "llvm/Support/raw_ostream.h" 29d5a2a073SDaniel Dunbar #include "llvm/System/Program.h" 30d6009ffbSDouglas Gregor #include <cstdlib> 31d6009ffbSDouglas Gregor #include <cstdio> 32d5a2a073SDaniel Dunbar 33028d3e4dSDouglas Gregor 34d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 35d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 36d5a2a073SDaniel Dunbar #include <arpa/inet.h> 37d5a2a073SDaniel Dunbar #include <sys/socket.h> 38d5a2a073SDaniel Dunbar #include <sys/types.h> 39d5a2a073SDaniel Dunbar #include <unistd.h> 40d5a2a073SDaniel Dunbar #endif 41d5a2a073SDaniel Dunbar 42d5a2a073SDaniel Dunbar using namespace clang; 43d5a2a073SDaniel Dunbar using namespace clang::cxstring; 44d5a2a073SDaniel Dunbar 45a2db793fSDouglas Gregor namespace { 46a2db793fSDouglas Gregor /// \brief Stored representation of a completion string. 47a2db793fSDouglas Gregor /// 48a2db793fSDouglas Gregor /// This is the representation behind a CXCompletionString. 49a2db793fSDouglas Gregor class CXStoredCodeCompletionString : public CodeCompletionString { 50a2db793fSDouglas Gregor unsigned Priority; 51f757a12dSDouglas Gregor CXAvailabilityKind Availability; 52a2db793fSDouglas Gregor 53a2db793fSDouglas Gregor public: 54f757a12dSDouglas Gregor CXStoredCodeCompletionString(unsigned Priority, 55f757a12dSDouglas Gregor CXAvailabilityKind Availability) 56f757a12dSDouglas Gregor : Priority(Priority), Availability(Availability) { } 57a2db793fSDouglas Gregor 58a2db793fSDouglas Gregor unsigned getPriority() const { return Priority; } 59f757a12dSDouglas Gregor CXAvailabilityKind getAvailability() const { return Availability; } 60a2db793fSDouglas Gregor }; 61a2db793fSDouglas Gregor } 62a2db793fSDouglas Gregor 63d5a2a073SDaniel Dunbar extern "C" { 64d5a2a073SDaniel Dunbar 65d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 66d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 67d5a2a073SDaniel Dunbar unsigned chunk_number) { 68a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 69a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 70d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 71d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 72d5a2a073SDaniel Dunbar 73d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 104d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 105d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 106d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 107d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 108d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 109d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 110d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 111d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 112d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 113d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 114d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 115d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 116d5a2a073SDaniel Dunbar } 117d5a2a073SDaniel Dunbar 118d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 119d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 120d5a2a073SDaniel Dunbar } 121d5a2a073SDaniel Dunbar 122d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 123d5a2a073SDaniel Dunbar unsigned chunk_number) { 124a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 125a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 126d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 127d5a2a073SDaniel Dunbar return createCXString(0); 128d5a2a073SDaniel Dunbar 129d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 136d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 137d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 138d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 140d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 141d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 142d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 143d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 144d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 145d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 146d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 147d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 148d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 14909737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 150*8ed5b770SDouglas Gregor return createCXString((*CCStr)[chunk_number].Text, false); 15109737ee8SDouglas Gregor 152d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 153d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 154d5a2a073SDaniel Dunbar return createCXString(""); 155d5a2a073SDaniel Dunbar } 156d5a2a073SDaniel Dunbar 157d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 158d5a2a073SDaniel Dunbar return createCXString(0); 159d5a2a073SDaniel Dunbar } 160d5a2a073SDaniel Dunbar 161d5a2a073SDaniel Dunbar 162d5a2a073SDaniel Dunbar CXCompletionString 163d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 164d5a2a073SDaniel Dunbar unsigned chunk_number) { 165a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 166a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 167d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 168d5a2a073SDaniel Dunbar return 0; 169d5a2a073SDaniel Dunbar 170d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 171d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 172d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 173d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 177d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 178d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 180d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 181d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 182d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 183d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 184d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 185d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 186d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 187d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 188d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 189d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 190d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 191d5a2a073SDaniel Dunbar return 0; 192d5a2a073SDaniel Dunbar 193d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 194d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 195d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 196d5a2a073SDaniel Dunbar } 197d5a2a073SDaniel Dunbar 198d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 199d5a2a073SDaniel Dunbar return 0; 200d5a2a073SDaniel Dunbar } 201d5a2a073SDaniel Dunbar 202d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 203a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 204a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 205d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 206d5a2a073SDaniel Dunbar } 207d5a2a073SDaniel Dunbar 208a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 209a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 210a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 21147bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 212a2db793fSDouglas Gregor } 213a2db793fSDouglas Gregor 214f757a12dSDouglas Gregor enum CXAvailabilityKind 215f757a12dSDouglas Gregor clang_getCompletionAvailability(CXCompletionString completion_string) { 216f757a12dSDouglas Gregor CXStoredCodeCompletionString *CCStr 217f757a12dSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 218f757a12dSDouglas Gregor return CCStr? CCStr->getAvailability() : CXAvailability_Available; 219f757a12dSDouglas Gregor } 220f757a12dSDouglas Gregor 221d5a2a073SDaniel Dunbar static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, 222d5a2a073SDaniel Dunbar unsigned &Value) { 223d5a2a073SDaniel Dunbar if (Memory + sizeof(unsigned) > MemoryEnd) 224d5a2a073SDaniel Dunbar return true; 225d5a2a073SDaniel Dunbar 226d5a2a073SDaniel Dunbar memmove(&Value, Memory, sizeof(unsigned)); 227d5a2a073SDaniel Dunbar Memory += sizeof(unsigned); 228d5a2a073SDaniel Dunbar return false; 229d5a2a073SDaniel Dunbar } 230d5a2a073SDaniel Dunbar 231d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 232d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 233d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 234d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults(); 235d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 236d5a2a073SDaniel Dunbar 237d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 238d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 239d5a2a073SDaniel Dunbar 240d5a2a073SDaniel Dunbar /// \brief Diag object 2418e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 242d5a2a073SDaniel Dunbar 243d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 244d5a2a073SDaniel Dunbar LangOptions LangOpts; 245d5a2a073SDaniel Dunbar 246d5a2a073SDaniel Dunbar /// \brief Source manager, used for diagnostics. 247d5a2a073SDaniel Dunbar SourceManager SourceMgr; 248d5a2a073SDaniel Dunbar 249d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 250d5a2a073SDaniel Dunbar FileManager FileMgr; 251d5a2a073SDaniel Dunbar 252d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 253d5a2a073SDaniel Dunbar /// with the code-completion results. 254d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 255d8a5dba9SDouglas Gregor 256d8a5dba9SDouglas Gregor /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. 257d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 258d5a2a073SDaniel Dunbar }; 259d5a2a073SDaniel Dunbar 260d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 261d8a5dba9SDouglas Gregor : CXCodeCompleteResults(), Diag(new Diagnostic), SourceMgr(*Diag) { } 262d5a2a073SDaniel Dunbar 263d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 264d5a2a073SDaniel Dunbar for (unsigned I = 0, N = NumResults; I != N; ++I) 265a2db793fSDouglas Gregor delete (CXStoredCodeCompletionString *)Results[I].CompletionString; 266d5a2a073SDaniel Dunbar delete [] Results; 267d5a2a073SDaniel Dunbar 268d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 269d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 270d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 271d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 272d5a2a073SDaniel Dunbar } 273d5a2a073SDaniel Dunbar 274d5a2a073SDaniel Dunbar CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, 275d5a2a073SDaniel Dunbar const char *source_filename, 276d5a2a073SDaniel Dunbar int num_command_line_args, 27757879fafSDouglas Gregor const char * const *command_line_args, 278d5a2a073SDaniel Dunbar unsigned num_unsaved_files, 279d5a2a073SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 280d5a2a073SDaniel Dunbar const char *complete_filename, 281d5a2a073SDaniel Dunbar unsigned complete_line, 282d5a2a073SDaniel Dunbar unsigned complete_column) { 283d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 284d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 285d5a2a073SDaniel Dunbar const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 286d5a2a073SDaniel Dunbar #endif 287d5a2a073SDaniel Dunbar #endif 288d5a2a073SDaniel Dunbar 289d6009ffbSDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 290d6009ffbSDouglas Gregor 291028d3e4dSDouglas Gregor llvm::OwningPtr<llvm::NamedRegionTimer> CCTimer; 292028d3e4dSDouglas Gregor if (getenv("LIBCLANG_TIMING")) { 293028d3e4dSDouglas Gregor llvm::SmallString<128> TimerName; 294028d3e4dSDouglas Gregor llvm::raw_svector_ostream TimerNameOut(TimerName); 2958ef4c807SDouglas Gregor TimerNameOut << "Code completion (out-of-process) @ " << complete_filename 2968ef4c807SDouglas Gregor << ":" << complete_line << ":" << complete_column; 297028d3e4dSDouglas Gregor CCTimer.reset(new llvm::NamedRegionTimer(TimerNameOut.str())); 298028d3e4dSDouglas Gregor } 299028d3e4dSDouglas Gregor 300d5a2a073SDaniel Dunbar // The indexer, which is mainly used to determine where diagnostics go. 301d5a2a073SDaniel Dunbar CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 302d5a2a073SDaniel Dunbar 303d5a2a073SDaniel Dunbar // Configure the diagnostics. 304d5a2a073SDaniel Dunbar DiagnosticOptions DiagOpts; 305d5a2a073SDaniel Dunbar llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 306d5a2a073SDaniel Dunbar Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 307d5a2a073SDaniel Dunbar 308d5a2a073SDaniel Dunbar // The set of temporary files that we've built. 309d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 310d5a2a073SDaniel Dunbar 311d5a2a073SDaniel Dunbar // Build up the arguments for invoking 'clang'. 312d5a2a073SDaniel Dunbar std::vector<const char *> argv; 313d5a2a073SDaniel Dunbar 314d5a2a073SDaniel Dunbar // First add the complete path to the 'clang' executable. 315d5a2a073SDaniel Dunbar llvm::sys::Path ClangPath = CXXIdx->getClangPath(); 316d5a2a073SDaniel Dunbar argv.push_back(ClangPath.c_str()); 317d5a2a073SDaniel Dunbar 318df000da8SDaniel Dunbar // Always use Clang C++ support. 319df000da8SDaniel Dunbar argv.push_back("-ccc-clang-cxx"); 320df000da8SDaniel Dunbar 321d5a2a073SDaniel Dunbar // Add the '-fsyntax-only' argument so that we only perform a basic 322d5a2a073SDaniel Dunbar // syntax check of the code. 323d5a2a073SDaniel Dunbar argv.push_back("-fsyntax-only"); 324d5a2a073SDaniel Dunbar 325d5a2a073SDaniel Dunbar // Add the appropriate '-code-completion-at=file:line:column' argument 326d5a2a073SDaniel Dunbar // to perform code completion, with an "-Xclang" preceding it. 327d5a2a073SDaniel Dunbar std::string code_complete_at; 328d5a2a073SDaniel Dunbar code_complete_at += complete_filename; 329d5a2a073SDaniel Dunbar code_complete_at += ":"; 330d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_line); 331d5a2a073SDaniel Dunbar code_complete_at += ":"; 332d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_column); 333d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 334d5a2a073SDaniel Dunbar argv.push_back("-code-completion-at"); 335d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 336d5a2a073SDaniel Dunbar argv.push_back(code_complete_at.c_str()); 337d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 338d5a2a073SDaniel Dunbar argv.push_back("-no-code-completion-debug-printer"); 339d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 340d5a2a073SDaniel Dunbar argv.push_back("-code-completion-macros"); 341d5a2a073SDaniel Dunbar argv.push_back("-fdiagnostics-binary"); 342d5a2a073SDaniel Dunbar 343d5a2a073SDaniel Dunbar // Remap any unsaved files to temporary files. 344d5a2a073SDaniel Dunbar std::vector<std::string> RemapArgs; 345d5a2a073SDaniel Dunbar if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) 346d5a2a073SDaniel Dunbar return 0; 347d5a2a073SDaniel Dunbar 348d5a2a073SDaniel Dunbar // The pointers into the elements of RemapArgs are stable because we 349d5a2a073SDaniel Dunbar // won't be adding anything to RemapArgs after this point. 350d5a2a073SDaniel Dunbar for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) 351d5a2a073SDaniel Dunbar argv.push_back(RemapArgs[i].c_str()); 352d5a2a073SDaniel Dunbar 353d5a2a073SDaniel Dunbar // Add the source file name (FIXME: later, we'll want to build temporary 354d5a2a073SDaniel Dunbar // file from the buffer, or just feed the source text via standard input). 355d5a2a073SDaniel Dunbar if (source_filename) 356d5a2a073SDaniel Dunbar argv.push_back(source_filename); 357d5a2a073SDaniel Dunbar 358d5a2a073SDaniel Dunbar // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. 359d5a2a073SDaniel Dunbar for (int i = 0; i < num_command_line_args; ++i) 360d5a2a073SDaniel Dunbar if (const char *arg = command_line_args[i]) { 361d5a2a073SDaniel Dunbar if (strcmp(arg, "-o") == 0) { 362d5a2a073SDaniel Dunbar ++i; // Also skip the matching argument. 363d5a2a073SDaniel Dunbar continue; 364d5a2a073SDaniel Dunbar } 365d5a2a073SDaniel Dunbar if (strcmp(arg, "-emit-ast") == 0 || 366d5a2a073SDaniel Dunbar strcmp(arg, "-c") == 0 || 367d5a2a073SDaniel Dunbar strcmp(arg, "-fsyntax-only") == 0) { 368d5a2a073SDaniel Dunbar continue; 369d5a2a073SDaniel Dunbar } 370d5a2a073SDaniel Dunbar 371d5a2a073SDaniel Dunbar // Keep the argument. 372d5a2a073SDaniel Dunbar argv.push_back(arg); 373d5a2a073SDaniel Dunbar } 374d5a2a073SDaniel Dunbar 375d6009ffbSDouglas Gregor if (EnableLogging) { 376d6009ffbSDouglas Gregor std::string Log = ClangPath.str(); 377d6009ffbSDouglas Gregor for (unsigned I = 0, N = argv.size(); I != N; ++I) { 378d6009ffbSDouglas Gregor Log += ' '; 379d6009ffbSDouglas Gregor Log += argv[I]; 380d6009ffbSDouglas Gregor } 381d6009ffbSDouglas Gregor fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str()); 382d6009ffbSDouglas Gregor } 383d6009ffbSDouglas Gregor 384d5a2a073SDaniel Dunbar // Add the null terminator. 385d5a2a073SDaniel Dunbar argv.push_back(NULL); 386d5a2a073SDaniel Dunbar 387d5a2a073SDaniel Dunbar // Generate a temporary name for the code-completion results file. 388d5a2a073SDaniel Dunbar char tmpFile[L_tmpnam]; 389d5a2a073SDaniel Dunbar char *tmpFileName = tmpnam(tmpFile); 390d5a2a073SDaniel Dunbar llvm::sys::Path ResultsFile(tmpFileName); 391d5a2a073SDaniel Dunbar TemporaryFiles.push_back(ResultsFile); 392d5a2a073SDaniel Dunbar 393d5a2a073SDaniel Dunbar // Generate a temporary name for the diagnostics file. 394d5a2a073SDaniel Dunbar char tmpFileResults[L_tmpnam]; 395d5a2a073SDaniel Dunbar char *tmpResultsFileName = tmpnam(tmpFileResults); 396d5a2a073SDaniel Dunbar llvm::sys::Path DiagnosticsFile(tmpResultsFileName); 397d5a2a073SDaniel Dunbar TemporaryFiles.push_back(DiagnosticsFile); 398d5a2a073SDaniel Dunbar 399d6009ffbSDouglas Gregor 400d6009ffbSDouglas Gregor 401d5a2a073SDaniel Dunbar // Invoke 'clang'. 402d5a2a073SDaniel Dunbar llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null 403d5a2a073SDaniel Dunbar // on Unix or NUL (Windows). 404d5a2a073SDaniel Dunbar std::string ErrMsg; 405d5a2a073SDaniel Dunbar const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, 406d5a2a073SDaniel Dunbar &DiagnosticsFile, 0 }; 407d5a2a073SDaniel Dunbar llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, 408d5a2a073SDaniel Dunbar /* redirects */ &Redirects[0], 409d5a2a073SDaniel Dunbar /* secondsToWait */ 0, 410d5a2a073SDaniel Dunbar /* memoryLimits */ 0, &ErrMsg); 411d5a2a073SDaniel Dunbar 412d5a2a073SDaniel Dunbar if (!ErrMsg.empty()) { 413d5a2a073SDaniel Dunbar std::string AllArgs; 414d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 415d5a2a073SDaniel Dunbar I != E; ++I) { 416d5a2a073SDaniel Dunbar AllArgs += ' '; 417d5a2a073SDaniel Dunbar if (*I) 418d5a2a073SDaniel Dunbar AllArgs += *I; 419d5a2a073SDaniel Dunbar } 420d5a2a073SDaniel Dunbar 421d5a2a073SDaniel Dunbar Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; 422d5a2a073SDaniel Dunbar } 423d5a2a073SDaniel Dunbar 424d5a2a073SDaniel Dunbar // Parse the resulting source file to find code-completion results. 425d5a2a073SDaniel Dunbar using llvm::MemoryBuffer; 426d5a2a073SDaniel Dunbar using llvm::StringRef; 427d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 428d5a2a073SDaniel Dunbar Results->Results = 0; 429d5a2a073SDaniel Dunbar Results->NumResults = 0; 430d5a2a073SDaniel Dunbar // FIXME: Set Results->LangOpts! 431d5a2a073SDaniel Dunbar if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { 432d5a2a073SDaniel Dunbar llvm::SmallVector<CXCompletionResult, 4> CompletionResults; 433d5a2a073SDaniel Dunbar StringRef Buffer = F->getBuffer(); 434d5a2a073SDaniel Dunbar for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); 435d5a2a073SDaniel Dunbar Str < StrEnd;) { 436d5a2a073SDaniel Dunbar unsigned KindValue; 437d5a2a073SDaniel Dunbar if (ReadUnsigned(Str, StrEnd, KindValue)) 438d5a2a073SDaniel Dunbar break; 439d5a2a073SDaniel Dunbar 440a2db793fSDouglas Gregor unsigned Priority; 441a2db793fSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Priority)) 442a2db793fSDouglas Gregor break; 443a2db793fSDouglas Gregor 444f757a12dSDouglas Gregor unsigned Availability; 445f757a12dSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Availability)) 446f757a12dSDouglas Gregor break; 447f757a12dSDouglas Gregor 448a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 449f757a12dSDouglas Gregor = new CXStoredCodeCompletionString(Priority, 450f757a12dSDouglas Gregor (CXAvailabilityKind)Availability); 451a2db793fSDouglas Gregor if (!CCStr->Deserialize(Str, StrEnd)) { 452a2db793fSDouglas Gregor delete CCStr; 453d5a2a073SDaniel Dunbar continue; 454a2db793fSDouglas Gregor } 455d5a2a073SDaniel Dunbar 456d5a2a073SDaniel Dunbar if (!CCStr->empty()) { 457d5a2a073SDaniel Dunbar // Vend the code-completion result to the caller. 458d5a2a073SDaniel Dunbar CXCompletionResult Result; 459d5a2a073SDaniel Dunbar Result.CursorKind = (CXCursorKind)KindValue; 460d5a2a073SDaniel Dunbar Result.CompletionString = CCStr; 461d5a2a073SDaniel Dunbar CompletionResults.push_back(Result); 462d5a2a073SDaniel Dunbar } 463d5a2a073SDaniel Dunbar }; 464d5a2a073SDaniel Dunbar 465d5a2a073SDaniel Dunbar // Allocate the results. 466d5a2a073SDaniel Dunbar Results->Results = new CXCompletionResult [CompletionResults.size()]; 467d5a2a073SDaniel Dunbar Results->NumResults = CompletionResults.size(); 468d5a2a073SDaniel Dunbar memcpy(Results->Results, CompletionResults.data(), 469d5a2a073SDaniel Dunbar CompletionResults.size() * sizeof(CXCompletionResult)); 470d8a5dba9SDouglas Gregor Results->TemporaryBuffers.push_back(F); 471d5a2a073SDaniel Dunbar } 472d5a2a073SDaniel Dunbar 473d5a2a073SDaniel Dunbar LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, 474d5a2a073SDaniel Dunbar Results->FileMgr, Results->SourceMgr, 475d5a2a073SDaniel Dunbar Results->Diagnostics); 476d5a2a073SDaniel Dunbar 477d5a2a073SDaniel Dunbar // Make sure we delete temporary files when the code-completion results are 478d5a2a073SDaniel Dunbar // destroyed. 479d5a2a073SDaniel Dunbar Results->TemporaryFiles.swap(TemporaryFiles); 480d5a2a073SDaniel Dunbar 481d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 482d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 483d5a2a073SDaniel Dunbar const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 484d5a2a073SDaniel Dunbar llvm::SmallString<256> LogResult; 485d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(LogResult); 486d5a2a073SDaniel Dunbar 487d5a2a073SDaniel Dunbar // Figure out the language and whether or not it uses PCH. 488d5a2a073SDaniel Dunbar const char *lang = 0; 489d5a2a073SDaniel Dunbar bool usesPCH = false; 490d5a2a073SDaniel Dunbar 491d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 492d5a2a073SDaniel Dunbar I != E; ++I) { 493d5a2a073SDaniel Dunbar if (*I == 0) 494d5a2a073SDaniel Dunbar continue; 495d5a2a073SDaniel Dunbar if (strcmp(*I, "-x") == 0) { 496d5a2a073SDaniel Dunbar if (I + 1 != E) { 497d5a2a073SDaniel Dunbar lang = *(++I); 498d5a2a073SDaniel Dunbar continue; 499d5a2a073SDaniel Dunbar } 500d5a2a073SDaniel Dunbar } 501d5a2a073SDaniel Dunbar else if (strcmp(*I, "-include") == 0) { 502d5a2a073SDaniel Dunbar if (I+1 != E) { 503d5a2a073SDaniel Dunbar const char *arg = *(++I); 504d5a2a073SDaniel Dunbar llvm::SmallString<512> pchName; 505d5a2a073SDaniel Dunbar { 506d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(pchName); 507d5a2a073SDaniel Dunbar os << arg << ".pth"; 508d5a2a073SDaniel Dunbar } 509d5a2a073SDaniel Dunbar pchName.push_back('\0'); 510d5a2a073SDaniel Dunbar struct stat stat_results; 511d5a2a073SDaniel Dunbar if (stat(pchName.data(), &stat_results) == 0) 512d5a2a073SDaniel Dunbar usesPCH = true; 513d5a2a073SDaniel Dunbar continue; 514d5a2a073SDaniel Dunbar } 515d5a2a073SDaniel Dunbar } 516d5a2a073SDaniel Dunbar } 517d5a2a073SDaniel Dunbar 518d5a2a073SDaniel Dunbar os << "{ "; 519d5a2a073SDaniel Dunbar os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 520d5a2a073SDaniel Dunbar os << ", \"numRes\": " << Results->NumResults; 521d5a2a073SDaniel Dunbar os << ", \"diags\": " << Results->Diagnostics.size(); 522d5a2a073SDaniel Dunbar os << ", \"pch\": " << (usesPCH ? "true" : "false"); 523d5a2a073SDaniel Dunbar os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 524d5a2a073SDaniel Dunbar const char *name = getlogin(); 525d5a2a073SDaniel Dunbar os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 526d5a2a073SDaniel Dunbar os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 527d5a2a073SDaniel Dunbar os << " }"; 528d5a2a073SDaniel Dunbar 529d5a2a073SDaniel Dunbar llvm::StringRef res = os.str(); 530d5a2a073SDaniel Dunbar if (res.size() > 0) { 531d5a2a073SDaniel Dunbar do { 532d5a2a073SDaniel Dunbar // Setup the UDP socket. 533d5a2a073SDaniel Dunbar struct sockaddr_in servaddr; 534d5a2a073SDaniel Dunbar bzero(&servaddr, sizeof(servaddr)); 535d5a2a073SDaniel Dunbar servaddr.sin_family = AF_INET; 536d5a2a073SDaniel Dunbar servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 537d5a2a073SDaniel Dunbar if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 538d5a2a073SDaniel Dunbar &servaddr.sin_addr) <= 0) 539d5a2a073SDaniel Dunbar break; 540d5a2a073SDaniel Dunbar 541d5a2a073SDaniel Dunbar int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 542d5a2a073SDaniel Dunbar if (sockfd < 0) 543d5a2a073SDaniel Dunbar break; 544d5a2a073SDaniel Dunbar 545d5a2a073SDaniel Dunbar sendto(sockfd, res.data(), res.size(), 0, 546d5a2a073SDaniel Dunbar (struct sockaddr *)&servaddr, sizeof(servaddr)); 547d5a2a073SDaniel Dunbar close(sockfd); 548d5a2a073SDaniel Dunbar } 549d5a2a073SDaniel Dunbar while (false); 550d5a2a073SDaniel Dunbar } 551d5a2a073SDaniel Dunbar #endif 552d5a2a073SDaniel Dunbar #endif 5539704c75bSDouglas Gregor clang_sortCodeCompletionResults(Results->Results, Results->NumResults); 554d5a2a073SDaniel Dunbar return Results; 555d5a2a073SDaniel Dunbar } 556d5a2a073SDaniel Dunbar 5578e984da8SDouglas Gregor } // end extern "C" 5588e984da8SDouglas Gregor 5598e984da8SDouglas Gregor namespace { 5608e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 5618e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 5628e984da8SDouglas Gregor 5638e984da8SDouglas Gregor public: 5648e984da8SDouglas Gregor explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 56539982197SDouglas Gregor : CodeCompleteConsumer(true, false, true, false), 56639982197SDouglas Gregor AllocatedResults(Results) { } 5678e984da8SDouglas Gregor 56800c37ef0SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, 56900c37ef0SDouglas Gregor CodeCompletionContext Context, 570276321a9SJohn McCall CodeCompletionResult *Results, 5718e984da8SDouglas Gregor unsigned NumResults) { 5728e984da8SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [NumResults]; 5738e984da8SDouglas Gregor AllocatedResults.NumResults = NumResults; 5748e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 5758e984da8SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 576f757a12dSDouglas Gregor = new CXStoredCodeCompletionString(Results[I].Priority, 577f757a12dSDouglas Gregor Results[I].Availability); 5788e984da8SDouglas Gregor (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); 579b14904c4SDouglas Gregor AllocatedResults.Results[I].CursorKind = Results[I].CursorKind; 5808e984da8SDouglas Gregor AllocatedResults.Results[I].CompletionString = StoredCompletion; 5818e984da8SDouglas Gregor } 5828e984da8SDouglas Gregor } 58349f67ce4SDouglas Gregor 58449f67ce4SDouglas Gregor // FIXME: Add ProcessOverloadCandidates? 5858e984da8SDouglas Gregor }; 5868e984da8SDouglas Gregor } 5878e984da8SDouglas Gregor 5888e984da8SDouglas Gregor extern "C" { 58977af1c56SDaniel Dunbar struct CodeCompleteAtInfo { 59077af1c56SDaniel Dunbar CXTranslationUnit TU; 59177af1c56SDaniel Dunbar const char *complete_filename; 59277af1c56SDaniel Dunbar unsigned complete_line; 59377af1c56SDaniel Dunbar unsigned complete_column; 59477af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files; 59577af1c56SDaniel Dunbar unsigned num_unsaved_files; 59677af1c56SDaniel Dunbar unsigned options; 59777af1c56SDaniel Dunbar CXCodeCompleteResults *result; 59877af1c56SDaniel Dunbar }; 59977af1c56SDaniel Dunbar void clang_codeCompleteAt_Impl(void *UserData) { 60077af1c56SDaniel Dunbar CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 60177af1c56SDaniel Dunbar CXTranslationUnit TU = CCAI->TU; 60277af1c56SDaniel Dunbar const char *complete_filename = CCAI->complete_filename; 60377af1c56SDaniel Dunbar unsigned complete_line = CCAI->complete_line; 60477af1c56SDaniel Dunbar unsigned complete_column = CCAI->complete_column; 60577af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 60677af1c56SDaniel Dunbar unsigned num_unsaved_files = CCAI->num_unsaved_files; 60777af1c56SDaniel Dunbar unsigned options = CCAI->options; 60877af1c56SDaniel Dunbar CCAI->result = 0; 60977af1c56SDaniel Dunbar 6108e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6118e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6128e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 6138e984da8SDouglas Gregor #endif 6148e984da8SDouglas Gregor #endif 6158e984da8SDouglas Gregor 6168e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 6178e984da8SDouglas Gregor 6188e984da8SDouglas Gregor ASTUnit *AST = static_cast<ASTUnit *>(TU); 6198e984da8SDouglas Gregor if (!AST) 62077af1c56SDaniel Dunbar return; 6218e984da8SDouglas Gregor 622ca5b0539SDouglas Gregor ASTUnit::ConcurrencyCheck Check(*AST); 623ca5b0539SDouglas Gregor 6248e984da8SDouglas Gregor // Perform the remapping of source files. 6258e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 6268e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 6278e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 6288e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 6298e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 6308e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 6318e984da8SDouglas Gregor Buffer)); 6328e984da8SDouglas Gregor } 6338e984da8SDouglas Gregor 6348e984da8SDouglas Gregor if (EnableLogging) { 6358e984da8SDouglas Gregor // FIXME: Add logging. 6368e984da8SDouglas Gregor } 6378e984da8SDouglas Gregor 6388e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 6398e984da8SDouglas Gregor AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 6408e984da8SDouglas Gregor Results->Results = 0; 6418e984da8SDouglas Gregor Results->NumResults = 0; 6428e984da8SDouglas Gregor 6438e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 6448e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 6458e984da8SDouglas Gregor 6468e984da8SDouglas Gregor // Perform completion. 6478e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 648b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 649b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 650b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 651b68bc59aSDouglas Gregor Capture, 6528e984da8SDouglas Gregor *Results->Diag, Results->LangOpts, Results->SourceMgr, 653b97b666cSDouglas Gregor Results->FileMgr, Results->Diagnostics, 654b97b666cSDouglas Gregor Results->TemporaryBuffers); 6558e984da8SDouglas Gregor 6568e984da8SDouglas Gregor 6578e984da8SDouglas Gregor 6588e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6598e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6608e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 6618e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 6628e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 6638e984da8SDouglas Gregor 6648e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 6658e984da8SDouglas Gregor const char *lang = 0; 6668e984da8SDouglas Gregor bool usesPCH = false; 6678e984da8SDouglas Gregor 6688e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 6698e984da8SDouglas Gregor I != E; ++I) { 6708e984da8SDouglas Gregor if (*I == 0) 6718e984da8SDouglas Gregor continue; 6728e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 6738e984da8SDouglas Gregor if (I + 1 != E) { 6748e984da8SDouglas Gregor lang = *(++I); 6758e984da8SDouglas Gregor continue; 6768e984da8SDouglas Gregor } 6778e984da8SDouglas Gregor } 6788e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 6798e984da8SDouglas Gregor if (I+1 != E) { 6808e984da8SDouglas Gregor const char *arg = *(++I); 6818e984da8SDouglas Gregor llvm::SmallString<512> pchName; 6828e984da8SDouglas Gregor { 6838e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 6848e984da8SDouglas Gregor os << arg << ".pth"; 6858e984da8SDouglas Gregor } 6868e984da8SDouglas Gregor pchName.push_back('\0'); 6878e984da8SDouglas Gregor struct stat stat_results; 6888e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 6898e984da8SDouglas Gregor usesPCH = true; 6908e984da8SDouglas Gregor continue; 6918e984da8SDouglas Gregor } 6928e984da8SDouglas Gregor } 6938e984da8SDouglas Gregor } 6948e984da8SDouglas Gregor 6958e984da8SDouglas Gregor os << "{ "; 6968e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 6978e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 6988e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 6998e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 7008e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 7018e984da8SDouglas Gregor const char *name = getlogin(); 7028e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 7038e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 7048e984da8SDouglas Gregor os << " }"; 7058e984da8SDouglas Gregor 7068e984da8SDouglas Gregor llvm::StringRef res = os.str(); 7078e984da8SDouglas Gregor if (res.size() > 0) { 7088e984da8SDouglas Gregor do { 7098e984da8SDouglas Gregor // Setup the UDP socket. 7108e984da8SDouglas Gregor struct sockaddr_in servaddr; 7118e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 7128e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 7138e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 7148e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 7158e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 7168e984da8SDouglas Gregor break; 7178e984da8SDouglas Gregor 7188e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 7198e984da8SDouglas Gregor if (sockfd < 0) 7208e984da8SDouglas Gregor break; 7218e984da8SDouglas Gregor 7228e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 7238e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 7248e984da8SDouglas Gregor close(sockfd); 7258e984da8SDouglas Gregor } 7268e984da8SDouglas Gregor while (false); 7278e984da8SDouglas Gregor } 7288e984da8SDouglas Gregor #endif 7298e984da8SDouglas Gregor #endif 73077af1c56SDaniel Dunbar CCAI->result = Results; 73177af1c56SDaniel Dunbar } 73277af1c56SDaniel Dunbar CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 73377af1c56SDaniel Dunbar const char *complete_filename, 73477af1c56SDaniel Dunbar unsigned complete_line, 73577af1c56SDaniel Dunbar unsigned complete_column, 73677af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 73777af1c56SDaniel Dunbar unsigned num_unsaved_files, 73877af1c56SDaniel Dunbar unsigned options) { 73977af1c56SDaniel Dunbar CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 74077af1c56SDaniel Dunbar complete_column, unsaved_files, num_unsaved_files, 74177af1c56SDaniel Dunbar options, 0 }; 74277af1c56SDaniel Dunbar llvm::CrashRecoveryContext CRC; 74377af1c56SDaniel Dunbar 74477af1c56SDaniel Dunbar if (!CRC.RunSafely(clang_codeCompleteAt_Impl, &CCAI)) { 74577af1c56SDaniel Dunbar fprintf(stderr, "libclang: crash detected in code completion\n"); 74677af1c56SDaniel Dunbar static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); 74777af1c56SDaniel Dunbar return 0; 74877af1c56SDaniel Dunbar } 74977af1c56SDaniel Dunbar 75077af1c56SDaniel Dunbar return CCAI.result; 7518e984da8SDouglas Gregor } 7528e984da8SDouglas Gregor 753b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 754b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 755b68bc59aSDouglas Gregor } 756b68bc59aSDouglas Gregor 757d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 758d5a2a073SDaniel Dunbar if (!ResultsIn) 759d5a2a073SDaniel Dunbar return; 760d5a2a073SDaniel Dunbar 761d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 762d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 763d5a2a073SDaniel Dunbar delete Results; 764d5a2a073SDaniel Dunbar } 765d5a2a073SDaniel Dunbar 766d5a2a073SDaniel Dunbar unsigned 767d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 768d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 769d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 770d5a2a073SDaniel Dunbar if (!Results) 771d5a2a073SDaniel Dunbar return 0; 772d5a2a073SDaniel Dunbar 773d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 774d5a2a073SDaniel Dunbar } 775d5a2a073SDaniel Dunbar 776d5a2a073SDaniel Dunbar CXDiagnostic 777d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 778d5a2a073SDaniel Dunbar unsigned Index) { 779d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 780d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 781d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 782d5a2a073SDaniel Dunbar return 0; 783d5a2a073SDaniel Dunbar 784d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 785d5a2a073SDaniel Dunbar } 786d5a2a073SDaniel Dunbar 787d5a2a073SDaniel Dunbar 788d5a2a073SDaniel Dunbar } // end extern "C" 78949f67ce4SDouglas Gregor 79049f67ce4SDouglas Gregor namespace { 79149f67ce4SDouglas Gregor struct OrderCompletionResults { 79249f67ce4SDouglas Gregor bool operator()(const CXCompletionResult &XR, 79349f67ce4SDouglas Gregor const CXCompletionResult &YR) const { 79449f67ce4SDouglas Gregor CXStoredCodeCompletionString *X 79549f67ce4SDouglas Gregor = (CXStoredCodeCompletionString *)XR.CompletionString; 79649f67ce4SDouglas Gregor CXStoredCodeCompletionString *Y 79749f67ce4SDouglas Gregor = (CXStoredCodeCompletionString *)YR.CompletionString; 79849f67ce4SDouglas Gregor 79949f67ce4SDouglas Gregor const char *XText = X->getTypedText(); 80049f67ce4SDouglas Gregor const char *YText = Y->getTypedText(); 80149f67ce4SDouglas Gregor if (!XText || !YText) 80249f67ce4SDouglas Gregor return XText != 0; 80349f67ce4SDouglas Gregor 80449f67ce4SDouglas Gregor int result = llvm::StringRef(XText).compare_lower(YText); 80549f67ce4SDouglas Gregor if (result < 0) 80649f67ce4SDouglas Gregor return true; 80749f67ce4SDouglas Gregor if (result > 0) 80849f67ce4SDouglas Gregor return false; 80949f67ce4SDouglas Gregor 81049f67ce4SDouglas Gregor result = llvm::StringRef(XText).compare(YText); 811c7a7d92aSDouglas Gregor return result < 0; 81249f67ce4SDouglas Gregor } 81349f67ce4SDouglas Gregor }; 81449f67ce4SDouglas Gregor } 81549f67ce4SDouglas Gregor 81649f67ce4SDouglas Gregor extern "C" { 81749f67ce4SDouglas Gregor void clang_sortCodeCompletionResults(CXCompletionResult *Results, 81849f67ce4SDouglas Gregor unsigned NumResults) { 81949f67ce4SDouglas Gregor std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); 82049f67ce4SDouglas Gregor } 82149f67ce4SDouglas Gregor } 822