diff options
author | Philip Withnall <philip.withnall@collabora.co.uk> | 2014-05-22 09:59:23 +0100 |
---|---|---|
committer | Philip Withnall <philip.withnall@collabora.co.uk> | 2014-05-22 10:37:17 +0100 |
commit | d2abb89006cfe6d68ca8f522983c79f0408f8f23 (patch) | |
tree | ef4e8c7eae7750ce3e54515e6f938a6ef58e3bed | |
parent | 9a2116a78d2da56d7254d2e009d45504e857e421 (diff) |
clang-plugin: Make all checkers inherit from a Checker parent class
This will allow common code, such as names, descriptions and enable
handling, to be factored out.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | clang-plugin/checker.cpp | 39 | ||||
-rw-r--r-- | clang-plugin/checker.h | 62 | ||||
-rw-r--r-- | clang-plugin/gir-attributes.cpp | 5 | ||||
-rw-r--r-- | clang-plugin/gir-attributes.h | 11 | ||||
-rw-r--r-- | clang-plugin/gsignal-checker.cpp | 5 | ||||
-rw-r--r-- | clang-plugin/gsignal-checker.h | 9 | ||||
-rw-r--r-- | clang-plugin/gvariant-checker.cpp | 5 | ||||
-rw-r--r-- | clang-plugin/gvariant-checker.h | 10 | ||||
-rw-r--r-- | clang-plugin/nullability-checker.cpp | 5 | ||||
-rw-r--r-- | clang-plugin/nullability-checker.h | 9 | ||||
-rw-r--r-- | clang-plugin/plugin.cpp | 11 |
12 files changed, 137 insertions, 36 deletions
diff --git a/Makefile.am b/Makefile.am index b98a573..bdc06cf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,6 +30,8 @@ clang_plugin_libtartan_la_SOURCES = \ clang-plugin/gvariant-checker.h \ clang-plugin/nullability-checker.cpp \ clang-plugin/nullability-checker.h \ + clang-plugin/checker.cpp \ + clang-plugin/checker.h \ $(NULL) clang_plugin_libtartan_la_CPPFLAGS = \ diff --git a/clang-plugin/checker.cpp b/clang-plugin/checker.cpp new file mode 100644 index 0000000..73cdd44 --- /dev/null +++ b/clang-plugin/checker.cpp @@ -0,0 +1,39 @@ +/* -*- Mode: C++; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* + * Tartan + * Copyright © 2014 Collabora Ltd. + * + * Tartan is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tartan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tartan. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Philip Withnall <philip.withnall@collabora.co.uk> + */ + +#include <unordered_set> + +#include "checker.h" + +namespace tartan { + +bool +Checker::is_enabled () const +{ + /* Run away if the plugin is disabled. */ + return (this->_disabled_plugins.get ()->find (this->get_name ()) == + this->_disabled_plugins.get ()->end () && + this->_disabled_plugins.get ()->find ("all") == + this->_disabled_plugins.get ()->end ()); +} + +} /* namespace tartan */ diff --git a/clang-plugin/checker.h b/clang-plugin/checker.h new file mode 100644 index 0000000..4c86a7e --- /dev/null +++ b/clang-plugin/checker.h @@ -0,0 +1,62 @@ +/* -*- Mode: C++; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* + * Tartan + * Copyright © 2014 Collabora Ltd. + * + * Tartan is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tartan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tartan. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Philip Withnall <philip.withnall@collabora.co.uk> + */ + +#ifndef TARTAN_CHECKER_H +#define TARTAN_CHECKER_H + +#include <unordered_set> + +#include <clang/AST/AST.h> +#include <clang/AST/ASTConsumer.h> +#include <clang/Frontend/CompilerInstance.h> + +#include <girepository.h> + +#include "gir-manager.h" + +namespace tartan { + +using namespace clang; + +class Checker : public clang::ASTConsumer { + +public: + explicit Checker ( + CompilerInstance& compiler, + std::shared_ptr<const GirManager> gir_manager, + std::shared_ptr<const std::unordered_set<std::string>> disabled_plugins) : + _compiler (compiler), _gir_manager (gir_manager), + _disabled_plugins (disabled_plugins) {} + +protected: + CompilerInstance& _compiler; + std::shared_ptr<const GirManager> _gir_manager; + std::shared_ptr<const std::unordered_set<std::string>> _disabled_plugins; + +public: + bool is_enabled () const; + virtual const std::string get_name () const = 0; +}; + +} /* namespace tartan */ + +#endif /* !TARTAN_CHECKER_H */ diff --git a/clang-plugin/gir-attributes.cpp b/clang-plugin/gir-attributes.cpp index e169fd6..f6278a8 100644 --- a/clang-plugin/gir-attributes.cpp +++ b/clang-plugin/gir-attributes.cpp @@ -478,10 +478,7 @@ GirAttributesChecker::HandleTopLevelDecl (DeclGroupRef decl_group) DeclGroupRef::iterator i, e; /* Run away if the plugin is disabled. */ - if (this->_disabled_plugins.get ()->find ("gir-attributes") != - this->_disabled_plugins.get ()->end () || - this->_disabled_plugins.get ()->find ("all") != - this->_disabled_plugins.get ()->end ()) { + if (!this->is_enabled ()) { return true; } diff --git a/clang-plugin/gir-attributes.h b/clang-plugin/gir-attributes.h index e6fce06..13ac489 100644 --- a/clang-plugin/gir-attributes.h +++ b/clang-plugin/gir-attributes.h @@ -31,6 +31,7 @@ #include <girepository.h> +#include "checker.h" #include "gir-manager.h" namespace tartan { @@ -53,24 +54,20 @@ public: }; -class GirAttributesChecker : public ASTConsumer { +class GirAttributesChecker : public tartan::Checker { public: explicit GirAttributesChecker ( CompilerInstance& compiler, std::shared_ptr<const GirManager> gir_manager, std::shared_ptr<const std::unordered_set<std::string>> disabled_plugins) : - _compiler (compiler), _gir_manager (gir_manager), - _disabled_plugins (disabled_plugins) {} + Checker (compiler, gir_manager, disabled_plugins) {} private: - CompilerInstance& _compiler; - std::shared_ptr<const GirManager> _gir_manager; - std::shared_ptr<const std::unordered_set<std::string>> _disabled_plugins; - void _handle_function_decl (FunctionDecl& func); public: virtual bool HandleTopLevelDecl (DeclGroupRef decl_group); + const std::string get_name () const { return "gir-attributes"; } }; } /* namespace tartan */ diff --git a/clang-plugin/gsignal-checker.cpp b/clang-plugin/gsignal-checker.cpp index c008c93..f28a932 100644 --- a/clang-plugin/gsignal-checker.cpp +++ b/clang-plugin/gsignal-checker.cpp @@ -867,10 +867,7 @@ void GSignalConsumer::HandleTranslationUnit (ASTContext& context) { /* Run away if the plugin is disabled. */ - if (this->_disabled_plugins.get ()->find ("gsignal") != - this->_disabled_plugins.get ()->end () || - this->_disabled_plugins.get ()->find ("all") != - this->_disabled_plugins.get ()->end ()) { + if (!this->is_enabled ()) { return; } diff --git a/clang-plugin/gsignal-checker.h b/clang-plugin/gsignal-checker.h index 69578e3..4119b23 100644 --- a/clang-plugin/gsignal-checker.h +++ b/clang-plugin/gsignal-checker.h @@ -30,6 +30,7 @@ #include <clang/AST/RecursiveASTVisitor.h> #include <clang/Frontend/CompilerInstance.h> +#include "checker.h" #include "gir-manager.h" namespace tartan { @@ -52,20 +53,20 @@ public: bool VisitCallExpr (CallExpr* call); }; -class GSignalConsumer : public ASTConsumer { +class GSignalConsumer : public tartan::Checker { public: GSignalConsumer (CompilerInstance& compiler, std::shared_ptr<const GirManager> gir_manager, std::shared_ptr<const std::unordered_set<std::string>> disabled_plugins) : - _visitor (compiler, gir_manager), - _disabled_plugins (disabled_plugins) {} + Checker (compiler, gir_manager, disabled_plugins), + _visitor (compiler, gir_manager) {}; private: GSignalVisitor _visitor; - std::shared_ptr<const std::unordered_set<std::string>> _disabled_plugins; public: virtual void HandleTranslationUnit (ASTContext& context); + const std::string get_name () const { return "gsignal"; } }; } /* namespace tartan */ diff --git a/clang-plugin/gvariant-checker.cpp b/clang-plugin/gvariant-checker.cpp index 7af716e..b4f8975 100644 --- a/clang-plugin/gvariant-checker.cpp +++ b/clang-plugin/gvariant-checker.cpp @@ -1063,10 +1063,7 @@ void GVariantConsumer::HandleTranslationUnit (ASTContext& context) { /* Run away if the plugin is disabled. */ - if (this->_disabled_plugins.get ()->find ("gvariant") != - this->_disabled_plugins.get ()->end () || - this->_disabled_plugins.get ()->find ("all") != - this->_disabled_plugins.get ()->end ()) { + if (!this->is_enabled ()) { return; } diff --git a/clang-plugin/gvariant-checker.h b/clang-plugin/gvariant-checker.h index 0ac8275..0d9f080 100644 --- a/clang-plugin/gvariant-checker.h +++ b/clang-plugin/gvariant-checker.h @@ -30,6 +30,8 @@ #include <clang/AST/RecursiveASTVisitor.h> #include <clang/Frontend/CompilerInstance.h> +#include "checker.h" + namespace tartan { using namespace clang; @@ -48,18 +50,20 @@ public: bool VisitCallExpr (CallExpr* call); }; -class GVariantConsumer : public ASTConsumer { +class GVariantConsumer : public tartan::Checker { public: GVariantConsumer (CompilerInstance& compiler, + std::shared_ptr<const GirManager> gir_manager, std::shared_ptr<const std::unordered_set<std::string>> disabled_plugins) : - _visitor (compiler), _disabled_plugins (disabled_plugins) {} + Checker (compiler, gir_manager, disabled_plugins), + _visitor (compiler) {} private: GVariantVisitor _visitor; - std::shared_ptr<const std::unordered_set<std::string>> _disabled_plugins; public: virtual void HandleTranslationUnit (ASTContext& context); + const std::string get_name () const { return "gvariant"; } }; } /* namespace tartan */ diff --git a/clang-plugin/nullability-checker.cpp b/clang-plugin/nullability-checker.cpp index 01ed1ce..1476f46 100644 --- a/clang-plugin/nullability-checker.cpp +++ b/clang-plugin/nullability-checker.cpp @@ -73,10 +73,7 @@ void NullabilityConsumer::HandleTranslationUnit (ASTContext& context) { /* Run away if the plugin is disabled. */ - if (this->_disabled_plugins.get ()->find ("nullability") != - this->_disabled_plugins.get ()->end () || - this->_disabled_plugins.get ()->find ("all") != - this->_disabled_plugins.get ()->end ()) { + if (!this->is_enabled ()) { return; } diff --git a/clang-plugin/nullability-checker.h b/clang-plugin/nullability-checker.h index ed7b7ad..44646f6 100644 --- a/clang-plugin/nullability-checker.h +++ b/clang-plugin/nullability-checker.h @@ -30,6 +30,7 @@ #include <clang/AST/RecursiveASTVisitor.h> #include <clang/Frontend/CompilerInstance.h> +#include "checker.h" #include "gir-manager.h" namespace tartan { @@ -52,20 +53,20 @@ public: bool TraverseFunctionDecl (FunctionDecl* func); }; -class NullabilityConsumer : public ASTConsumer { +class NullabilityConsumer : public tartan::Checker { public: NullabilityConsumer (CompilerInstance& compiler, std::shared_ptr<const GirManager> gir_manager, std::shared_ptr<const std::unordered_set<std::string>> disabled_plugins) : - _visitor (compiler, gir_manager), - _disabled_plugins (disabled_plugins) {} + Checker (compiler, gir_manager, disabled_plugins), + _visitor (compiler, gir_manager) {} private: NullabilityVisitor _visitor; - std::shared_ptr<const std::unordered_set<std::string>> _disabled_plugins; public: virtual void HandleTranslationUnit (ASTContext& context); + const std::string get_name () const { return "nullability"; } }; } /* namespace tartan */ diff --git a/clang-plugin/plugin.cpp b/clang-plugin/plugin.cpp index 383e0bf..30d2288 100644 --- a/clang-plugin/plugin.cpp +++ b/clang-plugin/plugin.cpp @@ -61,18 +61,25 @@ protected: CreateASTConsumer (CompilerInstance &compiler, llvm::StringRef in_file) { std::vector<ASTConsumer*> consumers; + + /* Annotaters. */ consumers.push_back ( new GirAttributesConsumer (this->_gir_manager)); - consumers.push_back (new GAssertAttributesConsumer ()); + consumers.push_back ( + new GAssertAttributesConsumer ()); + + /* Checkers. */ consumers.push_back ( new NullabilityConsumer (compiler, this->_gir_manager, this->_disabled_plugins)); consumers.push_back ( new GVariantConsumer (compiler, + this->_gir_manager, this->_disabled_plugins)); consumers.push_back ( - new GSignalConsumer (compiler, this->_gir_manager, + new GSignalConsumer (compiler, + this->_gir_manager, this->_disabled_plugins)); consumers.push_back ( new GirAttributesChecker (compiler, |