[cfe-commits] r93267 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndexUSRs.cpp
Ted Kremenek
kremenek at apple.com
Tue Jan 12 15:33:42 PST 2010
Author: kremenek
Date: Tue Jan 12 17:33:42 2010
New Revision: 93267
URL: http://llvm.org/viewvc/llvm-project?rev=93267&view=rev
Log:
Rename clang_getUSR() -> clang_getDeclUSR(). For now we take a CXDecl instead of a CXEntity.
Enhance USR generation a bit with support for records.
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/tools/CIndex/CIndexUSRs.cpp
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=93267&r1=93266&r2=93267&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Jan 12 17:33:42 2010
@@ -329,7 +329,6 @@
* in a specified translation unit. */
CINDEX_LINKAGE CXDecl clang_getDeclaration(CXEntity, CXTranslationUnit);
-CINDEX_LINKAGE CXString clang_getUSR(CXEntity);
/*
* CXDecl Operations.
*/
@@ -338,6 +337,7 @@
CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl);
CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl);
+CINDEX_LINKAGE CXString clang_getDeclUSR(CXDecl);
CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl);
Modified: cfe/trunk/tools/CIndex/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexUSRs.cpp?rev=93267&r1=93266&r2=93267&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexUSRs.cpp Tue Jan 12 17:33:42 2010
@@ -62,57 +62,55 @@
return ((CIndexer*) CIdx)->getProgram();
}
-extern "C" {
-
-/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
-/// in a specified translation unit.
-CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
- return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
-}
-
-
-CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
- if (Decl *D = (Decl *) CE)
- return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
- return NullCXEntity();
-}
-
//===----------------------------------------------------------------------===//
// USR generation.
//===----------------------------------------------------------------------===//
namespace {
-class USRGenerator : public DeclVisitor<USRGenerator> {
- llvm::raw_ostream &Out;
-public:
- USRGenerator(llvm::raw_ostream &out) : Out(out) {}
-
- void VisitNamedDecl(NamedDecl *D);
- void VisitObjCContainerDecl(ObjCContainerDecl *CD);
- void VisitObjCMethodDecl(ObjCMethodDecl *MD);
- void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
-};
+ class USRGenerator : public DeclVisitor<USRGenerator> {
+ llvm::raw_ostream &Out;
+ public:
+ USRGenerator(llvm::raw_ostream &out) : Out(out) {}
+
+ void VisitNamedDecl(NamedDecl *D);
+ void VisitObjCContainerDecl(ObjCContainerDecl *CD);
+ void VisitObjCMethodDecl(ObjCMethodDecl *MD);
+ void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
+ void VisitRecordDecl(RecordDecl *D);
+ void VisitTypedefDecl(TypedefDecl *D);
+ };
} // end anonymous namespace
void USRGenerator::VisitNamedDecl(NamedDecl *D) {
DeclContext *DC = D->getDeclContext();
- if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) {
+ if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Visit(DCN);
- Out << '_';
- }
- else {
- Out << '_';
- }
- Out << D->getName();
+
+ const std::string &s = D->getNameAsString();
+ assert(!s.empty());
+ Out << '@' << s;
}
+
+void USRGenerator::VisitRecordDecl(RecordDecl *D) {
+ DeclContext *DC = D->getDeclContext();
+ if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
+ Visit(DCN);
+ Out << "@struct^";
+ const std::string &s = D->getNameAsString();
+ if (s.empty())
+ Out << "^anon";
+ else
+ Out << s;
+}
+
void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Visit(cast<Decl>(D->getDeclContext()));
Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
- Out << DeclarationName(D->getSelector());
+ Out << DeclarationName(D->getSelector()).getAsString();
}
-
+
void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
switch (D->getKind()) {
default:
@@ -124,13 +122,13 @@
case Decl::ObjCCategory: {
ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Out << "objc(cy)" << CD->getClassInterface()->getName()
- << '_' << CD->getName();
+ << '_' << CD->getName();
break;
}
case Decl::ObjCCategoryImpl: {
ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Out << "objc(cy)" << CD->getClassInterface()->getName()
- << '_' << CD->getName();
+ << '_' << CD->getName();
break;
}
case Decl::ObjCProtocol:
@@ -138,29 +136,42 @@
break;
}
}
-
+
void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Visit(cast<Decl>(D->getDeclContext()));
Out << "(py)" << D->getName();
}
+
+void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
+ DeclContext *DC = D->getDeclContext();
+ if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
+ Visit(DCN);
+ Out << "typedef@" << D->getName();
+}
+
+extern "C" {
+
+/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
+/// in a specified translation unit.
+CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
+ return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
+}
+
+CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
+ if (Decl *D = (Decl *) CE)
+ return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
+ return NullCXEntity();
+}
+
// FIXME: This is a skeleton implementation. It will be overhauled.
-CXString clang_getUSR(CXEntity CE) {
- const Entity &E = GetEntity(CE);
-
- // FIXME: Support cross-translation unit CXEntities.
- if (!E.isInternalToTU())
- return CIndexer::createCXString(NULL);
-
- Decl *D = E.getInternalDecl();
- if (!D)
- return CIndexer::createCXString(NULL);
-
+CXString clang_getDeclUSR(CXDecl D) {
+ assert(D && "Null CXDecl passed to clang_getDeclUSR()");
llvm::SmallString<1024> StrBuf;
{
llvm::raw_svector_ostream Out(StrBuf);
USRGenerator UG(Out);
- UG.Visit(D);
+ UG.Visit(static_cast<Decl*>(D));
}
if (StrBuf.empty())
@@ -168,6 +179,6 @@
// Return a copy of the string that must be disposed by the caller.
return CIndexer::createCXString(StrBuf.c_str(), true);
-}
+}
} // end extern "C"
More information about the cfe-commits
mailing list