summaryrefslogtreecommitdiffstats
path: root/clang-plugin/plugin.cc
diff options
context:
space:
mode:
Diffstat (limited to 'clang-plugin/plugin.cc')
-rw-r--r--clang-plugin/plugin.cc75
1 files changed, 5 insertions, 70 deletions
diff --git a/clang-plugin/plugin.cc b/clang-plugin/plugin.cc
index 1d80ba231..324e5169b 100644
--- a/clang-plugin/plugin.cc
+++ b/clang-plugin/plugin.cc
@@ -26,17 +26,16 @@
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
-#include "clang/AST/Expr.h"
#include "clang/AST/ASTConsumer.h"
-#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Sema/Sema.h"
#include "llvm/Support/raw_ostream.h"
-#include <unordered_map>
+#include "printf_check.h"
+
using namespace clang;
-namespace {
+namespace rspamd {
class RspamdASTConsumer : public ASTConsumer {
CompilerInstance &Instance;
@@ -49,71 +48,7 @@ namespace {
void HandleTranslationUnit (ASTContext &context) override
{
- struct Visitor : public RecursiveASTVisitor<Visitor> {
- std::unordered_map<std::string, int> printf_functions;
- ASTContext *pcontext;
-
- Visitor (void)
- {
- /* name -> format string position */
- printf_functions = {
- {"rspamd_printf", 0},
- {"rspamd_default_log_function", 4},
- {"rspamd_snprintf", 2},
- {"rspamd_fprintf", 1}
- };
- };
-
- bool VisitCallExpr (CallExpr *E)
- {
- auto callee = dyn_cast<NamedDecl> (E->getCalleeDecl ());
- if (callee == NULL) {
- llvm::errs () << "Bad callee\n";
- return false;
- }
-
- auto fname = callee->getNameAsString ();
-
- auto pos_it = printf_functions.find (fname);
-
- if (pos_it != printf_functions.end ()) {
- const auto args = E->getArgs ();
- auto pos = pos_it->second;
- auto query = args[pos];
-
- if (!query->isEvaluatable(*pcontext)) {
- llvm::errs () << "Cannot evaluate query\n";
- return false;
- }
-
- clang::Expr::EvalResult r;
-
- if (!query->EvaluateAsRValue (r, *pcontext)) {
- llvm::errs () << "Cannot evaluate query\n";
- return false;
- }
-
- auto qval = dyn_cast<StringLiteral> (
- r.Val.getLValueBase ().get<const Expr *> ());
- if (qval) {
- llvm::errs () << "query string: "
- << qval->getString () << "\n";
- }
-
- for (auto i = pos + 1; i < E->getNumArgs (); i ++) {
- auto arg = args[i];
-
- if (arg) {
- arg->dump ();
- }
- }
- }
-
- return true;
- }
-
- } v;
- v.pcontext = &context;
+ rspamd::PrintfCheckVisitor v(&context);
v.TraverseDecl (context.getTranslationUnitDecl ());
}
};
@@ -141,5 +76,5 @@ namespace {
}
-static FrontendPluginRegistry::Add <RspamdASTAction>
+static FrontendPluginRegistry::Add <rspamd::RspamdASTAction>
X ("rspamd-ast", "rspamd ast checker");