Add ExtraArgs and ExtraArgsBefore options to enable clang warnings via configuration files.
Summary: This patch depends on http://reviews.llvm.org/D14191 Reviewers: djasper, klimek Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D14192 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@252485 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
1dfea28c51
Коммит
2e1c6fad8d
|
@ -36,6 +36,7 @@
|
|||
#include "clang/Tooling/Refactoring.h"
|
||||
#include "clang/Tooling/ReplacementsYaml.h"
|
||||
#include "clang/Tooling/Tooling.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/Path.h"
|
||||
#include "llvm/Support/Process.h"
|
||||
#include "llvm/Support/Signals.h"
|
||||
|
@ -376,6 +377,19 @@ runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
|
|||
std::vector<ClangTidyError> *Errors, ProfileData *Profile) {
|
||||
ClangTool Tool(Compilations, InputFiles);
|
||||
clang::tidy::ClangTidyContext Context(std::move(OptionsProvider));
|
||||
ArgumentsAdjuster PerFileExtraArgumentsInserter = [&Context](
|
||||
const CommandLineArguments &Args, StringRef Filename) {
|
||||
ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
|
||||
CommandLineArguments AdjustedArgs;
|
||||
if (Opts.ExtraArgsBefore)
|
||||
AdjustedArgs = *Opts.ExtraArgsBefore;
|
||||
AdjustedArgs.insert(AdjustedArgs.begin(), Args.begin(), Args.end());
|
||||
if (Opts.ExtraArgs)
|
||||
AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
|
||||
Opts.ExtraArgs->end());
|
||||
return AdjustedArgs;
|
||||
};
|
||||
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
|
||||
if (Profile)
|
||||
Context.setCheckProfileData(Profile);
|
||||
|
||||
|
|
|
@ -202,9 +202,7 @@ void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) {
|
|||
|
||||
void ClangTidyContext::setCurrentFile(StringRef File) {
|
||||
CurrentFile = File;
|
||||
// Safeguard against options with unset values.
|
||||
CurrentOptions = ClangTidyOptions::getDefaults().mergeWith(
|
||||
OptionsProvider->getOptions(CurrentFile));
|
||||
CurrentOptions = getOptionsForFile(CurrentFile);
|
||||
CheckFilter.reset(new GlobList(*getOptions().Checks));
|
||||
}
|
||||
|
||||
|
@ -221,6 +219,13 @@ const ClangTidyOptions &ClangTidyContext::getOptions() const {
|
|||
return CurrentOptions;
|
||||
}
|
||||
|
||||
ClangTidyOptions ClangTidyContext::getOptionsForFile(StringRef File) const {
|
||||
// Merge options on top of getDefaults() as a safeguard against options with
|
||||
// unset values.
|
||||
return ClangTidyOptions::getDefaults().mergeWith(
|
||||
OptionsProvider->getOptions(CurrentFile));
|
||||
}
|
||||
|
||||
void ClangTidyContext::setCheckProfileData(ProfileData *P) { Profile = P; }
|
||||
|
||||
GlobList &ClangTidyContext::getChecksFilter() {
|
||||
|
|
|
@ -149,7 +149,7 @@ public:
|
|||
/// \brief Sets ASTContext for the current translation unit.
|
||||
void setASTContext(ASTContext *Context);
|
||||
|
||||
/// \brief Gets the language options from the AST context
|
||||
/// \brief Gets the language options from the AST context.
|
||||
LangOptions getLangOpts() const { return LangOpts; }
|
||||
|
||||
/// \brief Returns the name of the clang-tidy check which produced this
|
||||
|
@ -157,14 +157,22 @@ public:
|
|||
StringRef getCheckName(unsigned DiagnosticID) const;
|
||||
|
||||
/// \brief Returns check filter for the \c CurrentFile.
|
||||
///
|
||||
/// The \c CurrentFile can be changed using \c setCurrentFile.
|
||||
GlobList &getChecksFilter();
|
||||
|
||||
/// \brief Returns global options.
|
||||
const ClangTidyGlobalOptions &getGlobalOptions() const;
|
||||
|
||||
/// \brief Returns options for \c CurrentFile.
|
||||
///
|
||||
/// The \c CurrentFile can be changed using \c setCurrentFile.
|
||||
const ClangTidyOptions &getOptions() const;
|
||||
|
||||
/// \brief Returns options for \c File. Does not change or depend on
|
||||
/// \c CurrentFile.
|
||||
ClangTidyOptions getOptionsForFile(StringRef File) const;
|
||||
|
||||
/// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic
|
||||
/// counters.
|
||||
const ClangTidyStats &getStats() const { return Stats; }
|
||||
|
|
|
@ -27,6 +27,7 @@ using clang::tidy::FileFilter;
|
|||
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter)
|
||||
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter::LineRange)
|
||||
LLVM_YAML_IS_SEQUENCE_VECTOR(ClangTidyOptions::StringPair)
|
||||
LLVM_YAML_IS_SEQUENCE_VECTOR(std::string)
|
||||
|
||||
namespace llvm {
|
||||
namespace yaml {
|
||||
|
@ -88,6 +89,8 @@ template <> struct MappingTraits<ClangTidyOptions> {
|
|||
IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors);
|
||||
IO.mapOptional("User", Options.User);
|
||||
IO.mapOptional("CheckOptions", NOpts->Options);
|
||||
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
|
||||
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -129,6 +132,10 @@ ClangTidyOptions::mergeWith(const ClangTidyOptions &Other) const {
|
|||
Result.AnalyzeTemporaryDtors = Other.AnalyzeTemporaryDtors;
|
||||
if (Other.User)
|
||||
Result.User = Other.User;
|
||||
if (Other.ExtraArgs)
|
||||
Result.ExtraArgs = Other.ExtraArgs;
|
||||
if (Other.ExtraArgsBefore)
|
||||
Result.ExtraArgsBefore = Other.ExtraArgsBefore;
|
||||
|
||||
for (const auto &KeyValue : Other.CheckOptions)
|
||||
Result.CheckOptions[KeyValue.first] = KeyValue.second;
|
||||
|
|
|
@ -83,6 +83,14 @@ struct ClangTidyOptions {
|
|||
|
||||
/// \brief Key-value mapping used to store check-specific options.
|
||||
OptionMap CheckOptions;
|
||||
|
||||
typedef std::vector<std::string> ArgList;
|
||||
|
||||
/// \brief Add extra compilation arguments to the end of the list.
|
||||
llvm::Optional<ArgList> ExtraArgs;
|
||||
|
||||
/// \brief Add extra compilation arguments to the start of the list.
|
||||
llvm::Optional<ArgList> ExtraArgsBefore;
|
||||
};
|
||||
|
||||
/// \brief Abstract interface for retrieving various ClangTidy options.
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
// RUN: clang-tidy -checks='-*,modernize-use-override,clang-diagnostic-shadow,clang-diagnostic-float-conversion' %s -- | count 0
|
||||
// RUN: clang-tidy -checks='-*,modernize-use-override,clang-diagnostic-shadow,clang-diagnostic-float-conversion' \
|
||||
// RUN: -config='{ExtraArgs: ["-Wshadow","-Wno-unused-variable"], ExtraArgsBefore: ["-Wno-shadow","-Wfloat-conversion","-Wunused-variable"]}' %s -- \
|
||||
// RUN: | FileCheck -implicit-check-not='{{warning:|error:}}' %s
|
||||
|
||||
void f(float x) {
|
||||
int a;
|
||||
{ int a; }
|
||||
// CHECK: :[[@LINE-1]]:9: warning: declaration shadows a local variable [clang-diagnostic-shadow]
|
||||
int b = x;
|
||||
// CHECK: :[[@LINE-1]]:11: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [clang-diagnostic-float-conversion]
|
||||
}
|
Загрузка…
Ссылка в новой задаче