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" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 16 | #include "CXString.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 17 | #include "CIndexDiagnostic.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/ASTUnit.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 23 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CrashRecoveryContext.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Timer.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 30 | #include "llvm/System/Program.h" |
Douglas Gregor | 3d398aa | 2010-07-26 16:29:14 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
| 32 | #include <cstdio> |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | da7af32 | 2010-04-15 01:02:28 +0000 | [diff] [blame] | 35 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 36 | #include "clang/Basic/Version.h" |
Ted Kremenek | da7af32 | 2010-04-15 01:02:28 +0000 | [diff] [blame] | 37 | #include <arpa/inet.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | #endif |
| 42 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 43 | using namespace clang; |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 44 | using namespace clang::cxstring; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | /// \brief Stored representation of a completion string. |
| 48 | /// |
| 49 | /// This is the representation behind a CXCompletionString. |
| 50 | class CXStoredCodeCompletionString : public CodeCompletionString { |
| 51 | unsigned Priority; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 52 | CXAvailabilityKind Availability; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 53 | |
| 54 | public: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 55 | CXStoredCodeCompletionString(unsigned Priority, |
| 56 | CXAvailabilityKind Availability) |
| 57 | : Priority(Priority), Availability(Availability) { } |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 58 | |
| 59 | unsigned getPriority() const { return Priority; } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 60 | CXAvailabilityKind getAvailability() const { return Availability; } |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 61 | }; |
| 62 | } |
| 63 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 64 | extern "C" { |
| 65 | |
| 66 | enum CXCompletionChunkKind |
| 67 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 68 | unsigned chunk_number) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 69 | CXStoredCodeCompletionString *CCStr |
| 70 | = (CXStoredCodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 71 | if (!CCStr || chunk_number >= CCStr->size()) |
| 72 | return CXCompletionChunk_Text; |
| 73 | |
| 74 | switch ((*CCStr)[chunk_number].Kind) { |
| 75 | case CodeCompletionString::CK_TypedText: |
| 76 | return CXCompletionChunk_TypedText; |
| 77 | case CodeCompletionString::CK_Text: |
| 78 | return CXCompletionChunk_Text; |
| 79 | case CodeCompletionString::CK_Optional: |
| 80 | return CXCompletionChunk_Optional; |
| 81 | case CodeCompletionString::CK_Placeholder: |
| 82 | return CXCompletionChunk_Placeholder; |
| 83 | case CodeCompletionString::CK_Informative: |
| 84 | return CXCompletionChunk_Informative; |
| 85 | case CodeCompletionString::CK_ResultType: |
| 86 | return CXCompletionChunk_ResultType; |
| 87 | case CodeCompletionString::CK_CurrentParameter: |
| 88 | return CXCompletionChunk_CurrentParameter; |
| 89 | case CodeCompletionString::CK_LeftParen: |
| 90 | return CXCompletionChunk_LeftParen; |
| 91 | case CodeCompletionString::CK_RightParen: |
| 92 | return CXCompletionChunk_RightParen; |
| 93 | case CodeCompletionString::CK_LeftBracket: |
| 94 | return CXCompletionChunk_LeftBracket; |
| 95 | case CodeCompletionString::CK_RightBracket: |
| 96 | return CXCompletionChunk_RightBracket; |
| 97 | case CodeCompletionString::CK_LeftBrace: |
| 98 | return CXCompletionChunk_LeftBrace; |
| 99 | case CodeCompletionString::CK_RightBrace: |
| 100 | return CXCompletionChunk_RightBrace; |
| 101 | case CodeCompletionString::CK_LeftAngle: |
| 102 | return CXCompletionChunk_LeftAngle; |
| 103 | case CodeCompletionString::CK_RightAngle: |
| 104 | return CXCompletionChunk_RightAngle; |
| 105 | case CodeCompletionString::CK_Comma: |
| 106 | return CXCompletionChunk_Comma; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 107 | case CodeCompletionString::CK_Colon: |
| 108 | return CXCompletionChunk_Colon; |
| 109 | case CodeCompletionString::CK_SemiColon: |
| 110 | return CXCompletionChunk_SemiColon; |
| 111 | case CodeCompletionString::CK_Equal: |
| 112 | return CXCompletionChunk_Equal; |
| 113 | case CodeCompletionString::CK_HorizontalSpace: |
| 114 | return CXCompletionChunk_HorizontalSpace; |
| 115 | case CodeCompletionString::CK_VerticalSpace: |
| 116 | return CXCompletionChunk_VerticalSpace; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Should be unreachable, but let's be careful. |
| 120 | return CXCompletionChunk_Text; |
| 121 | } |
| 122 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 123 | CXString clang_getCompletionChunkText(CXCompletionString completion_string, |
| 124 | unsigned chunk_number) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 125 | CXStoredCodeCompletionString *CCStr |
| 126 | = (CXStoredCodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 127 | if (!CCStr || chunk_number >= CCStr->size()) |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 128 | return createCXString(0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 129 | |
| 130 | switch ((*CCStr)[chunk_number].Kind) { |
| 131 | case CodeCompletionString::CK_TypedText: |
| 132 | case CodeCompletionString::CK_Text: |
| 133 | case CodeCompletionString::CK_Placeholder: |
| 134 | case CodeCompletionString::CK_CurrentParameter: |
| 135 | case CodeCompletionString::CK_Informative: |
| 136 | case CodeCompletionString::CK_LeftParen: |
| 137 | case CodeCompletionString::CK_RightParen: |
| 138 | case CodeCompletionString::CK_LeftBracket: |
| 139 | case CodeCompletionString::CK_RightBracket: |
| 140 | case CodeCompletionString::CK_LeftBrace: |
| 141 | case CodeCompletionString::CK_RightBrace: |
| 142 | case CodeCompletionString::CK_LeftAngle: |
| 143 | case CodeCompletionString::CK_RightAngle: |
| 144 | case CodeCompletionString::CK_Comma: |
| 145 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 146 | case CodeCompletionString::CK_Colon: |
| 147 | case CodeCompletionString::CK_SemiColon: |
| 148 | case CodeCompletionString::CK_Equal: |
| 149 | case CodeCompletionString::CK_HorizontalSpace: |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 150 | case CodeCompletionString::CK_VerticalSpace: |
Douglas Gregor | 5a9c0bc | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 151 | return createCXString((*CCStr)[chunk_number].Text, false); |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 153 | case CodeCompletionString::CK_Optional: |
| 154 | // Note: treated as an empty text block. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 155 | return createCXString(""); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // Should be unreachable, but let's be careful. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 159 | return createCXString(0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 163 | CXCompletionString |
| 164 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 165 | unsigned chunk_number) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 166 | CXStoredCodeCompletionString *CCStr |
| 167 | = (CXStoredCodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 168 | if (!CCStr || chunk_number >= CCStr->size()) |
| 169 | return 0; |
| 170 | |
| 171 | switch ((*CCStr)[chunk_number].Kind) { |
| 172 | case CodeCompletionString::CK_TypedText: |
| 173 | case CodeCompletionString::CK_Text: |
| 174 | case CodeCompletionString::CK_Placeholder: |
| 175 | case CodeCompletionString::CK_CurrentParameter: |
| 176 | case CodeCompletionString::CK_Informative: |
| 177 | case CodeCompletionString::CK_LeftParen: |
| 178 | case CodeCompletionString::CK_RightParen: |
| 179 | case CodeCompletionString::CK_LeftBracket: |
| 180 | case CodeCompletionString::CK_RightBracket: |
| 181 | case CodeCompletionString::CK_LeftBrace: |
| 182 | case CodeCompletionString::CK_RightBrace: |
| 183 | case CodeCompletionString::CK_LeftAngle: |
| 184 | case CodeCompletionString::CK_RightAngle: |
| 185 | case CodeCompletionString::CK_Comma: |
| 186 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 187 | case CodeCompletionString::CK_Colon: |
| 188 | case CodeCompletionString::CK_SemiColon: |
| 189 | case CodeCompletionString::CK_Equal: |
| 190 | case CodeCompletionString::CK_HorizontalSpace: |
| 191 | case CodeCompletionString::CK_VerticalSpace: |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | |
| 194 | case CodeCompletionString::CK_Optional: |
| 195 | // Note: treated as an empty text block. |
| 196 | return (*CCStr)[chunk_number].Optional; |
| 197 | } |
| 198 | |
| 199 | // Should be unreachable, but let's be careful. |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 204 | CXStoredCodeCompletionString *CCStr |
| 205 | = (CXStoredCodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 206 | return CCStr? CCStr->size() : 0; |
| 207 | } |
| 208 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 209 | unsigned clang_getCompletionPriority(CXCompletionString completion_string) { |
| 210 | CXStoredCodeCompletionString *CCStr |
| 211 | = (CXStoredCodeCompletionString *)completion_string; |
Bill Wendling | a2ace58 | 2010-05-27 18:35:05 +0000 | [diff] [blame] | 212 | return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 215 | enum CXAvailabilityKind |
| 216 | clang_getCompletionAvailability(CXCompletionString completion_string) { |
| 217 | CXStoredCodeCompletionString *CCStr |
| 218 | = (CXStoredCodeCompletionString *)completion_string; |
| 219 | return CCStr? CCStr->getAvailability() : CXAvailability_Available; |
| 220 | } |
| 221 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 222 | /// \brief The CXCodeCompleteResults structure we allocate internally; |
| 223 | /// the client only sees the initial CXCodeCompleteResults structure. |
| 224 | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 225 | AllocatedCXCodeCompleteResults(); |
| 226 | ~AllocatedCXCodeCompleteResults(); |
| 227 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 228 | /// \brief Diagnostics produced while performing code completion. |
| 229 | llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; |
| 230 | |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 231 | /// \brief Diag object |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 232 | llvm::IntrusiveRefCntPtr<Diagnostic> Diag; |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 234 | /// \brief Language options used to adjust source locations. |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 235 | LangOptions LangOpts; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 236 | |
| 237 | /// \brief File manager, used for diagnostics. |
| 238 | FileManager FileMgr; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 239 | |
| 240 | FileSystemOptions FileSystemOpts; |
| 241 | |
| 242 | /// \brief Source manager, used for diagnostics. |
| 243 | SourceManager SourceMgr; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 244 | |
| 245 | /// \brief Temporary files that should be removed once we have finished |
| 246 | /// with the code-completion results. |
| 247 | std::vector<llvm::sys::Path> TemporaryFiles; |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 248 | |
| 249 | /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. |
| 250 | llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 253 | AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 254 | : CXCodeCompleteResults(), Diag(new Diagnostic), |
| 255 | SourceMgr(*Diag, FileMgr, FileSystemOpts) { } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 256 | |
| 257 | AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { |
| 258 | for (unsigned I = 0, N = NumResults; I != N; ++I) |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 259 | delete (CXStoredCodeCompletionString *)Results[I].CompletionString; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 260 | delete [] Results; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 261 | |
| 262 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 263 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 264 | for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) |
| 265 | delete TemporaryBuffers[I]; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 268 | } // end extern "C" |
| 269 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 270 | namespace { |
| 271 | class CaptureCompletionResults : public CodeCompleteConsumer { |
| 272 | AllocatedCXCodeCompleteResults &AllocatedResults; |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 273 | llvm::SmallVector<CXCompletionResult, 16> StoredResults; |
| 274 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 275 | public: |
| 276 | explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 277 | : CodeCompleteConsumer(true, false, true, false), |
| 278 | AllocatedResults(Results) { } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 279 | ~CaptureCompletionResults() { Finish(); } |
| 280 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 281 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 282 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 283 | CodeCompletionResult *Results, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 284 | unsigned NumResults) { |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 285 | StoredResults.reserve(StoredResults.size() + NumResults); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 286 | for (unsigned I = 0; I != NumResults; ++I) { |
| 287 | CXStoredCodeCompletionString *StoredCompletion |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 288 | = new CXStoredCodeCompletionString(Results[I].Priority, |
| 289 | Results[I].Availability); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 290 | (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 291 | |
| 292 | CXCompletionResult R; |
| 293 | R.CursorKind = Results[I].CursorKind; |
| 294 | R.CompletionString = StoredCompletion; |
| 295 | StoredResults.push_back(R); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 299 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 300 | OverloadCandidate *Candidates, |
| 301 | unsigned NumCandidates) { |
| 302 | StoredResults.reserve(StoredResults.size() + NumCandidates); |
| 303 | for (unsigned I = 0; I != NumCandidates; ++I) { |
| 304 | // FIXME: Set priority, availability appropriately. |
| 305 | CXStoredCodeCompletionString *StoredCompletion |
| 306 | = new CXStoredCodeCompletionString(1, CXAvailability_Available); |
| 307 | (void)Candidates[I].CreateSignatureString(CurrentArg, S, |
| 308 | StoredCompletion); |
| 309 | |
| 310 | CXCompletionResult R; |
| 311 | R.CursorKind = CXCursor_NotImplemented; |
| 312 | R.CompletionString = StoredCompletion; |
| 313 | StoredResults.push_back(R); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private: |
| 318 | void Finish() { |
| 319 | AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; |
| 320 | AllocatedResults.NumResults = StoredResults.size(); |
| 321 | std::memcpy(AllocatedResults.Results, StoredResults.data(), |
| 322 | StoredResults.size() * sizeof(CXCompletionResult)); |
| 323 | StoredResults.clear(); |
| 324 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 325 | }; |
| 326 | } |
| 327 | |
| 328 | extern "C" { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 329 | struct CodeCompleteAtInfo { |
| 330 | CXTranslationUnit TU; |
| 331 | const char *complete_filename; |
| 332 | unsigned complete_line; |
| 333 | unsigned complete_column; |
| 334 | struct CXUnsavedFile *unsaved_files; |
| 335 | unsigned num_unsaved_files; |
| 336 | unsigned options; |
| 337 | CXCodeCompleteResults *result; |
| 338 | }; |
| 339 | void clang_codeCompleteAt_Impl(void *UserData) { |
| 340 | CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); |
| 341 | CXTranslationUnit TU = CCAI->TU; |
| 342 | const char *complete_filename = CCAI->complete_filename; |
| 343 | unsigned complete_line = CCAI->complete_line; |
| 344 | unsigned complete_column = CCAI->complete_column; |
| 345 | struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; |
| 346 | unsigned num_unsaved_files = CCAI->num_unsaved_files; |
| 347 | unsigned options = CCAI->options; |
| 348 | CCAI->result = 0; |
| 349 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 350 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 351 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 352 | const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); |
| 353 | #endif |
| 354 | #endif |
| 355 | |
| 356 | bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; |
| 357 | |
| 358 | ASTUnit *AST = static_cast<ASTUnit *>(TU); |
| 359 | if (!AST) |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 360 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 362 | ASTUnit::ConcurrencyCheck Check(*AST); |
| 363 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 364 | // Perform the remapping of source files. |
| 365 | llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
| 366 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 367 | llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
| 368 | const llvm::MemoryBuffer *Buffer |
| 369 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
| 370 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, |
| 371 | Buffer)); |
| 372 | } |
| 373 | |
| 374 | if (EnableLogging) { |
| 375 | // FIXME: Add logging. |
| 376 | } |
| 377 | |
| 378 | // Parse the resulting source file to find code-completion results. |
| 379 | AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; |
| 380 | Results->Results = 0; |
| 381 | Results->NumResults = 0; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 382 | |
| 383 | // Create a code-completion consumer to capture the results. |
| 384 | CaptureCompletionResults Capture(*Results); |
| 385 | |
| 386 | // Perform completion. |
| 387 | AST->CodeComplete(complete_filename, complete_line, complete_column, |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 388 | RemappedFiles.data(), RemappedFiles.size(), |
| 389 | (options & CXCodeComplete_IncludeMacros), |
| 390 | (options & CXCodeComplete_IncludeCodePatterns), |
| 391 | Capture, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 392 | *Results->Diag, Results->LangOpts, Results->SourceMgr, |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 393 | Results->FileMgr, Results->Diagnostics, |
| 394 | Results->TemporaryBuffers); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 395 | |
| 396 | |
| 397 | |
| 398 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 399 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 400 | const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); |
| 401 | llvm::SmallString<256> LogResult; |
| 402 | llvm::raw_svector_ostream os(LogResult); |
| 403 | |
| 404 | // Figure out the language and whether or not it uses PCH. |
| 405 | const char *lang = 0; |
| 406 | bool usesPCH = false; |
| 407 | |
| 408 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 409 | I != E; ++I) { |
| 410 | if (*I == 0) |
| 411 | continue; |
| 412 | if (strcmp(*I, "-x") == 0) { |
| 413 | if (I + 1 != E) { |
| 414 | lang = *(++I); |
| 415 | continue; |
| 416 | } |
| 417 | } |
| 418 | else if (strcmp(*I, "-include") == 0) { |
| 419 | if (I+1 != E) { |
| 420 | const char *arg = *(++I); |
| 421 | llvm::SmallString<512> pchName; |
| 422 | { |
| 423 | llvm::raw_svector_ostream os(pchName); |
| 424 | os << arg << ".pth"; |
| 425 | } |
| 426 | pchName.push_back('\0'); |
| 427 | struct stat stat_results; |
| 428 | if (stat(pchName.data(), &stat_results) == 0) |
| 429 | usesPCH = true; |
| 430 | continue; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | os << "{ "; |
| 436 | os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); |
| 437 | os << ", \"numRes\": " << Results->NumResults; |
| 438 | os << ", \"diags\": " << Results->Diagnostics.size(); |
| 439 | os << ", \"pch\": " << (usesPCH ? "true" : "false"); |
| 440 | os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; |
| 441 | const char *name = getlogin(); |
| 442 | os << ", \"user\": \"" << (name ? name : "unknown") << '"'; |
| 443 | os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; |
| 444 | os << " }"; |
| 445 | |
| 446 | llvm::StringRef res = os.str(); |
| 447 | if (res.size() > 0) { |
| 448 | do { |
| 449 | // Setup the UDP socket. |
| 450 | struct sockaddr_in servaddr; |
| 451 | bzero(&servaddr, sizeof(servaddr)); |
| 452 | servaddr.sin_family = AF_INET; |
| 453 | servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); |
| 454 | if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, |
| 455 | &servaddr.sin_addr) <= 0) |
| 456 | break; |
| 457 | |
| 458 | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 459 | if (sockfd < 0) |
| 460 | break; |
| 461 | |
| 462 | sendto(sockfd, res.data(), res.size(), 0, |
| 463 | (struct sockaddr *)&servaddr, sizeof(servaddr)); |
| 464 | close(sockfd); |
| 465 | } |
| 466 | while (false); |
| 467 | } |
| 468 | #endif |
| 469 | #endif |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 470 | CCAI->result = Results; |
| 471 | } |
| 472 | CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, |
| 473 | const char *complete_filename, |
| 474 | unsigned complete_line, |
| 475 | unsigned complete_column, |
| 476 | struct CXUnsavedFile *unsaved_files, |
| 477 | unsigned num_unsaved_files, |
| 478 | unsigned options) { |
| 479 | CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, |
| 480 | complete_column, unsaved_files, num_unsaved_files, |
| 481 | options, 0 }; |
| 482 | llvm::CrashRecoveryContext CRC; |
| 483 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 484 | if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 485 | fprintf(stderr, "libclang: crash detected in code completion\n"); |
| 486 | static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | return CCAI.result; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 493 | unsigned clang_defaultCodeCompleteOptions(void) { |
| 494 | return CXCodeComplete_IncludeMacros; |
| 495 | } |
| 496 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 497 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 498 | if (!ResultsIn) |
| 499 | return; |
| 500 | |
| 501 | AllocatedCXCodeCompleteResults *Results |
| 502 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 503 | delete Results; |
| 504 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 506 | unsigned |
| 507 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
| 508 | AllocatedCXCodeCompleteResults *Results |
| 509 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 510 | if (!Results) |
| 511 | return 0; |
| 512 | |
| 513 | return Results->Diagnostics.size(); |
| 514 | } |
| 515 | |
| 516 | CXDiagnostic |
| 517 | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
| 518 | unsigned Index) { |
| 519 | AllocatedCXCodeCompleteResults *Results |
| 520 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 521 | if (!Results || Index >= Results->Diagnostics.size()) |
| 522 | return 0; |
| 523 | |
| 524 | return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); |
| 525 | } |
| 526 | |
| 527 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 528 | } // end extern "C" |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 530 | /// \brief Simple utility function that appends a \p New string to the given |
| 531 | /// \p Old string, using the \p Buffer for storage. |
| 532 | /// |
| 533 | /// \param Old The string to which we are appending. This parameter will be |
| 534 | /// updated to reflect the complete string. |
| 535 | /// |
| 536 | /// |
| 537 | /// \param New The string to append to \p Old. |
| 538 | /// |
| 539 | /// \param Buffer A buffer that stores the actual, concatenated string. It will |
| 540 | /// be used if the old string is already-non-empty. |
| 541 | static void AppendToString(llvm::StringRef &Old, llvm::StringRef New, |
| 542 | llvm::SmallString<256> &Buffer) { |
| 543 | if (Old.empty()) { |
| 544 | Old = New; |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | if (Buffer.empty()) |
| 549 | Buffer.append(Old.begin(), Old.end()); |
| 550 | Buffer.append(New.begin(), New.end()); |
| 551 | Old = Buffer.str(); |
| 552 | } |
| 553 | |
| 554 | /// \brief Get the typed-text blocks from the given code-completion string |
| 555 | /// and return them as a single string. |
| 556 | /// |
| 557 | /// \param String The code-completion string whose typed-text blocks will be |
| 558 | /// concatenated. |
| 559 | /// |
| 560 | /// \param Buffer A buffer used for storage of the completed name. |
| 561 | static llvm::StringRef GetTypedName(CodeCompletionString *String, |
| 562 | llvm::SmallString<256> &Buffer) { |
| 563 | llvm::StringRef Result; |
| 564 | for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); |
| 565 | C != CEnd; ++C) { |
| 566 | if (C->Kind == CodeCompletionString::CK_TypedText) |
| 567 | AppendToString(Result, C->Text, Buffer); |
| 568 | } |
| 569 | |
| 570 | return Result; |
| 571 | } |
| 572 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 573 | namespace { |
| 574 | struct OrderCompletionResults { |
| 575 | bool operator()(const CXCompletionResult &XR, |
| 576 | const CXCompletionResult &YR) const { |
| 577 | CXStoredCodeCompletionString *X |
| 578 | = (CXStoredCodeCompletionString *)XR.CompletionString; |
| 579 | CXStoredCodeCompletionString *Y |
| 580 | = (CXStoredCodeCompletionString *)YR.CompletionString; |
| 581 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 582 | llvm::SmallString<256> XBuffer; |
| 583 | llvm::StringRef XText = GetTypedName(X, XBuffer); |
| 584 | llvm::SmallString<256> YBuffer; |
| 585 | llvm::StringRef YText = GetTypedName(Y, YBuffer); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 586 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 587 | if (XText.empty() || YText.empty()) |
| 588 | return !XText.empty(); |
| 589 | |
| 590 | int result = XText.compare_lower(YText); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 591 | if (result < 0) |
| 592 | return true; |
| 593 | if (result > 0) |
| 594 | return false; |
| 595 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 596 | result = XText.compare(YText); |
Douglas Gregor | 1aad340 | 2010-09-10 23:05:54 +0000 | [diff] [blame] | 597 | return result < 0; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 598 | } |
| 599 | }; |
| 600 | } |
| 601 | |
| 602 | extern "C" { |
| 603 | void clang_sortCodeCompletionResults(CXCompletionResult *Results, |
| 604 | unsigned NumResults) { |
| 605 | std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); |
| 606 | } |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 607 | } |