Teach clang-tidy how to upgrade warnings into errors.

Similar in format to the `-checks=` argument, this new `-warnings-as-errors=`
argument upgrades any warnings emitted by the former to errors.

http://reviews.llvm.org/D15528


git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@257642 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jonathan Roelofs 2016-01-13 17:36:41 +00:00
Родитель e2ddab9dc5
Коммит f502328e6c
10 изменённых файлов: 106 добавлений и 19 удалений

Просмотреть файл

@ -101,7 +101,8 @@ public:
Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
DiagPrinter),
SourceMgr(Diags, Files), Rewrite(SourceMgr, LangOpts),
ApplyFixes(ApplyFixes), TotalFixes(0), AppliedFixes(0) {
ApplyFixes(ApplyFixes), TotalFixes(0), AppliedFixes(0),
WarningsAsErrors(0) {
DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
DiagPrinter->BeginSourceFile(LangOpts);
}
@ -114,8 +115,14 @@ public:
SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
{
auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
std::string Name = Error.CheckName;
if (Error.IsWarningAsError) {
Name += ",-warnings-as-errors";
Level = DiagnosticsEngine::Error;
WarningsAsErrors++;
}
auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
<< Message.Message << Error.CheckName;
<< Message.Message << Name;
for (const tooling::Replacement &Fix : Error.Fix) {
SourceLocation FixLoc = getLocation(Fix.getFilePath(), Fix.getOffset());
SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Fix.getLength());
@ -147,6 +154,8 @@ public:
}
}
unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
private:
SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
if (FilePath.empty())
@ -174,6 +183,7 @@ private:
bool ApplyFixes;
unsigned TotalFixes;
unsigned AppliedFixes;
unsigned WarningsAsErrors;
};
class ClangTidyASTConsumer : public MultiplexConsumer {
@ -421,11 +431,13 @@ runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
return Context.getStats();
}
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix) {
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
unsigned &WarningsAsErrorsCount) {
ErrorReporter Reporter(Fix);
for (const ClangTidyError &Error : Errors)
Reporter.reportDiagnostic(Error);
Reporter.Finish();
WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
}
void exportReplacements(const std::vector<ClangTidyError> &Errors,

Просмотреть файл

@ -214,7 +214,8 @@ runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
//
/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
/// Errors containing fixes are automatically applied.
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix);
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
unsigned &WarningsAsErrorsCount);
/// \brief Serializes replacements into YAML and writes them to the specified
/// output stream.

Просмотреть файл

@ -115,8 +115,10 @@ ClangTidyMessage::ClangTidyMessage(StringRef Message,
}
ClangTidyError::ClangTidyError(StringRef CheckName,
ClangTidyError::Level DiagLevel)
: CheckName(CheckName), DiagLevel(DiagLevel) {}
ClangTidyError::Level DiagLevel,
bool IsWarningAsError)
: CheckName(CheckName), DiagLevel(DiagLevel),
IsWarningAsError(IsWarningAsError) {}
// Returns true if GlobList starts with the negative indicator ('-'), removes it
// from the GlobList.
@ -204,6 +206,7 @@ void ClangTidyContext::setCurrentFile(StringRef File) {
CurrentFile = File;
CurrentOptions = getOptionsForFile(CurrentFile);
CheckFilter.reset(new GlobList(*getOptions().Checks));
WarningAsErrorFilter.reset(new GlobList(*getOptions().WarningsAsErrors));
}
void ClangTidyContext::setASTContext(ASTContext *Context) {
@ -237,6 +240,11 @@ bool ClangTidyContext::isCheckEnabled(StringRef CheckName) const {
return CheckFilter->contains(CheckName);
}
GlobList &ClangTidyContext::getWarningAsErrorFilter() {
assert(WarningAsErrorFilter != nullptr);
return *WarningAsErrorFilter;
}
/// \brief Store a \c ClangTidyError.
void ClangTidyContext::storeError(const ClangTidyError &Error) {
Errors.push_back(Error);
@ -324,7 +332,10 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
LastErrorRelatesToUserCode = true;
LastErrorPassesLineFilter = true;
}
Errors.push_back(ClangTidyError(CheckName, Level));
bool IsWarningAsError =
DiagLevel == DiagnosticsEngine::Warning &&
Context.getWarningAsErrorFilter().contains(CheckName);
Errors.push_back(ClangTidyError(CheckName, Level, IsWarningAsError));
}
// FIXME: Provide correct LangOptions for each file.

