From: Michael Wu Date: Fri, 3 Aug 2018 05:20:23 +0000 (+0000) Subject: [libclang 6/8] Add support for reading implicit attributes X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1fc060c4f780c95c33a3724c1981bb5d3fbb9737;p=clang [libclang 6/8] Add support for reading implicit attributes Summary: Having access to implicit attributes is sometimes useful so users of libclang don't have to duplicate some of the logic in sema. This depends on D49081 since it also adds a CXTranslationUnit flag. Reviewers: yvvan, jbcoe Reviewed By: yvvan Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D49631 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@338815 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index a0ca0128c8..481f65ac43 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -1336,7 +1336,12 @@ enum CXTranslationUnit_Flags { /** * Used to indicate that attributed types should be included in CXType. */ - CXTranslationUnit_IncludeAttributedTypes = 0x1000 + CXTranslationUnit_IncludeAttributedTypes = 0x1000, + + /** + * Used to indicate that implicit attributes should be visited. + */ + CXTranslationUnit_VisitImplicitAttributes = 0x2000 }; /** diff --git a/test/Index/implicit-attrs.m b/test/Index/implicit-attrs.m new file mode 100644 index 0000000000..ca651cdb79 --- /dev/null +++ b/test/Index/implicit-attrs.m @@ -0,0 +1,6 @@ +@interface Foo +-(instancetype)init; +@end + +// RUN: env CINDEXTEST_VISIT_IMPLICIT_ATTRIBUTES=1 c-index-test -test-print-decl-attributes %s -fobjc-arc | FileCheck %s +// CHECK: ObjCInstanceMethodDecl=init:2:16 attribute(ns_consumes_self)= attribute(ns_returns_retained)= diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 336e35bc76..41dbbe73a4 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -86,6 +86,8 @@ static unsigned getDefaultParsingOptions() { options |= CXTranslationUnit_LimitSkipFunctionBodiesToPreamble; if (getenv("CINDEXTEST_INCLUDE_ATTRIBUTED_TYPES")) options |= CXTranslationUnit_IncludeAttributedTypes; + if (getenv("CINDEXTEST_VISIT_IMPLICIT_ATTRIBUTES")) + options |= CXTranslationUnit_VisitImplicitAttributes; return options; } @@ -1782,6 +1784,23 @@ static enum CXChildVisitResult PrintTypeDeclaration(CXCursor cursor, CXCursor p, return CXChildVisit_Recurse; } +/******************************************************************************/ +/* Declaration attributes testing */ +/******************************************************************************/ + +static enum CXChildVisitResult PrintDeclAttributes(CXCursor cursor, CXCursor p, + CXClientData d) { + if (clang_isDeclaration(cursor.kind)) { + printf("\n"); + PrintCursor(cursor, NULL); + return CXChildVisit_Recurse; + } else if (clang_isAttribute(cursor.kind)) { + printf(" "); + PrintCursor(cursor, NULL); + } + return CXChildVisit_Continue; +} + /******************************************************************************/ /* Target information testing. */ /******************************************************************************/ @@ -4793,6 +4812,9 @@ int cindextest_main(int argc, const char **argv) { else if (argc > 2 && strcmp(argv[1], "-test-print-type-declaration") == 0) return perform_test_load_source(argc - 2, argv + 2, "all", PrintTypeDeclaration, 0); + else if (argc > 2 && strcmp(argv[1], "-test-print-decl-attributes") == 0) + return perform_test_load_source(argc - 2, argv + 2, "all", + PrintDeclAttributes, 0); else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0) return perform_test_load_source(argc - 2, argv + 2, "all", PrintBitWidth, 0); diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 2f96d3058a..9ab0009b3f 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -1802,7 +1802,9 @@ bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { bool CursorVisitor::VisitAttributes(Decl *D) { for (const auto *I : D->attrs()) - if (!I->isImplicit() && Visit(MakeCXCursor(I, D, TU))) + if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes || + !I->isImplicit()) && + Visit(MakeCXCursor(I, D, TU))) return true; return false;