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