| 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 | |
| 13 | #include "clang/Basic/Diagnostic.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "clang/Tooling/Refactoring.h" |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Regex.h" |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 18 | |
| 19 | namespace clang { |
| 20 | |
| 21 | class CompilerInstance; |
| 22 | namespace ast_matchers { |
| 23 | class MatchFinder; |
| 24 | } |
| 25 | namespace tooling { |
| 26 | class CompilationDatabase; |
| 27 | } |
| 28 | |
| 29 | namespace tidy { |
| 30 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 31 | /// \brief A message from a clang-tidy check. |
| 32 | /// |
| 33 | /// Note that this is independent of a \c SourceManager. |
| 34 | struct ClangTidyMessage { |
| 35 | ClangTidyMessage(StringRef Message = ""); |
| 36 | ClangTidyMessage(StringRef Message, const SourceManager &Sources, |
| 37 | SourceLocation Loc); |
| 38 | std::string Message; |
| 39 | std::string FilePath; |
| 40 | unsigned FileOffset; |
| 41 | }; |
| 42 | |
| 43 | /// \brief A detected error complete with information to display diagnostic and |
| 44 | /// automatic fix. |
| 45 | /// |
| 46 | /// This is used as an intermediate format to transport Diagnostics without a |
| 47 | /// dependency on a SourceManager. |
| 48 | /// |
| 49 | /// FIXME: Make Diagnostics flexible enough to support this directly. |
| 50 | struct ClangTidyError { |
| Alexander Kornienko | 6fbc619 | 2014-03-10 13:11:17 +0000 | [diff] [blame] | 51 | ClangTidyError(StringRef CheckName); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 52 | |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 53 | std::string CheckName; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 54 | ClangTidyMessage Message; |
| 55 | tooling::Replacements Fix; |
| 56 | SmallVector<ClangTidyMessage, 1> Notes; |
| 57 | }; |
| 58 | |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 59 | /// \brief Filters checks by name. |
| 60 | class ChecksFilter { |
| 61 | public: |
| 62 | ChecksFilter(StringRef EnableChecksRegex, StringRef DisableChecksRegex); |
| 63 | bool isCheckEnabled(StringRef Name); |
| 64 | |
| 65 | private: |
| 66 | llvm::Regex EnableChecks; |
| 67 | llvm::Regex DisableChecks; |
| 68 | }; |
| 69 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 70 | /// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine |
| 71 | /// provided by this context. |
| 72 | /// |
| 73 | /// A \c ClangTidyCheck always has access to the active context to report |
| 74 | /// warnings like: |
| 75 | /// \code |
| 76 | /// Context->Diag(Loc, "Single-argument constructors must be explicit") |
| 77 | /// << FixItHint::CreateInsertion(Loc, "explicit "); |
| 78 | /// \endcode |
| 79 | class ClangTidyContext { |
| 80 | public: |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 81 | ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors, |
| 82 | StringRef EnableChecksRegex, StringRef DisableChecksRegex); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 83 | |
| 84 | /// \brief Report any errors detected using this method. |
| 85 | /// |
| 86 | /// This is still under heavy development and will likely change towards using |
| 87 | /// tablegen'd diagnostic IDs. |
| 88 | /// FIXME: Figure out a way to manage ID spaces. |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 89 | DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, |
| Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 90 | StringRef Message, |
| 91 | DiagnosticIDs::Level Level = DiagnosticIDs::Warning); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 92 | |
| 93 | /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated |
| 94 | /// correctly. |
| 95 | /// |
| 96 | /// This is called from the \c ClangTidyCheck base class. |
| 97 | void setDiagnosticsEngine(DiagnosticsEngine *Engine); |
| 98 | |
| 99 | /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine. |
| 100 | /// |
| 101 | /// This is called from the \c ClangTidyCheck base class. |
| 102 | void setSourceManager(SourceManager *SourceMgr); |
| 103 | |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 104 | /// \brief Returns the name of the clang-tidy check which produced this |
| 105 | /// diagnostic ID. |
| 106 | StringRef getCheckName(unsigned DiagnosticID) const; |
| 107 | |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 108 | ChecksFilter &getChecksFilter() { return Filter; } |
| 109 | |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 110 | private: |
| 111 | friend class ClangTidyDiagnosticConsumer; // Calls storeError(). |
| 112 | |
| 113 | /// \brief Store a \c ClangTidyError. |
| 114 | void storeError(const ClangTidyError &Error); |
| 115 | |
| 116 | SmallVectorImpl<ClangTidyError> *Errors; |
| NAKAMURA Takumi | 338eee1 | 2014-03-20 10:53:03 +0000 | [diff] [blame^] | 117 | DiagnosticsEngine *DiagEngine; |
| Alexander Kornienko | 09952d2 | 2014-03-20 09:38:22 +0000 | [diff] [blame] | 118 | ChecksFilter Filter; |
| 119 | |
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 120 | llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 123 | /// \brief A diagnostic consumer that turns each \c Diagnostic into a |
| 124 | /// \c SourceManager-independent \c ClangTidyError. |
| 125 | // |
| 126 | // FIXME: If we move away from unit-tests, this can be moved to a private |
| 127 | // implementation file. |
| 128 | class ClangTidyDiagnosticConsumer : public DiagnosticConsumer { |
| 129 | public: |
| Alexander Kornienko | 0ba86b7 | 2014-01-09 16:31:25 +0000 | [diff] [blame] | 130 | ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx); |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 131 | |
| 132 | // FIXME: The concept of converting between FixItHints and Replacements is |
| 133 | // more generic and should be pulled out into a more useful Diagnostics |
| 134 | // library. |
| Alexander Kornienko | cb9272f | 2014-02-27 13:14:51 +0000 | [diff] [blame] | 135 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 136 | const Diagnostic &Info) override; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 137 | |
| Alexander Kornienko | fc58987 | 2014-01-03 15:34:40 +0000 | [diff] [blame] | 138 | // Flushes the internal diagnostics buffer to the ClangTidyContext. |
| Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 139 | void finish() override; |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 140 | |
| 141 | private: |
| Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 142 | void finalizeLastError(); |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 143 | |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 144 | ClangTidyContext &Context; |
| Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 145 | std::unique_ptr<DiagnosticsEngine> Diags; |
| Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 146 | SmallVector<ClangTidyError, 8> Errors; |
| Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 147 | bool LastErrorRelatesToUserCode; |
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // end namespace tidy |
| 151 | } // end namespace clang |
| 152 | |
| 153 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H |