Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 1 | //===- USRGeneration.cpp - Routines for USR generation --------------------===// |
| 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 | |
Argyrios Kyrtzidis | 15a2fcc | 2013-08-17 00:40:41 +0000 | [diff] [blame] | 10 | #include "clang/Index/USRGeneration.h" |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/AST/DeclTemplate.h" |
| 13 | #include "clang/AST/DeclVisitor.h" |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 14 | #include "clang/Lex/PreprocessingRecord.h" |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | using namespace clang; |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 19 | using namespace clang::index; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // USR generation. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 25 | /// \returns true on error. |
| 26 | static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc, |
| 27 | const SourceManager &SM, bool IncludeOffset) { |
| 28 | if (Loc.isInvalid()) { |
| 29 | return true; |
| 30 | } |
| 31 | Loc = SM.getExpansionLoc(Loc); |
| 32 | const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc); |
| 33 | const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); |
| 34 | if (FE) { |
| 35 | OS << llvm::sys::path::filename(FE->getName()); |
| 36 | } else { |
| 37 | // This case really isn't interesting. |
| 38 | return true; |
| 39 | } |
| 40 | if (IncludeOffset) { |
| 41 | // Use the offest into the FileID to represent the location. Using |
| 42 | // a line/column can cause us to look back at the original source file, |
| 43 | // which is expensive. |
| 44 | OS << '@' << Decomposed.second; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 49 | static StringRef GetExternalSourceContainer(const NamedDecl *D) { |
| 50 | if (!D) |
| 51 | return StringRef(); |
| 52 | if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) { |
| 53 | return attr->getDefinedIn(); |
| 54 | } |
| 55 | return StringRef(); |
| 56 | } |
| 57 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | class USRGenerator : public ConstDeclVisitor<USRGenerator> { |
| 60 | SmallVectorImpl<char> &Buf; |
| 61 | llvm::raw_svector_ostream Out; |
| 62 | bool IgnoreResults; |
| 63 | ASTContext *Context; |
| 64 | bool generatedLoc; |
| 65 | |
| 66 | llvm::DenseMap<const Type *, unsigned> TypeSubstitutions; |
| 67 | |
| 68 | public: |
| 69 | explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf) |
| 70 | : Buf(Buf), |
| 71 | Out(Buf), |
| 72 | IgnoreResults(false), |
| 73 | Context(Ctx), |
| 74 | generatedLoc(false) |
| 75 | { |
| 76 | // Add the USR space prefix. |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 77 | Out << getUSRSpacePrefix(); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool ignoreResults() const { return IgnoreResults; } |
| 81 | |
| 82 | // Visitation methods from generating USRs from AST elements. |
| 83 | void VisitDeclContext(const DeclContext *D); |
| 84 | void VisitFieldDecl(const FieldDecl *D); |
| 85 | void VisitFunctionDecl(const FunctionDecl *D); |
| 86 | void VisitNamedDecl(const NamedDecl *D); |
| 87 | void VisitNamespaceDecl(const NamespaceDecl *D); |
| 88 | void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); |
| 89 | void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); |
| 90 | void VisitClassTemplateDecl(const ClassTemplateDecl *D); |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 91 | void VisitObjCContainerDecl(const ObjCContainerDecl *CD, |
| 92 | const ObjCCategoryDecl *CatD = nullptr); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 93 | void VisitObjCMethodDecl(const ObjCMethodDecl *MD); |
| 94 | void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); |
| 95 | void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); |
| 96 | void VisitTagDecl(const TagDecl *D); |
| 97 | void VisitTypedefDecl(const TypedefDecl *D); |
| 98 | void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); |
| 99 | void VisitVarDecl(const VarDecl *D); |
| 100 | void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); |
| 101 | void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 102 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 103 | void VisitLinkageSpecDecl(const LinkageSpecDecl *D) { |
| 104 | IgnoreResults = true; |
| 105 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 106 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 107 | void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { |
| 108 | IgnoreResults = true; |
| 109 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 110 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 111 | void VisitUsingDecl(const UsingDecl *D) { |
| 112 | IgnoreResults = true; |
| 113 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 114 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 115 | void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) { |
| 116 | IgnoreResults = true; |
| 117 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 118 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 119 | void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) { |
| 120 | IgnoreResults = true; |
| 121 | } |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 122 | |
| 123 | bool ShouldGenerateLocation(const NamedDecl *D); |
| 124 | |
| 125 | bool isLocal(const NamedDecl *D) { |
Craig Topper | 236bde3 | 2014-05-26 06:21:51 +0000 | [diff] [blame] | 126 | return D->getParentFunctionOrMethod() != nullptr; |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 129 | void GenExtSymbolContainer(const NamedDecl *D); |
| 130 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 131 | /// Generate the string component containing the location of the |
| 132 | /// declaration. |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 133 | bool GenLoc(const Decl *D, bool IncludeOffset); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 134 | |
| 135 | /// String generation methods used both by the visitation methods |
| 136 | /// and from other clients that want to directly generate USRs. These |
| 137 | /// methods do not construct complete USRs (which incorporate the parents |
| 138 | /// of an AST element), but only the fragments concerning the AST element |
| 139 | /// itself. |
| 140 | |
| 141 | /// Generate a USR for an Objective-C class. |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 142 | void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn, |
| 143 | StringRef CategoryContextExtSymbolDefinedIn) { |
| 144 | generateUSRForObjCClass(cls, Out, ExtSymDefinedIn, |
| 145 | CategoryContextExtSymbolDefinedIn); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 146 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 147 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 148 | /// Generate a USR for an Objective-C class category. |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 149 | void GenObjCCategory(StringRef cls, StringRef cat, |
| 150 | StringRef clsExt, StringRef catExt) { |
| 151 | generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 152 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 153 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 154 | /// Generate a USR fragment for an Objective-C property. |
Argyrios Kyrtzidis | d9849a9 | 2016-07-15 22:18:19 +0000 | [diff] [blame] | 155 | void GenObjCProperty(StringRef prop, bool isClassProp) { |
| 156 | generateUSRForObjCProperty(prop, isClassProp, Out); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 157 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 158 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 159 | /// Generate a USR for an Objective-C protocol. |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 160 | void GenObjCProtocol(StringRef prot, StringRef ext) { |
| 161 | generateUSRForObjCProtocol(prot, Out, ext); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void VisitType(QualType T); |
| 165 | void VisitTemplateParameterList(const TemplateParameterList *Params); |
| 166 | void VisitTemplateName(TemplateName Name); |
| 167 | void VisitTemplateArgument(const TemplateArgument &Arg); |
| 168 | |
| 169 | /// Emit a Decl's name using NamedDecl::printName() and return true if |
| 170 | /// the decl had no name. |
| 171 | bool EmitDeclName(const NamedDecl *D); |
| 172 | }; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 173 | } // end anonymous namespace |
| 174 | |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | // Generating USRs from ASTS. |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | |
| 179 | bool USRGenerator::EmitDeclName(const NamedDecl *D) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 180 | const unsigned startSize = Buf.size(); |
| 181 | D->printName(Out); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 182 | const unsigned endSize = Buf.size(); |
| 183 | return startSize == endSize; |
| 184 | } |
| 185 | |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 186 | bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) { |
| 187 | if (D->isExternallyVisible()) |
| 188 | return false; |
| 189 | if (D->getParentFunctionOrMethod()) |
| 190 | return true; |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 191 | SourceLocation Loc = D->getLocation(); |
| 192 | if (Loc.isInvalid()) |
| 193 | return false; |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 194 | const SourceManager &SM = Context->getSourceManager(); |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 195 | return !SM.isInSystemHeader(Loc); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void USRGenerator::VisitDeclContext(const DeclContext *DC) { |
| 199 | if (const NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 200 | Visit(D); |
| 201 | } |
| 202 | |
| 203 | void USRGenerator::VisitFieldDecl(const FieldDecl *D) { |
| 204 | // The USR for an ivar declared in a class extension is based on the |
| 205 | // ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 206 | if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) |
| 207 | Visit(ID); |
| 208 | else |
| 209 | VisitDeclContext(D->getDeclContext()); |
| 210 | Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@"); |
| 211 | if (EmitDeclName(D)) { |
| 212 | // Bit fields can be anonymous. |
| 213 | IgnoreResults = true; |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 219 | if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 220 | return; |
| 221 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 222 | const unsigned StartSize = Buf.size(); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 223 | VisitDeclContext(D->getDeclContext()); |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 224 | if (Buf.size() == StartSize) |
| 225 | GenExtSymbolContainer(D); |
| 226 | |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 227 | bool IsTemplate = false; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 228 | if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) { |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 229 | IsTemplate = true; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 230 | Out << "@FT@"; |
| 231 | VisitTemplateParameterList(FunTmpl->getTemplateParameters()); |
| 232 | } else |
| 233 | Out << "@F@"; |
Argyrios Kyrtzidis | d571908 | 2016-02-15 01:32:36 +0000 | [diff] [blame] | 234 | |
| 235 | PrintingPolicy Policy(Context->getLangOpts()); |
| 236 | // Forward references can have different template argument names. Suppress the |
| 237 | // template argument names in constructors to make their USR more stable. |
| 238 | Policy.SuppressTemplateArgsInCXXConstructors = true; |
| 239 | D->getDeclName().print(Out, Policy); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 240 | |
| 241 | ASTContext &Ctx = *Context; |
Argyrios Kyrtzidis | 2682f9e | 2016-03-04 07:17:48 +0000 | [diff] [blame] | 242 | if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) && |
| 243 | !D->hasAttr<OverloadableAttr>()) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 244 | return; |
| 245 | |
| 246 | if (const TemplateArgumentList * |
| 247 | SpecArgs = D->getTemplateSpecializationArgs()) { |
| 248 | Out << '<'; |
| 249 | for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) { |
| 250 | Out << '#'; |
| 251 | VisitTemplateArgument(SpecArgs->get(I)); |
| 252 | } |
| 253 | Out << '>'; |
| 254 | } |
| 255 | |
| 256 | // Mangle in type information for the arguments. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 257 | for (auto PD : D->parameters()) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 258 | Out << '#'; |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 259 | VisitType(PD->getType()); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 260 | } |
| 261 | if (D->isVariadic()) |
| 262 | Out << '.'; |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 263 | if (IsTemplate) { |
| 264 | // Function templates can be overloaded by return type, for example: |
| 265 | // \code |
| 266 | // template <class T> typename T::A foo() {} |
| 267 | // template <class T> typename T::B foo() {} |
| 268 | // \endcode |
| 269 | Out << '#'; |
| 270 | VisitType(D->getReturnType()); |
| 271 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 272 | Out << '#'; |
| 273 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 274 | if (MD->isStatic()) |
| 275 | Out << 'S'; |
| 276 | if (unsigned quals = MD->getTypeQualifiers()) |
| 277 | Out << (char)('0' + quals); |
Argyrios Kyrtzidis | f581909 | 2014-12-08 08:48:21 +0000 | [diff] [blame] | 278 | switch (MD->getRefQualifier()) { |
| 279 | case RQ_None: break; |
| 280 | case RQ_LValue: Out << '&'; break; |
| 281 | case RQ_RValue: Out << "&&"; break; |
| 282 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | void USRGenerator::VisitNamedDecl(const NamedDecl *D) { |
| 287 | VisitDeclContext(D->getDeclContext()); |
| 288 | Out << "@"; |
| 289 | |
| 290 | if (EmitDeclName(D)) { |
| 291 | // The string can be empty if the declaration has no name; e.g., it is |
| 292 | // the ParmDecl with no name for declaration of a function pointer type, |
| 293 | // e.g.: void (*f)(void *); |
| 294 | // In this case, don't generate a USR. |
| 295 | IgnoreResults = true; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void USRGenerator::VisitVarDecl(const VarDecl *D) { |
| 300 | // VarDecls can be declared 'extern' within a function or method body, |
| 301 | // but their enclosing DeclContext is the function, not the TU. We need |
| 302 | // to check the storage class to correctly generate the USR. |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 303 | if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 304 | return; |
| 305 | |
| 306 | VisitDeclContext(D->getDeclContext()); |
| 307 | |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 308 | if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) { |
| 309 | Out << "@VT"; |
| 310 | VisitTemplateParameterList(VarTmpl->getTemplateParameters()); |
| 311 | } else if (const VarTemplatePartialSpecializationDecl *PartialSpec |
| 312 | = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) { |
| 313 | Out << "@VP"; |
| 314 | VisitTemplateParameterList(PartialSpec->getTemplateParameters()); |
| 315 | } |
| 316 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 317 | // Variables always have simple names. |
| 318 | StringRef s = D->getName(); |
| 319 | |
| 320 | // The string can be empty if the declaration has no name; e.g., it is |
| 321 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 322 | // void (*f)(void *); |
| 323 | // In this case, don't generate a USR. |
| 324 | if (s.empty()) |
| 325 | IgnoreResults = true; |
| 326 | else |
| 327 | Out << '@' << s; |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 328 | |
| 329 | // For a template specialization, mangle the template arguments. |
| 330 | if (const VarTemplateSpecializationDecl *Spec |
| 331 | = dyn_cast<VarTemplateSpecializationDecl>(D)) { |
Argyrios Kyrtzidis | 7d90ed0 | 2017-02-15 16:16:27 +0000 | [diff] [blame] | 332 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 333 | Out << '>'; |
| 334 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 335 | Out << '#'; |
| 336 | VisitTemplateArgument(Args.get(I)); |
| 337 | } |
| 338 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void USRGenerator::VisitNonTypeTemplateParmDecl( |
| 342 | const NonTypeTemplateParmDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 343 | GenLoc(D, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void USRGenerator::VisitTemplateTemplateParmDecl( |
| 347 | const TemplateTemplateParmDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 348 | GenLoc(D, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) { |
| 352 | if (D->isAnonymousNamespace()) { |
| 353 | Out << "@aN"; |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | VisitDeclContext(D->getDeclContext()); |
| 358 | if (!IgnoreResults) |
| 359 | Out << "@N@" << D->getName(); |
| 360 | } |
| 361 | |
| 362 | void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { |
| 363 | VisitFunctionDecl(D->getTemplatedDecl()); |
| 364 | } |
| 365 | |
| 366 | void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) { |
| 367 | VisitTagDecl(D->getTemplatedDecl()); |
| 368 | } |
| 369 | |
| 370 | void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { |
| 371 | VisitDeclContext(D->getDeclContext()); |
| 372 | if (!IgnoreResults) |
| 373 | Out << "@NA@" << D->getName(); |
| 374 | } |
| 375 | |
| 376 | void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { |
| 377 | const DeclContext *container = D->getDeclContext(); |
| 378 | if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) { |
| 379 | Visit(pd); |
| 380 | } |
| 381 | else { |
| 382 | // The USR for a method declared in a class extension or category is based on |
| 383 | // the ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 384 | const ObjCInterfaceDecl *ID = D->getClassInterface(); |
| 385 | if (!ID) { |
| 386 | IgnoreResults = true; |
| 387 | return; |
| 388 | } |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 389 | auto getCategoryContext = [](const ObjCMethodDecl *D) -> |
| 390 | const ObjCCategoryDecl * { |
| 391 | if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
| 392 | return CD; |
| 393 | if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
| 394 | return ICD->getCategoryDecl(); |
| 395 | return nullptr; |
| 396 | }; |
| 397 | auto *CD = getCategoryContext(D); |
| 398 | VisitObjCContainerDecl(ID, CD); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 399 | } |
| 400 | // Ideally we would use 'GenObjCMethod', but this is such a hot path |
| 401 | // for Objective-C code that we don't want to use |
| 402 | // DeclarationName::getAsString(). |
| 403 | Out << (D->isInstanceMethod() ? "(im)" : "(cm)") |
| 404 | << DeclarationName(D->getSelector()); |
| 405 | } |
| 406 | |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 407 | void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D, |
| 408 | const ObjCCategoryDecl *CatD) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 409 | switch (D->getKind()) { |
| 410 | default: |
| 411 | llvm_unreachable("Invalid ObjC container."); |
| 412 | case Decl::ObjCInterface: |
| 413 | case Decl::ObjCImplementation: |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 414 | GenObjCClass(D->getName(), GetExternalSourceContainer(D), |
| 415 | GetExternalSourceContainer(CatD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 416 | break; |
| 417 | case Decl::ObjCCategory: { |
| 418 | const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
| 419 | const ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 420 | if (!ID) { |
| 421 | // Handle invalid code where the @interface might not |
| 422 | // have been specified. |
| 423 | // FIXME: We should be able to generate this USR even if the |
| 424 | // @interface isn't available. |
| 425 | IgnoreResults = true; |
| 426 | return; |
| 427 | } |
| 428 | // Specially handle class extensions, which are anonymous categories. |
| 429 | // We want to mangle in the location to uniquely distinguish them. |
| 430 | if (CD->IsClassExtension()) { |
| 431 | Out << "objc(ext)" << ID->getName() << '@'; |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 432 | GenLoc(CD, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 433 | } |
| 434 | else |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 435 | GenObjCCategory(ID->getName(), CD->getName(), |
| 436 | GetExternalSourceContainer(ID), |
| 437 | GetExternalSourceContainer(CD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 438 | |
| 439 | break; |
| 440 | } |
| 441 | case Decl::ObjCCategoryImpl: { |
| 442 | const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
| 443 | const ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 444 | if (!ID) { |
| 445 | // Handle invalid code where the @interface might not |
| 446 | // have been specified. |
| 447 | // FIXME: We should be able to generate this USR even if the |
| 448 | // @interface isn't available. |
| 449 | IgnoreResults = true; |
| 450 | return; |
| 451 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 452 | GenObjCCategory(ID->getName(), CD->getName(), |
| 453 | GetExternalSourceContainer(ID), |
| 454 | GetExternalSourceContainer(CD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 457 | case Decl::ObjCProtocol: { |
| 458 | const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); |
| 459 | GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 460 | break; |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 461 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { |
| 466 | // The USR for a property declared in a class extension or category is based |
| 467 | // on the ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 468 | if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) |
| 469 | Visit(ID); |
| 470 | else |
| 471 | Visit(cast<Decl>(D->getDeclContext())); |
Argyrios Kyrtzidis | d9849a9 | 2016-07-15 22:18:19 +0000 | [diff] [blame] | 472 | GenObjCProperty(D->getName(), D->isClassProperty()); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { |
| 476 | if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { |
| 477 | VisitObjCPropertyDecl(PD); |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | IgnoreResults = true; |
| 482 | } |
| 483 | |
| 484 | void USRGenerator::VisitTagDecl(const TagDecl *D) { |
| 485 | // Add the location of the tag decl to handle resolution across |
| 486 | // translation units. |
Argyrios Kyrtzidis | 7332106 | 2016-03-04 07:17:53 +0000 | [diff] [blame] | 487 | if (!isa<EnumDecl>(D) && |
| 488 | ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 489 | return; |
| 490 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 491 | GenExtSymbolContainer(D); |
| 492 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 493 | D = D->getCanonicalDecl(); |
| 494 | VisitDeclContext(D->getDeclContext()); |
| 495 | |
| 496 | bool AlreadyStarted = false; |
| 497 | if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) { |
| 498 | if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) { |
| 499 | AlreadyStarted = true; |
| 500 | |
| 501 | switch (D->getTagKind()) { |
| 502 | case TTK_Interface: |
Argyrios Kyrtzidis | f66cef7 | 2014-12-08 08:48:33 +0000 | [diff] [blame] | 503 | case TTK_Class: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 504 | case TTK_Struct: Out << "@ST"; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 505 | case TTK_Union: Out << "@UT"; break; |
| 506 | case TTK_Enum: llvm_unreachable("enum template"); |
| 507 | } |
| 508 | VisitTemplateParameterList(ClassTmpl->getTemplateParameters()); |
| 509 | } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec |
| 510 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) { |
| 511 | AlreadyStarted = true; |
| 512 | |
| 513 | switch (D->getTagKind()) { |
| 514 | case TTK_Interface: |
Argyrios Kyrtzidis | f66cef7 | 2014-12-08 08:48:33 +0000 | [diff] [blame] | 515 | case TTK_Class: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 516 | case TTK_Struct: Out << "@SP"; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 517 | case TTK_Union: Out << "@UP"; break; |
| 518 | case TTK_Enum: llvm_unreachable("enum partial specialization"); |
| 519 | } |
| 520 | VisitTemplateParameterList(PartialSpec->getTemplateParameters()); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (!AlreadyStarted) { |
| 525 | switch (D->getTagKind()) { |
| 526 | case TTK_Interface: |
Argyrios Kyrtzidis | f66cef7 | 2014-12-08 08:48:33 +0000 | [diff] [blame] | 527 | case TTK_Class: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 528 | case TTK_Struct: Out << "@S"; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 529 | case TTK_Union: Out << "@U"; break; |
| 530 | case TTK_Enum: Out << "@E"; break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | Out << '@'; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 535 | assert(Buf.size() > 0); |
| 536 | const unsigned off = Buf.size() - 1; |
| 537 | |
| 538 | if (EmitDeclName(D)) { |
| 539 | if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) { |
| 540 | Buf[off] = 'A'; |
| 541 | Out << '@' << *TD; |
| 542 | } |
Argyrios Kyrtzidis | 80537a4 | 2014-12-08 08:48:37 +0000 | [diff] [blame] | 543 | else { |
| 544 | if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) { |
| 545 | printLoc(Out, D->getLocation(), Context->getSourceManager(), true); |
Argyrios Kyrtzidis | 7332106 | 2016-03-04 07:17:53 +0000 | [diff] [blame] | 546 | } else { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 547 | Buf[off] = 'a'; |
Argyrios Kyrtzidis | 7332106 | 2016-03-04 07:17:53 +0000 | [diff] [blame] | 548 | if (auto *ED = dyn_cast<EnumDecl>(D)) { |
| 549 | // Distinguish USRs of anonymous enums by using their first enumerator. |
| 550 | auto enum_range = ED->enumerators(); |
| 551 | if (enum_range.begin() != enum_range.end()) { |
| 552 | Out << '@' << **enum_range.begin(); |
| 553 | } |
| 554 | } |
| 555 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 556 | } |
Argyrios Kyrtzidis | 80537a4 | 2014-12-08 08:48:37 +0000 | [diff] [blame] | 557 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 558 | |
| 559 | // For a class template specialization, mangle the template arguments. |
| 560 | if (const ClassTemplateSpecializationDecl *Spec |
| 561 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
Argyrios Kyrtzidis | 7d90ed0 | 2017-02-15 16:16:27 +0000 | [diff] [blame] | 562 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 563 | Out << '>'; |
| 564 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 565 | Out << '#'; |
| 566 | VisitTemplateArgument(Args.get(I)); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 572 | if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 573 | return; |
| 574 | const DeclContext *DC = D->getDeclContext(); |
| 575 | if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
| 576 | Visit(DCN); |
| 577 | Out << "@T@"; |
| 578 | Out << D->getName(); |
| 579 | } |
| 580 | |
| 581 | void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 582 | GenLoc(D, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 585 | void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) { |
| 586 | StringRef Container = GetExternalSourceContainer(D); |
| 587 | if (!Container.empty()) |
| 588 | Out << "@M@" << Container; |
| 589 | } |
| 590 | |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 591 | bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 592 | if (generatedLoc) |
| 593 | return IgnoreResults; |
| 594 | generatedLoc = true; |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 595 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 596 | // Guard against null declarations in invalid code. |
| 597 | if (!D) { |
| 598 | IgnoreResults = true; |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | // Use the location of canonical decl. |
| 603 | D = D->getCanonicalDecl(); |
| 604 | |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 605 | IgnoreResults = |
| 606 | IgnoreResults || printLoc(Out, D->getLocStart(), |
| 607 | Context->getSourceManager(), IncludeOffset); |
| 608 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 609 | return IgnoreResults; |
| 610 | } |
| 611 | |
| 612 | void USRGenerator::VisitType(QualType T) { |
| 613 | // This method mangles in USR information for types. It can possibly |
| 614 | // just reuse the naming-mangling logic used by codegen, although the |
| 615 | // requirements for USRs might not be the same. |
| 616 | ASTContext &Ctx = *Context; |
| 617 | |
| 618 | do { |
| 619 | T = Ctx.getCanonicalType(T); |
| 620 | Qualifiers Q = T.getQualifiers(); |
| 621 | unsigned qVal = 0; |
| 622 | if (Q.hasConst()) |
| 623 | qVal |= 0x1; |
| 624 | if (Q.hasVolatile()) |
| 625 | qVal |= 0x2; |
| 626 | if (Q.hasRestrict()) |
| 627 | qVal |= 0x4; |
| 628 | if(qVal) |
| 629 | Out << ((char) ('0' + qVal)); |
| 630 | |
| 631 | // Mangle in ObjC GC qualifiers? |
| 632 | |
| 633 | if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) { |
| 634 | Out << 'P'; |
| 635 | T = Expansion->getPattern(); |
| 636 | } |
| 637 | |
| 638 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 639 | unsigned char c = '\0'; |
| 640 | switch (BT->getKind()) { |
| 641 | case BuiltinType::Void: |
| 642 | c = 'v'; break; |
| 643 | case BuiltinType::Bool: |
| 644 | c = 'b'; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 645 | case BuiltinType::UChar: |
| 646 | c = 'c'; break; |
| 647 | case BuiltinType::Char16: |
| 648 | c = 'q'; break; |
| 649 | case BuiltinType::Char32: |
| 650 | c = 'w'; break; |
| 651 | case BuiltinType::UShort: |
| 652 | c = 's'; break; |
| 653 | case BuiltinType::UInt: |
| 654 | c = 'i'; break; |
| 655 | case BuiltinType::ULong: |
| 656 | c = 'l'; break; |
| 657 | case BuiltinType::ULongLong: |
| 658 | c = 'k'; break; |
| 659 | case BuiltinType::UInt128: |
| 660 | c = 'j'; break; |
Argyrios Kyrtzidis | 56af7e6 | 2014-12-08 09:09:05 +0000 | [diff] [blame] | 661 | case BuiltinType::Char_U: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 662 | case BuiltinType::Char_S: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 663 | c = 'C'; break; |
Argyrios Kyrtzidis | ca04454 | 2014-12-08 08:48:17 +0000 | [diff] [blame] | 664 | case BuiltinType::SChar: |
| 665 | c = 'r'; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 666 | case BuiltinType::WChar_S: |
| 667 | case BuiltinType::WChar_U: |
| 668 | c = 'W'; break; |
| 669 | case BuiltinType::Short: |
| 670 | c = 'S'; break; |
| 671 | case BuiltinType::Int: |
| 672 | c = 'I'; break; |
| 673 | case BuiltinType::Long: |
| 674 | c = 'L'; break; |
| 675 | case BuiltinType::LongLong: |
| 676 | c = 'K'; break; |
| 677 | case BuiltinType::Int128: |
| 678 | c = 'J'; break; |
| 679 | case BuiltinType::Half: |
| 680 | c = 'h'; break; |
| 681 | case BuiltinType::Float: |
| 682 | c = 'f'; break; |
| 683 | case BuiltinType::Double: |
| 684 | c = 'd'; break; |
| 685 | case BuiltinType::LongDouble: |
| 686 | c = 'D'; break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 687 | case BuiltinType::Float128: |
| 688 | c = 'Q'; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 689 | case BuiltinType::NullPtr: |
| 690 | c = 'n'; break; |
| 691 | #define BUILTIN_TYPE(Id, SingletonId) |
| 692 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 693 | #include "clang/AST/BuiltinTypes.def" |
| 694 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 695 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 696 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 697 | #include "clang/Basic/OpenCLImageTypes.def" |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 698 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 699 | case BuiltinType::OCLClkEvent: |
| 700 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 701 | case BuiltinType::OCLReserveID: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 702 | case BuiltinType::OCLSampler: |
| 703 | IgnoreResults = true; |
| 704 | return; |
| 705 | case BuiltinType::ObjCId: |
| 706 | c = 'o'; break; |
| 707 | case BuiltinType::ObjCClass: |
| 708 | c = 'O'; break; |
| 709 | case BuiltinType::ObjCSel: |
| 710 | c = 'e'; break; |
| 711 | } |
| 712 | Out << c; |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | // If we have already seen this (non-built-in) type, use a substitution |
| 717 | // encoding. |
| 718 | llvm::DenseMap<const Type *, unsigned>::iterator Substitution |
| 719 | = TypeSubstitutions.find(T.getTypePtr()); |
| 720 | if (Substitution != TypeSubstitutions.end()) { |
| 721 | Out << 'S' << Substitution->second << '_'; |
| 722 | return; |
| 723 | } else { |
| 724 | // Record this as a substitution. |
| 725 | unsigned Number = TypeSubstitutions.size(); |
| 726 | TypeSubstitutions[T.getTypePtr()] = Number; |
| 727 | } |
| 728 | |
| 729 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 730 | Out << '*'; |
| 731 | T = PT->getPointeeType(); |
| 732 | continue; |
| 733 | } |
Argyrios Kyrtzidis | 9c99867 | 2016-03-04 07:17:43 +0000 | [diff] [blame] | 734 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
| 735 | Out << '*'; |
| 736 | T = OPT->getPointeeType(); |
| 737 | continue; |
| 738 | } |
Argyrios Kyrtzidis | 0e387dc | 2014-12-08 08:48:27 +0000 | [diff] [blame] | 739 | if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) { |
| 740 | Out << "&&"; |
| 741 | T = RT->getPointeeType(); |
| 742 | continue; |
| 743 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 744 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 745 | Out << '&'; |
| 746 | T = RT->getPointeeType(); |
| 747 | continue; |
| 748 | } |
| 749 | if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { |
| 750 | Out << 'F'; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 751 | VisitType(FT->getReturnType()); |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 752 | for (const auto &I : FT->param_types()) |
| 753 | VisitType(I); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 754 | if (FT->isVariadic()) |
| 755 | Out << '.'; |
| 756 | return; |
| 757 | } |
| 758 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
| 759 | Out << 'B'; |
| 760 | T = BT->getPointeeType(); |
| 761 | continue; |
| 762 | } |
| 763 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
| 764 | Out << '<'; |
| 765 | T = CT->getElementType(); |
| 766 | continue; |
| 767 | } |
| 768 | if (const TagType *TT = T->getAs<TagType>()) { |
| 769 | Out << '$'; |
| 770 | VisitTagDecl(TT->getDecl()); |
| 771 | return; |
| 772 | } |
Argyrios Kyrtzidis | 9c99867 | 2016-03-04 07:17:43 +0000 | [diff] [blame] | 773 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
| 774 | Out << '$'; |
| 775 | VisitObjCInterfaceDecl(OIT->getDecl()); |
| 776 | return; |
| 777 | } |
| 778 | if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) { |
| 779 | Out << 'Q'; |
| 780 | VisitType(OIT->getBaseType()); |
| 781 | for (auto *Prot : OIT->getProtocols()) |
| 782 | VisitObjCProtocolDecl(Prot); |
| 783 | return; |
| 784 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 785 | if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) { |
| 786 | Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); |
| 787 | return; |
| 788 | } |
| 789 | if (const TemplateSpecializationType *Spec |
| 790 | = T->getAs<TemplateSpecializationType>()) { |
| 791 | Out << '>'; |
| 792 | VisitTemplateName(Spec->getTemplateName()); |
| 793 | Out << Spec->getNumArgs(); |
| 794 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 795 | VisitTemplateArgument(Spec->getArg(I)); |
| 796 | return; |
| 797 | } |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 798 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 799 | Out << '^'; |
| 800 | // FIXME: Encode the qualifier, don't just print it. |
| 801 | PrintingPolicy PO(Ctx.getLangOpts()); |
| 802 | PO.SuppressTagKeyword = true; |
| 803 | PO.SuppressUnwrittenScope = true; |
| 804 | PO.ConstantArraySizeAsWritten = false; |
| 805 | PO.AnonymousTagLocations = false; |
| 806 | DNT->getQualifier()->print(Out, PO); |
| 807 | Out << ':' << DNT->getIdentifier()->getName(); |
| 808 | return; |
| 809 | } |
Argyrios Kyrtzidis | 1d5e542 | 2014-12-08 08:48:43 +0000 | [diff] [blame] | 810 | if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) { |
| 811 | T = InjT->getInjectedSpecializationType(); |
| 812 | continue; |
| 813 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 814 | |
| 815 | // Unhandled type. |
| 816 | Out << ' '; |
| 817 | break; |
| 818 | } while (true); |
| 819 | } |
| 820 | |
| 821 | void USRGenerator::VisitTemplateParameterList( |
| 822 | const TemplateParameterList *Params) { |
| 823 | if (!Params) |
| 824 | return; |
| 825 | Out << '>' << Params->size(); |
| 826 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 827 | PEnd = Params->end(); |
| 828 | P != PEnd; ++P) { |
| 829 | Out << '#'; |
| 830 | if (isa<TemplateTypeParmDecl>(*P)) { |
| 831 | if (cast<TemplateTypeParmDecl>(*P)->isParameterPack()) |
| 832 | Out<< 'p'; |
| 833 | Out << 'T'; |
| 834 | continue; |
| 835 | } |
| 836 | |
| 837 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 838 | if (NTTP->isParameterPack()) |
| 839 | Out << 'p'; |
| 840 | Out << 'N'; |
| 841 | VisitType(NTTP->getType()); |
| 842 | continue; |
| 843 | } |
| 844 | |
| 845 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 846 | if (TTP->isParameterPack()) |
| 847 | Out << 'p'; |
| 848 | Out << 't'; |
| 849 | VisitTemplateParameterList(TTP->getTemplateParameters()); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | void USRGenerator::VisitTemplateName(TemplateName Name) { |
| 854 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 855 | if (TemplateTemplateParmDecl *TTP |
| 856 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 857 | Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | Visit(Template); |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | // FIXME: Visit dependent template names. |
| 866 | } |
| 867 | |
| 868 | void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { |
| 869 | switch (Arg.getKind()) { |
| 870 | case TemplateArgument::Null: |
| 871 | break; |
| 872 | |
| 873 | case TemplateArgument::Declaration: |
| 874 | Visit(Arg.getAsDecl()); |
| 875 | break; |
| 876 | |
| 877 | case TemplateArgument::NullPtr: |
| 878 | break; |
| 879 | |
| 880 | case TemplateArgument::TemplateExpansion: |
| 881 | Out << 'P'; // pack expansion of... |
| 882 | // Fall through |
| 883 | case TemplateArgument::Template: |
| 884 | VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); |
| 885 | break; |
| 886 | |
| 887 | case TemplateArgument::Expression: |
| 888 | // FIXME: Visit expressions. |
| 889 | break; |
| 890 | |
| 891 | case TemplateArgument::Pack: |
| 892 | Out << 'p' << Arg.pack_size(); |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 893 | for (const auto &P : Arg.pack_elements()) |
| 894 | VisitTemplateArgument(P); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 895 | break; |
| 896 | |
| 897 | case TemplateArgument::Type: |
| 898 | VisitType(Arg.getAsType()); |
| 899 | break; |
| 900 | |
| 901 | case TemplateArgument::Integral: |
| 902 | Out << 'V'; |
| 903 | VisitType(Arg.getIntegralType()); |
| 904 | Out << Arg.getAsIntegral(); |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | //===----------------------------------------------------------------------===// |
| 910 | // USR generation functions. |
| 911 | //===----------------------------------------------------------------------===// |
| 912 | |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 913 | static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn, |
| 914 | StringRef CatSymDefinedIn, |
| 915 | raw_ostream &OS) { |
| 916 | if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty()) |
| 917 | return; |
| 918 | if (CatSymDefinedIn.empty()) { |
| 919 | OS << "@M@" << ClsSymDefinedIn << '@'; |
| 920 | return; |
| 921 | } |
| 922 | OS << "@CM@" << CatSymDefinedIn << '@'; |
| 923 | if (ClsSymDefinedIn != CatSymDefinedIn) { |
| 924 | OS << ClsSymDefinedIn << '@'; |
| 925 | } |
| 926 | } |
| 927 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 928 | void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS, |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 929 | StringRef ExtSymDefinedIn, |
| 930 | StringRef CategoryContextExtSymbolDefinedIn) { |
| 931 | combineClassAndCategoryExtContainers(ExtSymDefinedIn, |
| 932 | CategoryContextExtSymbolDefinedIn, OS); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 933 | OS << "objc(cs)" << Cls; |
| 934 | } |
| 935 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 936 | void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat, |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 937 | raw_ostream &OS, |
| 938 | StringRef ClsSymDefinedIn, |
| 939 | StringRef CatSymDefinedIn) { |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 940 | combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 941 | OS << "objc(cy)" << Cls << '@' << Cat; |
| 942 | } |
| 943 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 944 | void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 945 | OS << '@' << Ivar; |
| 946 | } |
| 947 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 948 | void clang::index::generateUSRForObjCMethod(StringRef Sel, |
| 949 | bool IsInstanceMethod, |
| 950 | raw_ostream &OS) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 951 | OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel; |
| 952 | } |
| 953 | |
Argyrios Kyrtzidis | d9849a9 | 2016-07-15 22:18:19 +0000 | [diff] [blame] | 954 | void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp, |
| 955 | raw_ostream &OS) { |
| 956 | OS << (isClassProp ? "(cpy)" : "(py)") << Prop; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 959 | void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS, |
| 960 | StringRef ExtSymDefinedIn) { |
| 961 | if (!ExtSymDefinedIn.empty()) |
| 962 | OS << "@M@" << ExtSymDefinedIn << '@'; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 963 | OS << "objc(pl)" << Prot; |
| 964 | } |
| 965 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 966 | void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS, |
| 967 | StringRef ExtSymDefinedIn) { |
| 968 | if (!ExtSymDefinedIn.empty()) |
| 969 | OS << "@M@" << ExtSymDefinedIn; |
| 970 | OS << "@E@" << EnumName; |
| 971 | } |
| 972 | |
Argyrios Kyrtzidis | f363474 | 2017-04-21 22:27:06 +0000 | [diff] [blame^] | 973 | void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName, |
| 974 | raw_ostream &OS) { |
| 975 | OS << '@' << EnumConstantName; |
| 976 | } |
| 977 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 978 | bool clang::index::generateUSRForDecl(const Decl *D, |
| 979 | SmallVectorImpl<char> &Buf) { |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 980 | if (!D) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 981 | return true; |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 982 | // We don't ignore decls with invalid source locations. Implicit decls, like |
| 983 | // C++'s operator new function, can have invalid locations but it is fine to |
| 984 | // create USRs that can identify them. |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 985 | |
| 986 | USRGenerator UG(&D->getASTContext(), Buf); |
| 987 | UG.Visit(D); |
| 988 | return UG.ignoreResults(); |
| 989 | } |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 990 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 991 | bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD, |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 992 | const SourceManager &SM, |
| 993 | SmallVectorImpl<char> &Buf) { |
Argyrios Kyrtzidis | 5b7a09a | 2017-02-02 16:13:10 +0000 | [diff] [blame] | 994 | if (!MD) |
| 995 | return true; |
| 996 | return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(), |
| 997 | SM, Buf); |
| 998 | |
| 999 | } |
| 1000 | |
| 1001 | bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc, |
| 1002 | const SourceManager &SM, |
| 1003 | SmallVectorImpl<char> &Buf) { |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 1004 | // Don't generate USRs for things with invalid locations. |
Argyrios Kyrtzidis | 5b7a09a | 2017-02-02 16:13:10 +0000 | [diff] [blame] | 1005 | if (MacroName.empty() || Loc.isInvalid()) |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 1006 | return true; |
| 1007 | |
| 1008 | llvm::raw_svector_ostream Out(Buf); |
| 1009 | |
| 1010 | // Assume that system headers are sane. Don't put source location |
| 1011 | // information into the USR if the macro comes from a system header. |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 1012 | bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc); |
| 1013 | |
| 1014 | Out << getUSRSpacePrefix(); |
| 1015 | if (ShouldGenerateLocation) |
| 1016 | printLoc(Out, Loc, SM, /*IncludeOffset=*/true); |
| 1017 | Out << "@macro@"; |
Argyrios Kyrtzidis | 5b7a09a | 2017-02-02 16:13:10 +0000 | [diff] [blame] | 1018 | Out << MacroName; |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 1019 | return false; |
| 1020 | } |