2012-12-18 22:30:41 +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"
|
|
|
|
#include "CXCursor.h"
|
|
|
|
#include "CXString.h"
|
2014-03-29 06:21:26 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
2015-01-14 19:23:58 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2013-08-17 08:40:41 +08:00
|
|
|
#include "clang/Index/USRGeneration.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "clang/Lex/PreprocessingRecord.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2013-08-21 08:49:25 +08:00
|
|
|
using namespace clang::index;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// API hooks.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static inline StringRef extractUSRSuffix(StringRef s) {
|
|
|
|
return s.startswith("c:") ? s.substr(2) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
|
2013-08-21 08:49:25 +08:00
|
|
|
return generateUSRForDecl(D, Buf);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getCursorUSR(CXCursor C) {
|
|
|
|
const CXCursorKind &K = clang_getCursorKind(C);
|
|
|
|
|
|
|
|
if (clang_isDeclaration(K)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
CXTranslationUnit TU = cxcursor::getCursorTU(C);
|
|
|
|
if (!TU)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-02-03 21:52:47 +08:00
|
|
|
cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!buf)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
|
|
|
|
if (Ignore) {
|
2013-01-27 06:44:19 +08:00
|
|
|
buf->dispose();
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
if (!TU)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-02-03 21:52:47 +08:00
|
|
|
cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!buf)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-03-29 06:21:26 +08:00
|
|
|
bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C),
|
|
|
|
cxtu::getASTUnit(TU)->getSourceManager(),
|
|
|
|
buf->Data);
|
|
|
|
if (Ignore) {
|
|
|
|
buf->dispose();
|
|
|
|
return cxstring::createEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the C-string, but don't make a copy since it is already in
|
|
|
|
// the string buffer.
|
2012-12-18 22:30:41 +08:00
|
|
|
buf->Data.push_back('\0');
|
|
|
|
return createCXString(buf);
|
|
|
|
}
|
|
|
|
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
|
2013-08-21 09:51:19 +08:00
|
|
|
SmallString<128> Buf(getUSRSpacePrefix());
|
2013-08-17 02:17:55 +08:00
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
|
|
|
OS << extractUSRSuffix(clang_getCString(classUSR));
|
2013-08-21 08:49:25 +08:00
|
|
|
generateUSRForObjCIvar(name, OS);
|
2013-08-17 02:17:55 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCMethod(const char *name,
|
|
|
|
unsigned isInstanceMethod,
|
|
|
|
CXString classUSR) {
|
2013-08-21 09:51:19 +08:00
|
|
|
SmallString<128> Buf(getUSRSpacePrefix());
|
2013-08-17 02:17:55 +08:00
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
|
|
|
OS << extractUSRSuffix(clang_getCString(classUSR));
|
2013-08-21 08:49:25 +08:00
|
|
|
generateUSRForObjCMethod(name, isInstanceMethod, OS);
|
2013-08-17 02:17:55 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCClass(const char *name) {
|
2013-08-21 09:51:19 +08:00
|
|
|
SmallString<128> Buf(getUSRSpacePrefix());
|
2013-08-17 02:17:55 +08:00
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
2013-08-21 08:49:25 +08:00
|
|
|
generateUSRForObjCClass(name, OS);
|
2013-08-17 02:17:55 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCProtocol(const char *name) {
|
2013-08-21 09:51:19 +08:00
|
|
|
SmallString<128> Buf(getUSRSpacePrefix());
|
2013-08-17 02:17:55 +08:00
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
2013-08-21 08:49:25 +08:00
|
|
|
generateUSRForObjCProtocol(name, OS);
|
2013-08-17 02:17:55 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCCategory(const char *class_name,
|
|
|
|
const char *category_name) {
|
2013-08-21 09:51:19 +08:00
|
|
|
SmallString<128> Buf(getUSRSpacePrefix());
|
2013-08-17 02:17:55 +08:00
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
2013-08-21 08:49:25 +08:00
|
|
|
generateUSRForObjCCategory(class_name, category_name, OS);
|
2013-08-17 02:17:55 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_constructUSR_ObjCProperty(const char *property,
|
|
|
|
CXString classUSR) {
|
2013-08-21 09:51:19 +08:00
|
|
|
SmallString<128> Buf(getUSRSpacePrefix());
|
2013-08-17 02:17:55 +08:00
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
|
|
|
OS << extractUSRSuffix(clang_getCString(classUSR));
|
2016-07-16 06:18:19 +08:00
|
|
|
generateUSRForObjCProperty(property, /*isClassProp=*/false, OS);
|
2013-08-17 02:17:55 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|