Skip to content

Commit

Permalink
Add ExtraArgs and ExtraArgsBefore options to enable clang warnings vi…
Browse files Browse the repository at this point in the history
…a 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

llvm-svn: 252485
  • Loading branch information
alexfh committed Nov 9, 2015
1 parent 7dee82e commit 64956b5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
14 changes: 14 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
Expand Up @@ -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));
}

Expand All @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
Expand Up @@ -149,22 +149,30 @@ class ClangTidyContext {
/// \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
/// diagnostic ID.
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; }
Expand Down
7 changes: 7 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.h
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions clang-tools-extra/test/clang-tidy/custom-diagnostics.cpp
@@ -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]
}

0 comments on commit 64956b5

Please sign in to comment.