aboutsummaryrefslogtreecommitdiff
path: root/tools/libclang/CIndexUSRs.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/libclang/CIndexUSRs.cpp')
-rw-r--r--tools/libclang/CIndexUSRs.cpp106
1 files changed, 57 insertions, 49 deletions
diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp
index 4f1f071c1dd2..121d67d1d2e7 100644
--- a/tools/libclang/CIndexUSRs.cpp
+++ b/tools/libclang/CIndexUSRs.cpp
@@ -31,28 +31,28 @@ using namespace clang::cxstring;
namespace {
class USRGenerator : public DeclVisitor<USRGenerator> {
llvm::OwningPtr<llvm::SmallString<128> > OwnedBuf;
- llvm::SmallVectorImpl<char> &Buf;
+ SmallVectorImpl<char> &Buf;
llvm::raw_svector_ostream Out;
bool IgnoreResults;
- ASTUnit *AU;
+ ASTContext *Context;
bool generatedLoc;
llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
public:
- USRGenerator(const CXCursor *C = 0, llvm::SmallVectorImpl<char> *extBuf = 0)
+ explicit USRGenerator(ASTContext *Ctx = 0, SmallVectorImpl<char> *extBuf = 0)
: OwnedBuf(extBuf ? 0 : new llvm::SmallString<128>()),
Buf(extBuf ? *extBuf : *OwnedBuf.get()),
Out(Buf),
IgnoreResults(false),
- AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
+ Context(Ctx),
generatedLoc(false)
{
// Add the USR space prefix.
Out << "c:";
}
- llvm::StringRef str() {
+ StringRef str() {
return Out.str();
}
@@ -114,19 +114,19 @@ public:
/// itself.
/// Generate a USR for an Objective-C class.
- void GenObjCClass(llvm::StringRef cls);
+ void GenObjCClass(StringRef cls);
/// Generate a USR for an Objective-C class category.
- void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
+ void GenObjCCategory(StringRef cls, 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);
+ void GenObjCIvar(StringRef ivar);
/// Generate a USR fragment for an Objective-C method.
- void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
+ void GenObjCMethod(StringRef sel, bool isInstanceMethod);
/// Generate a USR fragment for an Objective-C property.
- void GenObjCProperty(llvm::StringRef prop);
+ void GenObjCProperty(StringRef prop);
/// Generate a USR for an Objective-C protocol.
- void GenObjCProtocol(llvm::StringRef prot);
+ void GenObjCProtocol(StringRef prot);
void VisitType(QualType T);
void VisitTemplateParameterList(const TemplateParameterList *Params);
@@ -190,7 +190,7 @@ void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Out << "@F@";
D->printName(Out);
- ASTContext &Ctx = AU->getASTContext();
+ ASTContext &Ctx = *Context;
if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
return;
@@ -235,7 +235,7 @@ void USRGenerator::VisitVarDecl(VarDecl *D) {
VisitDeclContext(D->getDeclContext());
// Variables always have simple names.
- llvm::StringRef s = D->getName();
+ StringRef s = D->getName();
// 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.:
@@ -320,7 +320,7 @@ void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
switch (D->getKind()) {
default:
- assert(false && "Invalid ObjC container.");
+ llvm_unreachable("Invalid ObjC container.");
case Decl::ObjCInterface:
case Decl::ObjCImplementation:
GenObjCClass(D->getName());
@@ -433,7 +433,7 @@ void USRGenerator::VisitTagDecl(TagDecl *D) {
if (EmitDeclName(D)) {
if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
Buf[off] = 'A';
- Out << '@' << TD;
+ Out << '@' << *TD;
}
else
Buf[off] = 'a';
@@ -480,13 +480,13 @@ bool USRGenerator::GenLoc(const Decl *D) {
// Use the location of canonical decl.
D = D->getCanonicalDecl();
- const SourceManager &SM = AU->getSourceManager();
+ const SourceManager &SM = Context->getSourceManager();
SourceLocation L = D->getLocStart();
if (L.isInvalid()) {
IgnoreResults = true;
return true;
}
- L = SM.getInstantiationLoc(L);
+ L = SM.getExpansionLoc(L);
const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
if (FE) {
@@ -508,7 +508,7 @@ 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.
- ASTContext &Ctx = AU->getASTContext();
+ ASTContext &Ctx = *Context;
do {
T = Ctx.getCanonicalType(T);
@@ -570,6 +570,8 @@ void USRGenerator::VisitType(QualType T) {
c = 'K'; break;
case BuiltinType::Int128:
c = 'J'; break;
+ case BuiltinType::Half:
+ c = 'h'; break;
case BuiltinType::Float:
c = 'f'; break;
case BuiltinType::Double:
@@ -755,27 +757,27 @@ void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
// General purpose USR generation methods.
//===----------------------------------------------------------------------===//
-void USRGenerator::GenObjCClass(llvm::StringRef cls) {
+void USRGenerator::GenObjCClass(StringRef cls) {
Out << "objc(cs)" << cls;
}
-void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
+void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) {
Out << "objc(cy)" << cls << '@' << cat;
}
-void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
+void USRGenerator::GenObjCIvar(StringRef ivar) {
Out << '@' << ivar;
}
-void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
+void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) {
Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
}
-void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
+void USRGenerator::GenObjCProperty(StringRef prop) {
Out << "(py)" << prop;
}
-void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
+void USRGenerator::GenObjCProtocol(StringRef prot) {
Out << "objc(pl)" << prot;
}
@@ -783,16 +785,14 @@ void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
// API hooks.
//===----------------------------------------------------------------------===//
-static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
+static inline StringRef extractUSRSuffix(StringRef s) {
return s.startswith("c:") ? s.substr(2) : "";
}
-static CXString getDeclCursorUSR(const CXCursor &C) {
- Decl *D = cxcursor::getCursorDecl(C);
-
+bool cxcursor::getDeclCursorUSR(Decl *D, SmallVectorImpl<char> &Buf) {
// Don't generate USRs for things with invalid locations.
if (!D || D->getLocStart().isInvalid())
- return createCXString("");
+ return true;
// Check if the cursor has 'NoLinkage'.
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
@@ -817,27 +817,15 @@ static CXString getDeclCursorUSR(const CXCursor &C) {
break;
}
- CXTranslationUnit TU = cxcursor::getCursorTU(C);
- if (!TU)
- return createCXString("");
-
- CXStringBuf *buf = cxstring::getCXStringBuf(TU);
- if (!buf)
- return createCXString("");
-
{
- USRGenerator UG(&C, &buf->Data);
+ USRGenerator UG(&D->getASTContext(), &Buf);
UG->Visit(D);
- if (UG->ignoreResults()) {
- disposeCXStringBuf(buf);
- return createCXString("");
- }
+ if (UG->ignoreResults())
+ return true;
}
- // Return the C-string, but don't make a copy since it is already in
- // the string buffer.
- buf->Data.push_back('\0');
- return createCXString(buf);
+
+ return false;
}
extern "C" {
@@ -845,8 +833,27 @@ extern "C" {
CXString clang_getCursorUSR(CXCursor C) {
const CXCursorKind &K = clang_getCursorKind(C);
- if (clang_isDeclaration(K))
- return getDeclCursorUSR(C);
+ if (clang_isDeclaration(K)) {
+ Decl *D = cxcursor::getCursorDecl(C);
+ CXTranslationUnit TU = cxcursor::getCursorTU(C);
+ if (!TU)
+ return createCXString("");
+
+ CXStringBuf *buf = cxstring::getCXStringBuf(TU);
+ if (!buf)
+ return createCXString("");
+
+ bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
+ if (Ignore) {
+ disposeCXStringBuf(buf);
+ return createCXString("");
+ }
+
+ // Return the C-string, but don't make a copy since it is already in
+ // the string buffer.
+ buf->Data.push_back('\0');
+ return createCXString(buf);
+ }
if (K == CXCursor_MacroDefinition) {
CXTranslationUnit TU = cxcursor::getCursorTU(C);
@@ -858,7 +865,8 @@ CXString clang_getCursorUSR(CXCursor C) {
return createCXString("");
{
- USRGenerator UG(&C, &buf->Data);
+ USRGenerator UG(&cxcursor::getCursorASTUnit(C)->getASTContext(),
+ &buf->Data);
UG << "macro@"
<< cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
}