| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H |
| 12 | |
| Alexander Kornienko | 9ff5b6f | 2014-05-05 14:54:47 +0000 | [diff] [blame] | 13 | #include "ClangTidyOptions.h" |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/Tooling/Refactoring.h" |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Regex.h" |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 19 | |
| 20 | namespace clang { |
| 21 | |
| Alexander Kornienko | ad21688 | 2014-07-14 14:10:03 +0000 | [diff] [blame^] | 22 | class ASTContext; |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 23 | class CompilerInstance; |
| 24 | namespace ast_matchers { |
| 25 | class MatchFinder; |
| 26 | } |
| 27 | namespace tooling { |
| 28 | class CompilationDatabase; |
| 29 | } |
| 30 | |
| 31 | namespace tidy { |
| 32 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 33 | /// \brief A message from a clang-tidy check. |
| 34 | /// |
| 35 | /// Note that this is independent of a \c SourceManager. |
| 36 | struct ClangTidyMessage { |
| 37 | ClangTidyMessage(StringRef Message = ""); |
| 38 | ClangTidyMessage(StringRef Message, const SourceManager &Sources, |
| 39 | SourceLocation Loc); |
| 40 | std::string Message; |
| 41 | std::string FilePath; |
| 42 | unsigned FileOffset; |
| 43 | }; |
| 44 | |
| 45 | /// \brief A detected error complete with information to display diagnostic and |
| 46 | /// automatic fix. |
| 47 | /// |
| 48 | /// This is used as an intermediate format to transport Diagnostics without a |
| 49 | /// dependency on a SourceManager. |
| 50 | /// |
| 51 | /// FIXME: Make Diagnostics flexible enough to support this directly. |
| 52 | struct ClangTidyError { |
| Alexander Kornienko | 348cae8 | 2014-06-02 20:44:32 +0000 | [diff] [blame] | 53 | enum Level { |
| 54 | Warning = DiagnosticsEngine::Warning, |
| 55 | Error = DiagnosticsEngine::Error |
| 56 | }; |
| 57 | |
| 58 | ClangTidyError(StringRef CheckName, Level DiagLevel); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 59 | |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 60 | std::string CheckName; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 61 | ClangTidyMessage Message; |
| 62 | tooling::Replacements Fix; |
| 63 | SmallVector<ClangTidyMessage, 1> Notes; |
| Alexander Kornienko | 348cae8 | 2014-06-02 20:44:32 +0000 | [diff] [blame] | 64 | |
| 65 | Level DiagLevel; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 68 | /// \brief Filters checks by name. |
| 69 | class ChecksFilter { |
| 70 | public: |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 71 | /// \brief \p GlobList is a comma-separated list of globs (only '*' |
| 72 | /// metacharacter is supported) with optional '-' prefix to denote exclusion. |
| Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 73 | ChecksFilter(StringRef GlobList); |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 74 | |
| 75 | /// \brief Returns \c true if the check with the specified \p Name should be |
| 76 | /// enabled. The result is the last matching glob's Positive flag. If \p Name |
| 77 | /// is not matched by any globs, the check is not enabled. |
| Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 78 | bool isCheckEnabled(StringRef Name) { return isCheckEnabled(Name, false); } |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
| Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 81 | bool isCheckEnabled(StringRef Name, bool Enabled); |
| 82 | |
| 83 | bool Positive; |
| 84 | llvm::Regex Regex; |
| 85 | std::unique_ptr<ChecksFilter> NextFilter; |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 88 | /// \brief Contains displayed and ignored diagnostic counters for a ClangTidy |
| 89 | /// run. |
| Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 90 | struct ClangTidyStats { |
| 91 | ClangTidyStats() |
| 92 | : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0), |
| Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 93 | ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {} |
| Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 94 | |
| 95 | unsigned ErrorsDisplayed; |
| 96 | unsigned ErrorsIgnoredCheckFilter; |
| 97 | unsigned ErrorsIgnoredNOLINT; |
| 98 | unsigned ErrorsIgnoredNonUserCode; |
| Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 99 | unsigned ErrorsIgnoredLineFilter; |
| 100 | |
| 101 | unsigned errorsIgnored() const { |
| 102 | return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter + |
| 103 | ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter; |
| 104 | } |
| Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 107 | /// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine |
| 108 | /// provided by this context. |
| 109 | /// |
| 110 | /// A \c ClangTidyCheck always has access to the active context to report |
| 111 | /// warnings like: |
| 112 | /// \code |
| 113 | /// Context->Diag(Loc, "Single-argument constructors must be explicit") |
| 114 | /// << FixItHint::CreateInsertion(Loc, "explicit "); |
| 115 | /// \endcode |
| 116 | class ClangTidyContext { |
| 117 | public: |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 118 | /// \brief Initializes \c ClangTidyContext instance. |
| 119 | /// |
| 120 | /// Takes ownership of the \c OptionsProvider. |
| 121 | ClangTidyContext(ClangTidyOptionsProvider *OptionsProvider); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 122 | |
| 123 | /// \brief Report any errors detected using this method. |
| 124 | /// |
| 125 | /// This is still under heavy development and will likely change towards using |
| 126 | /// tablegen'd diagnostic IDs. |
| 127 | /// FIXME: Figure out a way to manage ID spaces. |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 128 | DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, |
| Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 129 | StringRef Message, |
| 130 | DiagnosticIDs::Level Level = DiagnosticIDs::Warning); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 131 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 132 | /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine. |
| 133 | /// |
| 134 | /// This is called from the \c ClangTidyCheck base class. |
| 135 | void setSourceManager(SourceManager *SourceMgr); |
| 136 | |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 137 | /// \brief Should be called when starting to process new translation unit. |
| 138 | void setCurrentFile(StringRef File); |
| 139 | |
| Alexander Kornienko | ad21688 | 2014-07-14 14:10:03 +0000 | [diff] [blame^] | 140 | /// \brief Sets ASTContext for the current translation unit. |
| 141 | void setASTContext(ASTContext *Context); |
| 142 | |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 143 | /// \brief Returns the name of the clang-tidy check which produced this |
| 144 | /// diagnostic ID. |
| 145 | StringRef getCheckName(unsigned DiagnosticID) const; |
| 146 | |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 147 | /// \brief Returns check filter for the \c CurrentFile. |
| 148 | ChecksFilter &getChecksFilter(); |
| 149 | |
| 150 | /// \brief Returns global options. |
| 151 | const ClangTidyGlobalOptions &getGlobalOptions() const; |
| 152 | |
| 153 | /// \brief Returns options for \c CurrentFile. |
| 154 | const ClangTidyOptions &getOptions() const; |
| 155 | |
| 156 | /// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic |
| 157 | /// counters. |
| Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 158 | const ClangTidyStats &getStats() const { return Stats; } |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 159 | |
| 160 | /// \brief Returns all collected errors. |
| Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 161 | const std::vector<ClangTidyError> &getErrors() const { return Errors; } |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 162 | |
| 163 | /// \brief Clears collected errors. |
| Alexander Kornienko | c009360 | 2014-05-09 15:50:15 +0000 | [diff] [blame] | 164 | void clearErrors() { Errors.clear(); } |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 165 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 166 | private: |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 167 | // Calls setDiagnosticsEngine() and storeError(). |
| 168 | friend class ClangTidyDiagnosticConsumer; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 169 | |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 170 | /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated |
| 171 | /// correctly. |
| 172 | void setDiagnosticsEngine(DiagnosticsEngine *Engine); |
| 173 | |
| 174 | /// \brief Store an \p Error. |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 175 | void storeError(const ClangTidyError &Error); |
| 176 | |
| Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 177 | std::vector<ClangTidyError> Errors; |
| NAKAMURA Takumi | 338eee1 | 2014-03-20 10:53:03 +0000 | [diff] [blame] | 178 | DiagnosticsEngine *DiagEngine; |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 179 | std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider; |
| 180 | |
| 181 | std::string CurrentFile; |
| 182 | std::unique_ptr<ChecksFilter> CheckFilter; |
| 183 | |
| Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 184 | ClangTidyStats Stats; |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 185 | |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 186 | llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 189 | /// \brief A diagnostic consumer that turns each \c Diagnostic into a |
| 190 | /// \c SourceManager-independent \c ClangTidyError. |
| 191 | // |
| 192 | // FIXME: If we move away from unit-tests, this can be moved to a private |
| 193 | // implementation file. |
| 194 | class ClangTidyDiagnosticConsumer : public DiagnosticConsumer { |
| 195 | public: |
| Alexander Kornienko | 0ba86b7 | 2014-01-09 16:31:25 +0000 | [diff] [blame] | 196 | ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx); |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 197 | |
| 198 | // FIXME: The concept of converting between FixItHints and Replacements is |
| 199 | // more generic and should be pulled out into a more useful Diagnostics |
| 200 | // library. |
| Alexander Kornienko | cb9272f | 2014-02-27 13:14:51 +0000 | [diff] [blame] | 201 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 202 | const Diagnostic &Info) override; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 203 | |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 204 | /// \brief Sets \c HeaderFilter to the value configured for this file. |
| 205 | void BeginSourceFile(const LangOptions &LangOpts, |
| 206 | const Preprocessor *PP) override; |
| 207 | |
| 208 | /// \brief Flushes the internal diagnostics buffer to the ClangTidyContext. |
| Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 209 | void finish() override; |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 210 | |
| 211 | private: |
| Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 212 | void finalizeLastError(); |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 213 | |
| 214 | /// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter |
| 215 | /// according to the diagnostic \p Location. |
| Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 216 | void checkFilters(SourceLocation Location); |
| 217 | bool passesLineFilter(StringRef FileName, unsigned LineNumber) const; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 218 | |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 219 | ClangTidyContext &Context; |
| Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 220 | std::unique_ptr<DiagnosticsEngine> Diags; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 221 | SmallVector<ClangTidyError, 8> Errors; |
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 222 | std::unique_ptr<llvm::Regex> HeaderFilter; |
| Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 223 | bool LastErrorRelatesToUserCode; |
| Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 224 | bool LastErrorPassesLineFilter; |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | } // end namespace tidy |
| 228 | } // end namespace clang |
| 229 | |
| 230 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H |