| 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" |
| 16 | #include "llvm/ADT/SmallString.h" |
| 17 | |
| 18 | namespace clang { |
| 19 | |
| 20 | class CompilerInstance; |
| 21 | namespace ast_matchers { |
| 22 | class MatchFinder; |
| 23 | } |
| 24 | namespace tooling { |
| 25 | class CompilationDatabase; |
| 26 | } |
| 27 | |
| 28 | namespace tidy { |
| 29 | |
| 30 | /// \brief A diagnostic consumer that turns each \c Diagnostic into a |
| 31 | /// \c SourceManager-independent \c ClangTidyError. |
| 32 | // |
| 33 | // FIXME: If we move away from unit-tests, this can be moved to a private |
| 34 | // implementation file. |
| 35 | class ClangTidyDiagnosticConsumer : public DiagnosticConsumer { |
| 36 | public: |
| 37 | ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx) : Context(Ctx) { |
| 38 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 39 | Diags.reset(new DiagnosticsEngine( |
| 40 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this, |
| 41 | /*ShouldOwnClient=*/false)); |
| 42 | Context.setDiagnosticsEngine(Diags.get()); |
| 43 | } |
| 44 | |
| 45 | // FIXME: The concept of converting between FixItHints and Replacements is |
| 46 | // more generic and should be pulled out into a more useful Diagnostics |
| 47 | // library. |
| 48 | virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 49 | const Diagnostic &Info) { |
| 50 | tooling::Replacements Replacements; |
| 51 | SourceManager &SourceMgr = Info.getSourceManager(); |
| 52 | for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i) { |
| 53 | Replacements.insert(tooling::Replacement( |
| 54 | SourceMgr, Info.getFixItHint(i).RemoveRange.getBegin(), 0, |
| 55 | Info.getFixItHint(i).CodeToInsert)); |
| 56 | } |
| 57 | SmallString<100> Buf; |
| 58 | Info.FormatDiagnostic(Buf); |
| 59 | Context.storeError( |
| 60 | ClangTidyError(SourceMgr, Info.getLocation(), Buf.str(), Replacements)); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | ClangTidyContext &Context; |
| 65 | OwningPtr<DiagnosticsEngine> Diags; |
| 66 | }; |
| 67 | |
| 68 | } // end namespace tidy |
| 69 | } // end namespace clang |
| 70 | |
| 71 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H |