| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H |
| 11 |
|
| 12 |
#include "ClangTidyOptions.h" |
| 13 |
#include "ClangTidyProfiling.h" |
| 14 |
#include "NoLintDirectiveHandler.h" |
| 15 |
#include "clang/Basic/Diagnostic.h" |
| 16 |
#include "clang/Tooling/Core/Diagnostic.h" |
| 17 |
#include "llvm/ADT/DenseMap.h" |
| 18 |
#include "llvm/ADT/StringSet.h" |
| 19 |
#include "llvm/Support/Regex.h" |
| 20 |
#include <optional> |
| 21 |
|
| 22 |
namespace clang { |
| 23 |
|
| 24 |
class ASTContext; |
| 25 |
class SourceManager; |
| 26 |
|
| 27 |
namespace tidy { |
| 28 |
class CachedGlobList; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
struct ClangTidyError : tooling::Diagnostic { |
| 38 |
ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory, |
| 39 |
bool IsWarningAsError); |
| 40 |
|
| 41 |
bool IsWarningAsError; |
| 42 |
std::vector<std::string> EnabledDiagnosticAliases; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
struct ClangTidyStats { |
| 47 |
unsigned ErrorsDisplayed = 0; |
| 48 |
unsigned ErrorsIgnoredCheckFilter = 0; |
| 49 |
unsigned ErrorsIgnoredNOLINT = 0; |
| 50 |
unsigned ErrorsIgnoredNonUserCode = 0; |
| 51 |
unsigned ErrorsIgnoredLineFilter = 0; |
| 52 |
|
| 53 |
unsigned errorsIgnored() const { |
| 54 |
return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter + |
| 55 |
ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
class ClangTidyContext { |
| 69 |
public: |
| 70 |
|
| 71 |
ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider, |
| 72 |
bool AllowEnablingAnalyzerAlphaCheckers = false); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void setDiagnosticsEngine(DiagnosticsEngine *DiagEngine) { |
| 77 |
this->DiagEngine = DiagEngine; |
| 78 |
} |
| 79 |
|
| 80 |
~ClangTidyContext(); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, |
| 88 |
StringRef Message, |
| 89 |
DiagnosticIDs::Level Level = DiagnosticIDs::Warning); |
| 90 |
|
| 91 |
DiagnosticBuilder diag(StringRef CheckName, StringRef Message, |
| 92 |
DiagnosticIDs::Level Level = DiagnosticIDs::Warning); |
| 93 |
|
| 94 |
DiagnosticBuilder diag(const tooling::Diagnostic &Error); |
| 95 |
|
| 96 |
|
| 97 |
DiagnosticBuilder |
| 98 |
configurationDiag(StringRef Message, |
| 99 |
DiagnosticIDs::Level Level = DiagnosticIDs::Warning); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
bool |
| 116 |
shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 117 |
const Diagnostic &Info, |
| 118 |
SmallVectorImpl<tooling::Diagnostic> &NoLintErrors, |
| 119 |
bool AllowIO = true, bool EnableNoLintBlocks = true); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void setSourceManager(SourceManager *SourceMgr); |
| 125 |
|
| 126 |
|
| 127 |
void setCurrentFile(StringRef File); |
| 128 |
|
| 129 |
|
| 130 |
StringRef getCurrentFile() const { return CurrentFile; } |
| 131 |
|
| 132 |
|
| 133 |
void setASTContext(ASTContext *Context); |
| 134 |
|
| 135 |
|
| 136 |
const LangOptions &getLangOpts() const { return LangOpts; } |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
std::string getCheckName(unsigned DiagnosticID) const; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
bool isCheckEnabled(StringRef CheckName) const; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
bool treatAsError(StringRef CheckName) const; |
| 150 |
|
| 151 |
|
| 152 |
const ClangTidyGlobalOptions &getGlobalOptions() const; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
const ClangTidyOptions &getOptions() const; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
ClangTidyOptions getOptionsForFile(StringRef File) const; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
const ClangTidyStats &getStats() const { return Stats; } |
| 166 |
|
| 167 |
|
| 168 |
void setEnableProfiling(bool Profile); |
| 169 |
bool getEnableProfiling() const { return Profile; } |
| 170 |
|
| 171 |
|
| 172 |
void setProfileStoragePrefix(StringRef ProfilePrefix); |
| 173 |
std::optional<ClangTidyProfiling::StorageParams> |
| 174 |
getProfileStorageParams() const; |
| 175 |
|
| 176 |
|
| 177 |
void setCurrentBuildDirectory(StringRef BuildDirectory) { |
| 178 |
CurrentBuildDirectory = std::string(BuildDirectory); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
const std::string &getCurrentBuildDirectory() const { |
| 183 |
return CurrentBuildDirectory; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
bool canEnableAnalyzerAlphaCheckers() const { |
| 189 |
return AllowEnablingAnalyzerAlphaCheckers; |
| 190 |
} |
| 191 |
|
| 192 |
void setSelfContainedDiags(bool Value) { SelfContainedDiags = Value; } |
| 193 |
|
| 194 |
bool areDiagsSelfContained() const { return SelfContainedDiags; } |
| 195 |
|
| 196 |
using DiagLevelAndFormatString = std::pair<DiagnosticIDs::Level, std::string>; |
| 197 |
DiagLevelAndFormatString getDiagLevelAndFormatString(unsigned DiagnosticID, |
| 198 |
SourceLocation Loc) { |
| 199 |
return DiagLevelAndFormatString( |
| 200 |
static_cast<DiagnosticIDs::Level>( |
| 201 |
DiagEngine->getDiagnosticLevel(DiagnosticID, Loc)), |
| 202 |
std::string( |
| 203 |
DiagEngine->getDiagnosticIDs()->getDescription(DiagnosticID))); |
| 204 |
} |
| 205 |
|
| 206 |
void setOptionsCollector(llvm::StringSet<> *Collector) { |
| 207 |
OptionsCollector = Collector; |
| 208 |
} |
| 209 |
llvm::StringSet<> *getOptionsCollector() const { return OptionsCollector; } |
| 210 |
|
| 211 |
private: |
| 212 |
|
| 213 |
friend class ClangTidyDiagnosticConsumer; |
| 214 |
|
| 215 |
DiagnosticsEngine *DiagEngine; |
| 216 |
std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider; |
| 217 |
|
| 218 |
std::string CurrentFile; |
| 219 |
ClangTidyOptions CurrentOptions; |
| 220 |
|
| 221 |
std::unique_ptr<CachedGlobList> CheckFilter; |
| 222 |
std::unique_ptr<CachedGlobList> WarningAsErrorFilter; |
| 223 |
|
| 224 |
LangOptions LangOpts; |
| 225 |
|
| 226 |
ClangTidyStats Stats; |
| 227 |
|
| 228 |
std::string CurrentBuildDirectory; |
| 229 |
|
| 230 |
llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID; |
| 231 |
|
| 232 |
bool Profile; |
| 233 |
std::string ProfilePrefix; |
| 234 |
|
| 235 |
bool AllowEnablingAnalyzerAlphaCheckers; |
| 236 |
|
| 237 |
bool SelfContainedDiags; |
| 238 |
|
| 239 |
NoLintDirectiveHandler NoLintHandler; |
| 240 |
llvm::StringSet<> *OptionsCollector = nullptr; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
const llvm::StringMap<tooling::Replacements> * |
| 248 |
getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
class ClangTidyDiagnosticConsumer : public DiagnosticConsumer { |
| 255 |
public: |
| 256 |
|
| 257 |
|
| 258 |
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx, |
| 259 |
DiagnosticsEngine *ExternalDiagEngine = nullptr, |
| 260 |
bool RemoveIncompatibleErrors = true, |
| 261 |
bool GetFixesFromNotes = false, |
| 262 |
bool EnableNolintBlocks = true); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 268 |
const Diagnostic &Info) override; |
| 269 |
|
| 270 |
|
| 271 |
std::vector<ClangTidyError> take(); |
| 272 |
|
| 273 |
private: |
| 274 |
void finalizeLastError(); |
| 275 |
void removeIncompatibleErrors(); |
| 276 |
void removeDuplicatedDiagnosticsOfAliasCheckers(); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
llvm::Regex *getHeaderFilter(); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
void checkFilters(SourceLocation Location, const SourceManager &Sources); |
| 285 |
bool passesLineFilter(StringRef FileName, unsigned LineNumber) const; |
| 286 |
|
| 287 |
void forwardDiagnostic(const Diagnostic &Info); |
| 288 |
|
| 289 |
ClangTidyContext &Context; |
| 290 |
DiagnosticsEngine *ExternalDiagEngine; |
| 291 |
bool RemoveIncompatibleErrors; |
| 292 |
bool GetFixesFromNotes; |
| 293 |
bool EnableNolintBlocks; |
| 294 |
std::vector<ClangTidyError> Errors; |
| 295 |
std::unique_ptr<llvm::Regex> HeaderFilter; |
| 296 |
bool LastErrorRelatesToUserCode; |
| 297 |
bool LastErrorPassesLineFilter; |
| 298 |
bool LastErrorWasIgnored; |
| 299 |
}; |
| 300 |
|
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
#endif |