Просмотреть файл

@ -57,7 +57,7 @@ struct ClangTidyError {
Error = DiagnosticsEngine::Error
};
ClangTidyError(StringRef CheckName, Level DiagLevel);
ClangTidyError(StringRef CheckName, Level DiagLevel, bool IsWarningAsError);
std::string CheckName;
ClangTidyMessage Message;
@ -65,6 +65,7 @@ struct ClangTidyError {
SmallVector<ClangTidyMessage, 1> Notes;
Level DiagLevel;
bool IsWarningAsError;
};
/// \brief Read-only set of strings represented as a list of positive and
@ -164,6 +165,10 @@ public:
/// \brief Returns true if the check name is enabled for the \c CurrentFile.
bool isCheckEnabled(StringRef CheckName) const;
/// \brief Returns check filter for the \c CurrentFile which
/// selects checks for upgrade to error.
GlobList &getWarningAsErrorFilter();
/// \brief Returns global options.
const ClangTidyGlobalOptions &getGlobalOptions() const;
@ -211,6 +216,7 @@ private:
std::string CurrentFile;
ClangTidyOptions CurrentOptions;
std::unique_ptr<GlobList> CheckFilter;
std::unique_ptr<GlobList> WarningAsErrorFilter;
LangOptions LangOpts;

Просмотреть файл

