| 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-2016 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef CLANG_LAZY_CHECK_MANAGER_H |
| 11 |
#define CLANG_LAZY_CHECK_MANAGER_H |
| 12 |
|
| 13 |
#include "checkbase.h" |
| 14 |
|
| 15 |
#include <clang/Lex/PreprocessorOptions.h> |
| 16 |
|
| 17 |
#include <functional> |
| 18 |
#include <mutex> |
| 19 |
#include <string> |
| 20 |
#include <unordered_map> |
| 21 |
#include <utility> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
class ClazyContext; |
| 25 |
|
| 26 |
struct RegisteredFixIt { |
| 27 |
using List = std::vector<RegisteredFixIt>; |
| 28 |
RegisteredFixIt() |
| 29 |
: id(-1) |
| 30 |
{ |
| 31 |
} |
| 32 |
RegisteredFixIt(int id, const std::string &name) |
| 33 |
: id(id) |
| 34 |
, name(name) |
| 35 |
{ |
| 36 |
} |
| 37 |
int id = -1; |
| 38 |
std::string name; |
| 39 |
bool operator==(const RegisteredFixIt &other) const |
| 40 |
{ |
| 41 |
return id == other.id; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
using FactoryFunction = std::function<CheckBase *()>; |
| 46 |
|
| 47 |
struct RegisteredCheck { |
| 48 |
enum Option { Option_None = 0, Option_VisitsStmts = 2, Option_VisitsDecls = 4, Option_PreprocessorCallbacks = 8, Option_VisitAllTypeDefs = 16 }; |
| 49 |
|
| 50 |
using List = std::vector<RegisteredCheck>; |
| 51 |
using Options = int; |
| 52 |
|
| 53 |
std::string name; |
| 54 |
CheckLevel level; |
| 55 |
FactoryFunction factory; |
| 56 |
Options options; |
| 57 |
bool operator==(const RegisteredCheck &other) const |
| 58 |
{ |
| 59 |
return name == other.name; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
inline bool checkLessThan(const RegisteredCheck &c1, const RegisteredCheck &c2) |
| 64 |
{ |
| 65 |
return c1.name < c2.name; |
| 66 |
} |
| 67 |
|
| 68 |
inline bool checkLessThanByLevel(const RegisteredCheck &c1, const RegisteredCheck &c2) |
| 69 |
{ |
| 70 |
if (c1.level == c2.level) { |
| 71 |
return checkLessThan(c1, c2); |
| 72 |
} |
| 73 |
|
| 74 |
return c1.level < c2.level; |
| 75 |
} |
| 76 |
|
| 77 |
class CheckManager |
| 78 |
{ |
| 79 |
public: |
| 80 |
/** |
| 81 |
* @note You must hold the CheckManager lock when operating on the instance |
| 82 |
* |
| 83 |
* @sa lock() |
| 84 |
*/ |
| 85 |
static CheckManager *instance(); |
| 86 |
|
| 87 |
static std::mutex &lock() |
| 88 |
{ |
| 89 |
return m_lock; |
| 90 |
} |
| 91 |
RegisteredCheck::List availableChecks(CheckLevel maxLevel) const; |
| 92 |
RegisteredCheck::List requestedChecksThroughEnv(std::vector<std::string> &userDisabledChecks) const; |
| 93 |
std::vector<std::string> checksAsErrors() const; |
| 94 |
|
| 95 |
RegisteredCheck::List::const_iterator checkForName(const RegisteredCheck::List &checks, const std::string &name) const; |
| 96 |
RegisteredCheck::List checksForCommaSeparatedString(const std::string &str) const; |
| 97 |
RegisteredCheck::List checksForCommaSeparatedString(const std::string &str, std::vector<std::string> &userDisabledChecks) const; |
| 98 |
RegisteredFixIt::List availableFixIts(const std::string &checkName) const; |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns all the requested checks. |
| 102 |
* This is a union of the requested checks via env variable and via arguments passed to compiler |
| 103 |
*/ |
| 104 |
RegisteredCheck::List requestedChecks(std::vector<std::string> &args); |
| 105 |
std::vector<std::pair<CheckBase *, RegisteredCheck>> createChecks(const RegisteredCheck::List &requestedChecks, ClazyContext *context); |
| 106 |
|
| 107 |
static void removeChecksFromList(RegisteredCheck::List &list, std::vector<std::string> &checkNames); |
| 108 |
|
| 109 |
private: |
| 110 |
CheckManager(); |
| 111 |
static std::mutex m_lock; |
| 112 |
|
| 113 |
void registerChecks(); |
| 114 |
void registerFixIt(int id, const std::string &fititName, const std::string &checkName); |
| 115 |
void registerCheck(const RegisteredCheck &check); |
| 116 |
bool checkExists(const std::string &name) const; |
| 117 |
RegisteredCheck::List checksForLevel(int level) const; |
| 118 |
CheckBase *createCheck(const std::string &name); |
| 119 |
std::string checkNameForFixIt(const std::string &) const; |
| 120 |
RegisteredCheck::List m_registeredChecks; |
| 121 |
std::unordered_map<std::string, std::vector<RegisteredFixIt>> m_fixitsByCheckName; |
| 122 |
std::unordered_map<std::string, RegisteredFixIt> m_fixitByName; |
| 123 |
}; |
| 124 |
|
| 125 |
#endif |