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; 51a2db793fSDouglas Gregor 52a2db793fSDouglas Gregor public: 53a2db793fSDouglas Gregor CXStoredCodeCompletionString(unsigned Priority) : Priority(Priority) { } 54a2db793fSDouglas Gregor 55a2db793fSDouglas Gregor unsigned getPriority() const { return Priority; } 56a2db793fSDouglas Gregor }; 57a2db793fSDouglas Gregor } 58a2db793fSDouglas Gregor 59d5a2a073SDaniel Dunbar extern "C" { 60d5a2a073SDaniel Dunbar 61d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 62d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 63d5a2a073SDaniel Dunbar unsigned chunk_number) { 64a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 65a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 66d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 67d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 68d5a2a073SDaniel Dunbar 69d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 70d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 71d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 72d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 73d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 104d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 105d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 106d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 107d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 108d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 109d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 110d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 111d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 112d5a2a073SDaniel Dunbar } 113d5a2a073SDaniel Dunbar 114d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 115d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 116d5a2a073SDaniel Dunbar } 117d5a2a073SDaniel Dunbar 118d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 119d5a2a073SDaniel Dunbar unsigned chunk_number) { 120a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 121a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 122d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 123d5a2a073SDaniel Dunbar return createCXString(0); 124d5a2a073SDaniel Dunbar 125d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 126d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 127d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 128d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 129d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 136d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 137d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 138d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 140d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 141d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 142d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 143d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 144d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 145d5a2a073SDaniel Dunbar return createCXString((*CCStr)[chunk_number].Text, false); 146d5a2a073SDaniel Dunbar 14709737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 14809737ee8SDouglas Gregor // FIXME: Temporary hack until we figure out how to handle vertical space. 14909737ee8SDouglas Gregor return createCXString(" "); 15009737ee8SDouglas Gregor 151d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 152d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 153d5a2a073SDaniel Dunbar return createCXString(""); 154d5a2a073SDaniel Dunbar } 155d5a2a073SDaniel Dunbar 156d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 157d5a2a073SDaniel Dunbar return createCXString(0); 158d5a2a073SDaniel Dunbar } 159d5a2a073SDaniel Dunbar 160d5a2a073SDaniel Dunbar 161d5a2a073SDaniel Dunbar CXCompletionString 162d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 163d5a2a073SDaniel Dunbar unsigned chunk_number) { 164a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 165a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 166d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 167d5a2a073SDaniel Dunbar return 0; 168d5a2a073SDaniel Dunbar 169d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 170d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 171d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 172d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 173d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 177d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 178d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 180d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 181d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 182d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 183d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 184d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 185d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 186d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 187d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 188d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 189d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 190d5a2a073SDaniel Dunbar return 0; 191d5a2a073SDaniel Dunbar 192d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 193d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 194d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 195d5a2a073SDaniel Dunbar } 196d5a2a073SDaniel Dunbar 197d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 198d5a2a073SDaniel Dunbar return 0; 199d5a2a073SDaniel Dunbar } 200d5a2a073SDaniel Dunbar 201d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 202a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 203a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 204d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 205d5a2a073SDaniel Dunbar } 206d5a2a073SDaniel Dunbar 207a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 208a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 209a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 21047bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 211a2db793fSDouglas Gregor } 212a2db793fSDouglas Gregor 213d5a2a073SDaniel Dunbar static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, 214d5a2a073SDaniel Dunbar unsigned &Value) { 215d5a2a073SDaniel Dunbar if (Memory + sizeof(unsigned) > MemoryEnd) 216d5a2a073SDaniel Dunbar return true; 217d5a2a073SDaniel Dunbar 218d5a2a073SDaniel Dunbar memmove(&Value, Memory, sizeof(unsigned)); 219d5a2a073SDaniel Dunbar Memory += sizeof(unsigned); 220d5a2a073SDaniel Dunbar return false; 221d5a2a073SDaniel Dunbar } 222d5a2a073SDaniel Dunbar 223d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 224d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 225d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 226d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults(); 227d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 228d5a2a073SDaniel Dunbar 229d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 230d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 231d5a2a073SDaniel Dunbar 232d5a2a073SDaniel Dunbar /// \brief Diag object 2338e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 234d5a2a073SDaniel Dunbar 235d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 236d5a2a073SDaniel Dunbar LangOptions LangOpts; 237d5a2a073SDaniel Dunbar 238d5a2a073SDaniel Dunbar /// \brief Source manager, used for diagnostics. 239d5a2a073SDaniel Dunbar SourceManager SourceMgr; 240d5a2a073SDaniel Dunbar 241d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 242d5a2a073SDaniel Dunbar FileManager FileMgr; 243d5a2a073SDaniel Dunbar 244d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 245d5a2a073SDaniel Dunbar /// with the code-completion results. 246d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 247d8a5dba9SDouglas Gregor 248d8a5dba9SDouglas Gregor /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. 249d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 250d5a2a073SDaniel Dunbar }; 251d5a2a073SDaniel Dunbar 252d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 253d8a5dba9SDouglas Gregor : CXCodeCompleteResults(), Diag(new Diagnostic), 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 260d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 261d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 262d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 263d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 264d5a2a073SDaniel Dunbar } 265d5a2a073SDaniel Dunbar 266d5a2a073SDaniel Dunbar CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, 267d5a2a073SDaniel Dunbar const char *source_filename, 268d5a2a073SDaniel Dunbar int num_command_line_args, 269d5a2a073SDaniel Dunbar const char **command_line_args, 270d5a2a073SDaniel Dunbar unsigned num_unsaved_files, 271d5a2a073SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 272d5a2a073SDaniel Dunbar const char *complete_filename, 273d5a2a073SDaniel Dunbar unsigned complete_line, 274d5a2a073SDaniel Dunbar unsigned complete_column) { 275d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 276d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 277d5a2a073SDaniel Dunbar const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 278d5a2a073SDaniel Dunbar #endif 279d5a2a073SDaniel Dunbar #endif 280d5a2a073SDaniel Dunbar 281d6009ffbSDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 282d6009ffbSDouglas Gregor 283028d3e4dSDouglas Gregor llvm::OwningPtr<llvm::NamedRegionTimer> CCTimer; 284028d3e4dSDouglas Gregor if (getenv("LIBCLANG_TIMING")) { 285028d3e4dSDouglas Gregor llvm::SmallString<128> TimerName; 286028d3e4dSDouglas Gregor llvm::raw_svector_ostream TimerNameOut(TimerName); 2878ef4c807SDouglas Gregor TimerNameOut << "Code completion (out-of-process) @ " << complete_filename 2888ef4c807SDouglas Gregor << ":" << complete_line << ":" << complete_column; 289028d3e4dSDouglas Gregor CCTimer.reset(new llvm::NamedRegionTimer(TimerNameOut.str())); 290028d3e4dSDouglas Gregor } 291028d3e4dSDouglas Gregor 292d5a2a073SDaniel Dunbar // The indexer, which is mainly used to determine where diagnostics go. 293d5a2a073SDaniel Dunbar CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 294d5a2a073SDaniel Dunbar 295d5a2a073SDaniel Dunbar // Configure the diagnostics. 296d5a2a073SDaniel Dunbar DiagnosticOptions DiagOpts; 297d5a2a073SDaniel Dunbar llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 298d5a2a073SDaniel Dunbar Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 299d5a2a073SDaniel Dunbar 300d5a2a073SDaniel Dunbar // The set of temporary files that we've built. 301d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 302d5a2a073SDaniel Dunbar 303d5a2a073SDaniel Dunbar // Build up the arguments for invoking 'clang'. 304d5a2a073SDaniel Dunbar std::vector<const char *> argv; 305d5a2a073SDaniel Dunbar 306d5a2a073SDaniel Dunbar // First add the complete path to the 'clang' executable. 307d5a2a073SDaniel Dunbar llvm::sys::Path ClangPath = CXXIdx->getClangPath(); 308d5a2a073SDaniel Dunbar argv.push_back(ClangPath.c_str()); 309d5a2a073SDaniel Dunbar 310df000da8SDaniel Dunbar // Always use Clang C++ support. 311df000da8SDaniel Dunbar argv.push_back("-ccc-clang-cxx"); 312df000da8SDaniel Dunbar 313d5a2a073SDaniel Dunbar // Add the '-fsyntax-only' argument so that we only perform a basic 314d5a2a073SDaniel Dunbar // syntax check of the code. 315d5a2a073SDaniel Dunbar argv.push_back("-fsyntax-only"); 316d5a2a073SDaniel Dunbar 317d5a2a073SDaniel Dunbar // Add the appropriate '-code-completion-at=file:line:column' argument 318d5a2a073SDaniel Dunbar // to perform code completion, with an "-Xclang" preceding it. 319d5a2a073SDaniel Dunbar std::string code_complete_at; 320d5a2a073SDaniel Dunbar code_complete_at += complete_filename; 321d5a2a073SDaniel Dunbar code_complete_at += ":"; 322d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_line); 323d5a2a073SDaniel Dunbar code_complete_at += ":"; 324d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_column); 325d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 326d5a2a073SDaniel Dunbar argv.push_back("-code-completion-at"); 327d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 328d5a2a073SDaniel Dunbar argv.push_back(code_complete_at.c_str()); 329d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 330d5a2a073SDaniel Dunbar argv.push_back("-no-code-completion-debug-printer"); 331d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 332d5a2a073SDaniel Dunbar argv.push_back("-code-completion-macros"); 333d5a2a073SDaniel Dunbar argv.push_back("-fdiagnostics-binary"); 334d5a2a073SDaniel Dunbar 335d5a2a073SDaniel Dunbar // Remap any unsaved files to temporary files. 336d5a2a073SDaniel Dunbar std::vector<std::string> RemapArgs; 337d5a2a073SDaniel Dunbar if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) 338d5a2a073SDaniel Dunbar return 0; 339d5a2a073SDaniel Dunbar 340d5a2a073SDaniel Dunbar // The pointers into the elements of RemapArgs are stable because we 341d5a2a073SDaniel Dunbar // won't be adding anything to RemapArgs after this point. 342d5a2a073SDaniel Dunbar for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) 343d5a2a073SDaniel Dunbar argv.push_back(RemapArgs[i].c_str()); 344d5a2a073SDaniel Dunbar 345d5a2a073SDaniel Dunbar // Add the source file name (FIXME: later, we'll want to build temporary 346d5a2a073SDaniel Dunbar // file from the buffer, or just feed the source text via standard input). 347d5a2a073SDaniel Dunbar if (source_filename) 348d5a2a073SDaniel Dunbar argv.push_back(source_filename); 349d5a2a073SDaniel Dunbar 350d5a2a073SDaniel Dunbar // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. 351d5a2a073SDaniel Dunbar for (int i = 0; i < num_command_line_args; ++i) 352d5a2a073SDaniel Dunbar if (const char *arg = command_line_args[i]) { 353d5a2a073SDaniel Dunbar if (strcmp(arg, "-o") == 0) { 354d5a2a073SDaniel Dunbar ++i; // Also skip the matching argument. 355d5a2a073SDaniel Dunbar continue; 356d5a2a073SDaniel Dunbar } 357d5a2a073SDaniel Dunbar if (strcmp(arg, "-emit-ast") == 0 || 358d5a2a073SDaniel Dunbar strcmp(arg, "-c") == 0 || 359d5a2a073SDaniel Dunbar strcmp(arg, "-fsyntax-only") == 0) { 360d5a2a073SDaniel Dunbar continue; 361d5a2a073SDaniel Dunbar } 362d5a2a073SDaniel Dunbar 363d5a2a073SDaniel Dunbar // Keep the argument. 364d5a2a073SDaniel Dunbar argv.push_back(arg); 365d5a2a073SDaniel Dunbar } 366d5a2a073SDaniel Dunbar 367d6009ffbSDouglas Gregor if (EnableLogging) { 368d6009ffbSDouglas Gregor std::string Log = ClangPath.str(); 369d6009ffbSDouglas Gregor for (unsigned I = 0, N = argv.size(); I != N; ++I) { 370d6009ffbSDouglas Gregor Log += ' '; 371d6009ffbSDouglas Gregor Log += argv[I]; 372d6009ffbSDouglas Gregor } 373d6009ffbSDouglas Gregor fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str()); 374d6009ffbSDouglas Gregor } 375d6009ffbSDouglas Gregor 376d5a2a073SDaniel Dunbar // Add the null terminator. 377d5a2a073SDaniel Dunbar argv.push_back(NULL); 378d5a2a073SDaniel Dunbar 379d5a2a073SDaniel Dunbar // Generate a temporary name for the code-completion results file. 380d5a2a073SDaniel Dunbar char tmpFile[L_tmpnam]; 381d5a2a073SDaniel Dunbar char *tmpFileName = tmpnam(tmpFile); 382d5a2a073SDaniel Dunbar llvm::sys::Path ResultsFile(tmpFileName); 383d5a2a073SDaniel Dunbar TemporaryFiles.push_back(ResultsFile); 384d5a2a073SDaniel Dunbar 385d5a2a073SDaniel Dunbar // Generate a temporary name for the diagnostics file. 386d5a2a073SDaniel Dunbar char tmpFileResults[L_tmpnam]; 387d5a2a073SDaniel Dunbar char *tmpResultsFileName = tmpnam(tmpFileResults); 388d5a2a073SDaniel Dunbar llvm::sys::Path DiagnosticsFile(tmpResultsFileName); 389d5a2a073SDaniel Dunbar TemporaryFiles.push_back(DiagnosticsFile); 390d5a2a073SDaniel Dunbar 391d6009ffbSDouglas Gregor 392d6009ffbSDouglas Gregor 393d5a2a073SDaniel Dunbar // Invoke 'clang'. 394d5a2a073SDaniel Dunbar llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null 395d5a2a073SDaniel Dunbar // on Unix or NUL (Windows). 396d5a2a073SDaniel Dunbar std::string ErrMsg; 397d5a2a073SDaniel Dunbar const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, 398d5a2a073SDaniel Dunbar &DiagnosticsFile, 0 }; 399d5a2a073SDaniel Dunbar llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, 400d5a2a073SDaniel Dunbar /* redirects */ &Redirects[0], 401d5a2a073SDaniel Dunbar /* secondsToWait */ 0, 402d5a2a073SDaniel Dunbar /* memoryLimits */ 0, &ErrMsg); 403d5a2a073SDaniel Dunbar 404d5a2a073SDaniel Dunbar if (!ErrMsg.empty()) { 405d5a2a073SDaniel Dunbar std::string AllArgs; 406d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 407d5a2a073SDaniel Dunbar I != E; ++I) { 408d5a2a073SDaniel Dunbar AllArgs += ' '; 409d5a2a073SDaniel Dunbar if (*I) 410d5a2a073SDaniel Dunbar AllArgs += *I; 411d5a2a073SDaniel Dunbar } 412d5a2a073SDaniel Dunbar 413d5a2a073SDaniel Dunbar Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; 414d5a2a073SDaniel Dunbar } 415d5a2a073SDaniel Dunbar 416d5a2a073SDaniel Dunbar // Parse the resulting source file to find code-completion results. 417d5a2a073SDaniel Dunbar using llvm::MemoryBuffer; 418d5a2a073SDaniel Dunbar using llvm::StringRef; 419d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 420d5a2a073SDaniel Dunbar Results->Results = 0; 421d5a2a073SDaniel Dunbar Results->NumResults = 0; 422d5a2a073SDaniel Dunbar // FIXME: Set Results->LangOpts! 423d5a2a073SDaniel Dunbar if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { 424d5a2a073SDaniel Dunbar llvm::SmallVector<CXCompletionResult, 4> CompletionResults; 425d5a2a073SDaniel Dunbar StringRef Buffer = F->getBuffer(); 426d5a2a073SDaniel Dunbar for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); 427d5a2a073SDaniel Dunbar Str < StrEnd;) { 428d5a2a073SDaniel Dunbar unsigned KindValue; 429d5a2a073SDaniel Dunbar if (ReadUnsigned(Str, StrEnd, KindValue)) 430d5a2a073SDaniel Dunbar break; 431d5a2a073SDaniel Dunbar 432a2db793fSDouglas Gregor unsigned Priority; 433a2db793fSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Priority)) 434a2db793fSDouglas Gregor break; 435a2db793fSDouglas Gregor 436a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 437a2db793fSDouglas Gregor = new CXStoredCodeCompletionString(Priority); 438a2db793fSDouglas Gregor if (!CCStr->Deserialize(Str, StrEnd)) { 439a2db793fSDouglas Gregor delete CCStr; 440d5a2a073SDaniel Dunbar continue; 441a2db793fSDouglas Gregor } 442d5a2a073SDaniel Dunbar 443d5a2a073SDaniel Dunbar if (!CCStr->empty()) { 444d5a2a073SDaniel Dunbar // Vend the code-completion result to the caller. 445d5a2a073SDaniel Dunbar CXCompletionResult Result; 446d5a2a073SDaniel Dunbar Result.CursorKind = (CXCursorKind)KindValue; 447d5a2a073SDaniel Dunbar Result.CompletionString = CCStr; 448d5a2a073SDaniel Dunbar CompletionResults.push_back(Result); 449d5a2a073SDaniel Dunbar } 450d5a2a073SDaniel Dunbar }; 451d5a2a073SDaniel Dunbar 452d5a2a073SDaniel Dunbar // Allocate the results. 453d5a2a073SDaniel Dunbar Results->Results = new CXCompletionResult [CompletionResults.size()]; 454d5a2a073SDaniel Dunbar Results->NumResults = CompletionResults.size(); 455d5a2a073SDaniel Dunbar memcpy(Results->Results, CompletionResults.data(), 456d5a2a073SDaniel Dunbar CompletionResults.size() * sizeof(CXCompletionResult)); 457d8a5dba9SDouglas Gregor Results->TemporaryBuffers.push_back(F); 458d5a2a073SDaniel Dunbar } 459d5a2a073SDaniel Dunbar 460d5a2a073SDaniel Dunbar LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, 461d5a2a073SDaniel Dunbar Results->FileMgr, Results->SourceMgr, 462d5a2a073SDaniel Dunbar Results->Diagnostics); 463d5a2a073SDaniel Dunbar 464d5a2a073SDaniel Dunbar // Make sure we delete temporary files when the code-completion results are 465d5a2a073SDaniel Dunbar // destroyed. 466d5a2a073SDaniel Dunbar Results->TemporaryFiles.swap(TemporaryFiles); 467d5a2a073SDaniel Dunbar 468d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 469d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 470d5a2a073SDaniel Dunbar const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 471d5a2a073SDaniel Dunbar llvm::SmallString<256> LogResult; 472d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(LogResult); 473d5a2a073SDaniel Dunbar 474d5a2a073SDaniel Dunbar // Figure out the language and whether or not it uses PCH. 475d5a2a073SDaniel Dunbar const char *lang = 0; 476d5a2a073SDaniel Dunbar bool usesPCH = false; 477d5a2a073SDaniel Dunbar 478d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 479d5a2a073SDaniel Dunbar I != E; ++I) { 480d5a2a073SDaniel Dunbar if (*I == 0) 481d5a2a073SDaniel Dunbar continue; 482d5a2a073SDaniel Dunbar if (strcmp(*I, "-x") == 0) { 483d5a2a073SDaniel Dunbar if (I + 1 != E) { 484d5a2a073SDaniel Dunbar lang = *(++I); 485d5a2a073SDaniel Dunbar continue; 486d5a2a073SDaniel Dunbar } 487d5a2a073SDaniel Dunbar } 488d5a2a073SDaniel Dunbar else if (strcmp(*I, "-include") == 0) { 489d5a2a073SDaniel Dunbar if (I+1 != E) { 490d5a2a073SDaniel Dunbar const char *arg = *(++I); 491d5a2a073SDaniel Dunbar llvm::SmallString<512> pchName; 492d5a2a073SDaniel Dunbar { 493d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(pchName); 494d5a2a073SDaniel Dunbar os << arg << ".pth"; 495d5a2a073SDaniel Dunbar } 496d5a2a073SDaniel Dunbar pchName.push_back('\0'); 497d5a2a073SDaniel Dunbar struct stat stat_results; 498d5a2a073SDaniel Dunbar if (stat(pchName.data(), &stat_results) == 0) 499d5a2a073SDaniel Dunbar usesPCH = true; 500d5a2a073SDaniel Dunbar continue; 501d5a2a073SDaniel Dunbar } 502d5a2a073SDaniel Dunbar } 503d5a2a073SDaniel Dunbar } 504d5a2a073SDaniel Dunbar 505d5a2a073SDaniel Dunbar os << "{ "; 506d5a2a073SDaniel Dunbar os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 507d5a2a073SDaniel Dunbar os << ", \"numRes\": " << Results->NumResults; 508d5a2a073SDaniel Dunbar os << ", \"diags\": " << Results->Diagnostics.size(); 509d5a2a073SDaniel Dunbar os << ", \"pch\": " << (usesPCH ? "true" : "false"); 510d5a2a073SDaniel Dunbar os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 511d5a2a073SDaniel Dunbar const char *name = getlogin(); 512d5a2a073SDaniel Dunbar os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 513d5a2a073SDaniel Dunbar os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 514d5a2a073SDaniel Dunbar os << " }"; 515d5a2a073SDaniel Dunbar 516d5a2a073SDaniel Dunbar llvm::StringRef res = os.str(); 517d5a2a073SDaniel Dunbar if (res.size() > 0) { 518d5a2a073SDaniel Dunbar do { 519d5a2a073SDaniel Dunbar // Setup the UDP socket. 520d5a2a073SDaniel Dunbar struct sockaddr_in servaddr; 521d5a2a073SDaniel Dunbar bzero(&servaddr, sizeof(servaddr)); 522d5a2a073SDaniel Dunbar servaddr.sin_family = AF_INET; 523d5a2a073SDaniel Dunbar servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 524d5a2a073SDaniel Dunbar if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 525d5a2a073SDaniel Dunbar &servaddr.sin_addr) <= 0) 526d5a2a073SDaniel Dunbar break; 527d5a2a073SDaniel Dunbar 528d5a2a073SDaniel Dunbar int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 529d5a2a073SDaniel Dunbar if (sockfd < 0) 530d5a2a073SDaniel Dunbar break; 531d5a2a073SDaniel Dunbar 532d5a2a073SDaniel Dunbar sendto(sockfd, res.data(), res.size(), 0, 533d5a2a073SDaniel Dunbar (struct sockaddr *)&servaddr, sizeof(servaddr)); 534d5a2a073SDaniel Dunbar close(sockfd); 535d5a2a073SDaniel Dunbar } 536d5a2a073SDaniel Dunbar while (false); 537d5a2a073SDaniel Dunbar } 538d5a2a073SDaniel Dunbar #endif 539d5a2a073SDaniel Dunbar #endif 540d5a2a073SDaniel Dunbar return Results; 541d5a2a073SDaniel Dunbar } 542d5a2a073SDaniel Dunbar 5438e984da8SDouglas Gregor } // end extern "C" 5448e984da8SDouglas Gregor 5458e984da8SDouglas Gregor namespace { 5468e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 5478e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 5488e984da8SDouglas Gregor 5498e984da8SDouglas Gregor public: 5508e984da8SDouglas Gregor explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 55139982197SDouglas Gregor : CodeCompleteConsumer(true, false, true, false), 55239982197SDouglas Gregor AllocatedResults(Results) { } 5538e984da8SDouglas Gregor 55400c37ef0SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, 55500c37ef0SDouglas Gregor CodeCompletionContext Context, 55600c37ef0SDouglas Gregor Result *Results, 5578e984da8SDouglas Gregor unsigned NumResults) { 5588e984da8SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [NumResults]; 5598e984da8SDouglas Gregor AllocatedResults.NumResults = NumResults; 5608e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 5618e984da8SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 5628e984da8SDouglas Gregor = new CXStoredCodeCompletionString(Results[I].Priority); 5638e984da8SDouglas Gregor (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); 564b14904c4SDouglas Gregor AllocatedResults.Results[I].CursorKind = Results[I].CursorKind; 5658e984da8SDouglas Gregor AllocatedResults.Results[I].CompletionString = StoredCompletion; 5668e984da8SDouglas Gregor } 5678e984da8SDouglas Gregor } 5688e984da8SDouglas Gregor }; 5698e984da8SDouglas Gregor } 5708e984da8SDouglas Gregor 5718e984da8SDouglas Gregor extern "C" { 57277af1c56SDaniel Dunbar struct CodeCompleteAtInfo { 57377af1c56SDaniel Dunbar CXTranslationUnit TU; 57477af1c56SDaniel Dunbar const char *complete_filename; 57577af1c56SDaniel Dunbar unsigned complete_line; 57677af1c56SDaniel Dunbar unsigned complete_column; 57777af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files; 57877af1c56SDaniel Dunbar unsigned num_unsaved_files; 57977af1c56SDaniel Dunbar unsigned options; 58077af1c56SDaniel Dunbar CXCodeCompleteResults *result; 58177af1c56SDaniel Dunbar }; 58277af1c56SDaniel Dunbar void clang_codeCompleteAt_Impl(void *UserData) { 58377af1c56SDaniel Dunbar CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 58477af1c56SDaniel Dunbar CXTranslationUnit TU = CCAI->TU; 58577af1c56SDaniel Dunbar const char *complete_filename = CCAI->complete_filename; 58677af1c56SDaniel Dunbar unsigned complete_line = CCAI->complete_line; 58777af1c56SDaniel Dunbar unsigned complete_column = CCAI->complete_column; 58877af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 58977af1c56SDaniel Dunbar unsigned num_unsaved_files = CCAI->num_unsaved_files; 59077af1c56SDaniel Dunbar unsigned options = CCAI->options; 59177af1c56SDaniel Dunbar CCAI->result = 0; 59277af1c56SDaniel Dunbar 5938e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 5948e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 5958e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 5968e984da8SDouglas Gregor #endif 5978e984da8SDouglas Gregor #endif 5988e984da8SDouglas Gregor 5998e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 6008e984da8SDouglas Gregor 6018e984da8SDouglas Gregor ASTUnit *AST = static_cast<ASTUnit *>(TU); 6028e984da8SDouglas Gregor if (!AST) 60377af1c56SDaniel Dunbar return; 6048e984da8SDouglas Gregor 6058e984da8SDouglas Gregor // Perform the remapping of source files. 6068e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 6078e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 6088e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 6098e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 6108e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 6118e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 6128e984da8SDouglas Gregor Buffer)); 6138e984da8SDouglas Gregor } 6148e984da8SDouglas Gregor 6158e984da8SDouglas Gregor if (EnableLogging) { 6168e984da8SDouglas Gregor // FIXME: Add logging. 6178e984da8SDouglas Gregor } 6188e984da8SDouglas Gregor 6198e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 6208e984da8SDouglas Gregor AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 6218e984da8SDouglas Gregor Results->Results = 0; 6228e984da8SDouglas Gregor Results->NumResults = 0; 6238e984da8SDouglas Gregor 6248e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 6258e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 6268e984da8SDouglas Gregor 6278e984da8SDouglas Gregor // Perform completion. 6288e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 629b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 630b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 631b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 632b68bc59aSDouglas Gregor Capture, 6338e984da8SDouglas Gregor *Results->Diag, Results->LangOpts, Results->SourceMgr, 634*b97b666cSDouglas Gregor Results->FileMgr, Results->Diagnostics, 635*b97b666cSDouglas Gregor Results->TemporaryBuffers); 6368e984da8SDouglas Gregor 6378e984da8SDouglas Gregor 6388e984da8SDouglas Gregor 6398e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6408e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6418e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 6428e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 6438e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 6448e984da8SDouglas Gregor 6458e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 6468e984da8SDouglas Gregor const char *lang = 0; 6478e984da8SDouglas Gregor bool usesPCH = false; 6488e984da8SDouglas Gregor 6498e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 6508e984da8SDouglas Gregor I != E; ++I) { 6518e984da8SDouglas Gregor if (*I == 0) 6528e984da8SDouglas Gregor continue; 6538e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 6548e984da8SDouglas Gregor if (I + 1 != E) { 6558e984da8SDouglas Gregor lang = *(++I); 6568e984da8SDouglas Gregor continue; 6578e984da8SDouglas Gregor } 6588e984da8SDouglas Gregor } 6598e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 6608e984da8SDouglas Gregor if (I+1 != E) { 6618e984da8SDouglas Gregor const char *arg = *(++I); 6628e984da8SDouglas Gregor llvm::SmallString<512> pchName; 6638e984da8SDouglas Gregor { 6648e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 6658e984da8SDouglas Gregor os << arg << ".pth"; 6668e984da8SDouglas Gregor } 6678e984da8SDouglas Gregor pchName.push_back('\0'); 6688e984da8SDouglas Gregor struct stat stat_results; 6698e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 6708e984da8SDouglas Gregor usesPCH = true; 6718e984da8SDouglas Gregor continue; 6728e984da8SDouglas Gregor } 6738e984da8SDouglas Gregor } 6748e984da8SDouglas Gregor } 6758e984da8SDouglas Gregor 6768e984da8SDouglas Gregor os << "{ "; 6778e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 6788e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 6798e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 6808e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 6818e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 6828e984da8SDouglas Gregor const char *name = getlogin(); 6838e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 6848e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 6858e984da8SDouglas Gregor os << " }"; 6868e984da8SDouglas Gregor 6878e984da8SDouglas Gregor llvm::StringRef res = os.str(); 6888e984da8SDouglas Gregor if (res.size() > 0) { 6898e984da8SDouglas Gregor do { 6908e984da8SDouglas Gregor // Setup the UDP socket. 6918e984da8SDouglas Gregor struct sockaddr_in servaddr; 6928e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 6938e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 6948e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 6958e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 6968e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 6978e984da8SDouglas Gregor break; 6988e984da8SDouglas Gregor 6998e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 7008e984da8SDouglas Gregor if (sockfd < 0) 7018e984da8SDouglas Gregor break; 7028e984da8SDouglas Gregor 7038e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 7048e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 7058e984da8SDouglas Gregor close(sockfd); 7068e984da8SDouglas Gregor } 7078e984da8SDouglas Gregor while (false); 7088e984da8SDouglas Gregor } 7098e984da8SDouglas Gregor #endif 7108e984da8SDouglas Gregor #endif 71177af1c56SDaniel Dunbar CCAI->result = Results; 71277af1c56SDaniel Dunbar } 71377af1c56SDaniel Dunbar CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 71477af1c56SDaniel Dunbar const char *complete_filename, 71577af1c56SDaniel Dunbar unsigned complete_line, 71677af1c56SDaniel Dunbar unsigned complete_column, 71777af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 71877af1c56SDaniel Dunbar unsigned num_unsaved_files, 71977af1c56SDaniel Dunbar unsigned options) { 72077af1c56SDaniel Dunbar CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 72177af1c56SDaniel Dunbar complete_column, unsaved_files, num_unsaved_files, 72277af1c56SDaniel Dunbar options, 0 }; 72377af1c56SDaniel Dunbar llvm::CrashRecoveryContext CRC; 72477af1c56SDaniel Dunbar 72577af1c56SDaniel Dunbar if (!CRC.RunSafely(clang_codeCompleteAt_Impl, &CCAI)) { 72677af1c56SDaniel Dunbar fprintf(stderr, "libclang: crash detected in code completion\n"); 72777af1c56SDaniel Dunbar static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); 72877af1c56SDaniel Dunbar return 0; 72977af1c56SDaniel Dunbar } 73077af1c56SDaniel Dunbar 73177af1c56SDaniel Dunbar return CCAI.result; 7328e984da8SDouglas Gregor } 7338e984da8SDouglas Gregor 734b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 735b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 736b68bc59aSDouglas Gregor } 737b68bc59aSDouglas Gregor 738d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 739d5a2a073SDaniel Dunbar if (!ResultsIn) 740d5a2a073SDaniel Dunbar return; 741d5a2a073SDaniel Dunbar 742d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 743d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 744d5a2a073SDaniel Dunbar delete Results; 745d5a2a073SDaniel Dunbar } 746d5a2a073SDaniel Dunbar 747d5a2a073SDaniel Dunbar unsigned 748d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 749d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 750d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 751d5a2a073SDaniel Dunbar if (!Results) 752d5a2a073SDaniel Dunbar return 0; 753d5a2a073SDaniel Dunbar 754d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 755d5a2a073SDaniel Dunbar } 756d5a2a073SDaniel Dunbar 757d5a2a073SDaniel Dunbar CXDiagnostic 758d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 759d5a2a073SDaniel Dunbar unsigned Index) { 760d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 761d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 762d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 763d5a2a073SDaniel Dunbar return 0; 764d5a2a073SDaniel Dunbar 765d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 766d5a2a073SDaniel Dunbar } 767d5a2a073SDaniel Dunbar 768d5a2a073SDaniel Dunbar 769d5a2a073SDaniel Dunbar } // end extern "C" 770