2012-10-29 14:03:58 +08:00
|
|
|
//===- CursorVisitor.h - CursorVisitor interface ----------------*- C++ -*-===//
|
2011-11-04 03:02:34 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-11-04 03:02:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CURSORVISITOR_H
|
|
|
|
#define LLVM_CLANG_TOOLS_LIBCLANG_CURSORVISITOR_H
|
2011-11-04 03:02:34 +08:00
|
|
|
|
|
|
|
#include "CXCursor.h"
|
|
|
|
#include "CXTranslationUnit.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "Index_Internal.h"
|
2011-11-04 03:02:34 +08:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/TypeLocVisitor.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class PreprocessingRecord;
|
|
|
|
class ASTUnit;
|
|
|
|
|
|
|
|
namespace cxcursor {
|
|
|
|
|
|
|
|
class VisitorJob {
|
|
|
|
public:
|
|
|
|
enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind,
|
|
|
|
TypeLocVisitKind, OverloadExprPartsKind,
|
|
|
|
DeclRefExprPartsKind, LabelRefVisitKind,
|
|
|
|
ExplicitTemplateArgsVisitKind,
|
|
|
|
NestedNameSpecifierLocVisitKind,
|
|
|
|
DeclarationNameInfoVisitKind,
|
2012-02-15 08:54:55 +08:00
|
|
|
MemberRefVisitKind, SizeOfPackExprPartsKind,
|
2012-09-02 02:27:30 +08:00
|
|
|
LambdaExprPartsKind, PostChildrenVisitKind };
|
2011-11-04 03:02:34 +08:00
|
|
|
protected:
|
2013-01-26 23:29:08 +08:00
|
|
|
const void *data[3];
|
2011-11-04 03:02:34 +08:00
|
|
|
CXCursor parent;
|
|
|
|
Kind K;
|
2014-06-08 16:38:04 +08:00
|
|
|
VisitorJob(CXCursor C, Kind k, const void *d1, const void *d2 = nullptr,
|
|
|
|
const void *d3 = nullptr)
|
2011-11-04 03:02:34 +08:00
|
|
|
: parent(C), K(k) {
|
|
|
|
data[0] = d1;
|
|
|
|
data[1] = d2;
|
|
|
|
data[2] = d3;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
Kind getKind() const { return K; }
|
|
|
|
const CXCursor &getParent() const { return parent; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SmallVector<VisitorJob, 10> VisitorWorkList;
|
|
|
|
|
|
|
|
// Cursor visitor.
|
|
|
|
class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
|
|
|
|
public TypeLocVisitor<CursorVisitor, bool>
|
|
|
|
{
|
2012-09-02 02:27:30 +08:00
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Callback called after child nodes of a cursor have been visited.
|
2012-09-02 02:27:30 +08:00
|
|
|
/// Return true to break visitation or false to continue.
|
|
|
|
typedef bool (*PostChildrenVisitorTy)(CXCursor cursor,
|
|
|
|
CXClientData client_data);
|
|
|
|
|
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The translation unit we are traversing.
|
2011-11-04 03:02:34 +08:00
|
|
|
CXTranslationUnit TU;
|
|
|
|
ASTUnit *AU;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The parent cursor whose children we are traversing.
|
2011-11-04 03:02:34 +08:00
|
|
|
CXCursor Parent;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The declaration that serves at the parent of any statement or
|
2011-11-04 03:02:34 +08:00
|
|
|
/// expression nodes.
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *StmtParent;
|
2011-11-04 03:02:34 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The visitor function.
|
2011-11-04 03:02:34 +08:00
|
|
|
CXCursorVisitor Visitor;
|
|
|
|
|
2012-09-02 02:27:30 +08:00
|
|
|
PostChildrenVisitorTy PostChildrenVisitor;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The opaque client data, to be passed along to the visitor.
|
2011-11-04 03:02:34 +08:00
|
|
|
CXClientData ClientData;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether we should visit the preprocessing record entries last,
|
2011-11-04 03:02:34 +08:00
|
|
|
/// after visiting other declarations.
|
|
|
|
bool VisitPreprocessorLast;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether we should visit declarations or preprocessing record
|
2011-11-04 03:02:34 +08:00
|
|
|
/// entries that are #included inside the \arg RegionOfInterest.
|
|
|
|
bool VisitIncludedEntities;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// When valid, a source range to which the cursor should restrict
|
2011-11-04 03:02:34 +08:00
|
|
|
/// its search.
|
|
|
|
SourceRange RegionOfInterest;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether we should only visit declarations and not preprocessing
|
2011-11-29 11:14:11 +08:00
|
|
|
/// record entries.
|
|
|
|
bool VisitDeclsOnly;
|
|
|
|
|
2011-11-04 03:02:34 +08:00
|
|
|
// FIXME: Eventually remove. This part of a hack to support proper
|
|
|
|
// iteration over all Decls contained lexically within an ObjC container.
|
|
|
|
DeclContext::decl_iterator *DI_current;
|
|
|
|
DeclContext::decl_iterator DE_current;
|
2011-11-16 16:58:57 +08:00
|
|
|
SmallVectorImpl<Decl *>::iterator *FileDI_current;
|
|
|
|
SmallVectorImpl<Decl *>::iterator FileDE_current;
|
2011-11-04 03:02:34 +08:00
|
|
|
|
|
|
|
// Cache of pre-allocated worklists for data-recursion walk of Stmts.
|
|
|
|
SmallVector<VisitorWorkList*, 5> WorkListFreeList;
|
|
|
|
SmallVector<VisitorWorkList*, 5> WorkListCache;
|
|
|
|
|
|
|
|
using DeclVisitor<CursorVisitor, bool>::Visit;
|
|
|
|
using TypeLocVisitor<CursorVisitor, bool>::Visit;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether this particular source range comes before, comes
|
2011-11-04 03:02:34 +08:00
|
|
|
/// after, or overlaps the region of interest.
|
|
|
|
///
|
|
|
|
/// \param R a half-open source range retrieved from the abstract syntax tree.
|
|
|
|
RangeComparisonResult CompareRegionOfInterest(SourceRange R);
|
|
|
|
|
2013-03-09 04:42:33 +08:00
|
|
|
bool visitDeclsFromFileRegion(FileID File, unsigned Offset, unsigned Length);
|
2011-11-04 03:02:34 +08:00
|
|
|
|
|
|
|
class SetParentRAII {
|
|
|
|
CXCursor &Parent;
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *&StmtParent;
|
2011-11-04 03:02:34 +08:00
|
|
|
CXCursor OldParent;
|
|
|
|
|
|
|
|
public:
|
2013-01-24 01:25:27 +08:00
|
|
|
SetParentRAII(CXCursor &Parent, const Decl *&StmtParent,
|
|
|
|
CXCursor NewParent)
|
2011-11-04 03:02:34 +08:00
|
|
|
: Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
|
|
|
|
{
|
|
|
|
Parent = NewParent;
|
|
|
|
if (clang_isDeclaration(Parent.kind))
|
|
|
|
StmtParent = getCursorDecl(Parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SetParentRAII() {
|
|
|
|
Parent = OldParent;
|
|
|
|
if (clang_isDeclaration(Parent.kind))
|
|
|
|
StmtParent = getCursorDecl(Parent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor,
|
|
|
|
CXClientData ClientData,
|
|
|
|
bool VisitPreprocessorLast,
|
|
|
|
bool VisitIncludedPreprocessingEntries = false,
|
2011-11-29 11:14:11 +08:00
|
|
|
SourceRange RegionOfInterest = SourceRange(),
|
2012-09-02 02:27:30 +08:00
|
|
|
bool VisitDeclsOnly = false,
|
2014-06-08 16:38:04 +08:00
|
|
|
PostChildrenVisitorTy PostChildrenVisitor = nullptr)
|
2013-01-27 02:53:38 +08:00
|
|
|
: TU(TU), AU(cxtu::getASTUnit(TU)),
|
2012-09-02 02:27:30 +08:00
|
|
|
Visitor(Visitor), PostChildrenVisitor(PostChildrenVisitor),
|
|
|
|
ClientData(ClientData),
|
2011-11-04 03:02:34 +08:00
|
|
|
VisitPreprocessorLast(VisitPreprocessorLast),
|
|
|
|
VisitIncludedEntities(VisitIncludedPreprocessingEntries),
|
2011-11-29 11:14:11 +08:00
|
|
|
RegionOfInterest(RegionOfInterest),
|
|
|
|
VisitDeclsOnly(VisitDeclsOnly),
|
2014-06-08 16:38:04 +08:00
|
|
|
DI_current(nullptr), FileDI_current(nullptr)
|
2011-11-04 03:02:34 +08:00
|
|
|
{
|
|
|
|
Parent.kind = CXCursor_NoDeclFound;
|
2014-06-08 16:38:04 +08:00
|
|
|
Parent.data[0] = nullptr;
|
|
|
|
Parent.data[1] = nullptr;
|
|
|
|
Parent.data[2] = nullptr;
|
|
|
|
StmtParent = nullptr;
|
2011-11-04 03:02:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~CursorVisitor() {
|
|
|
|
// Free the pre-allocated worklists for data-recursion.
|
|
|
|
for (SmallVectorImpl<VisitorWorkList*>::iterator
|
|
|
|
I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) {
|
|
|
|
delete *I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *getASTUnit() const { return AU; }
|
2011-11-04 03:02:34 +08:00
|
|
|
CXTranslationUnit getTU() const { return TU; }
|
|
|
|
|
|
|
|
bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Visit declarations and preprocessed entities for the file region
|
2011-11-04 03:02:34 +08:00
|
|
|
/// designated by \see RegionOfInterest.
|
2013-03-09 04:42:33 +08:00
|
|
|
bool visitFileRegion();
|
2011-11-04 03:02:34 +08:00
|
|
|
|
|
|
|
bool visitPreprocessedEntitiesInRegion();
|
|
|
|
|
|
|
|
bool shouldVisitIncludedEntities() const {
|
|
|
|
return VisitIncludedEntities;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
bool visitPreprocessedEntities(InputIterator First, InputIterator Last,
|
|
|
|
PreprocessingRecord &PPRec,
|
|
|
|
FileID FID = FileID());
|
|
|
|
|
|
|
|
bool VisitChildren(CXCursor Parent);
|
|
|
|
|
|
|
|
// Declaration visitors
|
2015-11-15 21:48:32 +08:00
|
|
|
bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
|
2011-11-04 03:02:34 +08:00
|
|
|
bool VisitTypeAliasDecl(TypeAliasDecl *D);
|
|
|
|
bool VisitAttributes(Decl *D);
|
|
|
|
bool VisitBlockDecl(BlockDecl *B);
|
|
|
|
bool VisitCXXRecordDecl(CXXRecordDecl *D);
|
2013-02-21 09:29:01 +08:00
|
|
|
Optional<bool> shouldVisitCursor(CXCursor C);
|
2011-11-04 03:02:34 +08:00
|
|
|
bool VisitDeclContext(DeclContext *DC);
|
|
|
|
bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
|
|
|
|
bool VisitTypedefDecl(TypedefDecl *D);
|
|
|
|
bool VisitTagDecl(TagDecl *D);
|
|
|
|
bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D);
|
|
|
|
bool VisitClassTemplatePartialSpecializationDecl(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D);
|
|
|
|
bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
|
|
|
|
bool VisitEnumConstantDecl(EnumConstantDecl *D);
|
|
|
|
bool VisitDeclaratorDecl(DeclaratorDecl *DD);
|
|
|
|
bool VisitFunctionDecl(FunctionDecl *ND);
|
|
|
|
bool VisitFieldDecl(FieldDecl *D);
|
|
|
|
bool VisitVarDecl(VarDecl *);
|
|
|
|
bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
|
|
|
|
bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
|
|
|
|
bool VisitClassTemplateDecl(ClassTemplateDecl *D);
|
|
|
|
bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
|
2015-07-07 11:58:14 +08:00
|
|
|
bool VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
|
2011-11-04 03:02:34 +08:00
|
|
|
bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
|
|
|
|
bool VisitObjCContainerDecl(ObjCContainerDecl *D);
|
|
|
|
bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
|
|
|
|
bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
|
|
|
|
bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD);
|
2015-07-07 11:57:35 +08:00
|
|
|
bool VisitObjCTypeParamList(ObjCTypeParamList *typeParamList);
|
2011-11-04 03:02:34 +08:00
|
|
|
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
|
|
|
|
bool VisitObjCImplDecl(ObjCImplDecl *D);
|
|
|
|
bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
|
|
|
|
bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
|
|
|
|
// FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
|
|
|
|
bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD);
|
|
|
|
bool VisitLinkageSpecDecl(LinkageSpecDecl *D);
|
|
|
|
bool VisitNamespaceDecl(NamespaceDecl *D);
|
|
|
|
bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
|
|
|
|
bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
|
|
|
|
bool VisitUsingDecl(UsingDecl *D);
|
|
|
|
bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
|
|
|
|
bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
|
2016-06-10 00:15:55 +08:00
|
|
|
bool VisitStaticAssertDecl(StaticAssertDecl *D);
|
2016-11-04 14:29:27 +08:00
|
|
|
bool VisitFriendDecl(FriendDecl *D);
|
|
|
|
|
2011-11-04 03:02:34 +08:00
|
|
|
// Name visitor
|
|
|
|
bool VisitDeclarationNameInfo(DeclarationNameInfo Name);
|
|
|
|
bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range);
|
|
|
|
bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
|
|
|
|
|
|
|
|
// Template visitors
|
|
|
|
bool VisitTemplateParameters(const TemplateParameterList *Params);
|
|
|
|
bool VisitTemplateName(TemplateName Name, SourceLocation Loc);
|
|
|
|
bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL);
|
|
|
|
|
|
|
|
// Type visitors
|
|
|
|
#define ABSTRACT_TYPELOC(CLASS, PARENT)
|
|
|
|
#define TYPELOC(CLASS, PARENT) \
|
|
|
|
bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
|
|
|
|
#include "clang/AST/TypeLocNodes.def"
|
|
|
|
|
|
|
|
bool VisitTagTypeLoc(TagTypeLoc TL);
|
|
|
|
bool VisitArrayTypeLoc(ArrayTypeLoc TL);
|
|
|
|
bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false);
|
|
|
|
|
|
|
|
// Data-recursive visitor functions.
|
|
|
|
bool IsInRegionOfInterest(CXCursor C);
|
|
|
|
bool RunVisitorWorkList(VisitorWorkList &WL);
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueWorkList(VisitorWorkList &WL, const Stmt *S);
|
|
|
|
LLVM_ATTRIBUTE_NOINLINE bool Visit(const Stmt *S);
|
2016-07-02 03:10:54 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Optional<bool> handleDeclForVisitation(const Decl *D);
|
2011-11-04 03:02:34 +08:00
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
|
|
|
}
|
2011-11-04 03:02:34 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|