Skip to content

Instantly share code, notes, and snippets.

@varun93
Created March 24, 2019 13:26
Show Gist options
  • Save varun93/bca4168619f8ed0468cd2f3ec7da1111 to your computer and use it in GitHub Desktop.
Save varun93/bca4168619f8ed0468cd2f3ec7da1111 to your computer and use it in GitHub Desktop.
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include <clang/StaticAnalyzer/Core/CheckerRegistry.h>
#include <clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h>
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include <iostream>
using namespace clang;
using namespace ento;
using namespace std;
namespace {
class MainCallChecker : public Checker <check::PostCall,
check::PreCall> {
mutable std::unique_ptr<BugType> BT;
std::map<std::string, int> opcodeCount;
public:
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
void checkPreCall(const CallEvent &Call, CheckerContext &C) const
};
} // end anonymous namespace
void MainCallChecker::checkPostCall(const CallExpr *CE, CheckerContext &C) const {
const Expr *Callee = CE->getCallee();
const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl();
if (!FD)
return;
// Get the name of the callee.
IdentifierInfo *II = FD->getIdentifier();
if (!II) // if no identifier, not a simple C function
return;
const FunctionDecl *func = CE->getDirectCallee();
std::string funcName = func->getNameInfo().getName().getAsString();
std::string retType = func->getReturnType().getAsString();
std::cout << "Printing Function Params\n";
std::cout << "=================================================\n";
for(int i=0; i < func->getNumParams(); i++){
std::string argName = func->parameters()[i]->getQualifiedNameAsString();
std::string argType = func->getParamDecl(i)->getOriginalType().getAsString();
std::cout << "Argument Name is " << argName << " and type of the argument is " << argType << "\n";
}
std::cout << "=================================================\n";
std::cout << "Function name is " << funcName << "\n";
std::cout << "Return Name is " << retName << "\n";
std::cout << "Return Type is " << retType << "\n";
if (II->isStr("test")) {
ExplodedNode *N = C.generateErrorNode();
if (!N)
return;
if (!BT)
BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
std::unique_ptr<BugReport> report =
llvm::make_unique<BugReport>(*BT, BT->getName(), N);
report->addRange(Callee->getSourceRange());
C.emitReport(std::move(report));
}
}
/*
extern "C"
void clang_registerCheckers (CheckerRegistry &registry) {
registry.addChecker<MainCallChecker>(
"example.MainCallChecker", "Disallows calls to functions called main");
}
extern "C"
const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;
*/
void ento::registerMainCallChecker(CheckerManager &mgr) {
mgr.registerChecker<MainCallChecker>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment