OCLint

Write Custom Rules

To write custom rules, as a pre-requisite, a certain level of knowledge about Clang is necessary, especially Abstract Syntax representation of Clang should be familiar with. (Clang code is very easy to read and understand with awesome abstractions, so, you can just pick up the particular node type you need, and read that piece of code.)

Modifying rules currently open source in OCLint codebase is a good place to start. Check out headers and implementations.

Write Rule

In front of all, you need to have a working copy of OCLint, you can read this document about how to compile OCLint.

Same to the SampleRule shown below, your rule must be a sub-class of Rule. Write custom logic in the apply function, and give rule name in name function. Please notice that, to make this rule can be dynamically loaded into the main program, please initialize it in the static constructor.

#include "oclint/Rule.h"
#include "oclint/RuleSet.h"
#include "oclint/ViolationSet.h"
#include "oclint/Violation.h"

class SampleRule : public Rule {
private:
  static RuleSet rules;

public:
  virtual void apply(CXCursor& node, CXCursor& parentNode, ViolationSet& violationSet);
  virtual const string name() const;
};

RuleSet SampleRule::rules(new SampleRule());

void SampleRule::apply(CXCursor& node, CXCursor& parentNode, ViolationSet& violationSet) {
  // fill in custom logic here
}

const string SampleRule::name() const {
  return "sample rule";
}

Please make sure custom rules would never “contaminate” the AST. Unit tests are highly recommended for custom rules.

Compile Rule

Copy source code of rule to the same implementation folder, parallel with other rules. Add its class name to SET(LIST_OF_RULES RuleName) section in CMakeLists file.

Re-compile OCLint, and the new custom rule will be built together with other rules.

Use Rule

Same as other rules, you can take the advantages of plug-and-use, and use your rules wisely based on your project.

How it works will briefly present the architecture of OCLint.