2010-01-06 06:06:45 +08:00
|
|
|
//===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the generation and use of USRs from CXEntities.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CIndexer.h"
|
2010-01-19 04:23:29 +08:00
|
|
|
#include "CXCursor.h"
|
2010-01-12 19:32:40 +08:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
2010-04-16 05:51:13 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Lex/PreprocessingRecord.h"
|
2010-01-12 10:07:58 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-01-12 19:32:40 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-04-16 05:51:13 +08:00
|
|
|
|
2010-04-13 03:45:50 +08:00
|
|
|
using namespace clang;
|
2010-02-17 08:41:08 +08:00
|
|
|
using namespace clang::cxstring;
|
|
|
|
|
2010-01-12 10:07:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// USR generation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2010-01-14 09:50:21 +08:00
|
|
|
class USRGenerator : public DeclVisitor<USRGenerator> {
|
2010-05-08 04:07:23 +08:00
|
|
|
llvm::SmallString<1024> Buf;
|
|
|
|
llvm::raw_svector_ostream Out;
|
2010-01-19 06:02:49 +08:00
|
|
|
bool IgnoreResults;
|
2010-04-16 05:04:25 +08:00
|
|
|
ASTUnit *AU;
|
2010-05-07 07:38:28 +08:00
|
|
|
bool generatedLoc;
|
2010-01-14 09:50:21 +08:00
|
|
|
public:
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator(const CXCursor *C = 0)
|
|
|
|
: Out(Buf),
|
|
|
|
IgnoreResults(false),
|
|
|
|
AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
|
|
|
|
generatedLoc(false)
|
|
|
|
{
|
|
|
|
// Add the USR space prefix.
|
|
|
|
Out << "c:";
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringRef str() {
|
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
USRGenerator* operator->() { return this; }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
llvm::raw_svector_ostream &operator<<(const T &x) {
|
|
|
|
Out << x;
|
|
|
|
return Out;
|
|
|
|
}
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-01-19 06:02:49 +08:00
|
|
|
bool ignoreResults() const { return IgnoreResults; }
|
2010-03-13 10:50:34 +08:00
|
|
|
|
|
|
|
// Visitation methods from generating USRs from AST elements.
|
2010-01-14 09:50:21 +08:00
|
|
|
void VisitDeclContext(DeclContext *D);
|
2010-01-19 06:02:49 +08:00
|
|
|
void VisitFieldDecl(FieldDecl *D);
|
2010-01-14 09:50:21 +08:00
|
|
|
void VisitFunctionDecl(FunctionDecl *D);
|
|
|
|
void VisitNamedDecl(NamedDecl *D);
|
|
|
|
void VisitNamespaceDecl(NamespaceDecl *D);
|
2010-04-17 05:31:52 +08:00
|
|
|
void VisitObjCClassDecl(ObjCClassDecl *CD);
|
2010-03-13 10:50:34 +08:00
|
|
|
void VisitObjCContainerDecl(ObjCContainerDecl *CD);
|
2010-04-17 05:31:52 +08:00
|
|
|
void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
|
2010-04-21 07:15:40 +08:00
|
|
|
void VisitObjCMethodDecl(ObjCMethodDecl *MD);
|
2010-01-14 09:50:21 +08:00
|
|
|
void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
|
2010-04-21 07:15:40 +08:00
|
|
|
void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
|
2010-01-19 06:42:20 +08:00
|
|
|
void VisitTagDecl(TagDecl *D);
|
2010-01-14 09:50:21 +08:00
|
|
|
void VisitTypedefDecl(TypedefDecl *D);
|
2010-04-21 07:15:40 +08:00
|
|
|
void VisitVarDecl(VarDecl *D);
|
2010-05-07 09:04:32 +08:00
|
|
|
void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
|
|
|
|
IgnoreResults = true;
|
|
|
|
return;
|
|
|
|
}
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-04-16 05:51:13 +08:00
|
|
|
/// Generate the string component containing the location of the
|
|
|
|
/// declaration.
|
2010-05-07 07:38:28 +08:00
|
|
|
bool GenLoc(const Decl *D);
|
2010-04-16 05:51:13 +08:00
|
|
|
|
2010-03-13 10:50:34 +08:00
|
|
|
/// String generation methods used both by the visitation methods
|
|
|
|
/// and from other clients that want to directly generate USRs. These
|
|
|
|
/// methods do not construct complete USRs (which incorporate the parents
|
|
|
|
/// of an AST element), but only the fragments concerning the AST element
|
|
|
|
/// itself.
|
|
|
|
|
|
|
|
/// Generate a USR for an Objective-C class.
|
|
|
|
void GenObjCClass(llvm::StringRef cls);
|
|
|
|
/// Generate a USR for an Objective-C class category.
|
|
|
|
void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
|
|
|
|
/// Generate a USR fragment for an Objective-C instance variable. The
|
|
|
|
/// complete USR can be created by concatenating the USR for the
|
|
|
|
/// encompassing class with this USR fragment.
|
|
|
|
void GenObjCIvar(llvm::StringRef ivar);
|
|
|
|
/// Generate a USR fragment for an Objective-C method.
|
|
|
|
void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
|
|
|
|
/// Generate a USR fragment for an Objective-C property.
|
|
|
|
void GenObjCProperty(llvm::StringRef prop);
|
|
|
|
/// Generate a USR for an Objective-C protocol.
|
|
|
|
void GenObjCProtocol(llvm::StringRef prot);
|
2010-05-07 09:04:32 +08:00
|
|
|
|
|
|
|
void VisitType(QualType T);
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
/// Emit a Decl's name using NamedDecl::printName() and return true if
|
|
|
|
/// the decl had no name.
|
|
|
|
bool EmitDeclName(const NamedDecl *D);
|
2010-01-14 09:50:21 +08:00
|
|
|
};
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-01-12 10:07:58 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2010-03-13 10:50:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generating USRs from ASTS.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
bool USRGenerator::EmitDeclName(const NamedDecl *D) {
|
|
|
|
Out.flush();
|
|
|
|
const unsigned startSize = Buf.size();
|
|
|
|
D->printName(Out);
|
|
|
|
Out.flush();
|
|
|
|
const unsigned endSize = Buf.size();
|
|
|
|
return startSize == endSize;
|
|
|
|
}
|
|
|
|
|
2010-05-07 07:38:28 +08:00
|
|
|
static bool InAnonymousNamespace(const Decl *D) {
|
|
|
|
if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
|
|
|
|
return ND->isAnonymousNamespace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ShouldGenerateLocation(const NamedDecl *D) {
|
|
|
|
return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
|
|
|
|
}
|
|
|
|
|
2010-01-14 09:50:21 +08:00
|
|
|
void USRGenerator::VisitDeclContext(DeclContext *DC) {
|
|
|
|
if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
|
|
|
|
Visit(D);
|
|
|
|
}
|
|
|
|
|
2010-01-19 06:02:49 +08:00
|
|
|
void USRGenerator::VisitFieldDecl(FieldDecl *D) {
|
2010-05-08 04:07:23 +08:00
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
|
|
|
|
if (EmitDeclName(D)) {
|
2010-01-19 06:02:49 +08:00
|
|
|
// Bit fields can be anonymous.
|
|
|
|
IgnoreResults = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-14 09:50:21 +08:00
|
|
|
void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
|
2010-05-07 07:38:28 +08:00
|
|
|
if (ShouldGenerateLocation(D) && GenLoc(D))
|
|
|
|
return;
|
2010-04-30 01:43:29 +08:00
|
|
|
|
2010-05-07 07:38:28 +08:00
|
|
|
VisitDeclContext(D->getDeclContext());
|
2010-05-08 04:07:23 +08:00
|
|
|
Out << "@F@";
|
|
|
|
D->printName(Out);
|
2010-05-07 09:04:32 +08:00
|
|
|
|
|
|
|
ASTContext &Ctx = AU->getASTContext();
|
|
|
|
if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Mangle in type information for the arguments.
|
|
|
|
for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Out << '#';
|
|
|
|
if (ParmVarDecl *PD = *I)
|
|
|
|
VisitType(PD->getType());
|
|
|
|
}
|
|
|
|
if (D->isVariadic())
|
|
|
|
Out << '.';
|
2010-05-08 04:39:40 +08:00
|
|
|
Out << '#';
|
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
|
|
|
|
if (MD->isStatic())
|
|
|
|
Out << 'S';
|
|
|
|
if (unsigned quals = MD->getTypeQualifiers())
|
|
|
|
Out << (char)('0' + quals);
|
|
|
|
}
|
2010-01-14 09:50:21 +08:00
|
|
|
}
|
2010-01-13 03:35:53 +08:00
|
|
|
|
|
|
|
void USRGenerator::VisitNamedDecl(NamedDecl *D) {
|
2010-01-14 09:50:21 +08:00
|
|
|
VisitDeclContext(D->getDeclContext());
|
2010-05-08 04:07:23 +08:00
|
|
|
Out << "@";
|
|
|
|
|
|
|
|
if (EmitDeclName(D)) {
|
|
|
|
// The string can be empty if the declaration has no name; e.g., it is
|
|
|
|
// the ParmDecl with no name for declaration of a function pointer type,
|
|
|
|
// e.g.: void (*f)(void *);
|
|
|
|
// In this case, don't generate a USR.
|
2010-04-17 05:31:52 +08:00
|
|
|
IgnoreResults = true;
|
2010-05-08 04:07:23 +08:00
|
|
|
}
|
2010-01-14 09:50:21 +08:00
|
|
|
}
|
|
|
|
|
2010-04-21 07:15:40 +08:00
|
|
|
void USRGenerator::VisitVarDecl(VarDecl *D) {
|
|
|
|
// VarDecls can be declared 'extern' within a function or method body,
|
|
|
|
// but their enclosing DeclContext is the function, not the TU. We need
|
|
|
|
// to check the storage class to correctly generate the USR.
|
2010-05-07 07:38:28 +08:00
|
|
|
if (ShouldGenerateLocation(D) && GenLoc(D))
|
|
|
|
return;
|
|
|
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
2010-04-30 01:43:29 +08:00
|
|
|
|
|
|
|
// Variables always have simple names.
|
|
|
|
llvm::StringRef s = D->getName();
|
2010-04-21 07:15:40 +08:00
|
|
|
|
|
|
|
// The string can be empty if the declaration has no name; e.g., it is
|
|
|
|
// the ParmDecl with no name for declaration of a function pointer type, e.g.:
|
2010-08-22 09:00:03 +08:00
|
|
|
// void (*f)(void *);
|
2010-04-21 07:15:40 +08:00
|
|
|
// In this case, don't generate a USR.
|
|
|
|
if (s.empty())
|
|
|
|
IgnoreResults = true;
|
|
|
|
else
|
2010-05-08 04:07:23 +08:00
|
|
|
Out << '@' << s;
|
2010-04-21 07:15:40 +08:00
|
|
|
}
|
|
|
|
|
2010-01-14 09:50:21 +08:00
|
|
|
void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
|
2010-05-07 07:38:28 +08:00
|
|
|
if (D->isAnonymousNamespace()) {
|
|
|
|
Out << "@aN";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-14 09:50:21 +08:00
|
|
|
VisitDeclContext(D->getDeclContext());
|
2010-05-07 07:38:28 +08:00
|
|
|
if (!IgnoreResults)
|
|
|
|
Out << "@N@" << D->getName();
|
2010-01-13 03:35:53 +08:00
|
|
|
}
|
2010-01-13 07:33:42 +08:00
|
|
|
|
2010-01-12 10:07:58 +08:00
|
|
|
void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
|
|
|
|
Visit(cast<Decl>(D->getDeclContext()));
|
2010-05-08 04:07:23 +08:00
|
|
|
// Ideally we would use 'GenObjCMethod', but this is such a hot path
|
|
|
|
// for Objective-C code that we don't want to use
|
|
|
|
// DeclarationName::getAsString().
|
|
|
|
Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
|
|
|
|
DeclarationName N(D->getSelector());
|
|
|
|
N.printName(Out);
|
2010-01-12 10:07:58 +08:00
|
|
|
}
|
2010-01-13 07:33:42 +08:00
|
|
|
|
2010-04-17 05:31:52 +08:00
|
|
|
void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
|
|
|
|
// FIXME: @class declarations can refer to multiple classes. We need
|
|
|
|
// to be able to traverse these.
|
|
|
|
IgnoreResults = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
|
|
|
|
// FIXME: @protocol declarations can refer to multiple protocols. We need
|
|
|
|
// to be able to traverse these.
|
|
|
|
IgnoreResults = true;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:07:58 +08:00
|
|
|
void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
|
|
|
|
switch (D->getKind()) {
|
|
|
|
default:
|
|
|
|
assert(false && "Invalid ObjC container.");
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
case Decl::ObjCImplementation:
|
2010-03-13 10:50:34 +08:00
|
|
|
GenObjCClass(D->getName());
|
2010-01-12 10:07:58 +08:00
|
|
|
break;
|
|
|
|
case Decl::ObjCCategory: {
|
|
|
|
ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
|
2010-03-20 04:39:03 +08:00
|
|
|
ObjCInterfaceDecl *ID = CD->getClassInterface();
|
|
|
|
if (!ID) {
|
|
|
|
// Handle invalid code where the @interface might not
|
|
|
|
// have been specified.
|
|
|
|
// FIXME: We should be able to generate this USR even if the
|
|
|
|
// @interface isn't available.
|
|
|
|
IgnoreResults = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GenObjCCategory(ID->getName(), CD->getName());
|
2010-01-12 10:07:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ObjCCategoryImpl: {
|
|
|
|
ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
|
2010-03-20 04:39:03 +08:00
|
|
|
ObjCInterfaceDecl *ID = CD->getClassInterface();
|
|
|
|
if (!ID) {
|
|
|
|
// Handle invalid code where the @interface might not
|
|
|
|
// have been specified.
|
|
|
|
// FIXME: We should be able to generate this USR even if the
|
|
|
|
// @interface isn't available.
|
|
|
|
IgnoreResults = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GenObjCCategory(ID->getName(), CD->getName());
|
2010-01-12 10:07:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ObjCProtocol:
|
2010-03-13 10:50:34 +08:00
|
|
|
GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
|
2010-01-12 10:07:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 07:33:42 +08:00
|
|
|
|
2010-01-12 10:07:58 +08:00
|
|
|
void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
|
|
|
|
Visit(cast<Decl>(D->getDeclContext()));
|
2010-03-13 10:50:34 +08:00
|
|
|
GenObjCProperty(D->getName());
|
2010-01-12 10:07:58 +08:00
|
|
|
}
|
2010-01-13 07:33:42 +08:00
|
|
|
|
2010-04-21 07:15:40 +08:00
|
|
|
void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
|
|
|
|
if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
|
|
|
|
VisitObjCPropertyDecl(PD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IgnoreResults = true;
|
|
|
|
}
|
|
|
|
|
2010-01-19 06:42:20 +08:00
|
|
|
void USRGenerator::VisitTagDecl(TagDecl *D) {
|
2010-05-07 07:38:28 +08:00
|
|
|
// Add the location of the tag decl to handle resolution across
|
|
|
|
// translation units.
|
|
|
|
if (ShouldGenerateLocation(D) && GenLoc(D))
|
|
|
|
return;
|
2010-05-07 09:04:32 +08:00
|
|
|
|
2010-04-16 05:51:13 +08:00
|
|
|
D = D->getCanonicalDecl();
|
2010-01-19 06:42:20 +08:00
|
|
|
VisitDeclContext(D->getDeclContext());
|
2010-05-07 07:38:28 +08:00
|
|
|
|
2010-01-19 06:42:20 +08:00
|
|
|
switch (D->getTagKind()) {
|
2010-05-12 05:36:43 +08:00
|
|
|
case TTK_Struct: Out << "@S"; break;
|
|
|
|
case TTK_Class: Out << "@C"; break;
|
|
|
|
case TTK_Union: Out << "@U"; break;
|
|
|
|
case TTK_Enum: Out << "@E"; break;
|
2010-04-17 05:31:52 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
Out << '@';
|
|
|
|
Out.flush();
|
|
|
|
assert(Buf.size() > 0);
|
|
|
|
const unsigned off = Buf.size() - 1;
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
if (EmitDeclName(D)) {
|
|
|
|
if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
|
|
|
|
Buf[off] = 'A';
|
2010-04-17 17:33:03 +08:00
|
|
|
Out << '@' << TD;
|
2010-05-08 04:07:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Buf[off] = 'a';
|
2010-01-16 07:34:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 07:33:42 +08:00
|
|
|
void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
|
2010-05-07 07:38:28 +08:00
|
|
|
if (ShouldGenerateLocation(D) && GenLoc(D))
|
|
|
|
return;
|
2010-01-13 07:33:42 +08:00
|
|
|
DeclContext *DC = D->getDeclContext();
|
|
|
|
if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
|
2010-03-13 10:50:34 +08:00
|
|
|
Visit(DCN);
|
2010-04-17 05:31:52 +08:00
|
|
|
Out << "@T@";
|
2010-04-16 05:51:13 +08:00
|
|
|
Out << D->getName();
|
|
|
|
}
|
|
|
|
|
2010-05-07 07:38:28 +08:00
|
|
|
bool USRGenerator::GenLoc(const Decl *D) {
|
|
|
|
if (generatedLoc)
|
|
|
|
return IgnoreResults;
|
|
|
|
generatedLoc = true;
|
|
|
|
|
2010-04-16 05:51:13 +08:00
|
|
|
const SourceManager &SM = AU->getSourceManager();
|
|
|
|
SourceLocation L = D->getLocStart();
|
|
|
|
if (L.isInvalid()) {
|
|
|
|
IgnoreResults = true;
|
2010-05-07 07:38:28 +08:00
|
|
|
return true;
|
2010-04-16 05:51:13 +08:00
|
|
|
}
|
|
|
|
L = SM.getInstantiationLoc(L);
|
|
|
|
const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
|
2010-04-17 05:31:52 +08:00
|
|
|
if (FE) {
|
|
|
|
llvm::sys::Path P(FE->getName());
|
|
|
|
Out << P.getLast();
|
|
|
|
}
|
2010-04-16 05:51:13 +08:00
|
|
|
else {
|
|
|
|
// This case really isn't interesting.
|
|
|
|
IgnoreResults = true;
|
2010-05-07 07:38:28 +08:00
|
|
|
return true;
|
2010-04-16 05:51:13 +08:00
|
|
|
}
|
2010-07-22 19:14:15 +08:00
|
|
|
// Use the offest into the FileID to represent the location. Using
|
|
|
|
// a line/column can cause us to look back at the original source file,
|
|
|
|
// which is expensive.
|
|
|
|
Out << '@' << Decomposed.second;
|
2010-05-07 07:38:28 +08:00
|
|
|
return IgnoreResults;
|
2010-01-13 07:33:42 +08:00
|
|
|
}
|
|
|
|
|
2010-05-07 09:04:32 +08:00
|
|
|
void USRGenerator::VisitType(QualType T) {
|
|
|
|
// This method mangles in USR information for types. It can possibly
|
|
|
|
// just reuse the naming-mangling logic used by codegen, although the
|
|
|
|
// requirements for USRs might not be the same.
|
2010-05-08 04:39:40 +08:00
|
|
|
ASTContext &Ctx = AU->getASTContext();
|
|
|
|
|
2010-05-07 09:04:32 +08:00
|
|
|
do {
|
2010-05-08 04:39:40 +08:00
|
|
|
T = Ctx.getCanonicalType(T);
|
2010-05-07 09:04:32 +08:00
|
|
|
Qualifiers Q = T.getQualifiers();
|
2010-05-08 04:39:40 +08:00
|
|
|
unsigned qVal = 0;
|
2010-05-07 09:04:32 +08:00
|
|
|
if (Q.hasConst())
|
2010-05-08 04:39:40 +08:00
|
|
|
qVal |= 0x1;
|
2010-05-07 09:04:32 +08:00
|
|
|
if (Q.hasVolatile())
|
2010-05-08 04:39:40 +08:00
|
|
|
qVal |= 0x2;
|
2010-05-07 09:04:32 +08:00
|
|
|
if (Q.hasRestrict())
|
2010-05-08 04:39:40 +08:00
|
|
|
qVal |= 0x4;
|
|
|
|
if(qVal)
|
|
|
|
Out << ((char) ('0' + qVal));
|
2010-05-07 09:04:32 +08:00
|
|
|
|
|
|
|
// Mangle in ObjC GC qualifiers?
|
|
|
|
|
|
|
|
if (const PointerType *PT = T->getAs<PointerType>()) {
|
|
|
|
Out << '*';
|
|
|
|
T = PT->getPointeeType();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
|
|
|
|
Out << '&';
|
|
|
|
T = RT->getPointeeType();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
|
|
|
|
Out << 'F';
|
|
|
|
VisitType(FT->getResultType());
|
|
|
|
for (FunctionProtoType::arg_type_iterator
|
|
|
|
I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
|
|
|
|
VisitType(*I);
|
|
|
|
}
|
|
|
|
if (FT->isVariadic())
|
|
|
|
Out << '.';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
|
|
|
|
Out << 'B';
|
|
|
|
T = BT->getPointeeType();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
|
|
|
|
unsigned char c = '\0';
|
|
|
|
switch (BT->getKind()) {
|
|
|
|
case BuiltinType::Void:
|
|
|
|
c = 'v'; break;
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
c = 'b'; break;
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
c = 'c'; break;
|
|
|
|
case BuiltinType::Char16:
|
|
|
|
c = 'q'; break;
|
|
|
|
case BuiltinType::Char32:
|
|
|
|
c = 'w'; break;
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
c = 's'; break;
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
c = 'i'; break;
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
c = 'l'; break;
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
c = 'k'; break;
|
|
|
|
case BuiltinType::UInt128:
|
|
|
|
c = 'j'; break;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
c = 'C'; break;
|
|
|
|
case BuiltinType::WChar:
|
|
|
|
c = 'W'; break;
|
|
|
|
case BuiltinType::Short:
|
|
|
|
c = 'S'; break;
|
|
|
|
case BuiltinType::Int:
|
|
|
|
c = 'I'; break;
|
|
|
|
case BuiltinType::Long:
|
|
|
|
c = 'L'; break;
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
c = 'K'; break;
|
|
|
|
case BuiltinType::Int128:
|
|
|
|
c = 'J'; break;
|
|
|
|
case BuiltinType::Float:
|
|
|
|
c = 'f'; break;
|
|
|
|
case BuiltinType::Double:
|
|
|
|
c = 'd'; break;
|
|
|
|
case BuiltinType::LongDouble:
|
|
|
|
c = 'D'; break;
|
|
|
|
case BuiltinType::NullPtr:
|
|
|
|
c = 'n'; break;
|
|
|
|
case BuiltinType::Overload:
|
|
|
|
case BuiltinType::Dependent:
|
|
|
|
case BuiltinType::UndeducedAuto:
|
|
|
|
IgnoreResults = true;
|
|
|
|
return;
|
|
|
|
case BuiltinType::ObjCId:
|
|
|
|
c = 'o'; break;
|
|
|
|
case BuiltinType::ObjCClass:
|
|
|
|
c = 'O'; break;
|
|
|
|
case BuiltinType::ObjCSel:
|
|
|
|
c = 'e'; break;
|
|
|
|
}
|
|
|
|
Out << c;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const ComplexType *CT = T->getAs<ComplexType>()) {
|
|
|
|
Out << '<';
|
|
|
|
T = CT->getElementType();
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-08 04:39:40 +08:00
|
|
|
if (const TagType *TT = T->getAs<TagType>()) {
|
|
|
|
Out << '$';
|
|
|
|
VisitTagDecl(TT->getDecl());
|
|
|
|
return;
|
|
|
|
}
|
2010-05-07 09:04:32 +08:00
|
|
|
|
|
|
|
// Unhandled type.
|
|
|
|
Out << ' ';
|
|
|
|
break;
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
2010-03-13 10:50:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// General purpose USR generation methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void USRGenerator::GenObjCClass(llvm::StringRef cls) {
|
|
|
|
Out << "objc(cs)" << cls;
|
|
|
|
}
|
2010-01-19 04:23:29 +08:00
|
|
|
|
2010-03-13 10:50:34 +08:00
|
|
|
void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
|
2010-04-17 05:31:52 +08:00
|
|
|
Out << "objc(cy)" << cls << '@' << cat;
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
|
2010-05-08 04:07:23 +08:00
|
|
|
Out << '@' << ivar;
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
|
|
|
|
Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
|
|
|
|
Out << "(py)" << prop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
|
|
|
|
Out << "objc(pl)" << prot;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// API hooks.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-01-19 04:23:29 +08:00
|
|
|
|
2010-04-08 23:54:07 +08:00
|
|
|
static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
|
|
|
|
return s.startswith("c:") ? s.substr(2) : "";
|
|
|
|
}
|
|
|
|
|
2010-04-12 06:20:26 +08:00
|
|
|
static CXString getDeclCursorUSR(const CXCursor &C) {
|
2010-03-13 10:50:34 +08:00
|
|
|
Decl *D = cxcursor::getCursorDecl(C);
|
2010-04-17 05:31:52 +08:00
|
|
|
|
|
|
|
// Don't generate USRs for things with invalid locations.
|
|
|
|
if (!D || D->getLocStart().isInvalid())
|
2010-04-17 08:21:38 +08:00
|
|
|
return createCXString("");
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-04-16 05:04:25 +08:00
|
|
|
// Check if the cursor has 'NoLinkage'.
|
2010-04-17 05:31:52 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
2010-04-16 05:04:25 +08:00
|
|
|
switch (ND->getLinkage()) {
|
|
|
|
case ExternalLinkage:
|
|
|
|
// Generate USRs for all entities with external linkage.
|
|
|
|
break;
|
|
|
|
case NoLinkage:
|
2010-05-07 07:38:28 +08:00
|
|
|
case UniqueExternalLinkage:
|
2010-04-16 05:04:25 +08:00
|
|
|
// We allow enums, typedefs, and structs that have no linkage to
|
|
|
|
// have USRs that are anchored to the file they were defined in
|
|
|
|
// (e.g., the header). This is a little gross, but in principal
|
|
|
|
// enums/anonymous structs/etc. defined in a common header file
|
|
|
|
// are referred to across multiple translation units.
|
2010-04-16 05:51:13 +08:00
|
|
|
if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
|
2010-04-30 01:43:29 +08:00
|
|
|
isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
|
2010-05-07 07:38:28 +08:00
|
|
|
isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
|
2010-04-16 05:04:25 +08:00
|
|
|
break;
|
|
|
|
// Fall-through.
|
|
|
|
case InternalLinkage:
|
2010-04-30 01:43:29 +08:00
|
|
|
if (isa<FunctionDecl>(ND))
|
|
|
|
break;
|
2010-04-16 05:04:25 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG(&C);
|
|
|
|
UG->Visit(D);
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
if (UG->ignoreResults())
|
2010-03-20 04:39:03 +08:00
|
|
|
return createCXString("");
|
2010-03-13 10:50:34 +08:00
|
|
|
|
2010-05-08 04:07:23 +08:00
|
|
|
#if 0
|
2010-04-21 07:15:40 +08:00
|
|
|
// For development testing.
|
2010-05-08 04:07:23 +08:00
|
|
|
assert(UG.str().size() > 2);
|
|
|
|
#endif
|
2010-04-21 07:15:40 +08:00
|
|
|
|
2010-04-12 06:20:26 +08:00
|
|
|
// Return a copy of the string that must be disposed by the caller.
|
2010-05-08 04:07:23 +08:00
|
|
|
return createCXString(UG.str(), true);
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
2010-04-12 06:20:26 +08:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
CXString clang_getCursorUSR(CXCursor C) {
|
2010-04-12 06:20:34 +08:00
|
|
|
const CXCursorKind &K = clang_getCursorKind(C);
|
|
|
|
|
|
|
|
if (clang_isDeclaration(K))
|
2010-04-12 06:20:26 +08:00
|
|
|
return getDeclCursorUSR(C);
|
2010-04-12 06:20:34 +08:00
|
|
|
|
|
|
|
if (K == CXCursor_MacroDefinition) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG(&C);
|
|
|
|
UG << "macro@"
|
|
|
|
<< cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
|
|
|
|
return createCXString(UG.str(), true);
|
2010-04-12 06:20:34 +08:00
|
|
|
}
|
|
|
|
|
2010-04-12 06:20:26 +08:00
|
|
|
return createCXString("");
|
|
|
|
}
|
|
|
|
|
2010-03-13 10:50:34 +08:00
|
|
|
CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG;
|
|
|
|
UG << extractUSRSuffix(clang_getCString(classUSR));
|
|
|
|
UG->GenObjCIvar(name);
|
|
|
|
return createCXString(UG.str(), true);
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCMethod(const char *name,
|
|
|
|
unsigned isInstanceMethod,
|
|
|
|
CXString classUSR) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG;
|
|
|
|
UG << extractUSRSuffix(clang_getCString(classUSR));
|
|
|
|
UG->GenObjCMethod(name, isInstanceMethod);
|
|
|
|
return createCXString(UG.str(), true);
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCClass(const char *name) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG;
|
|
|
|
UG->GenObjCClass(name);
|
|
|
|
return createCXString(UG.str(), true);
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCProtocol(const char *name) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG;
|
|
|
|
UG->GenObjCProtocol(name);
|
|
|
|
return createCXString(UG.str(), true);
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
2010-03-16 01:38:58 +08:00
|
|
|
CXString clang_constructUSR_ObjCCategory(const char *class_name,
|
2010-03-25 10:00:36 +08:00
|
|
|
const char *category_name) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG;
|
|
|
|
UG->GenObjCCategory(class_name, category_name);
|
|
|
|
return createCXString(UG.str(), true);
|
2010-03-13 10:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCProperty(const char *property,
|
|
|
|
CXString classUSR) {
|
2010-05-08 04:07:23 +08:00
|
|
|
USRGenerator UG;
|
|
|
|
UG << extractUSRSuffix(clang_getCString(classUSR));
|
|
|
|
UG->GenObjCProperty(property);
|
|
|
|
return createCXString(UG.str(), true);
|
2010-01-19 04:23:29 +08:00
|
|
|
}
|
2010-01-06 06:06:45 +08:00
|
|
|
|
|
|
|
} // end extern "C"
|