From 806baca8ec6ca20af972e6842a3448416a7ee91e Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Wed, 3 Jan 2018 09:49:31 +0000 Subject: [PATCH] [libclang] Support querying whether a declaration is invalid This is useful for e.g. highlighting purposes in an IDE. Patch by Nikolai Kosjar. Differential Revision: https://reviews.llvm.org/D40072 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@321697 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang-c/Index.h | 36 ++++++++++++++++++++----------- test/Index/print-type-size.cpp | 16 +++++++------- tools/c-index-test/c-index-test.c | 14 ++++++------ tools/libclang/CIndex.cpp | 21 ++++++++++++------ tools/libclang/libclang.exports | 13 +++++------ 5 files changed, 61 insertions(+), 39 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 587008a721..2ea801913f 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -29,13 +29,13 @@ * CINDEX_VERSION_MAJOR is intended for "major" source/ABI breaking changes. * * The policy about the libclang API was always to keep it source and ABI - * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable. - */ -#define CINDEX_VERSION_MAJOR 0 -#define CINDEX_VERSION_MINOR 45 - -#define CINDEX_VERSION_ENCODE(major, minor) ( \ - ((major) * 10000) \ + * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable. + */ +#define CINDEX_VERSION_MAJOR 0 +#define CINDEX_VERSION_MINOR 46 + +#define CINDEX_VERSION_ENCODE(major, minor) ( \ + ((major) * 10000) \ + ((minor) * 1)) #define CINDEX_VERSION CINDEX_VERSION_ENCODE( \ @@ -2638,12 +2638,22 @@ CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor); /** * \brief Determine whether the given cursor kind represents a declaration. - */ -CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind); - -/** - * \brief Determine whether the given cursor kind represents a simple - * reference. + */ +CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind); + +/** + * \brief Determine whether the given declaration is invalid. + * + * A declaration is invalid if it could not be parsed successfully. + * + * \returns non-zero if the cursor represents a declaration and it is + * invalid, otherwise NULL. + */ +CINDEX_LINKAGE unsigned clang_isInvalidDeclaration(CXCursor); + +/** + * \brief Determine whether the given cursor kind represents a simple + * reference. * * Note that other kinds of cursors (such as expressions) can also refer to * other cursors. Use clang_getCursorReferenced() to determine whether a diff --git a/test/Index/print-type-size.cpp b/test/Index/print-type-size.cpp index 45de93f308..3aa172f99a 100644 --- a/test/Index/print-type-size.cpp +++ b/test/Index/print-type-size.cpp @@ -1,14 +1,14 @@ // from SemaCXX/class-layout.cpp // RUN: c-index-test -test-print-type-size %s -target x86_64-pc-linux-gnu | FileCheck -check-prefix=CHECK64 %s // RUN: c-index-test -test-print-type-size %s -target i386-apple-darwin9 | FileCheck -check-prefix=CHECK32 %s - -namespace basic { - -// CHECK64: VarDecl=v:[[@LINE+2]]:6 (Definition) [type=void] [typekind=Void] -// CHECK32: VarDecl=v:[[@LINE+1]]:6 (Definition) [type=void] [typekind=Void] -void v; - -// CHECK64: VarDecl=v1:[[@LINE+2]]:7 (Definition) [type=void *] [typekind=Pointer] [sizeof=8] [alignof=8] + +namespace basic { + +// CHECK64: VarDecl=v:[[@LINE+2]]:6 (Definition) (invalid) [type=void] [typekind=Void] +// CHECK32: VarDecl=v:[[@LINE+1]]:6 (Definition) (invalid) [type=void] [typekind=Void] +void v; + +// CHECK64: VarDecl=v1:[[@LINE+2]]:7 (Definition) [type=void *] [typekind=Pointer] [sizeof=8] [alignof=8] // CHECK32: VarDecl=v1:[[@LINE+1]]:7 (Definition) [type=void *] [typekind=Pointer] [sizeof=4] [alignof=4] void *v1; diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 99f05669b6..7ff814b6d6 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -809,12 +809,14 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) { if (clang_EnumDecl_isScoped(Cursor)) printf(" (scoped)"); if (clang_Cursor_isVariadic(Cursor)) - printf(" (variadic)"); - if (clang_Cursor_isObjCOptional(Cursor)) - printf(" (@optional)"); - - switch (clang_getCursorExceptionSpecificationType(Cursor)) - { + printf(" (variadic)"); + if (clang_Cursor_isObjCOptional(Cursor)) + printf(" (@optional)"); + if (clang_isInvalidDeclaration(Cursor)) + printf(" (invalid)"); + + switch (clang_getCursorExceptionSpecificationType(Cursor)) + { case CXCursor_ExceptionSpecificationKind_None: break; diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index f4d347108c..70bccc7eea 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -5418,12 +5418,21 @@ unsigned clang_isInvalid(enum CXCursorKind K) { unsigned clang_isDeclaration(enum CXCursorKind K) { return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || - (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); -} - -unsigned clang_isReference(enum CXCursorKind K) { - return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; -} + (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); +} + +unsigned clang_isInvalidDeclaration(CXCursor C) { + if (clang_isDeclaration(C.kind)) { + if (const Decl *D = getCursorDecl(C)) + return D->isInvalidDecl(); + } + + return 0; +} + +unsigned clang_isReference(enum CXCursorKind K) { + return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; +} unsigned clang_isExpression(enum CXCursorKind K) { return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index bf95b97073..f5d2de5823 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -288,12 +288,13 @@ clang_index_isEntityObjCContainerKind clang_index_setClientContainer clang_index_setClientEntity clang_isAttribute -clang_isConstQualifiedType -clang_isCursorDefinition -clang_isDeclaration -clang_isExpression -clang_isFileMultipleIncludeGuarded -clang_isFunctionTypeVariadic +clang_isConstQualifiedType +clang_isCursorDefinition +clang_isDeclaration +clang_isInvalidDeclaration +clang_isExpression +clang_isFileMultipleIncludeGuarded +clang_isFunctionTypeVariadic clang_isInvalid clang_isPODType clang_isPreprocessing -- 2.40.0