2010-01-15 10:39:40 -05: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-23 06:10:26 -05:00
|
|
|
#include "CXCursor.h"
|
2010-01-15 10:39:40 -05:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// USR generation.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
class USRGenerator : public DeclVisitor<USRGenerator> {
|
|
|
|
|
llvm::raw_ostream &Out;
|
2010-01-23 06:10:26 -05:00
|
|
|
bool IgnoreResults;
|
2010-01-15 10:39:40 -05:00
|
|
|
public:
|
2010-01-23 06:10:26 -05:00
|
|
|
USRGenerator(llvm::raw_ostream &out) : Out(out), IgnoreResults(false) {}
|
|
|
|
|
|
|
|
|
|
bool ignoreResults() const { return IgnoreResults; }
|
2010-01-15 10:39:40 -05:00
|
|
|
|
|
|
|
|
void VisitBlockDecl(BlockDecl *D);
|
|
|
|
|
void VisitDeclContext(DeclContext *D);
|
2010-01-23 06:10:26 -05:00
|
|
|
void VisitFieldDecl(FieldDecl *D);
|
2010-01-15 10:39:40 -05:00
|
|
|
void VisitFunctionDecl(FunctionDecl *D);
|
|
|
|
|
void VisitNamedDecl(NamedDecl *D);
|
|
|
|
|
void VisitNamespaceDecl(NamespaceDecl *D);
|
|
|
|
|
void VisitObjCContainerDecl(ObjCContainerDecl *CD);
|
|
|
|
|
void VisitObjCMethodDecl(ObjCMethodDecl *MD);
|
|
|
|
|
void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
|
2010-01-23 06:10:26 -05:00
|
|
|
void VisitTagDecl(TagDecl *D);
|
2010-01-15 10:39:40 -05:00
|
|
|
void VisitTypedefDecl(TypedefDecl *D);
|
|
|
|
|
};
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitBlockDecl(BlockDecl *D) {
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
|
// FIXME: Better support for anonymous blocks.
|
|
|
|
|
Out << "@B^anon";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitDeclContext(DeclContext *DC) {
|
|
|
|
|
if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
|
|
|
|
|
Visit(D);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 06:10:26 -05:00
|
|
|
void USRGenerator::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
|
const std::string &s = D->getNameAsString();
|
|
|
|
|
if (s.empty()) {
|
|
|
|
|
// Bit fields can be anonymous.
|
|
|
|
|
IgnoreResults = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
|
Out << "@^FI^" << s;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 10:39:40 -05:00
|
|
|
void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
|
Out << "@F^" << D->getNameAsString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitNamedDecl(NamedDecl *D) {
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
|
const std::string &s = D->getNameAsString();
|
2010-02-16 04:31:36 -05:00
|
|
|
// assert(!s.empty());
|
2010-01-15 10:39:40 -05:00
|
|
|
Out << "@^" << s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
|
Out << "@N^" << D->getNameAsString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
|
|
|
|
|
Visit(cast<Decl>(D->getDeclContext()));
|
|
|
|
|
Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
|
|
|
|
|
Out << DeclarationName(D->getSelector()).getAsString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
|
|
|
|
|
switch (D->getKind()) {
|
|
|
|
|
default:
|
|
|
|
|
assert(false && "Invalid ObjC container.");
|
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
|
Out << "objc(cs)" << D->getName();
|
|
|
|
|
break;
|
|
|
|
|
case Decl::ObjCCategory: {
|
|
|
|
|
ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
|
|
|
|
|
Out << "objc(cy)" << CD->getClassInterface()->getName()
|
2010-01-23 06:10:26 -05:00
|
|
|
<< '^' << CD->getName();
|
2010-01-15 10:39:40 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Decl::ObjCCategoryImpl: {
|
|
|
|
|
ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
|
|
|
|
|
Out << "objc(cy)" << CD->getClassInterface()->getName()
|
2010-01-23 06:10:26 -05:00
|
|
|
<< '^' << CD->getName();
|
2010-01-15 10:39:40 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
|
Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
|
|
|
|
|
Visit(cast<Decl>(D->getDeclContext()));
|
|
|
|
|
Out << "(py)" << D->getName();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 06:10:26 -05:00
|
|
|
void USRGenerator::VisitTagDecl(TagDecl *D) {
|
|
|
|
|
VisitDeclContext(D->getDeclContext());
|
|
|
|
|
switch (D->getTagKind()) {
|
|
|
|
|
case TagDecl::TK_struct: Out << "@S^"; break;
|
|
|
|
|
case TagDecl::TK_class: Out << "@C^"; break;
|
|
|
|
|
case TagDecl::TK_union: Out << "@U^"; break;
|
|
|
|
|
case TagDecl::TK_enum: Out << "@E^"; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: Better support for anonymous structures and enums.
|
|
|
|
|
const std::string &s = D->getNameAsString();
|
|
|
|
|
if (s.empty()) {
|
|
|
|
|
if (TypedefDecl *TD = D->getTypedefForAnonDecl())
|
|
|
|
|
Out << "^anontd^" << TD->getNameAsString();
|
|
|
|
|
else
|
|
|
|
|
Out << "^anon";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Out << s;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 10:39:40 -05:00
|
|
|
void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
|
DeclContext *DC = D->getDeclContext();
|
|
|
|
|
if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
|
|
|
|
|
Visit(DCN);
|
|
|
|
|
Out << "typedef@" << D->getName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: This is a skeleton implementation. It will be overhauled.
|
2010-01-23 06:10:26 -05:00
|
|
|
static CXString ConstructUSR(Decl *D) {
|
2010-01-15 10:39:40 -05:00
|
|
|
llvm::SmallString<1024> StrBuf;
|
|
|
|
|
{
|
|
|
|
|
llvm::raw_svector_ostream Out(StrBuf);
|
|
|
|
|
USRGenerator UG(Out);
|
|
|
|
|
UG.Visit(static_cast<Decl*>(D));
|
2010-01-23 06:10:26 -05:00
|
|
|
if (UG.ignoreResults())
|
|
|
|
|
return CIndexer::createCXString(NULL);
|
2010-01-15 10:39:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StrBuf.empty())
|
|
|
|
|
return CIndexer::createCXString(NULL);
|
2010-01-23 06:10:26 -05:00
|
|
|
|
2010-01-15 10:39:40 -05:00
|
|
|
// Return a copy of the string that must be disposed by the caller.
|
|
|
|
|
return CIndexer::createCXString(StrBuf.c_str(), true);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 06:10:26 -05:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
CXString clang_getCursorUSR(CXCursor C) {
|
|
|
|
|
if (Decl *D = cxcursor::getCursorDecl(C))
|
|
|
|
|
return ConstructUSR(D);
|
|
|
|
|
|
|
|
|
|
return CIndexer::createCXString(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 10:39:40 -05:00
|
|
|
} // end extern "C"
|