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