首页 > 解决方案 > clang-8 中的自定义 clang 静态分析器检查器

问题描述

我正在尝试使用下面给出的示例在 clang 中编写一个自定义分析器:

https://clang-analyzer.llvm.org/checker_dev_manual.html

在上面的教程中,它指定在 checkers.td 中添加以下内容:

let ParentPackage = UnixAlpha in {
...
def SimpleFunc : Checker<"SimpleFunc">,
  HelpText<"Check for misuses of stream APIs">,
  DescFile<"SimpleFunc.cpp">;
...
} // end "alpha.unix"

我已经这样做了,我尝试再次构建 clang,但不幸的是,它返回一个错误为“找不到类 DescFile”

所以我自己在 checkers.td 中环顾四周,没有其他检查者有 DescFile 指定他们有所谓的文档(可能是版本问题?)

所以我将其更改为具有值 HasAlphaDocumentation 的文档并尝试再次构建,但现在它无法构建并返回对 clang::ento::registerSimpleFunc(CheckerManager &) 的未定义引用

下面是我的 SimpleFunc.cpp 文件的样子:

#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"

using namespace clang ;
using namespace ento ;

namespace {
class SimpleFunc : public Checker < check :: PreCall > {
mutable std :: unique_ptr < BugType > BT ;

public :
void checkPreCall ( const CallEvent & Call , CheckerContext & C ) const ;
};
}

void SimpleFunc :: checkPreCall ( const CallEvent & Call ,
 CheckerContext & C ) const {
if ( const IdentifierInfo * II = Call . getCalleeIdentifier ())
if ( II - > isStr (" main ")) {
if (! BT )
BT . reset (new BugType (this , " Call to main ", " Example checker " ));
ExplodedNode *N = C . generateErrorNode ();
auto Report = llvm :: make_unique < BugReport >(* BT , BT - > getName () , N );
    C.emitReport ( std :: move ( Report ));
 }
 }
void ento :: registerSimpleFunc (CheckerManager & Mgr ) {
    Mgr.registerChecker<SimpleFunc>();
 }

标签: c++clangllvmllvm-clangclang-static-analyzer

解决方案


推荐阅读