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 | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 18 | #include "CXCursor.h" |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame^] | 19 | #include "CXString.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 20 | #include "CIndexDiagnostic.h" |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
| 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/ASTUnit.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 29 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Atomic.h" |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CrashRecoveryContext.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Program.h" |
Douglas Gregor | 3d398aa | 2010-07-26 16:29:14 +0000 | [diff] [blame] | 38 | #include <cstdlib> |
| 39 | #include <cstdio> |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | da7af32 | 2010-04-15 01:02:28 +0000 | [diff] [blame] | 42 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 43 | #include "clang/Basic/Version.h" |
Ted Kremenek | da7af32 | 2010-04-15 01:02:28 +0000 | [diff] [blame] | 44 | #include <arpa/inet.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <sys/types.h> |
| 47 | #include <unistd.h> |
| 48 | #endif |
| 49 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 50 | using namespace clang; |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 51 | using namespace clang::cxstring; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 52 | |
| 53 | extern "C" { |
| 54 | |
| 55 | enum CXCompletionChunkKind |
| 56 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 57 | unsigned chunk_number) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 58 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 59 | if (!CCStr || chunk_number >= CCStr->size()) |
| 60 | return CXCompletionChunk_Text; |
| 61 | |
| 62 | switch ((*CCStr)[chunk_number].Kind) { |
| 63 | case CodeCompletionString::CK_TypedText: |
| 64 | return CXCompletionChunk_TypedText; |
| 65 | case CodeCompletionString::CK_Text: |
| 66 | return CXCompletionChunk_Text; |
| 67 | case CodeCompletionString::CK_Optional: |
| 68 | return CXCompletionChunk_Optional; |
| 69 | case CodeCompletionString::CK_Placeholder: |
| 70 | return CXCompletionChunk_Placeholder; |
| 71 | case CodeCompletionString::CK_Informative: |
| 72 | return CXCompletionChunk_Informative; |
| 73 | case CodeCompletionString::CK_ResultType: |
| 74 | return CXCompletionChunk_ResultType; |
| 75 | case CodeCompletionString::CK_CurrentParameter: |
| 76 | return CXCompletionChunk_CurrentParameter; |
| 77 | case CodeCompletionString::CK_LeftParen: |
| 78 | return CXCompletionChunk_LeftParen; |
| 79 | case CodeCompletionString::CK_RightParen: |
| 80 | return CXCompletionChunk_RightParen; |
| 81 | case CodeCompletionString::CK_LeftBracket: |
| 82 | return CXCompletionChunk_LeftBracket; |
| 83 | case CodeCompletionString::CK_RightBracket: |
| 84 | return CXCompletionChunk_RightBracket; |
| 85 | case CodeCompletionString::CK_LeftBrace: |
| 86 | return CXCompletionChunk_LeftBrace; |
| 87 | case CodeCompletionString::CK_RightBrace: |
| 88 | return CXCompletionChunk_RightBrace; |
| 89 | case CodeCompletionString::CK_LeftAngle: |
| 90 | return CXCompletionChunk_LeftAngle; |
| 91 | case CodeCompletionString::CK_RightAngle: |
| 92 | return CXCompletionChunk_RightAngle; |
| 93 | case CodeCompletionString::CK_Comma: |
| 94 | return CXCompletionChunk_Comma; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 95 | case CodeCompletionString::CK_Colon: |
| 96 | return CXCompletionChunk_Colon; |
| 97 | case CodeCompletionString::CK_SemiColon: |
| 98 | return CXCompletionChunk_SemiColon; |
| 99 | case CodeCompletionString::CK_Equal: |
| 100 | return CXCompletionChunk_Equal; |
| 101 | case CodeCompletionString::CK_HorizontalSpace: |
| 102 | return CXCompletionChunk_HorizontalSpace; |
| 103 | case CodeCompletionString::CK_VerticalSpace: |
| 104 | return CXCompletionChunk_VerticalSpace; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Should be unreachable, but let's be careful. |
| 108 | return CXCompletionChunk_Text; |
| 109 | } |
| 110 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 111 | CXString clang_getCompletionChunkText(CXCompletionString completion_string, |
| 112 | unsigned chunk_number) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 113 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 114 | if (!CCStr || chunk_number >= CCStr->size()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 115 | return createCXString((const char*)0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 116 | |
| 117 | switch ((*CCStr)[chunk_number].Kind) { |
| 118 | case CodeCompletionString::CK_TypedText: |
| 119 | case CodeCompletionString::CK_Text: |
| 120 | case CodeCompletionString::CK_Placeholder: |
| 121 | case CodeCompletionString::CK_CurrentParameter: |
| 122 | case CodeCompletionString::CK_Informative: |
| 123 | case CodeCompletionString::CK_LeftParen: |
| 124 | case CodeCompletionString::CK_RightParen: |
| 125 | case CodeCompletionString::CK_LeftBracket: |
| 126 | case CodeCompletionString::CK_RightBracket: |
| 127 | case CodeCompletionString::CK_LeftBrace: |
| 128 | case CodeCompletionString::CK_RightBrace: |
| 129 | case CodeCompletionString::CK_LeftAngle: |
| 130 | case CodeCompletionString::CK_RightAngle: |
| 131 | case CodeCompletionString::CK_Comma: |
| 132 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 133 | case CodeCompletionString::CK_Colon: |
| 134 | case CodeCompletionString::CK_SemiColon: |
| 135 | case CodeCompletionString::CK_Equal: |
| 136 | case CodeCompletionString::CK_HorizontalSpace: |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 137 | case CodeCompletionString::CK_VerticalSpace: |
Douglas Gregor | 5a9c0bc | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 138 | return createCXString((*CCStr)[chunk_number].Text, false); |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 140 | case CodeCompletionString::CK_Optional: |
| 141 | // Note: treated as an empty text block. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 142 | return createCXString(""); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Should be unreachable, but let's be careful. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 146 | return createCXString((const char*)0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 150 | CXCompletionString |
| 151 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 152 | unsigned chunk_number) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 153 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 154 | if (!CCStr || chunk_number >= CCStr->size()) |
| 155 | return 0; |
| 156 | |
| 157 | switch ((*CCStr)[chunk_number].Kind) { |
| 158 | case CodeCompletionString::CK_TypedText: |
| 159 | case CodeCompletionString::CK_Text: |
| 160 | case CodeCompletionString::CK_Placeholder: |
| 161 | case CodeCompletionString::CK_CurrentParameter: |
| 162 | case CodeCompletionString::CK_Informative: |
| 163 | case CodeCompletionString::CK_LeftParen: |
| 164 | case CodeCompletionString::CK_RightParen: |
| 165 | case CodeCompletionString::CK_LeftBracket: |
| 166 | case CodeCompletionString::CK_RightBracket: |
| 167 | case CodeCompletionString::CK_LeftBrace: |
| 168 | case CodeCompletionString::CK_RightBrace: |
| 169 | case CodeCompletionString::CK_LeftAngle: |
| 170 | case CodeCompletionString::CK_RightAngle: |
| 171 | case CodeCompletionString::CK_Comma: |
| 172 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 173 | case CodeCompletionString::CK_Colon: |
| 174 | case CodeCompletionString::CK_SemiColon: |
| 175 | case CodeCompletionString::CK_Equal: |
| 176 | case CodeCompletionString::CK_HorizontalSpace: |
| 177 | case CodeCompletionString::CK_VerticalSpace: |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | |
| 180 | case CodeCompletionString::CK_Optional: |
| 181 | // Note: treated as an empty text block. |
| 182 | return (*CCStr)[chunk_number].Optional; |
| 183 | } |
| 184 | |
| 185 | // Should be unreachable, but let's be careful. |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 190 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 191 | return CCStr? CCStr->size() : 0; |
| 192 | } |
| 193 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 194 | unsigned clang_getCompletionPriority(CXCompletionString completion_string) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 195 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Bill Wendling | a2ace58 | 2010-05-27 18:35:05 +0000 | [diff] [blame] | 196 | return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 199 | enum CXAvailabilityKind |
| 200 | clang_getCompletionAvailability(CXCompletionString completion_string) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 201 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 202 | return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability()) |
| 203 | : CXAvailability_Available; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 206 | /// \brief The CXCodeCompleteResults structure we allocate internally; |
| 207 | /// the client only sees the initial CXCodeCompleteResults structure. |
| 208 | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 209 | AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 210 | ~AllocatedCXCodeCompleteResults(); |
| 211 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 212 | /// \brief Diagnostics produced while performing code completion. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 213 | SmallVector<StoredDiagnostic, 8> Diagnostics; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 215 | /// \brief Diag object |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 216 | llvm::IntrusiveRefCntPtr<Diagnostic> Diag; |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 218 | /// \brief Language options used to adjust source locations. |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 219 | LangOptions LangOpts; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 220 | |
| 221 | FileSystemOptions FileSystemOpts; |
| 222 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 223 | /// \brief File manager, used for diagnostics. |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 224 | llvm::IntrusiveRefCntPtr<FileManager> FileMgr; |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 225 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 226 | /// \brief Source manager, used for diagnostics. |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 227 | llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 228 | |
| 229 | /// \brief Temporary files that should be removed once we have finished |
| 230 | /// with the code-completion results. |
| 231 | std::vector<llvm::sys::Path> TemporaryFiles; |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 233 | /// \brief Temporary buffers that will be deleted once we have finished with |
| 234 | /// the code-completion results. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 235 | SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 237 | /// \brief Allocator used to store globally cached code-completion results. |
| 238 | llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> |
| 239 | CachedCompletionAllocator; |
| 240 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 241 | /// \brief Allocator used to store code completion results. |
Douglas Gregor | 110a68e | 2011-02-01 23:14:23 +0000 | [diff] [blame] | 242 | clang::CodeCompletionAllocator CodeCompletionAllocator; |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 243 | |
| 244 | /// \brief Context under which completion occurred. |
| 245 | enum clang::CodeCompletionContext::Kind ContextKind; |
| 246 | |
| 247 | /// \brief A bitfield representing the acceptable completions for the |
| 248 | /// current context. |
| 249 | unsigned long long Contexts; |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 251 | /// \brief The kind of the container for the current context for completions. |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 252 | enum CXCursorKind ContainerKind; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 253 | /// \brief The USR of the container for the current context for completions. |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 254 | CXString ContainerUSR; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 255 | /// \brief a boolean value indicating whether there is complete information |
| 256 | /// about the container |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 257 | unsigned ContainerIsIncomplete; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 258 | |
| 259 | /// \brief A string containing the Objective-C selector entered thus far for a |
| 260 | /// message send. |
| 261 | std::string Selector; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 264 | /// \brief Tracks the number of code-completion result objects that are |
| 265 | /// currently active. |
| 266 | /// |
| 267 | /// Used for debugging purposes only. |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 268 | static llvm::sys::cas_flag CodeCompletionResultObjects; |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 269 | |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 270 | AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( |
| 271 | const FileSystemOptions& FileSystemOpts) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 272 | : CXCodeCompleteResults(), |
| 273 | Diag(new Diagnostic( |
| 274 | llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))), |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 275 | FileSystemOpts(FileSystemOpts), |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 276 | FileMgr(new FileManager(FileSystemOpts)), |
| 277 | SourceMgr(new SourceManager(*Diag, *FileMgr)) { |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 278 | if (getenv("LIBCLANG_OBJTRACKING")) { |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 279 | llvm::sys::AtomicIncrement(&CodeCompletionResultObjects); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 280 | fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects); |
| 281 | } |
| 282 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 283 | |
| 284 | AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 285 | delete [] Results; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 286 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 287 | clang_disposeString(ContainerUSR); |
| 288 | |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 289 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 290 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 291 | for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) |
| 292 | delete TemporaryBuffers[I]; |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 293 | |
| 294 | if (getenv("LIBCLANG_OBJTRACKING")) { |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 295 | llvm::sys::AtomicDecrement(&CodeCompletionResultObjects); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 296 | fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects); |
| 297 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 300 | } // end extern "C" |
| 301 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 302 | static unsigned long long getContextsForContextKind( |
| 303 | enum CodeCompletionContext::Kind kind, |
| 304 | Sema &S) { |
| 305 | unsigned long long contexts = 0; |
| 306 | switch (kind) { |
| 307 | case CodeCompletionContext::CCC_OtherWithMacros: { |
| 308 | //We can allow macros here, but we don't know what else is permissible |
| 309 | //So we'll say the only thing permissible are macros |
| 310 | contexts = CXCompletionContext_MacroName; |
| 311 | break; |
| 312 | } |
| 313 | case CodeCompletionContext::CCC_TopLevel: |
| 314 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 315 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 316 | case CodeCompletionContext::CCC_Type: { |
| 317 | contexts = CXCompletionContext_AnyType | |
| 318 | CXCompletionContext_ObjCInterface; |
| 319 | if (S.getLangOptions().CPlusPlus) { |
| 320 | contexts |= CXCompletionContext_EnumTag | |
| 321 | CXCompletionContext_UnionTag | |
| 322 | CXCompletionContext_StructTag | |
| 323 | CXCompletionContext_ClassTag | |
| 324 | CXCompletionContext_NestedNameSpecifier; |
| 325 | } |
| 326 | break; |
| 327 | } |
| 328 | case CodeCompletionContext::CCC_Statement: { |
| 329 | contexts = CXCompletionContext_AnyType | |
| 330 | CXCompletionContext_ObjCInterface | |
| 331 | CXCompletionContext_AnyValue; |
| 332 | if (S.getLangOptions().CPlusPlus) { |
| 333 | contexts |= CXCompletionContext_EnumTag | |
| 334 | CXCompletionContext_UnionTag | |
| 335 | CXCompletionContext_StructTag | |
| 336 | CXCompletionContext_ClassTag | |
| 337 | CXCompletionContext_NestedNameSpecifier; |
| 338 | } |
| 339 | break; |
| 340 | } |
| 341 | case CodeCompletionContext::CCC_Expression: { |
| 342 | contexts = CXCompletionContext_AnyValue; |
| 343 | if (S.getLangOptions().CPlusPlus) { |
| 344 | contexts |= CXCompletionContext_AnyType | |
| 345 | CXCompletionContext_ObjCInterface | |
| 346 | CXCompletionContext_EnumTag | |
| 347 | CXCompletionContext_UnionTag | |
| 348 | CXCompletionContext_StructTag | |
| 349 | CXCompletionContext_ClassTag | |
| 350 | CXCompletionContext_NestedNameSpecifier; |
| 351 | } |
| 352 | break; |
| 353 | } |
| 354 | case CodeCompletionContext::CCC_ObjCMessageReceiver: { |
| 355 | contexts = CXCompletionContext_ObjCObjectValue | |
| 356 | CXCompletionContext_ObjCSelectorValue | |
| 357 | CXCompletionContext_ObjCInterface; |
| 358 | if (S.getLangOptions().CPlusPlus) { |
| 359 | contexts |= CXCompletionContext_CXXClassTypeValue | |
| 360 | CXCompletionContext_AnyType | |
| 361 | CXCompletionContext_EnumTag | |
| 362 | CXCompletionContext_UnionTag | |
| 363 | CXCompletionContext_StructTag | |
| 364 | CXCompletionContext_ClassTag | |
| 365 | CXCompletionContext_NestedNameSpecifier; |
| 366 | } |
| 367 | break; |
| 368 | } |
| 369 | case CodeCompletionContext::CCC_DotMemberAccess: { |
| 370 | contexts = CXCompletionContext_DotMemberAccess; |
| 371 | break; |
| 372 | } |
| 373 | case CodeCompletionContext::CCC_ArrowMemberAccess: { |
| 374 | contexts = CXCompletionContext_ArrowMemberAccess; |
| 375 | break; |
| 376 | } |
| 377 | case CodeCompletionContext::CCC_ObjCPropertyAccess: { |
| 378 | contexts = CXCompletionContext_ObjCPropertyAccess; |
| 379 | break; |
| 380 | } |
| 381 | case CodeCompletionContext::CCC_EnumTag: { |
| 382 | contexts = CXCompletionContext_EnumTag | |
| 383 | CXCompletionContext_NestedNameSpecifier; |
| 384 | break; |
| 385 | } |
| 386 | case CodeCompletionContext::CCC_UnionTag: { |
| 387 | contexts = CXCompletionContext_UnionTag | |
| 388 | CXCompletionContext_NestedNameSpecifier; |
| 389 | break; |
| 390 | } |
| 391 | case CodeCompletionContext::CCC_ClassOrStructTag: { |
| 392 | contexts = CXCompletionContext_StructTag | |
| 393 | CXCompletionContext_ClassTag | |
| 394 | CXCompletionContext_NestedNameSpecifier; |
| 395 | break; |
| 396 | } |
| 397 | case CodeCompletionContext::CCC_ObjCProtocolName: { |
| 398 | contexts = CXCompletionContext_ObjCProtocol; |
| 399 | break; |
| 400 | } |
| 401 | case CodeCompletionContext::CCC_Namespace: { |
| 402 | contexts = CXCompletionContext_Namespace; |
| 403 | break; |
| 404 | } |
| 405 | case CodeCompletionContext::CCC_PotentiallyQualifiedName: { |
| 406 | contexts = CXCompletionContext_NestedNameSpecifier; |
| 407 | break; |
| 408 | } |
| 409 | case CodeCompletionContext::CCC_MacroNameUse: { |
| 410 | contexts = CXCompletionContext_MacroName; |
| 411 | break; |
| 412 | } |
| 413 | case CodeCompletionContext::CCC_NaturalLanguage: { |
| 414 | contexts = CXCompletionContext_NaturalLanguage; |
| 415 | break; |
| 416 | } |
| 417 | case CodeCompletionContext::CCC_SelectorName: { |
| 418 | contexts = CXCompletionContext_ObjCSelectorName; |
| 419 | break; |
| 420 | } |
| 421 | case CodeCompletionContext::CCC_ParenthesizedExpression: { |
| 422 | contexts = CXCompletionContext_AnyType | |
| 423 | CXCompletionContext_ObjCInterface | |
| 424 | CXCompletionContext_AnyValue; |
| 425 | if (S.getLangOptions().CPlusPlus) { |
| 426 | contexts |= CXCompletionContext_EnumTag | |
| 427 | CXCompletionContext_UnionTag | |
| 428 | CXCompletionContext_StructTag | |
| 429 | CXCompletionContext_ClassTag | |
| 430 | CXCompletionContext_NestedNameSpecifier; |
| 431 | } |
| 432 | break; |
| 433 | } |
| 434 | case CodeCompletionContext::CCC_ObjCInstanceMessage: { |
| 435 | contexts = CXCompletionContext_ObjCInstanceMessage; |
| 436 | break; |
| 437 | } |
| 438 | case CodeCompletionContext::CCC_ObjCClassMessage: { |
| 439 | contexts = CXCompletionContext_ObjCClassMessage; |
| 440 | break; |
| 441 | } |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 442 | case CodeCompletionContext::CCC_ObjCInterfaceName: { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 443 | contexts = CXCompletionContext_ObjCInterface; |
| 444 | break; |
| 445 | } |
| 446 | case CodeCompletionContext::CCC_ObjCCategoryName: { |
| 447 | contexts = CXCompletionContext_ObjCCategory; |
| 448 | break; |
| 449 | } |
| 450 | case CodeCompletionContext::CCC_Other: |
| 451 | case CodeCompletionContext::CCC_ObjCInterface: |
| 452 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 453 | case CodeCompletionContext::CCC_Name: |
| 454 | case CodeCompletionContext::CCC_MacroName: |
| 455 | case CodeCompletionContext::CCC_PreprocessorExpression: |
| 456 | case CodeCompletionContext::CCC_PreprocessorDirective: |
| 457 | case CodeCompletionContext::CCC_TypeQualifiers: { |
| 458 | //Only Clang results should be accepted, so we'll set all of the other |
| 459 | //context bits to 0 (i.e. the empty set) |
| 460 | contexts = CXCompletionContext_Unexposed; |
| 461 | break; |
| 462 | } |
| 463 | case CodeCompletionContext::CCC_Recovery: { |
| 464 | //We don't know what the current context is, so we'll return unknown |
| 465 | //This is the equivalent of setting all of the other context bits |
| 466 | contexts = CXCompletionContext_Unknown; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | return contexts; |
| 471 | } |
| 472 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 473 | namespace { |
| 474 | class CaptureCompletionResults : public CodeCompleteConsumer { |
| 475 | AllocatedCXCodeCompleteResults &AllocatedResults; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 476 | SmallVector<CXCompletionResult, 16> StoredResults; |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 477 | CXTranslationUnit *TU; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 478 | public: |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 479 | CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results, |
| 480 | CXTranslationUnit *TranslationUnit) |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 481 | : CodeCompleteConsumer(true, false, true, false), |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 482 | AllocatedResults(Results), TU(TranslationUnit) { } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 483 | ~CaptureCompletionResults() { Finish(); } |
| 484 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 485 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 486 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 487 | CodeCompletionResult *Results, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 488 | unsigned NumResults) { |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 489 | StoredResults.reserve(StoredResults.size() + NumResults); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 490 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 491 | CodeCompletionString *StoredCompletion |
| 492 | = Results[I].CreateCodeCompletionString(S, |
| 493 | AllocatedResults.CodeCompletionAllocator); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 494 | |
| 495 | CXCompletionResult R; |
| 496 | R.CursorKind = Results[I].CursorKind; |
| 497 | R.CompletionString = StoredCompletion; |
| 498 | StoredResults.push_back(R); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 499 | } |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 501 | enum CodeCompletionContext::Kind contextKind = Context.getKind(); |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 503 | AllocatedResults.ContextKind = contextKind; |
| 504 | AllocatedResults.Contexts = getContextsForContextKind(contextKind, S); |
| 505 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 506 | AllocatedResults.Selector = ""; |
| 507 | if (Context.getNumSelIdents() > 0) { |
| 508 | for (unsigned i = 0; i < Context.getNumSelIdents(); i++) { |
| 509 | IdentifierInfo *selIdent = Context.getSelIdents()[i]; |
| 510 | if (selIdent != NULL) { |
| 511 | StringRef selectorString = Context.getSelIdents()[i]->getName(); |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 512 | AllocatedResults.Selector += selectorString; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 513 | } |
| 514 | AllocatedResults.Selector += ":"; |
| 515 | } |
| 516 | } |
| 517 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 518 | QualType baseType = Context.getBaseType(); |
| 519 | NamedDecl *D = NULL; |
| 520 | |
| 521 | if (!baseType.isNull()) { |
| 522 | // Get the declaration for a class/struct/union/enum type |
| 523 | if (const TagType *Tag = baseType->getAs<TagType>()) |
| 524 | D = Tag->getDecl(); |
| 525 | // Get the @interface declaration for a (possibly-qualified) Objective-C |
| 526 | // object pointer type, e.g., NSString* |
| 527 | else if (const ObjCObjectPointerType *ObjPtr = |
| 528 | baseType->getAs<ObjCObjectPointerType>()) |
| 529 | D = ObjPtr->getInterfaceDecl(); |
| 530 | // Get the @interface declaration for an Objective-C object type |
| 531 | else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>()) |
| 532 | D = Obj->getInterface(); |
| 533 | // Get the class for a C++ injected-class-name |
| 534 | else if (const InjectedClassNameType *Injected = |
| 535 | baseType->getAs<InjectedClassNameType>()) |
| 536 | D = Injected->getDecl(); |
| 537 | } |
| 538 | |
| 539 | if (D != NULL) { |
| 540 | CXCursor cursor = cxcursor::MakeCXCursor(D, *TU); |
| 541 | |
| 542 | CXCursorKind cursorKind = clang_getCursorKind(cursor); |
| 543 | CXString cursorUSR = clang_getCursorUSR(cursor); |
| 544 | |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame^] | 545 | // Normally, clients of CXString shouldn't care whether or not |
| 546 | // a CXString is managed by a pool or by explicitly malloc'ed memory. |
| 547 | // However, there are cases when AllocatedResults outlives the |
| 548 | // CXTranslationUnit. This is a workaround that failure mode. |
| 549 | if (cxstring::isManagedByPool(cursorUSR)) { |
| 550 | CXString heapStr = |
| 551 | cxstring::createCXString(clang_getCString(cursorUSR), true); |
| 552 | clang_disposeString(cursorUSR); |
| 553 | cursorUSR = heapStr; |
| 554 | } |
| 555 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 556 | AllocatedResults.ContainerKind = cursorKind; |
| 557 | AllocatedResults.ContainerUSR = cursorUSR; |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame^] | 558 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 559 | const Type *type = baseType.getTypePtrOrNull(); |
| 560 | if (type != NULL) { |
| 561 | AllocatedResults.ContainerIsIncomplete = type->isIncompleteType(); |
| 562 | } |
| 563 | else { |
| 564 | AllocatedResults.ContainerIsIncomplete = 1; |
| 565 | } |
| 566 | } |
| 567 | else { |
| 568 | AllocatedResults.ContainerKind = CXCursor_InvalidCode; |
| 569 | AllocatedResults.ContainerUSR = createCXString(""); |
| 570 | AllocatedResults.ContainerIsIncomplete = 1; |
| 571 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 572 | } |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 574 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 575 | OverloadCandidate *Candidates, |
| 576 | unsigned NumCandidates) { |
| 577 | StoredResults.reserve(StoredResults.size() + NumCandidates); |
| 578 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 579 | CodeCompletionString *StoredCompletion |
| 580 | = Candidates[I].CreateSignatureString(CurrentArg, S, |
| 581 | AllocatedResults.CodeCompletionAllocator); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 582 | |
| 583 | CXCompletionResult R; |
| 584 | R.CursorKind = CXCursor_NotImplemented; |
| 585 | R.CompletionString = StoredCompletion; |
| 586 | StoredResults.push_back(R); |
| 587 | } |
| 588 | } |
| 589 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 590 | virtual CodeCompletionAllocator &getAllocator() { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 591 | return AllocatedResults.CodeCompletionAllocator; |
| 592 | } |
| 593 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 594 | private: |
| 595 | void Finish() { |
| 596 | AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; |
| 597 | AllocatedResults.NumResults = StoredResults.size(); |
| 598 | std::memcpy(AllocatedResults.Results, StoredResults.data(), |
| 599 | StoredResults.size() * sizeof(CXCompletionResult)); |
| 600 | StoredResults.clear(); |
| 601 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 602 | }; |
| 603 | } |
| 604 | |
| 605 | extern "C" { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 606 | struct CodeCompleteAtInfo { |
| 607 | CXTranslationUnit TU; |
| 608 | const char *complete_filename; |
| 609 | unsigned complete_line; |
| 610 | unsigned complete_column; |
| 611 | struct CXUnsavedFile *unsaved_files; |
| 612 | unsigned num_unsaved_files; |
| 613 | unsigned options; |
| 614 | CXCodeCompleteResults *result; |
| 615 | }; |
| 616 | void clang_codeCompleteAt_Impl(void *UserData) { |
| 617 | CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); |
| 618 | CXTranslationUnit TU = CCAI->TU; |
| 619 | const char *complete_filename = CCAI->complete_filename; |
| 620 | unsigned complete_line = CCAI->complete_line; |
| 621 | unsigned complete_column = CCAI->complete_column; |
| 622 | struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; |
| 623 | unsigned num_unsaved_files = CCAI->num_unsaved_files; |
| 624 | unsigned options = CCAI->options; |
| 625 | CCAI->result = 0; |
| 626 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 627 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 628 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 629 | const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); |
| 630 | #endif |
| 631 | #endif |
| 632 | |
| 633 | bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; |
| 634 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 635 | ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 636 | if (!AST) |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 637 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 639 | ASTUnit::ConcurrencyCheck Check(*AST); |
| 640 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 641 | // Perform the remapping of source files. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 642 | SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 643 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 644 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 645 | const llvm::MemoryBuffer *Buffer |
| 646 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
| 647 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, |
| 648 | Buffer)); |
| 649 | } |
| 650 | |
| 651 | if (EnableLogging) { |
| 652 | // FIXME: Add logging. |
| 653 | } |
| 654 | |
| 655 | // Parse the resulting source file to find code-completion results. |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 656 | AllocatedCXCodeCompleteResults *Results = |
| 657 | new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts()); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 658 | Results->Results = 0; |
| 659 | Results->NumResults = 0; |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 661 | // Create a code-completion consumer to capture the results. |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 662 | CaptureCompletionResults Capture(*Results, &TU); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 663 | |
| 664 | // Perform completion. |
| 665 | AST->CodeComplete(complete_filename, complete_line, complete_column, |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 666 | RemappedFiles.data(), RemappedFiles.size(), |
| 667 | (options & CXCodeComplete_IncludeMacros), |
| 668 | (options & CXCodeComplete_IncludeCodePatterns), |
| 669 | Capture, |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 670 | *Results->Diag, Results->LangOpts, *Results->SourceMgr, |
| 671 | *Results->FileMgr, Results->Diagnostics, |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 672 | Results->TemporaryBuffers); |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 673 | |
| 674 | // Keep a reference to the allocator used for cached global completions, so |
| 675 | // that we can be sure that the memory used by our code completion strings |
| 676 | // doesn't get freed due to subsequent reparses (while the code completion |
| 677 | // results are still active). |
| 678 | Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 679 | |
| 680 | |
| 681 | |
| 682 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 683 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 684 | const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); |
| 685 | llvm::SmallString<256> LogResult; |
| 686 | llvm::raw_svector_ostream os(LogResult); |
| 687 | |
| 688 | // Figure out the language and whether or not it uses PCH. |
| 689 | const char *lang = 0; |
| 690 | bool usesPCH = false; |
| 691 | |
| 692 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 693 | I != E; ++I) { |
| 694 | if (*I == 0) |
| 695 | continue; |
| 696 | if (strcmp(*I, "-x") == 0) { |
| 697 | if (I + 1 != E) { |
| 698 | lang = *(++I); |
| 699 | continue; |
| 700 | } |
| 701 | } |
| 702 | else if (strcmp(*I, "-include") == 0) { |
| 703 | if (I+1 != E) { |
| 704 | const char *arg = *(++I); |
| 705 | llvm::SmallString<512> pchName; |
| 706 | { |
| 707 | llvm::raw_svector_ostream os(pchName); |
| 708 | os << arg << ".pth"; |
| 709 | } |
| 710 | pchName.push_back('\0'); |
| 711 | struct stat stat_results; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 712 | if (stat(pchName.str().c_str(), &stat_results) == 0) |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 713 | usesPCH = true; |
| 714 | continue; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | os << "{ "; |
| 720 | os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); |
| 721 | os << ", \"numRes\": " << Results->NumResults; |
| 722 | os << ", \"diags\": " << Results->Diagnostics.size(); |
| 723 | os << ", \"pch\": " << (usesPCH ? "true" : "false"); |
| 724 | os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; |
| 725 | const char *name = getlogin(); |
| 726 | os << ", \"user\": \"" << (name ? name : "unknown") << '"'; |
| 727 | os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; |
| 728 | os << " }"; |
| 729 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 730 | StringRef res = os.str(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 731 | if (res.size() > 0) { |
| 732 | do { |
| 733 | // Setup the UDP socket. |
| 734 | struct sockaddr_in servaddr; |
| 735 | bzero(&servaddr, sizeof(servaddr)); |
| 736 | servaddr.sin_family = AF_INET; |
| 737 | servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); |
| 738 | if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, |
| 739 | &servaddr.sin_addr) <= 0) |
| 740 | break; |
| 741 | |
| 742 | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 743 | if (sockfd < 0) |
| 744 | break; |
| 745 | |
| 746 | sendto(sockfd, res.data(), res.size(), 0, |
| 747 | (struct sockaddr *)&servaddr, sizeof(servaddr)); |
| 748 | close(sockfd); |
| 749 | } |
| 750 | while (false); |
| 751 | } |
| 752 | #endif |
| 753 | #endif |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 754 | CCAI->result = Results; |
| 755 | } |
| 756 | CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, |
| 757 | const char *complete_filename, |
| 758 | unsigned complete_line, |
| 759 | unsigned complete_column, |
| 760 | struct CXUnsavedFile *unsaved_files, |
| 761 | unsigned num_unsaved_files, |
| 762 | unsigned options) { |
| 763 | CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, |
| 764 | complete_column, unsaved_files, num_unsaved_files, |
| 765 | options, 0 }; |
| 766 | llvm::CrashRecoveryContext CRC; |
| 767 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 768 | if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 769 | fprintf(stderr, "libclang: crash detected in code completion\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 770 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 771 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 772 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 773 | PrintLibclangResourceUsage(TU); |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 774 | |
| 775 | return CCAI.result; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 778 | unsigned clang_defaultCodeCompleteOptions(void) { |
| 779 | return CXCodeComplete_IncludeMacros; |
| 780 | } |
| 781 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 782 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 783 | if (!ResultsIn) |
| 784 | return; |
| 785 | |
| 786 | AllocatedCXCodeCompleteResults *Results |
| 787 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 788 | delete Results; |
| 789 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 791 | unsigned |
| 792 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
| 793 | AllocatedCXCodeCompleteResults *Results |
| 794 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 795 | if (!Results) |
| 796 | return 0; |
| 797 | |
| 798 | return Results->Diagnostics.size(); |
| 799 | } |
| 800 | |
| 801 | CXDiagnostic |
| 802 | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
| 803 | unsigned Index) { |
| 804 | AllocatedCXCodeCompleteResults *Results |
| 805 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 806 | if (!Results || Index >= Results->Diagnostics.size()) |
| 807 | return 0; |
| 808 | |
| 809 | return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); |
| 810 | } |
| 811 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 812 | unsigned long long |
| 813 | clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) { |
| 814 | AllocatedCXCodeCompleteResults *Results |
| 815 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 816 | if (!Results) |
| 817 | return 0; |
| 818 | |
| 819 | return Results->Contexts; |
| 820 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 822 | enum CXCursorKind clang_codeCompleteGetContainerKind( |
| 823 | CXCodeCompleteResults *ResultsIn, |
| 824 | unsigned *IsIncomplete) { |
| 825 | AllocatedCXCodeCompleteResults *Results = |
| 826 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
| 827 | if (!Results) |
| 828 | return CXCursor_InvalidCode; |
| 829 | |
| 830 | if (IsIncomplete != NULL) { |
| 831 | *IsIncomplete = Results->ContainerIsIncomplete; |
| 832 | } |
| 833 | |
| 834 | return Results->ContainerKind; |
| 835 | } |
| 836 | |
| 837 | CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) { |
| 838 | AllocatedCXCodeCompleteResults *Results = |
| 839 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
| 840 | if (!Results) |
| 841 | return createCXString(""); |
| 842 | |
| 843 | return createCXString(clang_getCString(Results->ContainerUSR)); |
| 844 | } |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 845 | |
| 846 | |
| 847 | CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) { |
| 848 | AllocatedCXCodeCompleteResults *Results = |
| 849 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
| 850 | if (!Results) |
| 851 | return createCXString(""); |
| 852 | |
| 853 | return createCXString(Results->Selector); |
| 854 | } |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 855 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 856 | } // end extern "C" |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 858 | /// \brief Simple utility function that appends a \p New string to the given |
| 859 | /// \p Old string, using the \p Buffer for storage. |
| 860 | /// |
| 861 | /// \param Old The string to which we are appending. This parameter will be |
| 862 | /// updated to reflect the complete string. |
| 863 | /// |
| 864 | /// |
| 865 | /// \param New The string to append to \p Old. |
| 866 | /// |
| 867 | /// \param Buffer A buffer that stores the actual, concatenated string. It will |
| 868 | /// be used if the old string is already-non-empty. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 869 | static void AppendToString(StringRef &Old, StringRef New, |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 870 | llvm::SmallString<256> &Buffer) { |
| 871 | if (Old.empty()) { |
| 872 | Old = New; |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | if (Buffer.empty()) |
| 877 | Buffer.append(Old.begin(), Old.end()); |
| 878 | Buffer.append(New.begin(), New.end()); |
| 879 | Old = Buffer.str(); |
| 880 | } |
| 881 | |
| 882 | /// \brief Get the typed-text blocks from the given code-completion string |
| 883 | /// and return them as a single string. |
| 884 | /// |
| 885 | /// \param String The code-completion string whose typed-text blocks will be |
| 886 | /// concatenated. |
| 887 | /// |
| 888 | /// \param Buffer A buffer used for storage of the completed name. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 889 | static StringRef GetTypedName(CodeCompletionString *String, |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 890 | llvm::SmallString<256> &Buffer) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 891 | StringRef Result; |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 892 | for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); |
| 893 | C != CEnd; ++C) { |
| 894 | if (C->Kind == CodeCompletionString::CK_TypedText) |
| 895 | AppendToString(Result, C->Text, Buffer); |
| 896 | } |
| 897 | |
| 898 | return Result; |
| 899 | } |
| 900 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 901 | namespace { |
| 902 | struct OrderCompletionResults { |
| 903 | bool operator()(const CXCompletionResult &XR, |
| 904 | const CXCompletionResult &YR) const { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 905 | CodeCompletionString *X |
| 906 | = (CodeCompletionString *)XR.CompletionString; |
| 907 | CodeCompletionString *Y |
| 908 | = (CodeCompletionString *)YR.CompletionString; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 910 | llvm::SmallString<256> XBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 911 | StringRef XText = GetTypedName(X, XBuffer); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 912 | llvm::SmallString<256> YBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 913 | StringRef YText = GetTypedName(Y, YBuffer); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 914 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 915 | if (XText.empty() || YText.empty()) |
| 916 | return !XText.empty(); |
| 917 | |
| 918 | int result = XText.compare_lower(YText); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 919 | if (result < 0) |
| 920 | return true; |
| 921 | if (result > 0) |
| 922 | return false; |
| 923 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 924 | result = XText.compare(YText); |
Douglas Gregor | 1aad340 | 2010-09-10 23:05:54 +0000 | [diff] [blame] | 925 | return result < 0; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 926 | } |
| 927 | }; |
| 928 | } |
| 929 | |
| 930 | extern "C" { |
| 931 | void clang_sortCodeCompletionResults(CXCompletionResult *Results, |
| 932 | unsigned NumResults) { |
| 933 | std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); |
| 934 | } |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 935 | } |