| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2015-2017 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef CLAZY_AST_ACTION_H |
| 11 |
#define CLAZY_AST_ACTION_H |
| 12 |
|
| 13 |
#include "ClazyContext.h" |
| 14 |
#include "ClazyVisitHelper.h" |
| 15 |
#include "checkbase.h" |
| 16 |
#include "checkmanager.h" |
| 17 |
|
| 18 |
#include <clang/AST/ASTConsumer.h> |
| 19 |
#include <clang/AST/RecursiveASTVisitor.h> |
| 20 |
#include <clang/Frontend/FrontendAction.h> |
| 21 |
#include <llvm/ADT/StringRef.h> |
| 22 |
|
| 23 |
#include <memory> |
| 24 |
#include <string> |
| 25 |
#include <utility> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
namespace llvm |
| 29 |
{ |
| 30 |
class raw_ostream; |
| 31 |
} // namespace llvm |
| 32 |
|
| 33 |
namespace clang |
| 34 |
{ |
| 35 |
class CompilerInstance; |
| 36 |
class ASTContext; |
| 37 |
class Decl; |
| 38 |
class Stmt; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* This is the FrontendAction that is run when clazy is used as a clang plugin. |
| 43 |
*/ |
| 44 |
class ClazyASTAction : public clang::PluginASTAction |
| 45 |
{ |
| 46 |
public: |
| 47 |
ClazyASTAction(); |
| 48 |
|
| 49 |
protected: |
| 50 |
/// @note This function is reentrant |
| 51 |
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; |
| 52 |
/// @note This function is reentrant |
| 53 |
bool ParseArgs(const clang::CompilerInstance &ci, const std::vector<std::string> &args_) override; |
| 54 |
|
| 55 |
void PrintHelp(llvm::raw_ostream &ros) const; |
| 56 |
void PrintAnchorHeader(llvm::raw_ostream &ro, RegisteredCheck::List &checks) const; |
| 57 |
|
| 58 |
private: |
| 59 |
void printRequestedChecks() const; |
| 60 |
RegisteredCheck::List m_checks; |
| 61 |
ClazyContext::ClazyOptions m_options = 0; |
| 62 |
CheckManager *const m_checkManager; |
| 63 |
ClazyContext *m_context = nullptr; |
| 64 |
}; |
| 65 |
|
| 66 |
/** |
| 67 |
* This is the FrontendAction that is run when clazy is invoked via clazy-standalone. |
| 68 |
*/ |
| 69 |
class ClazyStandaloneASTAction : public clang::ASTFrontendAction |
| 70 |
{ |
| 71 |
public: |
| 72 |
explicit ClazyStandaloneASTAction(const std::string &checkList, |
| 73 |
const std::string &headerFilter, |
| 74 |
const std::string &ignoreDirs, |
| 75 |
const std::string &exportFixesFilename, |
| 76 |
const std::vector<std::string> &translationUnitPaths, |
| 77 |
ClazyContext::ClazyOptions = ClazyContext::ClazyOption_None); |
| 78 |
|
| 79 |
protected: |
| 80 |
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; |
| 81 |
|
| 82 |
private: |
| 83 |
const std::string m_checkList; |
| 84 |
const std::string m_headerFilter; |
| 85 |
const std::string m_ignoreDirs; |
| 86 |
const std::string m_exportFixesFilename; |
| 87 |
const std::vector<std::string> m_translationUnitPaths; |
| 88 |
const ClazyContext::ClazyOptions m_options; |
| 89 |
}; |
| 90 |
|
| 91 |
/** |
| 92 |
* Clazy's AST Consumer. |
| 93 |
*/ |
| 94 |
class ClazyASTConsumer : public clang::ASTConsumer, public clang::RecursiveASTVisitor<ClazyASTConsumer> |
| 95 |
{ |
| 96 |
public: |
| 97 |
explicit ClazyASTConsumer(ClazyContext *context); |
| 98 |
~ClazyASTConsumer() override; |
| 99 |
bool shouldVisitImplicitCode() const |
| 100 |
{ |
| 101 |
return m_context->isVisitImplicitCode(); |
| 102 |
} |
| 103 |
|
| 104 |
bool VisitDecl(clang::Decl *decl); |
| 105 |
bool VisitStmt(clang::Stmt *stm); |
| 106 |
void HandleTranslationUnit(clang::ASTContext &ctx) override; |
| 107 |
void addCheck(const std::pair<CheckBase *, RegisteredCheck> &check); |
| 108 |
|
| 109 |
ClazyContext *context() const |
| 110 |
{ |
| 111 |
return m_context; |
| 112 |
} |
| 113 |
|
| 114 |
private: |
| 115 |
ClazyASTConsumer(const ClazyASTConsumer &) = delete; |
| 116 |
ClazyContext *const m_context; |
| 117 |
clazy::VisitHelper::Visitors m_visitors; |
| 118 |
clang::ast_matchers::MatchFinder *m_matchFinder = nullptr; |
| 119 |
}; |
| 120 |
|
| 121 |
#endif |