CoatiSoftware / Sourcetrail

Sourcetrail - free and open-source interactive source explorer
https://www.sourcetrail.com/
GNU General Public License v3.0
14.96k stars 1.41k forks source link

Support Objective-C #36

Open egraether opened 8 years ago

CocoaBeans commented 7 years ago

+1

egraether commented 7 years ago

+1 via HN

egraether commented 7 years ago

+1 via mail

olbrichj commented 3 years ago

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

mlangkabel commented 3 years ago

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.

olbrichj commented 3 years ago

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