Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 1 | //===- CodeCompleteConsumer.cpp - Code Completion Interface ---------------===// |
Douglas Gregor | 2436e71 | 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 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang-c/Index.h" |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclBase.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclarationName.h" |
| 21 | #include "clang/AST/Type.h" |
| 22 | #include "clang/Basic/IdentifierTable.h" |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Ilya Biryukov | 2fab235 | 2018-08-30 13:08:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Sema.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallVector.h" |
| 27 | #include "llvm/ADT/StringRef.h" |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Twine.h" |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Casting.h" |
| 30 | #include "llvm/Support/Compiler.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
Ilya Biryukov | 2fab235 | 2018-08-30 13:08:03 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FormatVariadic.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include <algorithm> |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 35 | #include <cassert> |
| 36 | #include <cstdint> |
| 37 | #include <string> |
Douglas Gregor | ab6ccb5 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 42 | // Code completion context implementation |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | bool CodeCompletionContext::wantConstructorResults() const { |
Ilya Biryukov | 3289ab2 | 2018-02-19 13:53:49 +0000 | [diff] [blame] | 46 | switch (CCKind) { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 47 | case CCC_Recovery: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 48 | case CCC_Statement: |
| 49 | case CCC_Expression: |
| 50 | case CCC_ObjCMessageReceiver: |
| 51 | case CCC_ParenthesizedExpression: |
| 52 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 54 | case CCC_TopLevel: |
| 55 | case CCC_ObjCInterface: |
| 56 | case CCC_ObjCImplementation: |
| 57 | case CCC_ObjCIvarList: |
| 58 | case CCC_ClassStructUnion: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 59 | case CCC_DotMemberAccess: |
| 60 | case CCC_ArrowMemberAccess: |
| 61 | case CCC_ObjCPropertyAccess: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 62 | case CCC_EnumTag: |
| 63 | case CCC_UnionTag: |
| 64 | case CCC_ClassOrStructTag: |
| 65 | case CCC_ObjCProtocolName: |
| 66 | case CCC_Namespace: |
| 67 | case CCC_Type: |
| 68 | case CCC_Name: |
| 69 | case CCC_PotentiallyQualifiedName: |
| 70 | case CCC_MacroName: |
| 71 | case CCC_MacroNameUse: |
| 72 | case CCC_PreprocessorExpression: |
| 73 | case CCC_PreprocessorDirective: |
| 74 | case CCC_NaturalLanguage: |
| 75 | case CCC_SelectorName: |
| 76 | case CCC_TypeQualifiers: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 77 | case CCC_Other: |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 78 | case CCC_OtherWithMacros: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 79 | case CCC_ObjCInstanceMessage: |
| 80 | case CCC_ObjCClassMessage: |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 81 | case CCC_ObjCInterfaceName: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 82 | case CCC_ObjCCategoryName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 85 | |
| 86 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Ilya Biryukov | 3289ab2 | 2018-02-19 13:53:49 +0000 | [diff] [blame] | 89 | StringRef clang::getCompletionKindString(CodeCompletionContext::Kind Kind) { |
| 90 | using CCKind = CodeCompletionContext::Kind; |
Ilya Biryukov | 27d8258 | 2018-02-19 12:35:33 +0000 | [diff] [blame] | 91 | switch (Kind) { |
| 92 | case CCKind::CCC_Other: |
| 93 | return "Other"; |
| 94 | case CCKind::CCC_OtherWithMacros: |
| 95 | return "OtherWithMacros"; |
| 96 | case CCKind::CCC_TopLevel: |
| 97 | return "TopLevel"; |
| 98 | case CCKind::CCC_ObjCInterface: |
| 99 | return "ObjCInterface"; |
| 100 | case CCKind::CCC_ObjCImplementation: |
| 101 | return "ObjCImplementation"; |
| 102 | case CCKind::CCC_ObjCIvarList: |
| 103 | return "ObjCIvarList"; |
| 104 | case CCKind::CCC_ClassStructUnion: |
| 105 | return "ClassStructUnion"; |
| 106 | case CCKind::CCC_Statement: |
| 107 | return "Statement"; |
| 108 | case CCKind::CCC_Expression: |
| 109 | return "Expression"; |
| 110 | case CCKind::CCC_ObjCMessageReceiver: |
| 111 | return "ObjCMessageReceiver"; |
| 112 | case CCKind::CCC_DotMemberAccess: |
| 113 | return "DotMemberAccess"; |
| 114 | case CCKind::CCC_ArrowMemberAccess: |
| 115 | return "ArrowMemberAccess"; |
| 116 | case CCKind::CCC_ObjCPropertyAccess: |
| 117 | return "ObjCPropertyAccess"; |
| 118 | case CCKind::CCC_EnumTag: |
| 119 | return "EnumTag"; |
| 120 | case CCKind::CCC_UnionTag: |
| 121 | return "UnionTag"; |
| 122 | case CCKind::CCC_ClassOrStructTag: |
| 123 | return "ClassOrStructTag"; |
| 124 | case CCKind::CCC_ObjCProtocolName: |
| 125 | return "ObjCProtocolName"; |
| 126 | case CCKind::CCC_Namespace: |
| 127 | return "Namespace"; |
| 128 | case CCKind::CCC_Type: |
| 129 | return "Type"; |
| 130 | case CCKind::CCC_Name: |
| 131 | return "Name"; |
| 132 | case CCKind::CCC_PotentiallyQualifiedName: |
| 133 | return "PotentiallyQualifiedName"; |
| 134 | case CCKind::CCC_MacroName: |
| 135 | return "MacroName"; |
| 136 | case CCKind::CCC_MacroNameUse: |
| 137 | return "MacroNameUse"; |
| 138 | case CCKind::CCC_PreprocessorExpression: |
| 139 | return "PreprocessorExpression"; |
| 140 | case CCKind::CCC_PreprocessorDirective: |
| 141 | return "PreprocessorDirective"; |
| 142 | case CCKind::CCC_NaturalLanguage: |
| 143 | return "NaturalLanguage"; |
| 144 | case CCKind::CCC_SelectorName: |
| 145 | return "SelectorName"; |
| 146 | case CCKind::CCC_TypeQualifiers: |
| 147 | return "TypeQualifiers"; |
| 148 | case CCKind::CCC_ParenthesizedExpression: |
| 149 | return "ParenthesizedExpression"; |
| 150 | case CCKind::CCC_ObjCInstanceMessage: |
| 151 | return "ObjCInstanceMessage"; |
| 152 | case CCKind::CCC_ObjCClassMessage: |
| 153 | return "ObjCClassMessage"; |
| 154 | case CCKind::CCC_ObjCInterfaceName: |
| 155 | return "ObjCInterfaceName"; |
| 156 | case CCKind::CCC_ObjCCategoryName: |
| 157 | return "ObjCCategoryName"; |
| 158 | case CCKind::CCC_Recovery: |
| 159 | return "Recovery"; |
| 160 | } |
| 161 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
| 162 | } |
| 163 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 164 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 165 | // Code completion string implementation |
| 166 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 167 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 168 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 169 | : Kind(Kind), Text("") { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 170 | switch (Kind) { |
| 171 | case CK_TypedText: |
| 172 | case CK_Text: |
| 173 | case CK_Placeholder: |
| 174 | case CK_Informative: |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 175 | case CK_ResultType: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 176 | case CK_CurrentParameter: |
| 177 | this->Text = Text; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 178 | break; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 179 | |
| 180 | case CK_Optional: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 181 | llvm_unreachable("Optional strings cannot be created from text"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 183 | case CK_LeftParen: |
| 184 | this->Text = "("; |
| 185 | break; |
| 186 | |
| 187 | case CK_RightParen: |
| 188 | this->Text = ")"; |
| 189 | break; |
| 190 | |
| 191 | case CK_LeftBracket: |
| 192 | this->Text = "["; |
| 193 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 194 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 195 | case CK_RightBracket: |
| 196 | this->Text = "]"; |
| 197 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 199 | case CK_LeftBrace: |
| 200 | this->Text = "{"; |
| 201 | break; |
| 202 | |
| 203 | case CK_RightBrace: |
| 204 | this->Text = "}"; |
| 205 | break; |
| 206 | |
| 207 | case CK_LeftAngle: |
| 208 | this->Text = "<"; |
| 209 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 211 | case CK_RightAngle: |
| 212 | this->Text = ">"; |
| 213 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 215 | case CK_Comma: |
| 216 | this->Text = ", "; |
| 217 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 218 | |
| 219 | case CK_Colon: |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 220 | this->Text = ":"; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 221 | break; |
| 222 | |
| 223 | case CK_SemiColon: |
| 224 | this->Text = ";"; |
| 225 | break; |
| 226 | |
| 227 | case CK_Equal: |
| 228 | this->Text = " = "; |
| 229 | break; |
| 230 | |
| 231 | case CK_HorizontalSpace: |
| 232 | this->Text = " "; |
| 233 | break; |
| 234 | |
| 235 | case CK_VerticalSpace: |
| 236 | this->Text = "\n"; |
| 237 | break; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 238 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 242 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 243 | return Chunk(CK_Text, Text); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 246 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 247 | CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 248 | Chunk Result; |
| 249 | Result.Kind = CK_Optional; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 250 | Result.Optional = Optional; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 251 | return Result; |
| 252 | } |
| 253 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 254 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 255 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 256 | return Chunk(CK_Placeholder, Placeholder); |
| 257 | } |
| 258 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 259 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 260 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 261 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 264 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 265 | CodeCompletionString::Chunk::CreateResultType(const char *ResultType) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 266 | return Chunk(CK_ResultType, ResultType); |
| 267 | } |
| 268 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 269 | CodeCompletionString::Chunk |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 270 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 271 | const char *CurrentParameter) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 272 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 273 | } |
| 274 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 275 | CodeCompletionString::CodeCompletionString(const Chunk *Chunks, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 276 | unsigned NumChunks, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 277 | unsigned Priority, |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 278 | CXAvailabilityKind Availability, |
| 279 | const char **Annotations, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 280 | unsigned NumAnnotations, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 281 | StringRef ParentName, |
| 282 | const char *BriefComment) |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 283 | : NumChunks(NumChunks), NumAnnotations(NumAnnotations), |
| 284 | Priority(Priority), Availability(Availability), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 285 | ParentName(ParentName), BriefComment(BriefComment) { |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 286 | assert(NumChunks <= 0xffff); |
| 287 | assert(NumAnnotations <= 0xffff); |
| 288 | |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 289 | Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 290 | for (unsigned I = 0; I != NumChunks; ++I) |
| 291 | StoredChunks[I] = Chunks[I]; |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 292 | |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 293 | const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks); |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 294 | for (unsigned I = 0; I != NumAnnotations; ++I) |
| 295 | StoredAnnotations[I] = Annotations[I]; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 298 | unsigned CodeCompletionString::getAnnotationCount() const { |
| 299 | return NumAnnotations; |
| 300 | } |
| 301 | |
| 302 | const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const { |
| 303 | if (AnnotationNr < NumAnnotations) |
| 304 | return reinterpret_cast<const char * const*>(end())[AnnotationNr]; |
| 305 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 306 | return nullptr; |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 309 | std::string CodeCompletionString::getAsString() const { |
| 310 | std::string Result; |
| 311 | llvm::raw_string_ostream OS(Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 313 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
| 314 | switch (C->Kind) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 315 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 316 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 317 | |
| 318 | case CK_Informative: |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 319 | case CK_ResultType: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 320 | OS << "[#" << C->Text << "#]"; |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 321 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 323 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 324 | default: OS << C->Text; break; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Dan Gohman | 4888f1a | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 327 | return OS.str(); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 330 | const char *CodeCompletionString::getTypedText() const { |
| 331 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) |
| 332 | if (C->Kind == CK_TypedText) |
| 333 | return C->Text; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 334 | |
| 335 | return nullptr; |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 338 | const char *CodeCompletionAllocator::CopyString(const Twine &String) { |
| 339 | SmallString<128> Data; |
| 340 | StringRef Ref = String.toStringRef(Data); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 341 | // FIXME: It would be more efficient to teach Twine to tell us its size and |
| 342 | // then add a routine there to fill in an allocated char* with the contents |
| 343 | // of the string. |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 344 | char *Mem = (char *)Allocate(Ref.size() + 1, 1); |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 345 | std::copy(Ref.begin(), Ref.end(), Mem); |
| 346 | Mem[Ref.size()] = 0; |
| 347 | return Mem; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 350 | StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) { |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 351 | const NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 352 | if (!ND) |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 353 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 354 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 355 | // Check whether we've already cached the parent name. |
| 356 | StringRef &CachedParentName = ParentNames[DC]; |
| 357 | if (!CachedParentName.empty()) |
| 358 | return CachedParentName; |
| 359 | |
| 360 | // If we already processed this DeclContext and assigned empty to it, the |
| 361 | // data pointer will be non-null. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 362 | if (CachedParentName.data() != nullptr) |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 363 | return {}; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 364 | |
| 365 | // Find the interesting names. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 366 | SmallVector<const DeclContext *, 2> Contexts; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 367 | while (DC && !DC->isFunctionOrMethod()) { |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 368 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 369 | if (ND->getIdentifier()) |
| 370 | Contexts.push_back(DC); |
| 371 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 372 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 373 | DC = DC->getParent(); |
| 374 | } |
| 375 | |
| 376 | { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 377 | SmallString<128> S; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 378 | llvm::raw_svector_ostream OS(S); |
| 379 | bool First = true; |
| 380 | for (unsigned I = Contexts.size(); I != 0; --I) { |
| 381 | if (First) |
| 382 | First = false; |
| 383 | else { |
| 384 | OS << "::"; |
| 385 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 386 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 387 | const DeclContext *CurDC = Contexts[I-1]; |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 388 | if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC)) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 389 | CurDC = CatImpl->getCategoryDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 390 | |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 391 | if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 392 | const ObjCInterfaceDecl *Interface = Cat->getClassInterface(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 393 | if (!Interface) { |
| 394 | // Assign an empty StringRef but with non-null data to distinguish |
| 395 | // between empty because we didn't process the DeclContext yet. |
Reid Kleckner | d16cebe | 2016-02-10 19:09:15 +0000 | [diff] [blame] | 396 | CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0); |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 397 | return {}; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 398 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 399 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 400 | OS << Interface->getName() << '(' << Cat->getName() << ')'; |
| 401 | } else { |
| 402 | OS << cast<NamedDecl>(CurDC)->getName(); |
| 403 | } |
| 404 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 405 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 406 | CachedParentName = AllocatorRef->CopyString(OS.str()); |
| 407 | } |
| 408 | |
| 409 | return CachedParentName; |
| 410 | } |
| 411 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 412 | CodeCompletionString *CodeCompletionBuilder::TakeString() { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 413 | void *Mem = getAllocator().Allocate( |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 414 | sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() + |
| 415 | sizeof(const char *) * Annotations.size(), |
| 416 | alignof(CodeCompletionString)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 417 | CodeCompletionString *Result |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 418 | = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(), |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 419 | Priority, Availability, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 420 | Annotations.data(), Annotations.size(), |
Argyrios Kyrtzidis | 9ae3956 | 2012-09-26 16:39:56 +0000 | [diff] [blame] | 421 | ParentName, BriefComment); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 422 | Chunks.clear(); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 423 | return Result; |
| 424 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 425 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 426 | void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) { |
| 427 | Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text)); |
| 428 | } |
| 429 | |
| 430 | void CodeCompletionBuilder::AddTextChunk(const char *Text) { |
| 431 | Chunks.push_back(Chunk::CreateText(Text)); |
| 432 | } |
| 433 | |
| 434 | void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) { |
| 435 | Chunks.push_back(Chunk::CreateOptional(Optional)); |
| 436 | } |
| 437 | |
| 438 | void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) { |
| 439 | Chunks.push_back(Chunk::CreatePlaceholder(Placeholder)); |
| 440 | } |
| 441 | |
| 442 | void CodeCompletionBuilder::AddInformativeChunk(const char *Text) { |
| 443 | Chunks.push_back(Chunk::CreateInformative(Text)); |
| 444 | } |
| 445 | |
| 446 | void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) { |
| 447 | Chunks.push_back(Chunk::CreateResultType(ResultType)); |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) { |
| 452 | Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter)); |
| 453 | } |
| 454 | |
| 455 | void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK, |
| 456 | const char *Text) { |
| 457 | Chunks.push_back(Chunk(CK, Text)); |
| 458 | } |
| 459 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 460 | void CodeCompletionBuilder::addParentContext(const DeclContext *DC) { |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 461 | if (DC->isTranslationUnit()) |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 462 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 464 | if (DC->isFunctionOrMethod()) |
| 465 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 466 | |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 467 | const NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 468 | if (!ND) |
| 469 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 470 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 471 | ParentName = getCodeCompletionTUInfo().getParentName(DC); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 474 | void CodeCompletionBuilder::addBriefComment(StringRef Comment) { |
| 475 | BriefComment = Allocator.CopyString(Comment); |
| 476 | } |
| 477 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 478 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 479 | // Code completion overload candidate implementation |
| 480 | //===----------------------------------------------------------------------===// |
| 481 | FunctionDecl * |
| 482 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 483 | if (getKind() == CK_Function) |
| 484 | return Function; |
| 485 | else if (getKind() == CK_FunctionTemplate) |
| 486 | return FunctionTemplate->getTemplatedDecl(); |
| 487 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 488 | return nullptr; |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | const FunctionType * |
| 492 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 493 | switch (Kind) { |
| 494 | case CK_Function: |
| 495 | return Function->getType()->getAs<FunctionType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 497 | case CK_FunctionTemplate: |
| 498 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 499 | ->getAs<FunctionType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 501 | case CK_FunctionType: |
| 502 | return Type; |
| 503 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 504 | |
| 505 | llvm_unreachable("Invalid CandidateKind!"); |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 509 | // Code completion consumer implementation |
| 510 | //===----------------------------------------------------------------------===// |
| 511 | |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 512 | CodeCompleteConsumer::~CodeCompleteConsumer() = default; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 513 | |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 514 | bool PrintingCodeCompleteConsumer::isResultFilteredOut(StringRef Filter, |
| 515 | CodeCompletionResult Result) { |
| 516 | switch (Result.Kind) { |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 517 | case CodeCompletionResult::RK_Declaration: |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 518 | return !(Result.Declaration->getIdentifier() && |
| 519 | Result.Declaration->getIdentifier()->getName().startswith(Filter)); |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 520 | case CodeCompletionResult::RK_Keyword: |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 521 | return !StringRef(Result.Keyword).startswith(Filter); |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 522 | case CodeCompletionResult::RK_Macro: |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 523 | return !Result.Macro->getName().startswith(Filter); |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 524 | case CodeCompletionResult::RK_Pattern: |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 525 | return !StringRef(Result.Pattern->getAsString()).startswith(Filter); |
| 526 | } |
Simon Pilgrim | e71a1f9 | 2016-07-27 16:41:56 +0000 | [diff] [blame] | 527 | llvm_unreachable("Unknown code completion result Kind."); |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 530 | void |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 531 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 532 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 533 | CodeCompletionResult *Results, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 534 | unsigned NumResults) { |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 535 | std::stable_sort(Results, Results + NumResults); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 536 | |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 537 | StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter(); |
| 538 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 539 | // Print the results. |
| 540 | for (unsigned I = 0; I != NumResults; ++I) { |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 541 | if(!Filter.empty() && isResultFilteredOut(Filter, Results[I])) |
| 542 | continue; |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 543 | OS << "COMPLETION: "; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 544 | switch (Results[I].Kind) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 545 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 546 | OS << *Results[I].Declaration; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 547 | if (Results[I].Hidden) |
| 548 | OS << " (Hidden)"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 549 | if (CodeCompletionString *CCS |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 550 | = Results[I].CreateCodeCompletionString(SemaRef, Context, |
| 551 | getAllocator(), |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 552 | CCTUInfo, |
| 553 | includeBriefComments())) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 554 | OS << " : " << CCS->getAsString(); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 555 | if (const char *BriefComment = CCS->getBriefComment()) |
| 556 | OS << " : " << BriefComment; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 557 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 558 | for (const FixItHint &FixIt : Results[I].FixIts) { |
| 559 | const SourceLocation BLoc = FixIt.RemoveRange.getBegin(); |
| 560 | const SourceLocation ELoc = FixIt.RemoveRange.getEnd(); |
| 561 | |
| 562 | SourceManager &SM = SemaRef.SourceMgr; |
| 563 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc); |
| 564 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc); |
| 565 | // Adjust for token ranges. |
| 566 | if (FixIt.RemoveRange.isTokenRange()) |
| 567 | EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, SemaRef.LangOpts); |
| 568 | |
| 569 | OS << " (requires fix-it:" |
| 570 | << " {" << SM.getLineNumber(BInfo.first, BInfo.second) << ':' |
| 571 | << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' |
| 572 | << SM.getLineNumber(EInfo.first, EInfo.second) << ':' |
| 573 | << SM.getColumnNumber(EInfo.first, EInfo.second) << "}" |
| 574 | << " to \"" << FixIt.CodeToInsert << "\")"; |
| 575 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 576 | OS << '\n'; |
| 577 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 578 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 579 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 580 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 581 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 582 | |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 583 | case CodeCompletionResult::RK_Macro: |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 584 | OS << Results[I].Macro->getName(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 585 | if (CodeCompletionString *CCS |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 586 | = Results[I].CreateCodeCompletionString(SemaRef, Context, |
| 587 | getAllocator(), |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 588 | CCTUInfo, |
| 589 | includeBriefComments())) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 590 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 591 | } |
| 592 | OS << '\n'; |
| 593 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 594 | |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame] | 595 | case CodeCompletionResult::RK_Pattern: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 596 | OS << "Pattern : " |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 597 | << Results[I].Pattern->getAsString() << '\n'; |
| 598 | break; |
| 599 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 600 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 601 | } |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 602 | |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 603 | // This function is used solely to preserve the former presentation of overloads |
| 604 | // by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString |
| 605 | // needs to be improved for printing the newer and more detailed overload |
| 606 | // chunks. |
| 607 | static std::string getOverloadAsString(const CodeCompletionString &CCS) { |
| 608 | std::string Result; |
| 609 | llvm::raw_string_ostream OS(Result); |
| 610 | |
| 611 | for (auto &C : CCS) { |
| 612 | switch (C.Kind) { |
| 613 | case CodeCompletionString::CK_Informative: |
| 614 | case CodeCompletionString::CK_ResultType: |
| 615 | OS << "[#" << C.Text << "#]"; |
| 616 | break; |
| 617 | |
| 618 | case CodeCompletionString::CK_CurrentParameter: |
| 619 | OS << "<#" << C.Text << "#>"; |
| 620 | break; |
| 621 | |
Kadir Cetinkaya | a32d253 | 2018-09-10 13:46:28 +0000 | [diff] [blame] | 622 | // FIXME: We can also print optional parameters of an overload. |
| 623 | case CodeCompletionString::CK_Optional: |
| 624 | break; |
| 625 | |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 626 | default: OS << C.Text; break; |
| 627 | } |
| 628 | } |
| 629 | return OS.str(); |
| 630 | } |
| 631 | |
Ilya Biryukov | 2fab235 | 2018-08-30 13:08:03 +0000 | [diff] [blame] | 632 | void PrintingCodeCompleteConsumer::ProcessOverloadCandidates( |
| 633 | Sema &SemaRef, unsigned CurrentArg, OverloadCandidate *Candidates, |
| 634 | unsigned NumCandidates, SourceLocation OpenParLoc) { |
| 635 | OS << "OPENING_PAREN_LOC: "; |
| 636 | OpenParLoc.print(OS, SemaRef.getSourceManager()); |
| 637 | OS << "\n"; |
| 638 | |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 639 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Ilya Biryukov | 2fab235 | 2018-08-30 13:08:03 +0000 | [diff] [blame] | 640 | if (CodeCompletionString *CCS = Candidates[I].CreateSignatureString( |
| 641 | CurrentArg, SemaRef, getAllocator(), CCTUInfo, |
| 642 | includeBriefComments())) { |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 643 | OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n"; |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 646 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 647 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 648 | /// Retrieve the effective availability of the given declaration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 649 | static AvailabilityResult getDeclAvailability(const Decl *D) { |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 650 | AvailabilityResult AR = D->getAvailability(); |
| 651 | if (isa<EnumConstantDecl>(D)) |
| 652 | AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability()); |
| 653 | return AR; |
| 654 | } |
| 655 | |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 656 | void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 657 | switch (Kind) { |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 658 | case RK_Pattern: |
| 659 | if (!Declaration) { |
| 660 | // Do nothing: Patterns can come with cursor kinds! |
| 661 | break; |
| 662 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 663 | LLVM_FALLTHROUGH; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 665 | case RK_Declaration: { |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 666 | // Set the availability based on attributes. |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 667 | switch (getDeclAvailability(Declaration)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 668 | case AR_Available: |
| 669 | case AR_NotYetIntroduced: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 670 | Availability = CXAvailability_Available; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 671 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 672 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 673 | case AR_Deprecated: |
| 674 | Availability = CXAvailability_Deprecated; |
| 675 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 677 | case AR_Unavailable: |
| 678 | Availability = CXAvailability_NotAvailable; |
| 679 | break; |
| 680 | } |
| 681 | |
Eugene Zelenko | 1e95bc0 | 2018-04-05 22:15:42 +0000 | [diff] [blame] | 682 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 683 | if (Function->isDeleted()) |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 684 | Availability = CXAvailability_NotAvailable; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 685 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 686 | CursorKind = getCursorKindForDecl(Declaration); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 687 | if (CursorKind == CXCursor_UnexposedDecl) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 688 | // FIXME: Forward declarations of Objective-C classes and protocols |
| 689 | // are not directly exposed, but we want code completion to treat them |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 690 | // like a definition. |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 691 | if (isa<ObjCInterfaceDecl>(Declaration)) |
| 692 | CursorKind = CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 693 | else if (isa<ObjCProtocolDecl>(Declaration)) |
| 694 | CursorKind = CXCursor_ObjCProtocolDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 695 | else |
| 696 | CursorKind = CXCursor_NotImplemented; |
| 697 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 698 | break; |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 699 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 700 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 701 | case RK_Macro: |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 702 | case RK_Keyword: |
Benjamin Kramer | c25c0b9 | 2012-05-20 14:19:46 +0000 | [diff] [blame] | 703 | llvm_unreachable("Macro and keyword kinds are handled by the constructors"); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 704 | } |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 705 | |
| 706 | if (!Accessible) |
| 707 | Availability = CXAvailability_NotAccessible; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 708 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 709 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 710 | /// Retrieve the name that should be used to order a result. |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 711 | /// |
| 712 | /// If the name needs to be constructed as a string, that string will be |
| 713 | /// saved into Saved and the returned StringRef will refer to it. |
Sam McCall | 1102a86 | 2017-11-15 09:15:06 +0000 | [diff] [blame] | 714 | StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const { |
| 715 | switch (Kind) { |
| 716 | case RK_Keyword: |
| 717 | return Keyword; |
| 718 | case RK_Pattern: |
| 719 | return Pattern->getTypedText(); |
| 720 | case RK_Macro: |
| 721 | return Macro->getName(); |
| 722 | case RK_Declaration: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 723 | // Handle declarations below. |
| 724 | break; |
| 725 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 726 | |
Sam McCall | 1102a86 | 2017-11-15 09:15:06 +0000 | [diff] [blame] | 727 | DeclarationName Name = Declaration->getDeclName(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 728 | |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 729 | // If the name is a simple identifier (by far the common case), or a |
| 730 | // zero-argument selector, just return a reference to that identifier. |
| 731 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 732 | return Id->getName(); |
| 733 | if (Name.isObjCZeroArgSelector()) |
| 734 | if (IdentifierInfo *Id |
| 735 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 736 | return Id->getName(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 737 | |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 738 | Saved = Name.getAsString(); |
| 739 | return Saved; |
| 740 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 741 | |
| 742 | bool clang::operator<(const CodeCompletionResult &X, |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 743 | const CodeCompletionResult &Y) { |
| 744 | std::string XSaved, YSaved; |
Sam McCall | 1102a86 | 2017-11-15 09:15:06 +0000 | [diff] [blame] | 745 | StringRef XStr = X.getOrderedName(XSaved); |
| 746 | StringRef YStr = Y.getOrderedName(YSaved); |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 747 | int cmp = XStr.compare_lower(YStr); |
| 748 | if (cmp) |
| 749 | return cmp < 0; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 751 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 752 | cmp = XStr.compare(YStr); |
| 753 | if (cmp) |
| 754 | return cmp < 0; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 756 | return false; |
| 757 | } |