forked from OSchip/llvm-project
Store Errors inside ClangTidyContext instead of just pointer to an external
array. This simplifies usage of ClangTidyContext a bit and seems to be more consistent. Reviewers: klimek Reviewed By: klimek Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D3685 llvm-svn: 208407
This commit is contained in:
parent
f50871f460
commit
826b5adb6c
|
@ -40,7 +40,6 @@
|
|||
#include "llvm/Support/Signals.h"
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
using namespace clang::driver;
|
||||
|
@ -292,8 +291,7 @@ void ClangTidyCheck::setName(StringRef Name) {
|
|||
}
|
||||
|
||||
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) {
|
||||
SmallVector<ClangTidyError, 8> Errors;
|
||||
clang::tidy::ClangTidyContext Context(&Errors, Options);
|
||||
clang::tidy::ClangTidyContext Context(Options);
|
||||
ClangTidyASTConsumerFactory Factory(Context, Options);
|
||||
return Factory.getCheckNames();
|
||||
}
|
||||
|
@ -301,11 +299,11 @@ std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) {
|
|||
ClangTidyStats runClangTidy(const ClangTidyOptions &Options,
|
||||
const tooling::CompilationDatabase &Compilations,
|
||||
ArrayRef<std::string> Ranges,
|
||||
SmallVectorImpl<ClangTidyError> *Errors) {
|
||||
std::vector<ClangTidyError> *Errors) {
|
||||
// FIXME: Ranges are currently full files. Support selecting specific
|
||||
// (line-)ranges.
|
||||
ClangTool Tool(Compilations, Ranges);
|
||||
clang::tidy::ClangTidyContext Context(Errors, Options);
|
||||
clang::tidy::ClangTidyContext Context(Options);
|
||||
ClangTidyDiagnosticConsumer DiagConsumer(Context);
|
||||
|
||||
Tool.setDiagnosticConsumer(&DiagConsumer);
|
||||
|
@ -333,10 +331,11 @@ ClangTidyStats runClangTidy(const ClangTidyOptions &Options,
|
|||
};
|
||||
|
||||
Tool.run(new ActionFactory(new ClangTidyASTConsumerFactory(Context, Options)));
|
||||
*Errors = Context.getErrors();
|
||||
return Context.getStats();
|
||||
}
|
||||
|
||||
void handleErrors(SmallVectorImpl<ClangTidyError> &Errors, bool Fix) {
|
||||
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix) {
|
||||
ErrorReporter Reporter(Fix);
|
||||
for (const ClangTidyError &Error : Errors) {
|
||||
Reporter.reportDiagnostic(Error.Message, DiagnosticsEngine::Warning,
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include "clang/Basic/Diagnostic.h"
|
||||
#include "clang/Basic/SourceManager.h"
|
||||
#include "clang/Tooling/Refactoring.h"
|
||||
#include <vector>
|
||||
|
||||
namespace clang {
|
||||
|
||||
|
@ -124,14 +125,14 @@ std::vector<std::string> getCheckNames(const ClangTidyOptions &Options);
|
|||
ClangTidyStats runClangTidy(const ClangTidyOptions &Options,
|
||||
const tooling::CompilationDatabase &Compilations,
|
||||
ArrayRef<std::string> Ranges,
|
||||
SmallVectorImpl<ClangTidyError> *Errors);
|
||||
std::vector<ClangTidyError> *Errors);
|
||||
|
||||
// FIXME: This interface will need to be significantly extended to be useful.
|
||||
// FIXME: Implement confidence levels for displaying/fixing errors.
|
||||
//
|
||||
/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
|
||||
/// Errors containing fixes are automatically applied.
|
||||
void handleErrors(SmallVectorImpl<ClangTidyError> &Errors, bool Fix);
|
||||
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix);
|
||||
|
||||
} // end namespace tidy
|
||||
} // end namespace clang
|
||||
|
|
|
@ -119,9 +119,8 @@ bool ChecksFilter::isCheckEnabled(StringRef Name) {
|
|||
return EnableChecks.match(Name) && !DisableChecks.match(Name);
|
||||
}
|
||||
|
||||
ClangTidyContext::ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
|
||||
const ClangTidyOptions &Options)
|
||||
: Errors(Errors), DiagEngine(nullptr), Options(Options), Filter(Options) {}
|
||||
ClangTidyContext::ClangTidyContext(const ClangTidyOptions &Options)
|
||||
: DiagEngine(nullptr), Options(Options), Filter(Options) {}
|
||||
|
||||
DiagnosticBuilder ClangTidyContext::diag(
|
||||
StringRef CheckName, SourceLocation Loc, StringRef Description,
|
||||
|
@ -158,7 +157,7 @@ void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) {
|
|||
|
||||
/// \brief Store a \c ClangTidyError.
|
||||
void ClangTidyContext::storeError(const ClangTidyError &Error) {
|
||||
Errors->push_back(Error);
|
||||
Errors.push_back(Error);
|
||||
}
|
||||
|
||||
StringRef ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
|
||||
|
@ -201,6 +200,7 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
|
|||
assert(!Errors.empty() &&
|
||||
"A diagnostic note can only be appended to a message.");
|
||||
} else {
|
||||
// FIXME: Pass all errors here regardless of filters and non-user code.
|
||||
finalizeLastError();
|
||||
StringRef WarningOption =
|
||||
Context.DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(
|
||||
|
|
|
@ -90,8 +90,7 @@ struct ClangTidyStats {
|
|||
/// \endcode
|
||||
class ClangTidyContext {
|
||||
public:
|
||||
ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
|
||||
const ClangTidyOptions &Options);
|
||||
ClangTidyContext(const ClangTidyOptions &Options);
|
||||
|
||||
/// \brief Report any errors detected using this method.
|
||||
///
|
||||
|
@ -120,6 +119,7 @@ public:
|
|||
ChecksFilter &getChecksFilter() { return Filter; }
|
||||
const ClangTidyOptions &getOptions() const { return Options; }
|
||||
const ClangTidyStats &getStats() const { return Stats; }
|
||||
const std::vector<ClangTidyError> &getErrors() const { return Errors; }
|
||||
|
||||
private:
|
||||
friend class ClangTidyDiagnosticConsumer; // Calls storeError().
|
||||
|
@ -127,7 +127,7 @@ private:
|
|||
/// \brief Store a \c ClangTidyError.
|
||||
void storeError(const ClangTidyError &Error);
|
||||
|
||||
SmallVectorImpl<ClangTidyError> *Errors;
|
||||
std::vector<ClangTidyError> Errors;
|
||||
DiagnosticsEngine *DiagEngine;
|
||||
ClangTidyOptions Options;
|
||||
ChecksFilter Filter;
|
||||
|
|
|
@ -102,7 +102,7 @@ int main(int argc, const char **argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
|
||||
std::vector<clang::tidy::ClangTidyError> Errors;
|
||||
clang::tidy::ClangTidyStats Stats =
|
||||
clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(),
|
||||
OptionsParser.getSourcePathList(), &Errors);
|
||||
|
|
|
@ -20,8 +20,8 @@ public:
|
|||
};
|
||||
|
||||
TEST(ClangTidyDiagnosticConsumer, SortsErrors) {
|
||||
SmallVector<ClangTidyError, 8> Errors;
|
||||
runCheckOnCode<TestCheck>("int a;", Errors);
|
||||
std::vector<ClangTidyError> Errors;
|
||||
runCheckOnCode<TestCheck>("int a;", &Errors);
|
||||
EXPECT_EQ(2ul, Errors.size());
|
||||
// FIXME: Remove " []" once the check name is removed from the message text.
|
||||
EXPECT_EQ("type specifier []", Errors[0].Message.Message);
|
||||
|
|
|
@ -41,9 +41,10 @@ private:
|
|||
|
||||
template <typename T>
|
||||
std::string runCheckOnCode(StringRef Code,
|
||||
SmallVectorImpl<ClangTidyError> &Errors) {
|
||||
std::vector<ClangTidyError> *Errors = nullptr) {
|
||||
T Check;
|
||||
ClangTidyContext Context(&Errors, ClangTidyOptions());
|
||||
ClangTidyOptions Options;
|
||||
ClangTidyContext Context(Options);
|
||||
ClangTidyDiagnosticConsumer DiagConsumer(Context);
|
||||
Check.setContext(&Context);
|
||||
std::vector<std::string> ArgCXX11(1, "-std=c++11");
|
||||
|
@ -59,16 +60,13 @@ std::string runCheckOnCode(StringRef Code,
|
|||
return "";
|
||||
DiagConsumer.finish();
|
||||
tooling::Replacements Fixes;
|
||||
for (const ClangTidyError &Error : Errors)
|
||||
for (const ClangTidyError &Error : Context.getErrors())
|
||||
Fixes.insert(Error.Fix.begin(), Error.Fix.end());
|
||||
if (Errors)
|
||||
*Errors = Context.getErrors();
|
||||
return tooling::applyAllReplacements(Code, Fixes);
|
||||
}
|
||||
|
||||
template <typename T> std::string runCheckOnCode(StringRef Code) {
|
||||
SmallVector<ClangTidyError, 16> Errors;
|
||||
return runCheckOnCode<T>(Code, Errors);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
|
|
Loading…
Reference in New Issue