diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h index c50ac1b6d248..bb2cbe30e3a3 100644 --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -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 diff --git a/clang/test/Index/get-cursor.m b/clang/test/Index/get-cursor.m index af277d45fdf4..e85d49fb0c3e 100644 --- a/clang/test/Index/get-cursor.m +++ b/clang/test/Index/get-cursor.m @@ -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 \ No newline at end of file diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 1179fbf39113..1f5d60443197 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -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)); diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index c251d83e2097..9c795ae9c5b7 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -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()) + return attr; + if (auto *dcd = dyn_cast(D->getDeclContext())) { + if (auto *attr = dcd->getAttr()) + 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(); diff --git a/clang/tools/libclang/libclang.exports b/clang/tools/libclang/libclang.exports index 895dd804b008..d9a406e5741b 100644 --- a/clang/tools/libclang/libclang.exports +++ b/clang/tools/libclang/libclang.exports @@ -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