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