Sourcetrail icon indicating copy to clipboard operation
Sourcetrail copied to clipboard

Support Objective-C

Open egraether opened this issue 8 years ago • 6 comments

egraether avatar Feb 26 '16 11:02 egraether

+1

CocoaBeans avatar Dec 14 '16 22:12 CocoaBeans

+1 via HN

egraether avatar Jun 23 '17 13:06 egraether

+1 via mail

egraether avatar Aug 28 '17 10:08 egraether

Searching around in the code, I guess it's not tooooooo hard. It should be possible to either extend the CxxAstVisitor or create a similar component. The question in that would be how to handle Obj-C++, as that is a mixture of Obj-C and C++.

Edit:// Looking further it would require the CxxAstVisitor to inherit from LexicallyOrderedRecursiveASTVisitor instead of RecursiveASTVisitor as described here: https://clang.llvm.org/doxygen/classclang_1_1LexicallyOrderedRecursiveASTVisitor.html#details

olbrichj avatar Jun 09 '21 16:06 olbrichj

I would recommend to create an entirely new AST visitor class for Objective-C support that derives from the LexicallyOrderedRecursiveASTVisitor. Mainly because the existing CxxAstVisitor is quite complex due to templates in C++. If parts of code are getting used in both visitors, we could still extract those parts into helper functions.

mlangkabel avatar Jun 10 '21 06:06 mlangkabel

I looked a little bit deeper, and if I understand this correctly, it needs to be part of the CxxAstVisitor. This is due to the existance of Obj-C++. Furthermore for some reason the LexicallyOrderedRecursiveASTVisitor, crashes on Obj-C++ files (Obj-C works fine).

A minimal example without c++

class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
private:
	ASTContext *astContext;

public:
	explicit ExampleVisitor(CompilerInstance *CI)
		: astContext(&(CI->getASTContext()))
	{
	}

	bool shouldVisitDecl(const Decl* decl) {
		const SourceManager& sourceManager = astContext->getSourceManager();
		SourceLocation loc = sourceManager.getExpansionLoc(decl->getLocation());

		return sourceManager.isInMainFile(loc);
	}

	bool shouldVisitExpr(const Expr* expr) {
		const SourceManager& sourceManager = astContext->getSourceManager();
		SourceLocation loc = sourceManager.getExpansionLoc(expr->getExprLoc());

		return sourceManager.isInMainFile(loc);
	}

	string getFilename(const Decl* decl) {
		const SourceManager& sourceManager = astContext->getSourceManager();
		return string(sourceManager.getFilename(decl->getLocation()));
	}

	bool VisitObjCContainerDecl(ObjCContainerDecl *decl) {
		if(!shouldVisitDecl(decl)) {
			return true;
		}

		recorder->endRecording();
		recorder->beginRecording();

		return true;
	}

	bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *decl) {
		if (!shouldVisitDecl(decl)) {
			return true;
		}

		errs() << "- Found ObjC Interface Declaration: " << decl->getQualifiedNameAsString() << "\n";
		return true;
	}

	bool VisitObjCImplementationDecl(ObjCImplementationDecl *decl) {
		if(!shouldVisitDecl(decl)) {
			return true;
		}

		errs() << "- Found ObjC Implementation: " << decl->getQualifiedNameAsString() << "\n";
		return true;
	}

	bool VisitObjCCategoryDecl(ObjCCategoryDecl *decl) {
		if (!shouldVisitDecl(decl)) {
			return true;
		}

		errs() << "- Found ObjC Category Declaration: " << decl->getClassInterface()->getQualifiedNameAsString() << " " << decl->getQualifiedNameAsString() << "\n";
		return true;
	}

	bool VisitObjCMethodDecl(ObjCMethodDecl *decl) {
		if (!shouldVisitDecl(decl)) {
			return true;
		}

		errs() << "| - Found ObjC Method Declaration: " << decl->getQualifiedNameAsString() << "\n";
		if (auto body = decl->getBody()) {
			body->getStmtClassName();
		}

		return true;
	}

	bool VisitObjCPropertyDecl(ObjCPropertyDecl *decl) {
		if (!shouldVisitDecl(decl)) {
			return true;
		}

		errs() << "| - Found ObjC Property Declaration: " << decl->getQualifiedNameAsString() << "\n";
		return true;
	}

	bool VisitFunctionDecl(FunctionDecl *decl) {
		if (!shouldVisitDecl(decl)) {
			return true;
		}

		errs() << "| - Found Function Declaration: " << decl->getQualifiedNameAsString() << "\n";
		return true;
	}

	bool VisitCallExpr(CallExpr *expr) {
		if (!shouldVisitExpr(expr)) {
			return true;
		}

		errs() << "| - - Found FunctionCall\n";
		return true;
	}

	bool VisitObjCMessageExpr(ObjCMessageExpr *expr) {
		if (!shouldVisitExpr(expr)) {
			return true;
		}

		errs() << "| - - Found MethodCall: ["<< expr->getReceiverInterface()->getQualifiedNameAsString() << " " << expr->getSelector().getAsString() << "]\n";
		errs() << "| - - - file: " << getFilename(expr->getReceiverInterface()) << "\n";
		return true;
	}
};

olbrichj avatar Jul 05 '21 07:07 olbrichj