2013-07-29 16:19:24 +08:00
|
|
|
//===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
|
|
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
class CompilerInstance;
|
|
|
|
namespace ast_matchers {
|
|
|
|
class MatchFinder;
|
|
|
|
}
|
|
|
|
namespace tooling {
|
|
|
|
class CompilationDatabase;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace tidy {
|
|
|
|
|
2013-11-14 23:49:44 +08:00
|
|
|
/// \brief A message from a clang-tidy check.
|
|
|
|
///
|
|
|
|
/// Note that this is independent of a \c SourceManager.
|
|
|
|
struct ClangTidyMessage {
|
|
|
|
ClangTidyMessage(StringRef Message = "");
|
|
|
|
ClangTidyMessage(StringRef Message, const SourceManager &Sources,
|
|
|
|
SourceLocation Loc);
|
|
|
|
std::string Message;
|
|
|
|
std::string FilePath;
|
|
|
|
unsigned FileOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief A detected error complete with information to display diagnostic and
|
|
|
|
/// automatic fix.
|
|
|
|
///
|
|
|
|
/// This is used as an intermediate format to transport Diagnostics without a
|
|
|
|
/// dependency on a SourceManager.
|
|
|
|
///
|
|
|
|
/// FIXME: Make Diagnostics flexible enough to support this directly.
|
|
|
|
struct ClangTidyError {
|
|
|
|
ClangTidyError(const ClangTidyMessage &Message);
|
|
|
|
|
|
|
|
ClangTidyMessage Message;
|
|
|
|
tooling::Replacements Fix;
|
|
|
|
SmallVector<ClangTidyMessage, 1> Notes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
|
|
|
|
/// provided by this context.
|
|
|
|
///
|
|
|
|
/// A \c ClangTidyCheck always has access to the active context to report
|
|
|
|
/// warnings like:
|
|
|
|
/// \code
|
|
|
|
/// Context->Diag(Loc, "Single-argument constructors must be explicit")
|
|
|
|
/// << FixItHint::CreateInsertion(Loc, "explicit ");
|
|
|
|
/// \endcode
|
|
|
|
class ClangTidyContext {
|
|
|
|
public:
|
2014-01-03 17:31:57 +08:00
|
|
|
ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors)
|
|
|
|
: Errors(Errors), DiagEngine(0) {}
|
2013-11-14 23:49:44 +08:00
|
|
|
|
|
|
|
/// \brief Report any errors detected using this method.
|
|
|
|
///
|
|
|
|
/// This is still under heavy development and will likely change towards using
|
|
|
|
/// tablegen'd diagnostic IDs.
|
|
|
|
/// FIXME: Figure out a way to manage ID spaces.
|
|
|
|
DiagnosticBuilder Diag(SourceLocation Loc, StringRef Message);
|
|
|
|
|
|
|
|
/// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
|
|
|
|
/// correctly.
|
|
|
|
///
|
|
|
|
/// This is called from the \c ClangTidyCheck base class.
|
|
|
|
void setDiagnosticsEngine(DiagnosticsEngine *Engine);
|
|
|
|
|
|
|
|
/// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
|
|
|
|
///
|
|
|
|
/// This is called from the \c ClangTidyCheck base class.
|
|
|
|
void setSourceManager(SourceManager *SourceMgr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class ClangTidyDiagnosticConsumer; // Calls storeError().
|
|
|
|
|
|
|
|
/// \brief Store a \c ClangTidyError.
|
|
|
|
void storeError(const ClangTidyError &Error);
|
|
|
|
|
|
|
|
SmallVectorImpl<ClangTidyError> *Errors;
|
|
|
|
DiagnosticsEngine *DiagEngine;
|
|
|
|
};
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
/// \brief A diagnostic consumer that turns each \c Diagnostic into a
|
|
|
|
/// \c SourceManager-independent \c ClangTidyError.
|
|
|
|
//
|
|
|
|
// FIXME: If we move away from unit-tests, this can be moved to a private
|
|
|
|
// implementation file.
|
|
|
|
class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
|
|
|
|
public:
|
2014-01-10 00:31:25 +08:00
|
|
|
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
// FIXME: The concept of converting between FixItHints and Replacements is
|
|
|
|
// more generic and should be pulled out into a more useful Diagnostics
|
|
|
|
// library.
|
|
|
|
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
2014-01-10 00:31:25 +08:00
|
|
|
const Diagnostic &Info) LLVM_OVERRIDE;
|
2013-11-14 23:49:44 +08:00
|
|
|
|
2014-01-03 23:34:40 +08:00
|
|
|
// Flushes the internal diagnostics buffer to the ClangTidyContext.
|
2014-01-10 00:31:25 +08:00
|
|
|
virtual void finish() LLVM_OVERRIDE;
|
2013-11-14 23:49:44 +08:00
|
|
|
|
|
|
|
private:
|
2014-01-10 00:31:25 +08:00
|
|
|
void addFixes(const Diagnostic &Info, ClangTidyError &Error);
|
|
|
|
ClangTidyMessage getMessage(const Diagnostic &Info) const;
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
ClangTidyContext &Context;
|
|
|
|
OwningPtr<DiagnosticsEngine> Diags;
|
2013-11-14 23:49:44 +08:00
|
|
|
SmallVector<ClangTidyError, 8> Errors;
|
2013-07-29 16:19:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace tidy
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
|