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