[clang-tools-extra] r252485 - Add ExtraArgs and ExtraArgsBefore options to enable clang warnings via configuration files.

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 08:28:13 PST 2015


Author: alexfh
Date: Mon Nov  9 10:28:11 2015
New Revision: 252485

URL: http://llvm.org/viewvc/llvm-project?rev=252485&view=rev
Log:
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

Added:
    clang-tools-extra/trunk/test/clang-tidy/custom-diagnostics.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
    clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=252485&r1=252484&r2=252485&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Mon Nov  9 10:28:11 2015
@@ -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<ClangTidyOp
              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);
 

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp?rev=252485&r1=252484&r2=252485&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Mon Nov  9 10:28:11 2015
@@ -202,9 +202,7 @@ void ClangTidyContext::setSourceManager(
 
 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
   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() {

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h?rev=252485&r1=252484&r2=252485&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h Mon Nov  9 10:28:11 2015
@@ -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; }

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp?rev=252485&r1=252484&r2=252485&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp Mon Nov  9 10:28:11 2015
@@ -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<ClangTi
     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 ClangT
     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;

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h?rev=252485&r1=252484&r2=252485&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h Mon Nov  9 10:28:11 2015
@@ -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.

Added: clang-tools-extra/trunk/test/clang-tidy/custom-diagnostics.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/custom-diagnostics.cpp?rev=252485&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/custom-diagnostics.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/custom-diagnostics.cpp Mon Nov  9 10:28:11 2015
@@ -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]
+}




More information about the cfe-commits mailing list