Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1 | //===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===// |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 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 CodeCompleteConsumer class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang-c/Index.h" |
Douglas Gregor | 75b7128 | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Scope.h" |
| 20 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <algorithm> |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 26 | #include <cstring> |
| 27 | #include <functional> |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 32 | // Code completion context implementation |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | bool CodeCompletionContext::wantConstructorResults() const { |
| 36 | switch (Kind) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 37 | case CCC_Recovery: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 38 | case CCC_Statement: |
| 39 | case CCC_Expression: |
| 40 | case CCC_ObjCMessageReceiver: |
| 41 | case CCC_ParenthesizedExpression: |
| 42 | return true; |
| 43 | |
| 44 | case CCC_TopLevel: |
| 45 | case CCC_ObjCInterface: |
| 46 | case CCC_ObjCImplementation: |
| 47 | case CCC_ObjCIvarList: |
| 48 | case CCC_ClassStructUnion: |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 49 | case CCC_DotMemberAccess: |
| 50 | case CCC_ArrowMemberAccess: |
| 51 | case CCC_ObjCPropertyAccess: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 52 | case CCC_EnumTag: |
| 53 | case CCC_UnionTag: |
| 54 | case CCC_ClassOrStructTag: |
| 55 | case CCC_ObjCProtocolName: |
| 56 | case CCC_Namespace: |
| 57 | case CCC_Type: |
| 58 | case CCC_Name: |
| 59 | case CCC_PotentiallyQualifiedName: |
| 60 | case CCC_MacroName: |
| 61 | case CCC_MacroNameUse: |
| 62 | case CCC_PreprocessorExpression: |
| 63 | case CCC_PreprocessorDirective: |
| 64 | case CCC_NaturalLanguage: |
| 65 | case CCC_SelectorName: |
| 66 | case CCC_TypeQualifiers: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 67 | case CCC_Other: |
Douglas Gregor | 5c722c70 | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 68 | case CCC_OtherWithMacros: |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 69 | case CCC_ObjCInstanceMessage: |
| 70 | case CCC_ObjCClassMessage: |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 71 | case CCC_ObjCInterfaceName: |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 72 | case CCC_ObjCCategoryName: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 73 | return false; |
| 74 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 75 | |
| 76 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 80 | // Code completion string implementation |
| 81 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 82 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
Daniel Dunbar | ad5757f | 2009-11-12 18:40:12 +0000 | [diff] [blame] | 83 | : Kind(Kind), Text("") |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 84 | { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 85 | switch (Kind) { |
| 86 | case CK_TypedText: |
| 87 | case CK_Text: |
| 88 | case CK_Placeholder: |
| 89 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 90 | case CK_ResultType: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 91 | case CK_CurrentParameter: |
| 92 | this->Text = Text; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 93 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 94 | |
| 95 | case CK_Optional: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 96 | llvm_unreachable("Optional strings cannot be created from text"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 97 | |
| 98 | case CK_LeftParen: |
| 99 | this->Text = "("; |
| 100 | break; |
| 101 | |
| 102 | case CK_RightParen: |
| 103 | this->Text = ")"; |
| 104 | break; |
| 105 | |
| 106 | case CK_LeftBracket: |
| 107 | this->Text = "["; |
| 108 | break; |
| 109 | |
| 110 | case CK_RightBracket: |
| 111 | this->Text = "]"; |
| 112 | break; |
| 113 | |
| 114 | case CK_LeftBrace: |
| 115 | this->Text = "{"; |
| 116 | break; |
| 117 | |
| 118 | case CK_RightBrace: |
| 119 | this->Text = "}"; |
| 120 | break; |
| 121 | |
| 122 | case CK_LeftAngle: |
| 123 | this->Text = "<"; |
| 124 | break; |
| 125 | |
| 126 | case CK_RightAngle: |
| 127 | this->Text = ">"; |
| 128 | break; |
| 129 | |
| 130 | case CK_Comma: |
| 131 | this->Text = ", "; |
| 132 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 133 | |
| 134 | case CK_Colon: |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 135 | this->Text = ":"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 136 | break; |
| 137 | |
| 138 | case CK_SemiColon: |
| 139 | this->Text = ";"; |
| 140 | break; |
| 141 | |
| 142 | case CK_Equal: |
| 143 | this->Text = " = "; |
| 144 | break; |
| 145 | |
| 146 | case CK_HorizontalSpace: |
| 147 | this->Text = " "; |
| 148 | break; |
| 149 | |
| 150 | case CK_VerticalSpace: |
| 151 | this->Text = "\n"; |
| 152 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 153 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 157 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 158 | return Chunk(CK_Text, Text); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 162 | CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 163 | Chunk Result; |
| 164 | Result.Kind = CK_Optional; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 165 | Result.Optional = Optional; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 166 | return Result; |
| 167 | } |
| 168 | |
| 169 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 170 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 171 | return Chunk(CK_Placeholder, Placeholder); |
| 172 | } |
| 173 | |
| 174 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 175 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 176 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 179 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 180 | CodeCompletionString::Chunk::CreateResultType(const char *ResultType) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 181 | return Chunk(CK_ResultType, ResultType); |
| 182 | } |
| 183 | |
| 184 | CodeCompletionString::Chunk |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 185 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 186 | const char *CurrentParameter) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 187 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 188 | } |
| 189 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 190 | CodeCompletionString::CodeCompletionString(const Chunk *Chunks, |
| 191 | unsigned NumChunks, |
| 192 | unsigned Priority, |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 193 | CXAvailabilityKind Availability, |
| 194 | const char **Annotations, |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 195 | unsigned NumAnnotations, |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 196 | StringRef ParentName, |
| 197 | const char *BriefComment) |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 198 | : NumChunks(NumChunks), NumAnnotations(NumAnnotations), |
Argyrios Kyrtzidis | 526d244 | 2012-09-26 16:39:56 +0000 | [diff] [blame] | 199 | Priority(Priority), Availability(Availability), |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 200 | ParentName(ParentName), BriefComment(BriefComment) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 201 | { |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 202 | assert(NumChunks <= 0xffff); |
| 203 | assert(NumAnnotations <= 0xffff); |
| 204 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 205 | Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1); |
| 206 | for (unsigned I = 0; I != NumChunks; ++I) |
| 207 | StoredChunks[I] = Chunks[I]; |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 208 | |
| 209 | const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks); |
| 210 | for (unsigned I = 0; I != NumAnnotations; ++I) |
| 211 | StoredAnnotations[I] = Annotations[I]; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 214 | unsigned CodeCompletionString::getAnnotationCount() const { |
| 215 | return NumAnnotations; |
| 216 | } |
| 217 | |
| 218 | const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const { |
| 219 | if (AnnotationNr < NumAnnotations) |
| 220 | return reinterpret_cast<const char * const*>(end())[AnnotationNr]; |
| 221 | else |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 226 | std::string CodeCompletionString::getAsString() const { |
| 227 | std::string Result; |
| 228 | llvm::raw_string_ostream OS(Result); |
| 229 | |
| 230 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
| 231 | switch (C->Kind) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 232 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 233 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 234 | |
| 235 | case CK_Informative: |
| 236 | case CK_ResultType: |
| 237 | OS << "[#" << C->Text << "#]"; |
| 238 | break; |
| 239 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 240 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 241 | default: OS << C->Text; break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
Dan Gohman | 6bdeb40 | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 244 | return OS.str(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 247 | const char *CodeCompletionString::getTypedText() const { |
| 248 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) |
| 249 | if (C->Kind == CK_TypedText) |
| 250 | return C->Text; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | const char *CodeCompletionAllocator::CopyString(StringRef String) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 256 | char *Mem = (char *)Allocate(String.size() + 1, 1); |
| 257 | std::copy(String.begin(), String.end(), Mem); |
| 258 | Mem[String.size()] = 0; |
| 259 | return Mem; |
| 260 | } |
| 261 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | const char *CodeCompletionAllocator::CopyString(Twine String) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 263 | // FIXME: It would be more efficient to teach Twine to tell us its size and |
| 264 | // then add a routine there to fill in an allocated char* with the contents |
| 265 | // of the string. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 266 | SmallString<128> Data; |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 267 | return CopyString(String.toStringRef(Data)); |
| 268 | } |
| 269 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 270 | StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) { |
| 271 | const NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 272 | if (!ND) |
| 273 | return StringRef(); |
| 274 | |
| 275 | // Check whether we've already cached the parent name. |
| 276 | StringRef &CachedParentName = ParentNames[DC]; |
| 277 | if (!CachedParentName.empty()) |
| 278 | return CachedParentName; |
| 279 | |
| 280 | // If we already processed this DeclContext and assigned empty to it, the |
| 281 | // data pointer will be non-null. |
| 282 | if (CachedParentName.data() != 0) |
| 283 | return StringRef(); |
| 284 | |
| 285 | // Find the interesting names. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 286 | SmallVector<const DeclContext *, 2> Contexts; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 287 | while (DC && !DC->isFunctionOrMethod()) { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 288 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 289 | if (ND->getIdentifier()) |
| 290 | Contexts.push_back(DC); |
| 291 | } |
| 292 | |
| 293 | DC = DC->getParent(); |
| 294 | } |
| 295 | |
| 296 | { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 297 | SmallString<128> S; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 298 | llvm::raw_svector_ostream OS(S); |
| 299 | bool First = true; |
| 300 | for (unsigned I = Contexts.size(); I != 0; --I) { |
| 301 | if (First) |
| 302 | First = false; |
| 303 | else { |
| 304 | OS << "::"; |
| 305 | } |
| 306 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 307 | const DeclContext *CurDC = Contexts[I-1]; |
| 308 | if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC)) |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 309 | CurDC = CatImpl->getCategoryDecl(); |
| 310 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 311 | if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) { |
| 312 | const ObjCInterfaceDecl *Interface = Cat->getClassInterface(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 313 | if (!Interface) { |
| 314 | // Assign an empty StringRef but with non-null data to distinguish |
| 315 | // between empty because we didn't process the DeclContext yet. |
| 316 | CachedParentName = StringRef((const char *)~0U, 0); |
| 317 | return StringRef(); |
| 318 | } |
| 319 | |
| 320 | OS << Interface->getName() << '(' << Cat->getName() << ')'; |
| 321 | } else { |
| 322 | OS << cast<NamedDecl>(CurDC)->getName(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | CachedParentName = AllocatorRef->CopyString(OS.str()); |
| 327 | } |
| 328 | |
| 329 | return CachedParentName; |
| 330 | } |
| 331 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 332 | CodeCompletionString *CodeCompletionBuilder::TakeString() { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 333 | void *Mem = getAllocator().Allocate( |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 334 | sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() |
| 335 | + sizeof(const char *) * Annotations.size(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 336 | llvm::alignOf<CodeCompletionString>()); |
| 337 | CodeCompletionString *Result |
| 338 | = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(), |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 339 | Priority, Availability, |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 340 | Annotations.data(), Annotations.size(), |
Argyrios Kyrtzidis | 526d244 | 2012-09-26 16:39:56 +0000 | [diff] [blame] | 341 | ParentName, BriefComment); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 342 | Chunks.clear(); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 343 | return Result; |
| 344 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 345 | |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 346 | void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) { |
| 347 | Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text)); |
| 348 | } |
| 349 | |
| 350 | void CodeCompletionBuilder::AddTextChunk(const char *Text) { |
| 351 | Chunks.push_back(Chunk::CreateText(Text)); |
| 352 | } |
| 353 | |
| 354 | void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) { |
| 355 | Chunks.push_back(Chunk::CreateOptional(Optional)); |
| 356 | } |
| 357 | |
| 358 | void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) { |
| 359 | Chunks.push_back(Chunk::CreatePlaceholder(Placeholder)); |
| 360 | } |
| 361 | |
| 362 | void CodeCompletionBuilder::AddInformativeChunk(const char *Text) { |
| 363 | Chunks.push_back(Chunk::CreateInformative(Text)); |
| 364 | } |
| 365 | |
| 366 | void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) { |
| 367 | Chunks.push_back(Chunk::CreateResultType(ResultType)); |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) { |
| 372 | Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter)); |
| 373 | } |
| 374 | |
| 375 | void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK, |
| 376 | const char *Text) { |
| 377 | Chunks.push_back(Chunk(CK, Text)); |
| 378 | } |
| 379 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 380 | void CodeCompletionBuilder::addParentContext(const DeclContext *DC) { |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 381 | if (DC->isTranslationUnit()) { |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | |
| 385 | if (DC->isFunctionOrMethod()) |
| 386 | return; |
| 387 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 388 | const NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 389 | if (!ND) |
| 390 | return; |
| 391 | |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 392 | ParentName = getCodeCompletionTUInfo().getParentName(DC); |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 395 | void CodeCompletionBuilder::addBriefComment(StringRef Comment) { |
| 396 | BriefComment = Allocator.CopyString(Comment); |
| 397 | } |
| 398 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 400 | // Code completion overload candidate implementation |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | FunctionDecl * |
| 403 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 404 | if (getKind() == CK_Function) |
| 405 | return Function; |
| 406 | else if (getKind() == CK_FunctionTemplate) |
| 407 | return FunctionTemplate->getTemplatedDecl(); |
| 408 | else |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | const FunctionType * |
| 413 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 414 | switch (Kind) { |
| 415 | case CK_Function: |
| 416 | return Function->getType()->getAs<FunctionType>(); |
| 417 | |
| 418 | case CK_FunctionTemplate: |
| 419 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 420 | ->getAs<FunctionType>(); |
| 421 | |
| 422 | case CK_FunctionType: |
| 423 | return Type; |
| 424 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 425 | |
| 426 | llvm_unreachable("Invalid CandidateKind!"); |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 430 | // Code completion consumer implementation |
| 431 | //===----------------------------------------------------------------------===// |
| 432 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 433 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 434 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 435 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 436 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 437 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 438 | CodeCompletionResult *Results, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 439 | unsigned NumResults) { |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 440 | std::stable_sort(Results, Results + NumResults); |
| 441 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 442 | // Print the results. |
| 443 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 444 | OS << "COMPLETION: "; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 445 | switch (Results[I].Kind) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 446 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 447 | OS << *Results[I].Declaration; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 448 | if (Results[I].Hidden) |
| 449 | OS << " (Hidden)"; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 450 | if (CodeCompletionString *CCS |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 451 | = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(), |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 452 | CCTUInfo, |
| 453 | includeBriefComments())) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 454 | OS << " : " << CCS->getAsString(); |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 455 | if (const char *BriefComment = CCS->getBriefComment()) |
| 456 | OS << " : " << BriefComment; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 459 | OS << '\n'; |
| 460 | break; |
| 461 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 462 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 463 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 464 | break; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 465 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 466 | case CodeCompletionResult::RK_Macro: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 467 | OS << Results[I].Macro->getName(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 468 | if (CodeCompletionString *CCS |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 469 | = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(), |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 470 | CCTUInfo, |
| 471 | includeBriefComments())) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 472 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 473 | } |
| 474 | OS << '\n'; |
| 475 | break; |
| 476 | } |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 477 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 478 | case CodeCompletionResult::RK_Pattern: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 479 | OS << "Pattern : " |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 480 | << Results[I].Pattern->getAsString() << '\n'; |
| 481 | break; |
| 482 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 485 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 486 | |
| 487 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 488 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 489 | unsigned CurrentArg, |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 490 | OverloadCandidate *Candidates, |
| 491 | unsigned NumCandidates) { |
| 492 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 493 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 494 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef, |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 495 | getAllocator(), CCTUInfo)) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 496 | OS << "OVERLOAD: " << CCS->getAsString() << "\n"; |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 499 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 8e42f27 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 501 | /// \brief Retrieve the effective availability of the given declaration. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 502 | static AvailabilityResult getDeclAvailability(const Decl *D) { |
Douglas Gregor | 8e42f27 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 503 | AvailabilityResult AR = D->getAvailability(); |
| 504 | if (isa<EnumConstantDecl>(D)) |
| 505 | AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability()); |
| 506 | return AR; |
| 507 | } |
| 508 | |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 509 | void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) { |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 510 | switch (Kind) { |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 511 | case RK_Pattern: |
| 512 | if (!Declaration) { |
| 513 | // Do nothing: Patterns can come with cursor kinds! |
| 514 | break; |
| 515 | } |
| 516 | // Fall through |
| 517 | |
Douglas Gregor | 8e42f27 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 518 | case RK_Declaration: { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 519 | // Set the availability based on attributes. |
Douglas Gregor | 8e42f27 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 520 | switch (getDeclAvailability(Declaration)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 521 | case AR_Available: |
| 522 | case AR_NotYetIntroduced: |
| 523 | Availability = CXAvailability_Available; |
| 524 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 526 | case AR_Deprecated: |
| 527 | Availability = CXAvailability_Deprecated; |
| 528 | break; |
| 529 | |
| 530 | case AR_Unavailable: |
| 531 | Availability = CXAvailability_NotAvailable; |
| 532 | break; |
| 533 | } |
| 534 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 535 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 536 | if (Function->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 537 | Availability = CXAvailability_NotAvailable; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 538 | |
| 539 | CursorKind = getCursorKindForDecl(Declaration); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 540 | if (CursorKind == CXCursor_UnexposedDecl) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 541 | // FIXME: Forward declarations of Objective-C classes and protocols |
| 542 | // are not directly exposed, but we want code completion to treat them |
| 543 | // like a definition. |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 544 | if (isa<ObjCInterfaceDecl>(Declaration)) |
| 545 | CursorKind = CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 546 | else if (isa<ObjCProtocolDecl>(Declaration)) |
| 547 | CursorKind = CXCursor_ObjCProtocolDecl; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 548 | else |
| 549 | CursorKind = CXCursor_NotImplemented; |
| 550 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 551 | break; |
Douglas Gregor | 8e42f27 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 552 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 553 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 554 | case RK_Macro: |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 555 | case RK_Keyword: |
Benjamin Kramer | e5f8372 | 2012-05-20 14:19:46 +0000 | [diff] [blame] | 556 | llvm_unreachable("Macro and keyword kinds are handled by the constructors"); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 557 | } |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 558 | |
| 559 | if (!Accessible) |
| 560 | Availability = CXAvailability_NotAccessible; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 561 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 562 | |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 563 | /// \brief Retrieve the name that should be used to order a result. |
| 564 | /// |
| 565 | /// If the name needs to be constructed as a string, that string will be |
| 566 | /// saved into Saved and the returned StringRef will refer to it. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 567 | static StringRef getOrderedName(const CodeCompletionResult &R, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 568 | std::string &Saved) { |
| 569 | switch (R.Kind) { |
| 570 | case CodeCompletionResult::RK_Keyword: |
| 571 | return R.Keyword; |
| 572 | |
| 573 | case CodeCompletionResult::RK_Pattern: |
| 574 | return R.Pattern->getTypedText(); |
| 575 | |
| 576 | case CodeCompletionResult::RK_Macro: |
| 577 | return R.Macro->getName(); |
| 578 | |
| 579 | case CodeCompletionResult::RK_Declaration: |
| 580 | // Handle declarations below. |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | DeclarationName Name = R.Declaration->getDeclName(); |
| 585 | |
| 586 | // If the name is a simple identifier (by far the common case), or a |
| 587 | // zero-argument selector, just return a reference to that identifier. |
| 588 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 589 | return Id->getName(); |
| 590 | if (Name.isObjCZeroArgSelector()) |
| 591 | if (IdentifierInfo *Id |
| 592 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 593 | return Id->getName(); |
| 594 | |
| 595 | Saved = Name.getAsString(); |
| 596 | return Saved; |
| 597 | } |
| 598 | |
| 599 | bool clang::operator<(const CodeCompletionResult &X, |
| 600 | const CodeCompletionResult &Y) { |
| 601 | std::string XSaved, YSaved; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 602 | StringRef XStr = getOrderedName(X, XSaved); |
| 603 | StringRef YStr = getOrderedName(Y, YSaved); |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 604 | int cmp = XStr.compare_lower(YStr); |
| 605 | if (cmp) |
| 606 | return cmp < 0; |
| 607 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 608 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 609 | cmp = XStr.compare(YStr); |
| 610 | if (cmp) |
| 611 | return cmp < 0; |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 612 | |
| 613 | return false; |
| 614 | } |