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: |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 149 | case CodeCompletionString::CK_VerticalSpace: |
Douglas Gregor | 5a9c0bc | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 150 | return createCXString((*CCStr)[chunk_number].Text, false); |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 152 | case CodeCompletionString::CK_Optional: |
| 153 | // Note: treated as an empty text block. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 154 | return createCXString(""); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Should be unreachable, but let's be careful. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 158 | return createCXString(0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 162 | CXCompletionString |
| 163 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 164 | unsigned chunk_number) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 165 | CXStoredCodeCompletionString *CCStr |
| 166 | = (CXStoredCodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 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 | 01dfea0 | 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 | ab18893 | 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 | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 203 | CXStoredCodeCompletionString *CCStr |
| 204 | = (CXStoredCodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 205 | return CCStr? CCStr->size() : 0; |
| 206 | } |
| 207 | |
Douglas Gregor | 12e1313 | 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 | a2ace58 | 2010-05-27 18:35:05 +0000 | [diff] [blame] | 211 | return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Douglas Gregor | 58ddb60 | 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 | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 221 | /// \brief The CXCodeCompleteResults structure we allocate internally; |
| 222 | /// the client only sees the initial CXCodeCompleteResults structure. |
| 223 | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 224 | AllocatedCXCodeCompleteResults(); |
| 225 | ~AllocatedCXCodeCompleteResults(); |
| 226 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 227 | /// \brief Diagnostics produced while performing code completion. |
| 228 | llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; |
| 229 | |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 230 | /// \brief Diag object |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 231 | llvm::IntrusiveRefCntPtr<Diagnostic> Diag; |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 233 | /// \brief Language options used to adjust source locations. |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 234 | LangOptions LangOpts; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 235 | |
| 236 | /// \brief File manager, used for diagnostics. |
| 237 | FileManager FileMgr; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 238 | |
| 239 | FileSystemOptions FileSystemOpts; |
| 240 | |
| 241 | /// \brief Source manager, used for diagnostics. |
| 242 | SourceManager SourceMgr; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 243 | |
| 244 | /// \brief Temporary files that should be removed once we have finished |
| 245 | /// with the code-completion results. |
| 246 | std::vector<llvm::sys::Path> TemporaryFiles; |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 247 | |
| 248 | /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. |
| 249 | llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 252 | AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 253 | : CXCodeCompleteResults(), Diag(new Diagnostic), |
| 254 | SourceMgr(*Diag, FileMgr, FileSystemOpts) { } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 255 | |
| 256 | AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { |
| 257 | for (unsigned I = 0, N = NumResults; I != N; ++I) |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 258 | delete (CXStoredCodeCompletionString *)Results[I].CompletionString; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 259 | delete [] Results; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 260 | |
| 261 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 262 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 263 | for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) |
| 264 | delete TemporaryBuffers[I]; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 267 | } // end extern "C" |
| 268 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 269 | namespace { |
| 270 | class CaptureCompletionResults : public CodeCompleteConsumer { |
| 271 | AllocatedCXCodeCompleteResults &AllocatedResults; |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 272 | llvm::SmallVector<CXCompletionResult, 16> StoredResults; |
| 273 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 274 | public: |
| 275 | explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 276 | : CodeCompleteConsumer(true, false, true, false), |
| 277 | AllocatedResults(Results) { } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 278 | ~CaptureCompletionResults() { Finish(); } |
| 279 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 280 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 281 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 282 | CodeCompletionResult *Results, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 283 | unsigned NumResults) { |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 284 | StoredResults.reserve(StoredResults.size() + NumResults); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 285 | for (unsigned I = 0; I != NumResults; ++I) { |
| 286 | CXStoredCodeCompletionString *StoredCompletion |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 287 | = new CXStoredCodeCompletionString(Results[I].Priority, |
| 288 | Results[I].Availability); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 289 | (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 290 | |
| 291 | CXCompletionResult R; |
| 292 | R.CursorKind = Results[I].CursorKind; |
| 293 | R.CompletionString = StoredCompletion; |
| 294 | StoredResults.push_back(R); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 297 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 298 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 299 | OverloadCandidate *Candidates, |
| 300 | unsigned NumCandidates) { |
| 301 | StoredResults.reserve(StoredResults.size() + NumCandidates); |
| 302 | for (unsigned I = 0; I != NumCandidates; ++I) { |
| 303 | // FIXME: Set priority, availability appropriately. |
| 304 | CXStoredCodeCompletionString *StoredCompletion |
| 305 | = new CXStoredCodeCompletionString(1, CXAvailability_Available); |
| 306 | (void)Candidates[I].CreateSignatureString(CurrentArg, S, |
| 307 | StoredCompletion); |
| 308 | |
| 309 | CXCompletionResult R; |
| 310 | R.CursorKind = CXCursor_NotImplemented; |
| 311 | R.CompletionString = StoredCompletion; |
| 312 | StoredResults.push_back(R); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private: |
| 317 | void Finish() { |
| 318 | AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; |
| 319 | AllocatedResults.NumResults = StoredResults.size(); |
| 320 | std::memcpy(AllocatedResults.Results, StoredResults.data(), |
| 321 | StoredResults.size() * sizeof(CXCompletionResult)); |
| 322 | StoredResults.clear(); |
| 323 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 324 | }; |
| 325 | } |
| 326 | |
| 327 | extern "C" { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 328 | struct CodeCompleteAtInfo { |
| 329 | CXTranslationUnit TU; |
| 330 | const char *complete_filename; |
| 331 | unsigned complete_line; |
| 332 | unsigned complete_column; |
| 333 | struct CXUnsavedFile *unsaved_files; |
| 334 | unsigned num_unsaved_files; |
| 335 | unsigned options; |
| 336 | CXCodeCompleteResults *result; |
| 337 | }; |
| 338 | void clang_codeCompleteAt_Impl(void *UserData) { |
| 339 | CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); |
| 340 | CXTranslationUnit TU = CCAI->TU; |
| 341 | const char *complete_filename = CCAI->complete_filename; |
| 342 | unsigned complete_line = CCAI->complete_line; |
| 343 | unsigned complete_column = CCAI->complete_column; |
| 344 | struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; |
| 345 | unsigned num_unsaved_files = CCAI->num_unsaved_files; |
| 346 | unsigned options = CCAI->options; |
| 347 | CCAI->result = 0; |
| 348 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 349 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 350 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 351 | const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); |
| 352 | #endif |
| 353 | #endif |
| 354 | |
| 355 | bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; |
| 356 | |
| 357 | ASTUnit *AST = static_cast<ASTUnit *>(TU); |
| 358 | if (!AST) |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 359 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 361 | ASTUnit::ConcurrencyCheck Check(*AST); |
| 362 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 363 | // Perform the remapping of source files. |
| 364 | llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
| 365 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 366 | llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
| 367 | const llvm::MemoryBuffer *Buffer |
| 368 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
| 369 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, |
| 370 | Buffer)); |
| 371 | } |
| 372 | |
| 373 | if (EnableLogging) { |
| 374 | // FIXME: Add logging. |
| 375 | } |
| 376 | |
| 377 | // Parse the resulting source file to find code-completion results. |
| 378 | AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; |
| 379 | Results->Results = 0; |
| 380 | Results->NumResults = 0; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 381 | |
| 382 | // Create a code-completion consumer to capture the results. |
| 383 | CaptureCompletionResults Capture(*Results); |
| 384 | |
| 385 | // Perform completion. |
| 386 | AST->CodeComplete(complete_filename, complete_line, complete_column, |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 387 | RemappedFiles.data(), RemappedFiles.size(), |
| 388 | (options & CXCodeComplete_IncludeMacros), |
| 389 | (options & CXCodeComplete_IncludeCodePatterns), |
| 390 | Capture, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 391 | *Results->Diag, Results->LangOpts, Results->SourceMgr, |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 392 | Results->FileMgr, Results->Diagnostics, |
| 393 | Results->TemporaryBuffers); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 394 | |
| 395 | |
| 396 | |
| 397 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 398 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 399 | const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); |
| 400 | llvm::SmallString<256> LogResult; |
| 401 | llvm::raw_svector_ostream os(LogResult); |
| 402 | |
| 403 | // Figure out the language and whether or not it uses PCH. |
| 404 | const char *lang = 0; |
| 405 | bool usesPCH = false; |
| 406 | |
| 407 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 408 | I != E; ++I) { |
| 409 | if (*I == 0) |
| 410 | continue; |
| 411 | if (strcmp(*I, "-x") == 0) { |
| 412 | if (I + 1 != E) { |
| 413 | lang = *(++I); |
| 414 | continue; |
| 415 | } |
| 416 | } |
| 417 | else if (strcmp(*I, "-include") == 0) { |
| 418 | if (I+1 != E) { |
| 419 | const char *arg = *(++I); |
| 420 | llvm::SmallString<512> pchName; |
| 421 | { |
| 422 | llvm::raw_svector_ostream os(pchName); |
| 423 | os << arg << ".pth"; |
| 424 | } |
| 425 | pchName.push_back('\0'); |
| 426 | struct stat stat_results; |
| 427 | if (stat(pchName.data(), &stat_results) == 0) |
| 428 | usesPCH = true; |
| 429 | continue; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | os << "{ "; |
| 435 | os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); |
| 436 | os << ", \"numRes\": " << Results->NumResults; |
| 437 | os << ", \"diags\": " << Results->Diagnostics.size(); |
| 438 | os << ", \"pch\": " << (usesPCH ? "true" : "false"); |
| 439 | os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; |
| 440 | const char *name = getlogin(); |
| 441 | os << ", \"user\": \"" << (name ? name : "unknown") << '"'; |
| 442 | os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; |
| 443 | os << " }"; |
| 444 | |
| 445 | llvm::StringRef res = os.str(); |
| 446 | if (res.size() > 0) { |
| 447 | do { |
| 448 | // Setup the UDP socket. |
| 449 | struct sockaddr_in servaddr; |
| 450 | bzero(&servaddr, sizeof(servaddr)); |
| 451 | servaddr.sin_family = AF_INET; |
| 452 | servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); |
| 453 | if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, |
| 454 | &servaddr.sin_addr) <= 0) |
| 455 | break; |
| 456 | |
| 457 | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 458 | if (sockfd < 0) |
| 459 | break; |
| 460 | |
| 461 | sendto(sockfd, res.data(), res.size(), 0, |
| 462 | (struct sockaddr *)&servaddr, sizeof(servaddr)); |
| 463 | close(sockfd); |
| 464 | } |
| 465 | while (false); |
| 466 | } |
| 467 | #endif |
| 468 | #endif |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 469 | CCAI->result = Results; |
| 470 | } |
| 471 | CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, |
| 472 | const char *complete_filename, |
| 473 | unsigned complete_line, |
| 474 | unsigned complete_column, |
| 475 | struct CXUnsavedFile *unsaved_files, |
| 476 | unsigned num_unsaved_files, |
| 477 | unsigned options) { |
| 478 | CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, |
| 479 | complete_column, unsaved_files, num_unsaved_files, |
| 480 | options, 0 }; |
| 481 | llvm::CrashRecoveryContext CRC; |
| 482 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame^] | 483 | if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 484 | fprintf(stderr, "libclang: crash detected in code completion\n"); |
| 485 | static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | return CCAI.result; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 492 | unsigned clang_defaultCodeCompleteOptions(void) { |
| 493 | return CXCodeComplete_IncludeMacros; |
| 494 | } |
| 495 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 496 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 497 | if (!ResultsIn) |
| 498 | return; |
| 499 | |
| 500 | AllocatedCXCodeCompleteResults *Results |
| 501 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 502 | delete Results; |
| 503 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 505 | unsigned |
| 506 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
| 507 | AllocatedCXCodeCompleteResults *Results |
| 508 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 509 | if (!Results) |
| 510 | return 0; |
| 511 | |
| 512 | return Results->Diagnostics.size(); |
| 513 | } |
| 514 | |
| 515 | CXDiagnostic |
| 516 | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
| 517 | unsigned Index) { |
| 518 | AllocatedCXCodeCompleteResults *Results |
| 519 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 520 | if (!Results || Index >= Results->Diagnostics.size()) |
| 521 | return 0; |
| 522 | |
| 523 | return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); |
| 524 | } |
| 525 | |
| 526 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 527 | } // end extern "C" |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 529 | /// \brief Simple utility function that appends a \p New string to the given |
| 530 | /// \p Old string, using the \p Buffer for storage. |
| 531 | /// |
| 532 | /// \param Old The string to which we are appending. This parameter will be |
| 533 | /// updated to reflect the complete string. |
| 534 | /// |
| 535 | /// |
| 536 | /// \param New The string to append to \p Old. |
| 537 | /// |
| 538 | /// \param Buffer A buffer that stores the actual, concatenated string. It will |
| 539 | /// be used if the old string is already-non-empty. |
| 540 | static void AppendToString(llvm::StringRef &Old, llvm::StringRef New, |
| 541 | llvm::SmallString<256> &Buffer) { |
| 542 | if (Old.empty()) { |
| 543 | Old = New; |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | if (Buffer.empty()) |
| 548 | Buffer.append(Old.begin(), Old.end()); |
| 549 | Buffer.append(New.begin(), New.end()); |
| 550 | Old = Buffer.str(); |
| 551 | } |
| 552 | |
| 553 | /// \brief Get the typed-text blocks from the given code-completion string |
| 554 | /// and return them as a single string. |
| 555 | /// |
| 556 | /// \param String The code-completion string whose typed-text blocks will be |
| 557 | /// concatenated. |
| 558 | /// |
| 559 | /// \param Buffer A buffer used for storage of the completed name. |
| 560 | static llvm::StringRef GetTypedName(CodeCompletionString *String, |
| 561 | llvm::SmallString<256> &Buffer) { |
| 562 | llvm::StringRef Result; |
| 563 | for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); |
| 564 | C != CEnd; ++C) { |
| 565 | if (C->Kind == CodeCompletionString::CK_TypedText) |
| 566 | AppendToString(Result, C->Text, Buffer); |
| 567 | } |
| 568 | |
| 569 | return Result; |
| 570 | } |
| 571 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 572 | namespace { |
| 573 | struct OrderCompletionResults { |
| 574 | bool operator()(const CXCompletionResult &XR, |
| 575 | const CXCompletionResult &YR) const { |
| 576 | CXStoredCodeCompletionString *X |
| 577 | = (CXStoredCodeCompletionString *)XR.CompletionString; |
| 578 | CXStoredCodeCompletionString *Y |
| 579 | = (CXStoredCodeCompletionString *)YR.CompletionString; |
| 580 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 581 | llvm::SmallString<256> XBuffer; |
| 582 | llvm::StringRef XText = GetTypedName(X, XBuffer); |
| 583 | llvm::SmallString<256> YBuffer; |
| 584 | llvm::StringRef YText = GetTypedName(Y, YBuffer); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 586 | if (XText.empty() || YText.empty()) |
| 587 | return !XText.empty(); |
| 588 | |
| 589 | int result = XText.compare_lower(YText); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 590 | if (result < 0) |
| 591 | return true; |
| 592 | if (result > 0) |
| 593 | return false; |
| 594 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 595 | result = XText.compare(YText); |
Douglas Gregor | 1aad340 | 2010-09-10 23:05:54 +0000 | [diff] [blame] | 596 | return result < 0; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 597 | } |
| 598 | }; |
| 599 | } |
| 600 | |
| 601 | extern "C" { |
| 602 | void clang_sortCodeCompletionResults(CXCompletionResult *Results, |
| 603 | unsigned NumResults) { |
| 604 | std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); |
| 605 | } |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 606 | } |