diff options
Diffstat (limited to 'tools/libclang/CXCursor.cpp')
-rw-r--r-- | tools/libclang/CXCursor.cpp | 261 |
1 files changed, 189 insertions, 72 deletions
diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp index db275001..d84cf29 100644 --- a/tools/libclang/CXCursor.cpp +++ b/tools/libclang/CXCursor.cpp @@ -46,6 +46,7 @@ static CXCursorKind GetCursorKind(const Attr *A) { case attr::Final: return CXCursor_CXXFinalAttr; case attr::Override: return CXCursor_CXXOverrideAttr; case attr::Annotate: return CXCursor_AnnotateAttr; + case attr::AsmLabel: return CXCursor_AsmLabelAttr; } return CXCursor_UnexposedAttr; @@ -205,6 +206,7 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, case Stmt::AtomicExprClass: case Stmt::BinaryConditionalOperatorClass: case Stmt::BinaryTypeTraitExprClass: + case Stmt::TypeTraitExprClass: case Stmt::CXXBindTemporaryExprClass: case Stmt::CXXDefaultArgExprClass: case Stmt::CXXScalarValueInitExprClass: @@ -219,16 +221,29 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, case Stmt::MaterializeTemporaryExprClass: case Stmt::ObjCIndirectCopyRestoreExprClass: case Stmt::OffsetOfExprClass: - case Stmt::OpaqueValueExprClass: case Stmt::ParenListExprClass: case Stmt::PredefinedExprClass: case Stmt::ShuffleVectorExprClass: case Stmt::UnaryExprOrTypeTraitExprClass: case Stmt::UnaryTypeTraitExprClass: case Stmt::VAArgExprClass: + case Stmt::ObjCArrayLiteralClass: + case Stmt::ObjCDictionaryLiteralClass: + case Stmt::ObjCNumericLiteralClass: + case Stmt::ObjCSubscriptRefExprClass: + K = CXCursor_UnexposedExpr; + break; + + case Stmt::OpaqueValueExprClass: + if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr()) + return MakeCXCursor(Src, Parent, TU, RegionOfInterest); K = CXCursor_UnexposedExpr; break; + case Stmt::PseudoObjectExprClass: + return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), + Parent, TU, RegionOfInterest); + case Stmt::CompoundStmtClass: K = CXCursor_CompoundStmt; break; @@ -384,7 +399,11 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, case Stmt::ObjCProtocolExprClass: K = CXCursor_ObjCProtocolExpr; break; - + + case Stmt::ObjCBoolLiteralExprClass: + K = CXCursor_ObjCBoolLiteralExpr; + break; + case Stmt::ObjCBridgedCastExprClass: K = CXCursor_ObjCBridgedCastExpr; break; @@ -401,7 +420,6 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, K = CXCursor_SizeOfPackExpr; break; - case Stmt::BlockDeclRefExprClass: case Stmt::DeclRefExprClass: case Stmt::DependentScopeDeclRefExprClass: case Stmt::SubstNonTypeTemplateParmExprClass: @@ -427,10 +445,15 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, case Stmt::CXXConstructExprClass: case Stmt::CXXTemporaryObjectExprClass: case Stmt::CXXUnresolvedConstructExprClass: + case Stmt::UserDefinedLiteralClass: K = CXCursor_CallExpr; break; - case Stmt::ObjCMessageExprClass: + case Stmt::LambdaExprClass: + K = CXCursor_LambdaExpr; + break; + + case Stmt::ObjCMessageExprClass: { K = CXCursor_ObjCMessageExpr; int SelectorIdIndex = -1; // Check if cursor points to a selector id. @@ -446,6 +469,11 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, CXCursor C = { K, 0, { Parent, S, TU } }; return getSelectorIdentifierCursor(SelectorIdIndex, C); } + + case Stmt::MSDependentExistsStmtClass: + K = CXCursor_UnexposedStmt; + break; + } CXCursor C = { K, 0, { Parent, S, TU } }; return C; @@ -468,12 +496,12 @@ cxcursor::getCursorObjCSuperClassRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super, +CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto, SourceLocation Loc, CXTranslationUnit TU) { - assert(Super && TU && "Invalid arguments!"); + assert(Proto && TU && "Invalid arguments!"); void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); - CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Super, RawLoc, TU } }; + CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } }; return C; } @@ -485,7 +513,7 @@ cxcursor::getCursorObjCProtocolRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, +CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class, SourceLocation Loc, CXTranslationUnit TU) { // 'Class' can be null for invalid code. @@ -493,7 +521,7 @@ CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, return MakeCXCursorInvalid(CXCursor_InvalidCode); assert(TU && "Invalid arguments!"); void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); - CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } }; + CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } }; return C; } @@ -505,11 +533,11 @@ cxcursor::getCursorObjCClassRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc, +CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc, CXTranslationUnit TU) { assert(Type && TU && "Invalid arguments!"); void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); - CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } }; + CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } }; return C; } @@ -521,12 +549,12 @@ cxcursor::getCursorTypeRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template, +CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc, CXTranslationUnit TU) { assert(Template && TU && "Invalid arguments!"); void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); - CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } }; + CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } }; return C; } @@ -538,13 +566,14 @@ cxcursor::getCursorTemplateRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, +CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS, + SourceLocation Loc, CXTranslationUnit TU) { assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU && "Invalid arguments!"); void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); - CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } }; + CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } }; return C; } @@ -556,12 +585,29 @@ cxcursor::getCursorNamespaceRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc, +CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc, + CXTranslationUnit TU) { + + assert(Var && TU && "Invalid arguments!"); + void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); + CXCursor C = { CXCursor_VariableRef, 0, { (void*)Var, RawLoc, TU } }; + return C; +} + +std::pair<VarDecl *, SourceLocation> +cxcursor::getCursorVariableRef(CXCursor C) { + assert(C.kind == CXCursor_VariableRef); + return std::make_pair(static_cast<VarDecl *>(C.data[0]), + SourceLocation::getFromRawEncoding( + reinterpret_cast<uintptr_t>(C.data[1]))); +} + +CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc, CXTranslationUnit TU) { assert(Field && TU && "Invalid arguments!"); void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); - CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } }; + CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } }; return C; } @@ -573,9 +619,9 @@ cxcursor::getCursorMemberRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } -CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, +CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B, CXTranslationUnit TU){ - CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } }; + CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } }; return C; } @@ -730,30 +776,47 @@ ASTContext &cxcursor::getCursorContext(CXCursor Cursor) { } ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) { - return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2]) - ->TUData); + CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]); + if (!TU) + return 0; + return static_cast<ASTUnit *>(TU->TUData); } CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) { return static_cast<CXTranslationUnit>(Cursor.data[2]); } -static void CollectOverriddenMethods(CXTranslationUnit TU, - DeclContext *Ctx, +static void CollectOverriddenMethodsRecurse(CXTranslationUnit TU, + ObjCContainerDecl *Container, ObjCMethodDecl *Method, - SmallVectorImpl<CXCursor> &Methods) { - if (!Ctx) + SmallVectorImpl<CXCursor> &Methods, + bool MovedToSuper) { + if (!Container) return; - // If we have a class or category implementation, jump straight to the - // interface. - if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx)) - return CollectOverriddenMethods(TU, Impl->getClassInterface(), - Method, Methods); - - ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx); - if (!Container) + // In categories look for overriden methods from protocols. A method from + // category is not "overriden" since it is considered as the "same" method + // (same USR) as the one from the interface. + if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { + // Check whether we have a matching method at this category but only if we + // are at the super class level. + if (MovedToSuper) + if (ObjCMethodDecl * + Overridden = Container->getMethod(Method->getSelector(), + Method->isInstanceMethod())) + if (Method != Overridden) { + // We found an override at this category; there is no need to look + // into its protocols. + Methods.push_back(MakeCXCursor(Overridden, TU)); + return; + } + + for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), + PEnd = Category->protocol_end(); + P != PEnd; ++P) + CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper); return; + } // Check whether we have a matching method at this level. if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), @@ -769,38 +832,37 @@ static void CollectOverriddenMethods(CXTranslationUnit TU, for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), PEnd = Protocol->protocol_end(); P != PEnd; ++P) - CollectOverriddenMethods(TU, *P, Method, Methods); - } - - if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { - for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), - PEnd = Category->protocol_end(); - P != PEnd; ++P) - CollectOverriddenMethods(TU, *P, Method, Methods); + CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper); } if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), PEnd = Interface->protocol_end(); P != PEnd; ++P) - CollectOverriddenMethods(TU, *P, Method, Methods); + CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper); for (ObjCCategoryDecl *Category = Interface->getCategoryList(); Category; Category = Category->getNextClassCategory()) - CollectOverriddenMethods(TU, Category, Method, Methods); + CollectOverriddenMethodsRecurse(TU, Category, Method, Methods, + MovedToSuper); - // We only look into the superclass if we haven't found anything yet. - if (Methods.empty()) - if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) - return CollectOverriddenMethods(TU, Super, Method, Methods); + if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) + return CollectOverriddenMethodsRecurse(TU, Super, Method, Methods, + /*MovedToSuper=*/true); } } +static inline void CollectOverriddenMethods(CXTranslationUnit TU, + ObjCContainerDecl *Container, + ObjCMethodDecl *Method, + SmallVectorImpl<CXCursor> &Methods) { + CollectOverriddenMethodsRecurse(TU, Container, Method, Methods, + /*MovedToSuper=*/false); +} + void cxcursor::getOverriddenCursors(CXCursor cursor, SmallVectorImpl<CXCursor> &overridden) { - if (!clang_isDeclaration(cursor.kind)) - return; - + assert(clang_isDeclaration(cursor.kind)); Decl *D = getCursorDecl(cursor); if (!D) return; @@ -820,8 +882,39 @@ void cxcursor::getOverriddenCursors(CXCursor cursor, if (!Method) return; - // Handle Objective-C methods. - CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden); + if (ObjCProtocolDecl * + ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) { + CollectOverriddenMethods(TU, ProtD, Method, overridden); + + } else if (ObjCImplDecl * + IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) { + ObjCInterfaceDecl *ID = IMD->getClassInterface(); + if (!ID) + return; + // Start searching for overridden methods using the method from the + // interface as starting point. + if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), + Method->isInstanceMethod())) + Method = IFaceMeth; + CollectOverriddenMethods(TU, ID, Method, overridden); + + } else if (ObjCCategoryDecl * + CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) { + ObjCInterfaceDecl *ID = CatD->getClassInterface(); + if (!ID) + return; + // Start searching for overridden methods using the method from the + // interface as starting point. + if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), + Method->isInstanceMethod())) + Method = IFaceMeth; + CollectOverriddenMethods(TU, ID, Method, overridden); + + } else { + CollectOverriddenMethods(TU, + dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()), + Method, overridden); + } } std::pair<int, SourceLocation> @@ -931,6 +1024,35 @@ CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) { return getCursorTU(cursor); } +int clang_Cursor_getNumArguments(CXCursor C) { + if (clang_isDeclaration(C.kind)) { + Decl *D = cxcursor::getCursorDecl(C); + if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) + return MD->param_size(); + if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) + return FD->param_size(); + } + + return -1; +} + +CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) { + if (clang_isDeclaration(C.kind)) { + Decl *D = cxcursor::getCursorDecl(C); + if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) { + if (i < MD->param_size()) + return cxcursor::MakeCXCursor(MD->param_begin()[i], + cxcursor::getCursorTU(C)); + } else if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { + if (i < FD->param_size()) + return cxcursor::MakeCXCursor(FD->param_begin()[i], + cxcursor::getCursorTU(C)); + } + } + + return clang_getNullCursor(); +} + } // end: extern "C" //===----------------------------------------------------------------------===// @@ -1001,33 +1123,28 @@ CXCompletionString clang_getCursorCompletionString(CXCursor cursor) { enum CXCursorKind kind = clang_getCursorKind(cursor); if (clang_isDeclaration(kind)) { Decl *decl = getCursorDecl(cursor); - if (isa<NamedDecl>(decl)) { - NamedDecl *namedDecl = (NamedDecl *)decl; + if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) { ASTUnit *unit = getCursorASTUnit(cursor); - if (unit->hasSema()) { - Sema &S = unit->getSema(); - CodeCompletionAllocator *Allocator - = unit->getCursorCompletionAllocator().getPtr(); - CodeCompletionResult Result(namedDecl); - CodeCompletionString *String - = Result.CreateCodeCompletionString(S, *Allocator); - return String; - } + CodeCompletionResult Result(namedDecl); + CodeCompletionString *String + = Result.CreateCodeCompletionString(unit->getASTContext(), + unit->getPreprocessor(), + unit->getCodeCompletionTUInfo().getAllocator(), + unit->getCodeCompletionTUInfo()); + return String; } } else if (kind == CXCursor_MacroDefinition) { MacroDefinition *definition = getCursorMacroDefinition(cursor); const IdentifierInfo *MacroInfo = definition->getName(); ASTUnit *unit = getCursorASTUnit(cursor); - if (unit->hasSema()) { - Sema &S = unit->getSema(); - CodeCompletionAllocator *Allocator - = unit->getCursorCompletionAllocator().getPtr(); - CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo)); - CodeCompletionString *String - = Result.CreateCodeCompletionString(S, *Allocator); - return String; - } + CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo)); + CodeCompletionString *String + = Result.CreateCodeCompletionString(unit->getASTContext(), + unit->getPreprocessor(), + unit->getCodeCompletionTUInfo().getAllocator(), + unit->getCodeCompletionTUInfo()); + return String; } return NULL; } |