@ -85,6 +85,7 @@ template <> struct MappingTraits<ClangTidyOptions> {
MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(
IO, Options.CheckOptions);
IO.mapOptional("Checks", Options.Checks);
IO.mapOptional("WarningsAsErrors", Options.WarningsAsErrors);
IO.mapOptional("HeaderFilterRegex", Options.HeaderFilterRegex);
IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors);
IO.mapOptional("User", Options.User);
@ -103,6 +104,7 @@ namespace tidy {
ClangTidyOptions ClangTidyOptions::getDefaults() {
ClangTidyOptions Options;
Options.Checks = "";
Options.WarningsAsErrors = "";
Options.HeaderFilterRegex = "";
Options.SystemHeaders = false;
Options.AnalyzeTemporaryDtors = false;
@ -123,6 +125,12 @@ ClangTidyOptions::mergeWith(const ClangTidyOptions &Other) const {
Result.Checks =
(Result.Checks && !Result.Checks->empty() ? *Result.Checks + "," : "") +
*Other.Checks;
if (Other.WarningsAsErrors)
Result.WarningsAsErrors =
(Result.WarningsAsErrors && !Result.WarningsAsErrors->empty()
? *Result.WarningsAsErrors + ","
: "") +
*Other.WarningsAsErrors;
if (Other.HeaderFilterRegex)
Result.HeaderFilterRegex = Other.HeaderFilterRegex;

Просмотреть файл

@ -62,6 +62,9 @@ struct ClangTidyOptions {
/// \brief Checks filter.
llvm::Optional<std::string> Checks;
/// \brief WarningsAsErrors filter.
llvm::Optional<std::string> WarningsAsErrors;
/// \brief Output warnings from headers matching this filter. Warnings from
/// main files will always be displayed.
llvm::Optional<std::string> HeaderFilterRegex;

Просмотреть файл

@ -64,15 +64,21 @@ Checks("checks", cl::desc("Comma-separated list of globs with optional '-'\n"
cl::init(""), cl::cat(ClangTidyCategory));
static cl::opt<std::string>
HeaderFilter("header-filter",
cl::desc("Regular expression matching the names of the\n"
"headers to output diagnostics from. Diagnostics\n"
"from the main file of each translation unit are\n"
"always displayed.\n"
"Can be used together with -line-filter.\n"
"This option overrides the value read from a\n"
".clang-tidy file."),
cl::init(""), cl::cat(ClangTidyCategory));
WarningsAsErrors("warnings-as-errors",
cl::desc("Upgrades warnings to errors. "
"Same format as '-checks'."),
cl::init(""), cl::cat(ClangTidyCategory));
static cl::opt<std::string>
HeaderFilter("header-filter",
cl::desc("Regular expression matching the names of the\n"
"headers to output diagnostics from. Diagnostics\n"
"from the main file of each translation unit are\n"
"always displayed.\n"
"Can be used together with -line-filter.\n"
"This option overrides the value read from a\n"
".clang-tidy file."),
cl::init(""), cl::cat(ClangTidyCategory));
static cl::opt<bool>
SystemHeaders("system-headers",
@ -227,6 +233,7 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
ClangTidyOptions DefaultOptions;
DefaultOptions.Checks = DefaultChecks;
DefaultOptions.WarningsAsErrors = "";
DefaultOptions.HeaderFilterRegex = HeaderFilter;
DefaultOptions.SystemHeaders = SystemHeaders;
DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
@ -238,6 +245,8 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
ClangTidyOptions OverrideOptions;
if (Checks.getNumOccurrences() > 0)
OverrideOptions.Checks = Checks;
if (WarningsAsErrors.getNumOccurrences() > 0)
OverrideOptions.WarningsAsErrors = WarningsAsErrors;
if (HeaderFilter.getNumOccurrences() > 0)
OverrideOptions.HeaderFilterRegex = HeaderFilter;
if (SystemHeaders.getNumOccurrences() > 0)
@ -322,8 +331,10 @@ static int clangTidyMain(int argc, const char **argv) {
const bool DisableFixes = Fix && FoundErrors && !FixErrors;
unsigned WErrorCount = 0;
// -fix-errors implies -fix.
handleErrors(Errors, (FixErrors || Fix) && !DisableFixes);
handleErrors(Errors, (FixErrors || Fix) && !DisableFixes, WErrorCount);
if (!ExportFixes.empty() && !Errors.empty()) {
std::error_code EC;
@ -344,6 +355,13 @@ static int clangTidyMain(int argc, const char **argv) {
if (EnableCheckProfile)
printProfileData(Profile, llvm::errs());
if (WErrorCount) {
StringRef Plural = WErrorCount == 1 ? "" : "s";
llvm::errs() << WErrorCount << " warning" << Plural << " treated as error"
<< Plural << "\n";
return WErrorCount;
}
return 0;
}

Просмотреть файл

@ -68,7 +68,9 @@ Clang diagnostics are treated in a similar way as check diagnostics. Clang
diagnostics are displayed by clang-tidy and can be filtered out using
``-checks=`` option. However, the ``-checks=`` option does not affect
compilation arguments, so it can not turn on Clang warnings which are not
already turned on in build configuration.
already turned on in build configuration. The ``-warnings-as-errors=`` option
upgrades any warnings emitted under the ``-checks=`` flag to errors (but it
does not enable any checks itself).
Clang diagnostics have check names starting with ``clang-diagnostic-``.
Diagnostics which have a corresponding warning option, are named
@ -150,6 +152,7 @@ An overview of all the command-line options:
-checks=* to list all available checks.
-p=<string> - Build path
-system-headers - Display the errors from system headers.
-warnings-as-errors=<string> - Upgrades warnings to errors. Same format as '-checks'.
-p <build-path> is used to read a compile command database.

Просмотреть файл

@ -0,0 +1,15 @@
// RUN: clang-tidy %s -checks='-*,llvm-namespace-comment' -- 2>&1 | FileCheck %s --check-prefix=CHECK-WARN
// RUN: not clang-tidy %s -checks='-*,llvm-namespace-comment' -warnings-as-errors='llvm-namespace-comment' -- 2>&1 | FileCheck %s --check-prefix=CHECK-WERR
namespace j {
}
// CHECK-WARN: warning: namespace 'j' not terminated with a closing comment [llvm-namespace-comment]
// CHECK-WERR: error: namespace 'j' not terminated with a closing comment [llvm-namespace-comment,-warnings-as-errors]
namespace k {
}
// CHECK-WARN: warning: namespace 'k' not terminated with a closing comment [llvm-namespace-comment]
// CHECK-WERR: error: namespace 'k' not terminated with a closing comment [llvm-namespace-comment,-warnings-as-errors]
// CHECK-WARN-NOT: treated as
// CHECK-WERR: 2 warnings treated as errors

Просмотреть файл

@ -0,0 +1,10 @@
// RUN: clang-tidy %s -checks='-*,llvm-namespace-comment' -- 2>&1 | FileCheck %s --check-prefix=CHECK-WARN
// RUN: not clang-tidy %s -checks='-*,llvm-namespace-comment' -warnings-as-errors='llvm-namespace-comment' -- 2>&1 | FileCheck %s --check-prefix=CHECK-WERR
namespace i {
}
// CHECK-WARN: warning: namespace 'i' not terminated with a closing comment [llvm-namespace-comment]
// CHECK-WERR: error: namespace 'i' not terminated with a closing comment [llvm-namespace-comment,-warnings-as-errors]
// CHECK-WARN-NOT: treated as
// CHECK-WERR: 1 warning treated as error