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" 19*8e984da8SDouglas 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" 23d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 24d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 25d5a2a073SDaniel Dunbar #include "llvm/System/Program.h" 26d6009ffbSDouglas Gregor #include <cstdlib> 27d6009ffbSDouglas Gregor #include <cstdio> 28d5a2a073SDaniel Dunbar 29d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 30d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 31d5a2a073SDaniel Dunbar #include "llvm/ADT/SmallString.h" 32d5a2a073SDaniel Dunbar #include "llvm/Support/Timer.h" 33d5a2a073SDaniel Dunbar #include "llvm/Support/raw_ostream.h" 34d5a2a073SDaniel Dunbar #include <arpa/inet.h> 35d5a2a073SDaniel Dunbar #include <sys/socket.h> 36d5a2a073SDaniel Dunbar #include <sys/types.h> 37d5a2a073SDaniel Dunbar #include <unistd.h> 38d5a2a073SDaniel Dunbar #endif 39d5a2a073SDaniel Dunbar 40d5a2a073SDaniel Dunbar using namespace clang; 41d5a2a073SDaniel Dunbar using namespace clang::cxstring; 42d5a2a073SDaniel Dunbar 43a2db793fSDouglas Gregor namespace { 44a2db793fSDouglas Gregor /// \brief Stored representation of a completion string. 45a2db793fSDouglas Gregor /// 46a2db793fSDouglas Gregor /// This is the representation behind a CXCompletionString. 47a2db793fSDouglas Gregor class CXStoredCodeCompletionString : public CodeCompletionString { 48a2db793fSDouglas Gregor unsigned Priority; 49a2db793fSDouglas Gregor 50a2db793fSDouglas Gregor public: 51a2db793fSDouglas Gregor CXStoredCodeCompletionString(unsigned Priority) : Priority(Priority) { } 52a2db793fSDouglas Gregor 53a2db793fSDouglas Gregor unsigned getPriority() const { return Priority; } 54a2db793fSDouglas Gregor }; 55a2db793fSDouglas Gregor } 56a2db793fSDouglas Gregor 57d5a2a073SDaniel Dunbar extern "C" { 58d5a2a073SDaniel Dunbar 59d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 60d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 61d5a2a073SDaniel Dunbar unsigned chunk_number) { 62a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 63a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 64d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 65d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 66d5a2a073SDaniel Dunbar 67d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 68d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 69d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 70d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 71d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 72d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 73d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 104d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 105d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 106d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 107d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 108d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 109d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 110d5a2a073SDaniel Dunbar } 111d5a2a073SDaniel Dunbar 112d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 113d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 114d5a2a073SDaniel Dunbar } 115d5a2a073SDaniel Dunbar 116d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 117d5a2a073SDaniel Dunbar unsigned chunk_number) { 118a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 119a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 120d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 121d5a2a073SDaniel Dunbar return createCXString(0); 122d5a2a073SDaniel Dunbar 123d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 124d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 125d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 126d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 127d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 128d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 129d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 136d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 137d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 138d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 140d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 141d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 142d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 143d5a2a073SDaniel Dunbar return createCXString((*CCStr)[chunk_number].Text, false); 144d5a2a073SDaniel Dunbar 14509737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 14609737ee8SDouglas Gregor // FIXME: Temporary hack until we figure out how to handle vertical space. 14709737ee8SDouglas Gregor return createCXString(" "); 14809737ee8SDouglas Gregor 149d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 150d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 151d5a2a073SDaniel Dunbar return createCXString(""); 152d5a2a073SDaniel Dunbar } 153d5a2a073SDaniel Dunbar 154d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 155d5a2a073SDaniel Dunbar return createCXString(0); 156d5a2a073SDaniel Dunbar } 157d5a2a073SDaniel Dunbar 158d5a2a073SDaniel Dunbar 159d5a2a073SDaniel Dunbar CXCompletionString 160d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 161d5a2a073SDaniel Dunbar unsigned chunk_number) { 162a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 163a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 164d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 165d5a2a073SDaniel Dunbar return 0; 166d5a2a073SDaniel Dunbar 167d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 168d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 169d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 170d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 171d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 172d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 173d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 177d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 178d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 180d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 181d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 182d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 183d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 184d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 185d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 186d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 187d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 188d5a2a073SDaniel Dunbar return 0; 189d5a2a073SDaniel Dunbar 190d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 191d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 192d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 193d5a2a073SDaniel Dunbar } 194d5a2a073SDaniel Dunbar 195d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 196d5a2a073SDaniel Dunbar return 0; 197d5a2a073SDaniel Dunbar } 198d5a2a073SDaniel Dunbar 199d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 200a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 201a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 202d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 203d5a2a073SDaniel Dunbar } 204d5a2a073SDaniel Dunbar 205a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 206a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 207a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 20847bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 209a2db793fSDouglas Gregor } 210a2db793fSDouglas Gregor 211d5a2a073SDaniel Dunbar static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, 212d5a2a073SDaniel Dunbar unsigned &Value) { 213d5a2a073SDaniel Dunbar if (Memory + sizeof(unsigned) > MemoryEnd) 214d5a2a073SDaniel Dunbar return true; 215d5a2a073SDaniel Dunbar 216d5a2a073SDaniel Dunbar memmove(&Value, Memory, sizeof(unsigned)); 217d5a2a073SDaniel Dunbar Memory += sizeof(unsigned); 218d5a2a073SDaniel Dunbar return false; 219d5a2a073SDaniel Dunbar } 220d5a2a073SDaniel Dunbar 221d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 222d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 223d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 224d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults(); 225d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 226d5a2a073SDaniel Dunbar 227d5a2a073SDaniel Dunbar /// \brief The memory buffer from which we parsed the results. We 228d5a2a073SDaniel Dunbar /// retain this buffer because the completion strings point into it. 229d5a2a073SDaniel Dunbar llvm::MemoryBuffer *Buffer; 230d5a2a073SDaniel Dunbar 231d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 232d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 233d5a2a073SDaniel Dunbar 234d5a2a073SDaniel Dunbar /// \brief Diag object 235*8e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 236d5a2a073SDaniel Dunbar 237d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 238d5a2a073SDaniel Dunbar LangOptions LangOpts; 239d5a2a073SDaniel Dunbar 240d5a2a073SDaniel Dunbar /// \brief Source manager, used for diagnostics. 241d5a2a073SDaniel Dunbar SourceManager SourceMgr; 242d5a2a073SDaniel Dunbar 243d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 244d5a2a073SDaniel Dunbar FileManager FileMgr; 245d5a2a073SDaniel Dunbar 246d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 247d5a2a073SDaniel Dunbar /// with the code-completion results. 248d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 249d5a2a073SDaniel Dunbar }; 250d5a2a073SDaniel Dunbar 251d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 252*8e984da8SDouglas Gregor : CXCodeCompleteResults(), Buffer(0), Diag(new Diagnostic), 253*8e984da8SDouglas Gregor SourceMgr(*Diag) { } 254d5a2a073SDaniel Dunbar 255d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 256d5a2a073SDaniel Dunbar for (unsigned I = 0, N = NumResults; I != N; ++I) 257a2db793fSDouglas Gregor delete (CXStoredCodeCompletionString *)Results[I].CompletionString; 258d5a2a073SDaniel Dunbar delete [] Results; 259d5a2a073SDaniel Dunbar delete Buffer; 260d5a2a073SDaniel Dunbar 261d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 262d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 263d5a2a073SDaniel Dunbar } 264d5a2a073SDaniel Dunbar 265d5a2a073SDaniel Dunbar CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, 266d5a2a073SDaniel Dunbar const char *source_filename, 267d5a2a073SDaniel Dunbar int num_command_line_args, 268d5a2a073SDaniel Dunbar const char **command_line_args, 269d5a2a073SDaniel Dunbar unsigned num_unsaved_files, 270d5a2a073SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 271d5a2a073SDaniel Dunbar const char *complete_filename, 272d5a2a073SDaniel Dunbar unsigned complete_line, 273d5a2a073SDaniel Dunbar unsigned complete_column) { 274d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 275d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 276d5a2a073SDaniel Dunbar const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 277d5a2a073SDaniel Dunbar #endif 278d5a2a073SDaniel Dunbar #endif 279d5a2a073SDaniel Dunbar 280d6009ffbSDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 281d6009ffbSDouglas Gregor 282d5a2a073SDaniel Dunbar // The indexer, which is mainly used to determine where diagnostics go. 283d5a2a073SDaniel Dunbar CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 284d5a2a073SDaniel Dunbar 285d5a2a073SDaniel Dunbar // Configure the diagnostics. 286d5a2a073SDaniel Dunbar DiagnosticOptions DiagOpts; 287d5a2a073SDaniel Dunbar llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 288d5a2a073SDaniel Dunbar Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 289d5a2a073SDaniel Dunbar 290d5a2a073SDaniel Dunbar // The set of temporary files that we've built. 291d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 292d5a2a073SDaniel Dunbar 293d5a2a073SDaniel Dunbar // Build up the arguments for invoking 'clang'. 294d5a2a073SDaniel Dunbar std::vector<const char *> argv; 295d5a2a073SDaniel Dunbar 296d5a2a073SDaniel Dunbar // First add the complete path to the 'clang' executable. 297d5a2a073SDaniel Dunbar llvm::sys::Path ClangPath = CXXIdx->getClangPath(); 298d5a2a073SDaniel Dunbar argv.push_back(ClangPath.c_str()); 299d5a2a073SDaniel Dunbar 300df000da8SDaniel Dunbar // Always use Clang C++ support. 301df000da8SDaniel Dunbar argv.push_back("-ccc-clang-cxx"); 302df000da8SDaniel Dunbar 303d5a2a073SDaniel Dunbar // Add the '-fsyntax-only' argument so that we only perform a basic 304d5a2a073SDaniel Dunbar // syntax check of the code. 305d5a2a073SDaniel Dunbar argv.push_back("-fsyntax-only"); 306d5a2a073SDaniel Dunbar 307d5a2a073SDaniel Dunbar // Add the appropriate '-code-completion-at=file:line:column' argument 308d5a2a073SDaniel Dunbar // to perform code completion, with an "-Xclang" preceding it. 309d5a2a073SDaniel Dunbar std::string code_complete_at; 310d5a2a073SDaniel Dunbar code_complete_at += complete_filename; 311d5a2a073SDaniel Dunbar code_complete_at += ":"; 312d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_line); 313d5a2a073SDaniel Dunbar code_complete_at += ":"; 314d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_column); 315d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 316d5a2a073SDaniel Dunbar argv.push_back("-code-completion-at"); 317d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 318d5a2a073SDaniel Dunbar argv.push_back(code_complete_at.c_str()); 319d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 320d5a2a073SDaniel Dunbar argv.push_back("-no-code-completion-debug-printer"); 321d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 322d5a2a073SDaniel Dunbar argv.push_back("-code-completion-macros"); 323d5a2a073SDaniel Dunbar argv.push_back("-fdiagnostics-binary"); 324d5a2a073SDaniel Dunbar 325d5a2a073SDaniel Dunbar // Remap any unsaved files to temporary files. 326d5a2a073SDaniel Dunbar std::vector<std::string> RemapArgs; 327d5a2a073SDaniel Dunbar if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) 328d5a2a073SDaniel Dunbar return 0; 329d5a2a073SDaniel Dunbar 330d5a2a073SDaniel Dunbar // The pointers into the elements of RemapArgs are stable because we 331d5a2a073SDaniel Dunbar // won't be adding anything to RemapArgs after this point. 332d5a2a073SDaniel Dunbar for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) 333d5a2a073SDaniel Dunbar argv.push_back(RemapArgs[i].c_str()); 334d5a2a073SDaniel Dunbar 335d5a2a073SDaniel Dunbar // Add the source file name (FIXME: later, we'll want to build temporary 336d5a2a073SDaniel Dunbar // file from the buffer, or just feed the source text via standard input). 337d5a2a073SDaniel Dunbar if (source_filename) 338d5a2a073SDaniel Dunbar argv.push_back(source_filename); 339d5a2a073SDaniel Dunbar 340d5a2a073SDaniel Dunbar // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. 341d5a2a073SDaniel Dunbar for (int i = 0; i < num_command_line_args; ++i) 342d5a2a073SDaniel Dunbar if (const char *arg = command_line_args[i]) { 343d5a2a073SDaniel Dunbar if (strcmp(arg, "-o") == 0) { 344d5a2a073SDaniel Dunbar ++i; // Also skip the matching argument. 345d5a2a073SDaniel Dunbar continue; 346d5a2a073SDaniel Dunbar } 347d5a2a073SDaniel Dunbar if (strcmp(arg, "-emit-ast") == 0 || 348d5a2a073SDaniel Dunbar strcmp(arg, "-c") == 0 || 349d5a2a073SDaniel Dunbar strcmp(arg, "-fsyntax-only") == 0) { 350d5a2a073SDaniel Dunbar continue; 351d5a2a073SDaniel Dunbar } 352d5a2a073SDaniel Dunbar 353d5a2a073SDaniel Dunbar // Keep the argument. 354d5a2a073SDaniel Dunbar argv.push_back(arg); 355d5a2a073SDaniel Dunbar } 356d5a2a073SDaniel Dunbar 357d6009ffbSDouglas Gregor if (EnableLogging) { 358d6009ffbSDouglas Gregor std::string Log = ClangPath.str(); 359d6009ffbSDouglas Gregor for (unsigned I = 0, N = argv.size(); I != N; ++I) { 360d6009ffbSDouglas Gregor Log += ' '; 361d6009ffbSDouglas Gregor Log += argv[I]; 362d6009ffbSDouglas Gregor } 363d6009ffbSDouglas Gregor fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str()); 364d6009ffbSDouglas Gregor } 365d6009ffbSDouglas Gregor 366d5a2a073SDaniel Dunbar // Add the null terminator. 367d5a2a073SDaniel Dunbar argv.push_back(NULL); 368d5a2a073SDaniel Dunbar 369d5a2a073SDaniel Dunbar // Generate a temporary name for the code-completion results file. 370d5a2a073SDaniel Dunbar char tmpFile[L_tmpnam]; 371d5a2a073SDaniel Dunbar char *tmpFileName = tmpnam(tmpFile); 372d5a2a073SDaniel Dunbar llvm::sys::Path ResultsFile(tmpFileName); 373d5a2a073SDaniel Dunbar TemporaryFiles.push_back(ResultsFile); 374d5a2a073SDaniel Dunbar 375d5a2a073SDaniel Dunbar // Generate a temporary name for the diagnostics file. 376d5a2a073SDaniel Dunbar char tmpFileResults[L_tmpnam]; 377d5a2a073SDaniel Dunbar char *tmpResultsFileName = tmpnam(tmpFileResults); 378d5a2a073SDaniel Dunbar llvm::sys::Path DiagnosticsFile(tmpResultsFileName); 379d5a2a073SDaniel Dunbar TemporaryFiles.push_back(DiagnosticsFile); 380d5a2a073SDaniel Dunbar 381d6009ffbSDouglas Gregor 382d6009ffbSDouglas Gregor 383d5a2a073SDaniel Dunbar // Invoke 'clang'. 384d5a2a073SDaniel Dunbar llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null 385d5a2a073SDaniel Dunbar // on Unix or NUL (Windows). 386d5a2a073SDaniel Dunbar std::string ErrMsg; 387d5a2a073SDaniel Dunbar const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, 388d5a2a073SDaniel Dunbar &DiagnosticsFile, 0 }; 389d5a2a073SDaniel Dunbar llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, 390d5a2a073SDaniel Dunbar /* redirects */ &Redirects[0], 391d5a2a073SDaniel Dunbar /* secondsToWait */ 0, 392d5a2a073SDaniel Dunbar /* memoryLimits */ 0, &ErrMsg); 393d5a2a073SDaniel Dunbar 394d5a2a073SDaniel Dunbar if (!ErrMsg.empty()) { 395d5a2a073SDaniel Dunbar std::string AllArgs; 396d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 397d5a2a073SDaniel Dunbar I != E; ++I) { 398d5a2a073SDaniel Dunbar AllArgs += ' '; 399d5a2a073SDaniel Dunbar if (*I) 400d5a2a073SDaniel Dunbar AllArgs += *I; 401d5a2a073SDaniel Dunbar } 402d5a2a073SDaniel Dunbar 403d5a2a073SDaniel Dunbar Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; 404d5a2a073SDaniel Dunbar } 405d5a2a073SDaniel Dunbar 406d5a2a073SDaniel Dunbar // Parse the resulting source file to find code-completion results. 407d5a2a073SDaniel Dunbar using llvm::MemoryBuffer; 408d5a2a073SDaniel Dunbar using llvm::StringRef; 409d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 410d5a2a073SDaniel Dunbar Results->Results = 0; 411d5a2a073SDaniel Dunbar Results->NumResults = 0; 412d5a2a073SDaniel Dunbar Results->Buffer = 0; 413d5a2a073SDaniel Dunbar // FIXME: Set Results->LangOpts! 414d5a2a073SDaniel Dunbar if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { 415d5a2a073SDaniel Dunbar llvm::SmallVector<CXCompletionResult, 4> CompletionResults; 416d5a2a073SDaniel Dunbar StringRef Buffer = F->getBuffer(); 417d5a2a073SDaniel Dunbar for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); 418d5a2a073SDaniel Dunbar Str < StrEnd;) { 419d5a2a073SDaniel Dunbar unsigned KindValue; 420d5a2a073SDaniel Dunbar if (ReadUnsigned(Str, StrEnd, KindValue)) 421d5a2a073SDaniel Dunbar break; 422d5a2a073SDaniel Dunbar 423a2db793fSDouglas Gregor unsigned Priority; 424a2db793fSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Priority)) 425a2db793fSDouglas Gregor break; 426a2db793fSDouglas Gregor 427a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 428a2db793fSDouglas Gregor = new CXStoredCodeCompletionString(Priority); 429a2db793fSDouglas Gregor if (!CCStr->Deserialize(Str, StrEnd)) { 430a2db793fSDouglas Gregor delete CCStr; 431d5a2a073SDaniel Dunbar continue; 432a2db793fSDouglas Gregor } 433d5a2a073SDaniel Dunbar 434d5a2a073SDaniel Dunbar if (!CCStr->empty()) { 435d5a2a073SDaniel Dunbar // Vend the code-completion result to the caller. 436d5a2a073SDaniel Dunbar CXCompletionResult Result; 437d5a2a073SDaniel Dunbar Result.CursorKind = (CXCursorKind)KindValue; 438d5a2a073SDaniel Dunbar Result.CompletionString = CCStr; 439d5a2a073SDaniel Dunbar CompletionResults.push_back(Result); 440d5a2a073SDaniel Dunbar } 441d5a2a073SDaniel Dunbar }; 442d5a2a073SDaniel Dunbar 443d5a2a073SDaniel Dunbar // Allocate the results. 444d5a2a073SDaniel Dunbar Results->Results = new CXCompletionResult [CompletionResults.size()]; 445d5a2a073SDaniel Dunbar Results->NumResults = CompletionResults.size(); 446d5a2a073SDaniel Dunbar memcpy(Results->Results, CompletionResults.data(), 447d5a2a073SDaniel Dunbar CompletionResults.size() * sizeof(CXCompletionResult)); 448d5a2a073SDaniel Dunbar Results->Buffer = F; 449d5a2a073SDaniel Dunbar } 450d5a2a073SDaniel Dunbar 451d5a2a073SDaniel Dunbar LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, 452d5a2a073SDaniel Dunbar Results->FileMgr, Results->SourceMgr, 453d5a2a073SDaniel Dunbar Results->Diagnostics); 454d5a2a073SDaniel Dunbar 455d5a2a073SDaniel Dunbar // Make sure we delete temporary files when the code-completion results are 456d5a2a073SDaniel Dunbar // destroyed. 457d5a2a073SDaniel Dunbar Results->TemporaryFiles.swap(TemporaryFiles); 458d5a2a073SDaniel Dunbar 459d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 460d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 461d5a2a073SDaniel Dunbar const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 462d5a2a073SDaniel Dunbar llvm::SmallString<256> LogResult; 463d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(LogResult); 464d5a2a073SDaniel Dunbar 465d5a2a073SDaniel Dunbar // Figure out the language and whether or not it uses PCH. 466d5a2a073SDaniel Dunbar const char *lang = 0; 467d5a2a073SDaniel Dunbar bool usesPCH = false; 468d5a2a073SDaniel Dunbar 469d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 470d5a2a073SDaniel Dunbar I != E; ++I) { 471d5a2a073SDaniel Dunbar if (*I == 0) 472d5a2a073SDaniel Dunbar continue; 473d5a2a073SDaniel Dunbar if (strcmp(*I, "-x") == 0) { 474d5a2a073SDaniel Dunbar if (I + 1 != E) { 475d5a2a073SDaniel Dunbar lang = *(++I); 476d5a2a073SDaniel Dunbar continue; 477d5a2a073SDaniel Dunbar } 478d5a2a073SDaniel Dunbar } 479d5a2a073SDaniel Dunbar else if (strcmp(*I, "-include") == 0) { 480d5a2a073SDaniel Dunbar if (I+1 != E) { 481d5a2a073SDaniel Dunbar const char *arg = *(++I); 482d5a2a073SDaniel Dunbar llvm::SmallString<512> pchName; 483d5a2a073SDaniel Dunbar { 484d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(pchName); 485d5a2a073SDaniel Dunbar os << arg << ".pth"; 486d5a2a073SDaniel Dunbar } 487d5a2a073SDaniel Dunbar pchName.push_back('\0'); 488d5a2a073SDaniel Dunbar struct stat stat_results; 489d5a2a073SDaniel Dunbar if (stat(pchName.data(), &stat_results) == 0) 490d5a2a073SDaniel Dunbar usesPCH = true; 491d5a2a073SDaniel Dunbar continue; 492d5a2a073SDaniel Dunbar } 493d5a2a073SDaniel Dunbar } 494d5a2a073SDaniel Dunbar } 495d5a2a073SDaniel Dunbar 496d5a2a073SDaniel Dunbar os << "{ "; 497d5a2a073SDaniel Dunbar os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 498d5a2a073SDaniel Dunbar os << ", \"numRes\": " << Results->NumResults; 499d5a2a073SDaniel Dunbar os << ", \"diags\": " << Results->Diagnostics.size(); 500d5a2a073SDaniel Dunbar os << ", \"pch\": " << (usesPCH ? "true" : "false"); 501d5a2a073SDaniel Dunbar os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 502d5a2a073SDaniel Dunbar const char *name = getlogin(); 503d5a2a073SDaniel Dunbar os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 504d5a2a073SDaniel Dunbar os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 505d5a2a073SDaniel Dunbar os << " }"; 506d5a2a073SDaniel Dunbar 507d5a2a073SDaniel Dunbar llvm::StringRef res = os.str(); 508d5a2a073SDaniel Dunbar if (res.size() > 0) { 509d5a2a073SDaniel Dunbar do { 510d5a2a073SDaniel Dunbar // Setup the UDP socket. 511d5a2a073SDaniel Dunbar struct sockaddr_in servaddr; 512d5a2a073SDaniel Dunbar bzero(&servaddr, sizeof(servaddr)); 513d5a2a073SDaniel Dunbar servaddr.sin_family = AF_INET; 514d5a2a073SDaniel Dunbar servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 515d5a2a073SDaniel Dunbar if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 516d5a2a073SDaniel Dunbar &servaddr.sin_addr) <= 0) 517d5a2a073SDaniel Dunbar break; 518d5a2a073SDaniel Dunbar 519d5a2a073SDaniel Dunbar int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 520d5a2a073SDaniel Dunbar if (sockfd < 0) 521d5a2a073SDaniel Dunbar break; 522d5a2a073SDaniel Dunbar 523d5a2a073SDaniel Dunbar sendto(sockfd, res.data(), res.size(), 0, 524d5a2a073SDaniel Dunbar (struct sockaddr *)&servaddr, sizeof(servaddr)); 525d5a2a073SDaniel Dunbar close(sockfd); 526d5a2a073SDaniel Dunbar } 527d5a2a073SDaniel Dunbar while (false); 528d5a2a073SDaniel Dunbar } 529d5a2a073SDaniel Dunbar #endif 530d5a2a073SDaniel Dunbar #endif 531d5a2a073SDaniel Dunbar return Results; 532d5a2a073SDaniel Dunbar } 533d5a2a073SDaniel Dunbar 534*8e984da8SDouglas Gregor } // end extern "C" 535*8e984da8SDouglas Gregor 536*8e984da8SDouglas Gregor namespace clang { 537*8e984da8SDouglas Gregor // FIXME: defined in CodeCompleteConsumer.cpp, but should be a 538*8e984da8SDouglas Gregor // static function here. 539*8e984da8SDouglas Gregor CXCursorKind 540*8e984da8SDouglas Gregor getCursorKindForCompletionResult(const CodeCompleteConsumer::Result &R); 541*8e984da8SDouglas Gregor } 542*8e984da8SDouglas Gregor 543*8e984da8SDouglas Gregor 544*8e984da8SDouglas Gregor namespace { 545*8e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 546*8e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 547*8e984da8SDouglas Gregor 548*8e984da8SDouglas Gregor public: 549*8e984da8SDouglas Gregor explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 550*8e984da8SDouglas Gregor : CodeCompleteConsumer(true, false, false), AllocatedResults(Results) { } 551*8e984da8SDouglas Gregor 552*8e984da8SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, Result *Results, 553*8e984da8SDouglas Gregor unsigned NumResults) { 554*8e984da8SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [NumResults]; 555*8e984da8SDouglas Gregor AllocatedResults.NumResults = NumResults; 556*8e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 557*8e984da8SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 558*8e984da8SDouglas Gregor = new CXStoredCodeCompletionString(Results[I].Priority); 559*8e984da8SDouglas Gregor (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); 560*8e984da8SDouglas Gregor AllocatedResults.Results[I].CursorKind 561*8e984da8SDouglas Gregor = getCursorKindForCompletionResult(Results[I]); 562*8e984da8SDouglas Gregor AllocatedResults.Results[I].CompletionString = StoredCompletion; 563*8e984da8SDouglas Gregor } 564*8e984da8SDouglas Gregor } 565*8e984da8SDouglas Gregor }; 566*8e984da8SDouglas Gregor } 567*8e984da8SDouglas Gregor 568*8e984da8SDouglas Gregor extern "C" { 569*8e984da8SDouglas Gregor CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 570*8e984da8SDouglas Gregor const char *complete_filename, 571*8e984da8SDouglas Gregor unsigned complete_line, 572*8e984da8SDouglas Gregor unsigned complete_column, 573*8e984da8SDouglas Gregor struct CXUnsavedFile *unsaved_files, 574*8e984da8SDouglas Gregor unsigned num_unsaved_files) { 575*8e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 576*8e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 577*8e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 578*8e984da8SDouglas Gregor #endif 579*8e984da8SDouglas Gregor #endif 580*8e984da8SDouglas Gregor 581*8e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 582*8e984da8SDouglas Gregor 583*8e984da8SDouglas Gregor ASTUnit *AST = static_cast<ASTUnit *>(TU); 584*8e984da8SDouglas Gregor if (!AST) 585*8e984da8SDouglas Gregor return 0; 586*8e984da8SDouglas Gregor 587*8e984da8SDouglas Gregor // Perform the remapping of source files. 588*8e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 589*8e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 590*8e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 591*8e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 592*8e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 593*8e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 594*8e984da8SDouglas Gregor Buffer)); 595*8e984da8SDouglas Gregor } 596*8e984da8SDouglas Gregor 597*8e984da8SDouglas Gregor if (EnableLogging) { 598*8e984da8SDouglas Gregor // FIXME: Add logging. 599*8e984da8SDouglas Gregor } 600*8e984da8SDouglas Gregor 601*8e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 602*8e984da8SDouglas Gregor AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 603*8e984da8SDouglas Gregor Results->Results = 0; 604*8e984da8SDouglas Gregor Results->NumResults = 0; 605*8e984da8SDouglas Gregor Results->Buffer = 0; 606*8e984da8SDouglas Gregor 607*8e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 608*8e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 609*8e984da8SDouglas Gregor 610*8e984da8SDouglas Gregor // Perform completion. 611*8e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 612*8e984da8SDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), Capture, 613*8e984da8SDouglas Gregor *Results->Diag, Results->LangOpts, Results->SourceMgr, 614*8e984da8SDouglas Gregor Results->FileMgr, Results->Diagnostics); 615*8e984da8SDouglas Gregor 616*8e984da8SDouglas Gregor 617*8e984da8SDouglas Gregor 618*8e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 619*8e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 620*8e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 621*8e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 622*8e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 623*8e984da8SDouglas Gregor 624*8e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 625*8e984da8SDouglas Gregor const char *lang = 0; 626*8e984da8SDouglas Gregor bool usesPCH = false; 627*8e984da8SDouglas Gregor 628*8e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 629*8e984da8SDouglas Gregor I != E; ++I) { 630*8e984da8SDouglas Gregor if (*I == 0) 631*8e984da8SDouglas Gregor continue; 632*8e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 633*8e984da8SDouglas Gregor if (I + 1 != E) { 634*8e984da8SDouglas Gregor lang = *(++I); 635*8e984da8SDouglas Gregor continue; 636*8e984da8SDouglas Gregor } 637*8e984da8SDouglas Gregor } 638*8e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 639*8e984da8SDouglas Gregor if (I+1 != E) { 640*8e984da8SDouglas Gregor const char *arg = *(++I); 641*8e984da8SDouglas Gregor llvm::SmallString<512> pchName; 642*8e984da8SDouglas Gregor { 643*8e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 644*8e984da8SDouglas Gregor os << arg << ".pth"; 645*8e984da8SDouglas Gregor } 646*8e984da8SDouglas Gregor pchName.push_back('\0'); 647*8e984da8SDouglas Gregor struct stat stat_results; 648*8e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 649*8e984da8SDouglas Gregor usesPCH = true; 650*8e984da8SDouglas Gregor continue; 651*8e984da8SDouglas Gregor } 652*8e984da8SDouglas Gregor } 653*8e984da8SDouglas Gregor } 654*8e984da8SDouglas Gregor 655*8e984da8SDouglas Gregor os << "{ "; 656*8e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 657*8e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 658*8e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 659*8e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 660*8e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 661*8e984da8SDouglas Gregor const char *name = getlogin(); 662*8e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 663*8e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 664*8e984da8SDouglas Gregor os << " }"; 665*8e984da8SDouglas Gregor 666*8e984da8SDouglas Gregor llvm::StringRef res = os.str(); 667*8e984da8SDouglas Gregor if (res.size() > 0) { 668*8e984da8SDouglas Gregor do { 669*8e984da8SDouglas Gregor // Setup the UDP socket. 670*8e984da8SDouglas Gregor struct sockaddr_in servaddr; 671*8e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 672*8e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 673*8e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 674*8e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 675*8e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 676*8e984da8SDouglas Gregor break; 677*8e984da8SDouglas Gregor 678*8e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 679*8e984da8SDouglas Gregor if (sockfd < 0) 680*8e984da8SDouglas Gregor break; 681*8e984da8SDouglas Gregor 682*8e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 683*8e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 684*8e984da8SDouglas Gregor close(sockfd); 685*8e984da8SDouglas Gregor } 686*8e984da8SDouglas Gregor while (false); 687*8e984da8SDouglas Gregor } 688*8e984da8SDouglas Gregor #endif 689*8e984da8SDouglas Gregor #endif 690*8e984da8SDouglas Gregor return Results; 691*8e984da8SDouglas Gregor } 692*8e984da8SDouglas Gregor 693d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 694d5a2a073SDaniel Dunbar if (!ResultsIn) 695d5a2a073SDaniel Dunbar return; 696d5a2a073SDaniel Dunbar 697d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 698d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 699d5a2a073SDaniel Dunbar delete Results; 700d5a2a073SDaniel Dunbar } 701d5a2a073SDaniel Dunbar 702d5a2a073SDaniel Dunbar unsigned 703d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 704d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 705d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 706d5a2a073SDaniel Dunbar if (!Results) 707d5a2a073SDaniel Dunbar return 0; 708d5a2a073SDaniel Dunbar 709d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 710d5a2a073SDaniel Dunbar } 711d5a2a073SDaniel Dunbar 712d5a2a073SDaniel Dunbar CXDiagnostic 713d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 714d5a2a073SDaniel Dunbar unsigned Index) { 715d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 716d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 717d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 718d5a2a073SDaniel Dunbar return 0; 719d5a2a073SDaniel Dunbar 720d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 721d5a2a073SDaniel Dunbar } 722d5a2a073SDaniel Dunbar 723d5a2a073SDaniel Dunbar 724d5a2a073SDaniel Dunbar } // end extern "C" 725