From: Argyrios Kyrtzidis Date: Wed, 12 Oct 2011 07:07:36 +0000 (+0000) Subject: [libclang] Allow using getDeclCursorUSR function with a Decl* and not a cursor. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b6a4ac41353476ed771d02ae6a96c2663625ffcd;p=clang [libclang] Allow using getDeclCursorUSR function with a Decl* and not a cursor. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141770 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp index f32063cfe3..d22f945391 100644 --- a/tools/libclang/CIndexUSRs.cpp +++ b/tools/libclang/CIndexUSRs.cpp @@ -34,18 +34,18 @@ class USRGenerator : public DeclVisitor { SmallVectorImpl &Buf; llvm::raw_svector_ostream Out; bool IgnoreResults; - ASTUnit *AU; + ASTContext *Context; bool generatedLoc; llvm::DenseMap TypeSubstitutions; public: - USRGenerator(const CXCursor *C = 0, SmallVectorImpl *extBuf = 0) + explicit USRGenerator(ASTContext *Ctx = 0, SmallVectorImpl *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. @@ -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; @@ -480,7 +480,7 @@ 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; @@ -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); @@ -787,12 +787,10 @@ 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 &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(D)) @@ -817,27 +815,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 +831,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 +863,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(); } diff --git a/tools/libclang/CXCursor.h b/tools/libclang/CXCursor.h index e3ca2680f1..e402d7f970 100644 --- a/tools/libclang/CXCursor.h +++ b/tools/libclang/CXCursor.h @@ -218,6 +218,11 @@ static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) { CXCursor getTypeRefCursor(CXCursor cursor); +/// \brief Generate a USR for \arg D and put it in \arg Buf. +/// \returns true if no USR was computed or the result should be ignored, +/// false otherwise. +bool getDeclCursorUSR(Decl *D, SmallVectorImpl &Buf); + bool operator==(CXCursor X, CXCursor Y); inline bool operator!=(CXCursor X, CXCursor Y) {