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