| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef CLAZY_CONTEXT_H |
| 8 |
#define CLAZY_CONTEXT_H |
| 9 |
|
| 10 |
#include "SuppressionManager.h" |
| 11 |
#include "clazy_stl.h" |
| 12 |
|
| 13 |
#include <clang/Basic/FileManager.h> |
| 14 |
#include <clang/Basic/SourceLocation.h> |
| 15 |
#include <clang/Basic/SourceManager.h> |
| 16 |
#include <clang/Frontend/CompilerInstance.h> |
| 17 |
#include <clang/Lex/PreprocessorOptions.h> |
| 18 |
#include <llvm/ADT/StringRef.h> |
| 19 |
#include <llvm/Support/Regex.h> |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
// ClazyContext is just a struct to share data and code between all checks |
| 27 |
|
| 28 |
namespace clang |
| 29 |
{ |
| 30 |
class CompilerInstance; |
| 31 |
class ASTContext; |
| 32 |
class ParentMap; |
| 33 |
class SourceManager; |
| 34 |
class CXXMethodDecl; |
| 35 |
class Decl; |
| 36 |
} |
| 37 |
|
| 38 |
class AccessSpecifierManager; |
| 39 |
class PreProcessorVisitor; |
| 40 |
class FixItExporter; |
| 41 |
|
| 42 |
class ClazyContext |
| 43 |
{ |
| 44 |
public: |
| 45 |
using WarningReporter = std::function<void(std::string checkName, |
| 46 |
const clang::SourceLocation &loc, |
| 47 |
clang::DiagnosticIDs::Level level, |
| 48 |
std::string error, |
| 49 |
const std::vector<clang::FixItHint> &fixits)>; |
| 50 |
|
| 51 |
enum ClazyOption { |
| 52 |
ClazyOption_None = 0, |
| 53 |
ClazyOption_ExportFixes = 1, |
| 54 |
ClazyOption_OnlyQt = 4, // Ignore non-Qt files. This is done by bailing out if QT_CORE_LIB is not set. |
| 55 |
ClazyOption_QtDeveloper = 8, // For running clazy on Qt itself, optional, but honours specific guidelines |
| 56 |
ClazyOption_VisitImplicitCode = 16, // Inspect compiler generated code aswell, useful for custom checks, if they need it |
| 57 |
ClazyOption_IgnoreIncludedFiles = 32 // Only warn for the current file being compiled, not on includes (useful for performance reasons); note that the |
| 58 |
// check has to support this feature i.e. has clazy::CheckBase::Option_CanIgnoreIncludes set |
| 59 |
}; |
| 60 |
using ClazyOptions = int; |
| 61 |
using OptionalFileEntryRef = clang::CustomizableOptional<clang::FileEntryRef>; |
| 62 |
|
| 63 |
explicit ClazyContext(clang::ASTContext *context, // maybe null, in case we lazy initialize it |
| 64 |
clang::SourceManager &manager, |
| 65 |
const clang::LangOptions &lo, |
| 66 |
const clang::PreprocessorOptions &pp, |
| 67 |
const std::string &headerFilter, |
| 68 |
const std::string &ignoreDirs, |
| 69 |
std::string exportFixesFilename, |
| 70 |
const std::vector<std::string> &translationUnitPaths, |
| 71 |
ClazyOptions opts, |
| 72 |
std::optional<WarningReporter> warningReporter = std::nullopt, |
| 73 |
bool isClangTidy = false); |
| 74 |
|
| 75 |
~ClazyContext(); |
| 76 |
|
| 77 |
void registerPreprocessorCallbacks(clang::Preprocessor &pp); |
| 78 |
|
| 79 |
bool usingPreCompiledHeaders() const; |
| 80 |
|
| 81 |
bool userDisabledWError() const |
| 82 |
{ |
| 83 |
return m_noWerror; |
| 84 |
} |
| 85 |
|
| 86 |
bool exportFixesEnabled() const |
| 87 |
{ |
| 88 |
return options & ClazyOption_ExportFixes; |
| 89 |
} |
| 90 |
|
| 91 |
bool isQtDeveloper() const |
| 92 |
{ |
| 93 |
return options & ClazyOption_QtDeveloper; |
| 94 |
} |
| 95 |
|
| 96 |
bool ignoresIncludedFiles() const |
| 97 |
{ |
| 98 |
return options & ClazyOption_IgnoreIncludedFiles; |
| 99 |
} |
| 100 |
|
| 101 |
bool isVisitImplicitCode() const |
| 102 |
{ |
| 103 |
return options & ClazyContext::ClazyOption_VisitImplicitCode; |
| 104 |
} |
| 105 |
|
| 106 |
bool isOptionSet(const std::string &optionName) const |
| 107 |
{ |
| 108 |
return clazy::contains(extraOptions, optionName); |
| 109 |
} |
| 110 |
|
| 111 |
bool fileMatchesLoc(const std::unique_ptr<llvm::Regex> ®ex, clang::SourceLocation loc, ClazyContext::OptionalFileEntryRef &file) const |
| 112 |
{ |
| 113 |
if (!regex) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
if (!file) { |
| 118 |
clang::FileID fid = sm.getDecomposedExpansionLoc(loc).first; |
| 119 |
file = sm.getFileEntryRefForID(fid); |
| 120 |
if (!file) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return regex->match(file->getName()); |
| 126 |
} |
| 127 |
|
| 128 |
bool shouldIgnoreFile(clang::SourceLocation loc) const |
| 129 |
{ |
| 130 |
// 1. Process the regexp that excludes files |
| 131 |
ClazyContext::OptionalFileEntryRef file; |
| 132 |
if (ignoreDirsRegex) { |
| 133 |
const bool matches = fileMatchesLoc(ignoreDirsRegex, loc, file); |
| 134 |
if (matches) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// 2. Process the regexp that includes files. Has lower priority. |
| 140 |
if (!headerFilterRegex || isMainFile(loc)) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
const bool matches = fileMatchesLoc(headerFilterRegex, loc, file); |
| 145 |
if (!file) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
return !matches; |
| 150 |
} |
| 151 |
|
| 152 |
bool isMainFile(clang::SourceLocation loc) const |
| 153 |
{ |
| 154 |
if (loc.isMacroID()) { |
| 155 |
loc = sm.getExpansionLoc(loc); |
| 156 |
} |
| 157 |
|
| 158 |
return sm.isInFileID(loc, sm.getMainFileID()); |
| 159 |
} |
| 160 |
|
| 161 |
bool treatAsError(const std::string &checkName) const |
| 162 |
{ |
| 163 |
return clazy::contains(m_checksPromotedToErrors, checkName); |
| 164 |
} |
| 165 |
|
| 166 |
bool isQt() const; |
| 167 |
|
| 168 |
std::string qtNamespace() const; |
| 169 |
|
| 170 |
/** |
| 171 |
* While the preprocessor visitor is running, we might not have initialized this. |
| 172 |
* Before VisitStmt/VisitDecl/any AST callback in the CheckBase instance is run, this will be set! |
| 173 |
*/ |
| 174 |
clang::ASTContext *astContext; |
| 175 |
|
| 176 |
clang::SourceManager &sm; |
| 177 |
const clang::LangOptions &lo; // Can be deducted from ASTContext, but we might want to lazy initialize the context |
| 178 |
AccessSpecifierManager *accessSpecifierManager = nullptr; |
| 179 |
PreProcessorVisitor *preprocessorVisitor = nullptr; |
| 180 |
SuppressionManager suppressionManager; |
| 181 |
const bool m_noWerror; |
| 182 |
std::vector<std::string> m_checksPromotedToErrors; |
| 183 |
clang::ParentMap *parentMap = nullptr; |
| 184 |
const ClazyOptions options; |
| 185 |
const std::vector<std::string> extraOptions; |
| 186 |
FixItExporter *exporter = nullptr; |
| 187 |
clang::CXXMethodDecl *lastMethodDecl = nullptr; |
| 188 |
clang::FunctionDecl *lastFunctionDecl = nullptr; |
| 189 |
clang::Decl *lastDecl = nullptr; |
| 190 |
std::unique_ptr<llvm::Regex> headerFilterRegex; |
| 191 |
std::unique_ptr<llvm::Regex> ignoreDirsRegex; |
| 192 |
const std::vector<std::string> m_translationUnitPaths; |
| 193 |
const clang::PreprocessorOptions m_pp; |
| 194 |
const WarningReporter p_warningReporter; |
| 195 |
|
| 196 |
const bool m_isClangTidy; |
| 197 |
}; |
| 198 |
|
| 199 |
#endif |