GithubHelp home page GithubHelp logo

Support Objective-C about sourcetrail HOT 6 OPEN

coatisoftware avatar coatisoftware commented on August 11, 2024 4
Support Objective-C

from sourcetrail.

Comments (6)

CocoaBeans avatar CocoaBeans commented on August 11, 2024

+1

from sourcetrail.

egraether avatar egraether commented on August 11, 2024

+1 via HN

from sourcetrail.

egraether avatar egraether commented on August 11, 2024

+1 via mail

from sourcetrail.

olbrichj avatar olbrichj commented on August 11, 2024

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

from sourcetrail.

mlangkabel avatar mlangkabel commented on August 11, 2024

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.

from sourcetrail.

olbrichj avatar olbrichj commented on August 11, 2024

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;
	}
};

from sourcetrail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.