OCLint.4.Rules
Rules
1. Rule Base Class: RuleBase
The RuleBase class is the base class for all rules. It defines the interfaces of rule class.
class RuleBase { protected: RuleCarrier *_carrier; public: void takeoff(RuleCarrier *carrier); virtual ~RuleBase() {} virtual void apply() = 0; virtual const std::string name() const = 0; virtual const std::string attributeName() const; virtual const std::string identifier() const; virtual const std::string category() const = 0; virtual int priority() const = 0; };
1.1 takeoff() function
This function assigns `carrier` parameter to the member variable `_carrier` and call the apply() function.
Each derived class should provide the implementations of apply() function.
// RuleBase.cpp // void RuleBase::takeoff(RuleCarrier *carrier) { _carrier = carrier;
// The subclass should provide the implementation for apply() function.
apply();
}
1.2 As entry of Rule
The takeoff() function is the entry of rule, it is called by the Analyzer (e.g. RulesetBasedAnalyzer):
void RulesetBasedAnalyzer::analyze(std::vector<clang::ASTContext *> &contexts) { for (const auto& context : contexts) { auto violationSet = new ViolationSet(); auto carrier = new RuleCarrier(context, violationSet); for (RuleBase *rule : _filteredRules) { rule->takeoff(carrier); // Run the rule on the `context` and insert each violation into `violationSet`. } ResultCollector *results = ResultCollector::getInstance(); results->add(violationSet); } }
2. AbstractASTVisitorRule class
In the OCLintAbstractRule library, the class AbstractASTVisitorRule is subclass of class AbstractASTRuleBase which in turn is
the subclass of class RuleBase.
The class AbstractASTVisitorRule has itself own implementation for apply() method:
virtual void apply() { if (isCUDASourceFile()) { applyCUDA(); } else { applyC(); } } void applyC() { // for C/C++/Objective-C source code if (!isLanguageSupported() || !(supportedCUDAFunctionAttrs() & CUDA_HOST)) { return; } setUp(); clang::DeclContext *tu = getTranslationUnit(); for (clang::DeclContext::decl_iterator it = tu->decls_begin(), declEnd = tu->decls_end(); it != declEnd; ++it) { if (isValidDecl(*it)) { traverse(*it); } } tearDown(); }
---