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