美文网首页
OCLINT自定义规则-长函数限定LongMethodRule

OCLINT自定义规则-长函数限定LongMethodRule

作者: 熊猫人和熊猫君 | 来源:发表于2018-07-07 16:17 被阅读0次

目的:

总所周知,优雅的代码结构,可以使代码结构更清晰,维护更方便。但,不是团队的每一个成员都有如此编写习惯,在codereview的时候可能需要人为去审核提出建议。这样做人力成本大。而此脚本急速oclint自定义检查校验长函数的动态库脚本。

思路剖析:

1.寻找AST函数定义,分为VisitObjCMethodDecl与VisitFunctionDecl,其中VisitObjCMethodDecl为oc方法定义,VisitFunctionDecl为c函数定义。
2.访问到函数定义后可根据Stmt和文件管理器SourceManager计算出该函数的带代码总个数


class LongMethodRule : public AbstractASTVisitorRule<LongMethodRule>
{
private:
    void applyDecl(Decl *decl)
    {
        //hasBody函数有实现内容吗
        if (decl->hasBody() &&
            !isCppMethodDeclLocatedInCppRecordDecl(dyn_cast<CXXMethodDecl>(decl)))
        {
            //有实现
            Stmt *stmt = decl->getBody();
            //计算函数里面代码总长度,getLineCount为帮助函数,后面有其方法
            int length = getLineCount(stmt->getSourceRange(), _carrier->getSourceManager());
            //oclint自身配置的长度可以调整修改
            int threshold = RuleConfiguration::intForKey("LONG_METHOD", 50);
            if (length > threshold)
            {
                //告警
                string description = "Method with " +
                    toString<int>(length) + " lines exceeds limit of " + toString<int>(threshold);
                addViolation(decl, this, description);
            }
        }
    }

public:
    virtual const string name() const override
    {
        return "long method";
    }

    virtual int priority() const override
    {
        return 3;
    }

    virtual const string category() const override
    {
        return "size";
    }

#ifdef DOCGEN
    virtual const std::string since() const override
    {
        return "0.4";
    }

    virtual const std::string description() const override
    {
        return "Long method generally indicates that this method tries to do many things. "
            "Each method should do one thing and that one thing well.";
    }

    virtual const std::string example() const override
    {
        return R"rst(
.. code-block:: cpp

    void example()
    {
        cout << "hello world";
        cout << "hello world";
        // repeat 48 times
    }
        )rst";
    }

    virtual const std::map<std::string, std::string> thresholds() const override
    {
        std::map<std::string, std::string> thresholdMapping;
        thresholdMapping["LONG_METHOD"] =
            "The long method reporting threshold, default value is 50.";
        return thresholdMapping;
    }
#endif
    //oc函数:如果ViewController里面的- (void)viewDidLoad
    bool VisitObjCMethodDecl(ObjCMethodDecl *decl)
    {
        applyDecl(decl);
        return true;
    }
    //c函数的函数定义,eg:void testMyFunction(){***}
    bool VisitFunctionDecl(FunctionDecl *decl)
    {
        applyDecl(decl);
        return true;
    }
};

static RuleSet rules(new LongMethodRule());

帮助函数:总行数

int getLineCount(clang::SourceRange sourceRange, const clang::SourceManager& sourceManager)
{
    clang::SourceLocation startLocation = sourceRange.getBegin();
    clang::SourceLocation endLocation = sourceRange.getEnd();
    unsigned startLineNumber = sourceManager.getPresumedLineNumber(startLocation);
    unsigned endLineNumber = sourceManager.getPresumedLineNumber(endLocation);
    return endLineNumber - startLineNumber + 1;
}

相关文章

  • OCLINT自定义规则-长函数限定LongMethodRule

    目的: 总所周知,优雅的代码结构,可以使代码结构更清晰,维护更方便。但,不是团队的每一个成员都有如此编写习惯,在c...

  • OCLint 自定义规则

    可能OCLint官方提供的检测规则不能满足我们的日常需求,OCLint也是支持自定义规则滴。 OCLint环境安装...

  • OCLint自定义规则1-源代码安装及问题总结

    源代码安装 如果要自定义规则,那么必须下载源码进行安装: clone源码: 进入oclint-scripts目录执...

  • 如何调试OCLint自定义规则

    oclint自定义规则的编写,一般我们会生成xcode工程,然后进行编码,build之后会输出dylib。如果我们...

  • oclint技术辅导

    oclint技术辅导(短时间快速熟悉从clangllvm到oclint的体系学会编写基于oclint的代码走查规则...

  • OCLint的规则

    一.前言 最新的OCLint中有71个检查的规则http://docs.oclint.org/en/stable/...

  • OCLint CodeReview静态代码分析

    前言 OCLint 是一个静态分析代码的工具,支持自定义规则,可以很好地帮助我们规范代码,提高代码质量,是团队开发...

  • 记录一次oclint之旅

    以前也了解过oclint,看了一下大概的相关规则,里面的自定义规则用的很少.这次有空,详细的记录一下对一个工程的O...

  • OClint自定义规则3-调试规则

    调试前已经生成了规则的xcode文件。现在dylib,需要主程序来引导启动,配置如下: 接下来还需要配置入口调试参...

  • 让XCode自动CodeReview你的代码-OCLint使用

    前言 OCLint 是一个静态分析代码的工具,支持自定义规则,可以很好地帮助我们规范代码,这篇文章主要介绍通过OC...

网友评论

      本文标题:OCLINT自定义规则-长函数限定LongMethodRule

      本文链接:https://www.haomeiwen.com/subject/fevwuftx.html