[libclang] Introduce clang_Cursor_isExternalSymbol that provides info about decls marked with external_source_symbol attribute

llvm-svn: 302677
This commit is contained in:
Argyrios Kyrtzidis 2017-05-10 15:10:36 +00:00
parent 30a7157372
commit 0381cc74c7
5 changed files with 72 additions and 1 deletions

View File

@ -32,7 +32,7 @@
* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
*/
#define CINDEX_VERSION_MAJOR 0
#define CINDEX_VERSION_MINOR 38
#define CINDEX_VERSION_MINOR 39
#define CINDEX_VERSION_ENCODE(major, minor) ( \
((major) * 10000) \
@ -4080,6 +4080,23 @@ CINDEX_LINKAGE unsigned clang_Cursor_isObjCOptional(CXCursor C);
*/
CINDEX_LINKAGE unsigned clang_Cursor_isVariadic(CXCursor C);
/**
* \brief Returns non-zero if the given cursor points to a symbol marked with
* external_source_symbol attribute.
*
* \param language If non-NULL, and the attribute is present, will be set to
* the 'language' string from the attribute.
*
* \param definedIn If non-NULL, and the attribute is present, will be set to
* the 'definedIn' string from the attribute.
*
* \param isGenerated If non-NULL, and the attribute is present, will be set to
* non-zero is the 'generated_declaration' is set in the attribute.
*/
CINDEX_LINKAGE unsigned clang_Cursor_isExternalSymbol(CXCursor C,
CXString *language, CXString *definedIn,
unsigned *isGenerated);
/**
* \brief Given a cursor that represents a declaration, return the associated
* comment's source range. The range may include multiple consecutive comments

View File

@ -154,6 +154,12 @@ SomeT someVar;
typedef MY_TYPE2(SomeT2) { int x; };
SomeT2 someVar2;
#define GEN_DECL(mod_name) __attribute__((external_source_symbol(language="Swift", defined_in=mod_name, generated_declaration)))
GEN_DECL("some_module")
@interface ExtCls
-(void)method;
@end
// RUN: c-index-test -cursor-at=%s:4:28 -cursor-at=%s:5:28 %s | FileCheck -check-prefix=CHECK-PROP %s
// CHECK-PROP: ObjCPropertyDecl=foo1:4:26
@ -226,3 +232,8 @@ SomeT2 someVar2;
// CHECK-TRANSPARENT: 147:1 TypeRef=TokenPaste_t:144:9 Extent=[147:1 - 147:13] Spelling=TokenPaste_t ([147:1 - 147:13])
// CHECK-TRANSPARENT: 151:1 TypeRef=SomeT:150:17 (Transparent: struct SomeT) Extent=[151:1 - 151:6] Spelling=SomeT ([151:1 - 151:6])
// CHECK-TRANSPARENT: 155:1 TypeRef=SomeT2:154:18 Extent=[155:1 - 155:7] Spelling=SomeT2 ([155:1 - 155:7])
// RUN: c-index-test -cursor-at=%s:160:12 -cursor-at=%s:161:8 %s | FileCheck -check-prefix=CHECK-EXTERNAL %s
// CHECK-EXTERNAL: 160:12 ObjCInterfaceDecl=ExtCls:160:12 (external lang: Swift, defined: some_module, gen: 1)
// CHECK-EXTERNAL: 161:8 ObjCInstanceMethodDecl=method:161:8 (external lang: Swift, defined: some_module, gen: 1)
C

View File

@ -809,6 +809,19 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
if (clang_Cursor_isObjCOptional(Cursor))
printf(" (@optional)");
{
CXString language;
CXString definedIn;
unsigned generated;
if (clang_Cursor_isExternalSymbol(Cursor, &language, &definedIn,
&generated)) {
printf(" (external lang: %s, defined: %s, gen: %d)",
clang_getCString(language), clang_getCString(definedIn), generated);
clang_disposeString(language);
clang_disposeString(definedIn);
}
}
if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
CXType T =
clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));

View File

@ -7479,6 +7479,35 @@ unsigned clang_Cursor_isVariadic(CXCursor C) {
return 0;
}
unsigned clang_Cursor_isExternalSymbol(CXCursor C,
CXString *language, CXString *definedIn,
unsigned *isGenerated) {
if (!clang_isDeclaration(C.kind))
return 0;
const Decl *D = getCursorDecl(C);
auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* {
if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>())
return attr;
if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) {
if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>())
return attr;
}
return nullptr;
};
if (auto *attr = getExternalSymAttr(D)) {
if (language)
*language = cxstring::createDup(attr->getLanguage());
if (definedIn)
*definedIn = cxstring::createDup(attr->getDefinedIn());
if (isGenerated)
*isGenerated = attr->getGeneratedDeclaration();
return 1;
}
return 0;
}
CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
if (!clang_isDeclaration(C.kind))
return clang_getNullRange();

View File

@ -35,6 +35,7 @@ clang_Cursor_getReceiverType
clang_Cursor_isAnonymous
clang_Cursor_isBitField
clang_Cursor_isDynamicCall
clang_Cursor_isExternalSymbol
clang_Cursor_isNull
clang_Cursor_isObjCOptional
clang_Cursor_isVariadic