File indexing completed on 2024-04-21 05:38:46
0001 /* 0002 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com 0003 SPDX-FileContributor: Sérgio Martins <sergio.martins@kdab.com> 0004 0005 SPDX-FileCopyrightText: 2015-2017 Sergio Martins <smartins@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #ifndef CLAZY_AST_ACTION_H 0011 #define CLAZY_AST_ACTION_H 0012 0013 #include "ClazyContext.h" 0014 #include "checkbase.h" 0015 #include "checkmanager.h" 0016 0017 #include <clang/AST/ASTConsumer.h> 0018 #include <clang/AST/RecursiveASTVisitor.h> 0019 #include <clang/Frontend/FrontendAction.h> 0020 #include <llvm/ADT/StringRef.h> 0021 0022 #include <memory> 0023 #include <string> 0024 #include <utility> 0025 #include <vector> 0026 0027 namespace llvm 0028 { 0029 class raw_ostream; 0030 } // namespace llvm 0031 0032 namespace clang 0033 { 0034 class CompilerInstance; 0035 class ASTContext; 0036 class Decl; 0037 class Stmt; 0038 } 0039 0040 /** 0041 * This is the FrontendAction that is run when clazy is used as a clang plugin. 0042 */ 0043 class ClazyASTAction : public clang::PluginASTAction 0044 { 0045 public: 0046 ClazyASTAction(); 0047 0048 protected: 0049 /// @note This function is reentrant 0050 std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; 0051 /// @note This function is reentrant 0052 bool ParseArgs(const clang::CompilerInstance &ci, const std::vector<std::string> &args_) override; 0053 0054 void PrintHelp(llvm::raw_ostream &ros) const; 0055 void PrintAnchorHeader(llvm::raw_ostream &ro, RegisteredCheck::List &checks) const; 0056 0057 private: 0058 void printRequestedChecks() const; 0059 RegisteredCheck::List m_checks; 0060 ClazyContext::ClazyOptions m_options = 0; 0061 CheckManager *const m_checkManager; 0062 ClazyContext *m_context = nullptr; 0063 }; 0064 0065 /** 0066 * This is the FrontendAction that is run when clazy is invoked via clazy-standalone. 0067 */ 0068 class ClazyStandaloneASTAction : public clang::ASTFrontendAction 0069 { 0070 public: 0071 explicit ClazyStandaloneASTAction(const std::string &checkList, 0072 const std::string &headerFilter, 0073 const std::string &ignoreDirs, 0074 const std::string &exportFixesFilename, 0075 const std::vector<std::string> &translationUnitPaths, 0076 ClazyContext::ClazyOptions = ClazyContext::ClazyOption_None); 0077 0078 protected: 0079 std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; 0080 0081 private: 0082 const std::string m_checkList; 0083 const std::string m_headerFilter; 0084 const std::string m_ignoreDirs; 0085 const std::string m_exportFixesFilename; 0086 const std::vector<std::string> m_translationUnitPaths; 0087 const ClazyContext::ClazyOptions m_options; 0088 }; 0089 0090 /** 0091 * Clazy's AST Consumer. 0092 */ 0093 class ClazyASTConsumer : public clang::ASTConsumer, public clang::RecursiveASTVisitor<ClazyASTConsumer> 0094 { 0095 public: 0096 explicit ClazyASTConsumer(ClazyContext *context); 0097 ~ClazyASTConsumer() override; 0098 bool shouldVisitImplicitCode() const 0099 { 0100 return m_context->isVisitImplicitCode(); 0101 } 0102 0103 bool VisitDecl(clang::Decl *decl); 0104 bool VisitStmt(clang::Stmt *stm); 0105 void HandleTranslationUnit(clang::ASTContext &ctx) override; 0106 void addCheck(const std::pair<CheckBase *, RegisteredCheck> &check); 0107 0108 ClazyContext *context() const 0109 { 0110 return m_context; 0111 } 0112 0113 private: 0114 ClazyASTConsumer(const ClazyASTConsumer &) = delete; 0115 clang::Stmt *lastStm = nullptr; 0116 ClazyContext *const m_context; 0117 // CheckBase::List m_createdChecks; 0118 CheckBase::List m_checksToVisitStmts; 0119 CheckBase::List m_checksToVisitDecls; 0120 #ifndef CLAZY_DISABLE_AST_MATCHERS 0121 clang::ast_matchers::MatchFinder *m_matchFinder = nullptr; 0122 #endif 0123 }; 0124 0125 #endif