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