[clang-tools-extra] r207970 - Add clang-tidy -header-filter option

Alexander Kornienko alexfh at google.com
Mon May 5 07:54:48 PDT 2014


Author: alexfh
Date: Mon May  5 09:54:47 2014
New Revision: 207970

URL: http://llvm.org/viewvc/llvm-project?rev=207970&view=rev
Log:
Add clang-tidy -header-filter option

Summary:
Add clang-tidy -header-filter option to specify from which headers we
want diagnostics to be printed. By default we don't print diagnostics from
headers. We always print diagnostics from the main file of each translation
unit.

Reviewers: djasper, klimek

Reviewed By: klimek

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D3590

Added:
    clang-tools-extra/trunk/test/clang-tidy/Inputs/
    clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/
    clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header1.h
    clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header2.h
    clang-tools-extra/trunk/test/clang-tidy/file-filter.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
    clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
    clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp

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=207970&r1=207969&r2=207970&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Mon May  5 09:54:47 2014
@@ -124,7 +124,7 @@ bool ChecksFilter::isCheckEnabled(String
 
 ClangTidyContext::ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
                                    const ClangTidyOptions &Options)
-    : Errors(Errors), DiagEngine(nullptr), Filter(Options) {}
+    : Errors(Errors), DiagEngine(nullptr), Options(Options), Filter(Options) {}
 
 DiagnosticBuilder ClangTidyContext::diag(
     StringRef CheckName, SourceLocation Loc, StringRef Description,
@@ -171,7 +171,8 @@ StringRef ClangTidyContext::getCheckName
 }
 
 ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx)
-    : Context(Ctx), LastErrorRelatesToUserCode(false) {
+    : Context(Ctx), HeaderFilter(Ctx.getOptions().HeaderFilterRegex),
+      LastErrorRelatesToUserCode(false) {
   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
   Diags.reset(new DiagnosticsEngine(
       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this,
@@ -217,12 +218,30 @@ void ClangTidyDiagnosticConsumer::Handle
       Sources);
 
   // Let argument parsing-related warnings through.
-  if (!Info.getLocation().isValid() ||
-      !Diags->getSourceManager().isInSystemHeader(Info.getLocation())) {
+  if (relatesToUserCode(Info.getLocation())) {
     LastErrorRelatesToUserCode = true;
   }
 }
 
+bool ClangTidyDiagnosticConsumer::relatesToUserCode(SourceLocation Location) {
+  // Invalid location may mean a diagnostic in a command line, don't skip these.
+  if (!Location.isValid())
+    return true;
+
+  const SourceManager &Sources = Diags->getSourceManager();
+  if (Sources.isInSystemHeader(Location))
+    return false;
+
+  // FIXME: We start with a conservative approach here, but the actual type of
+  // location needed depends on the check (in particular, where this check wants
+  // to apply fixes).
+  FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
+  if (FID == Sources.getMainFileID())
+    return true;
+
+  return HeaderFilter.match(Sources.getFileEntryForID(FID)->getName());
+}
+
 struct LessClangTidyError {
   bool operator()(const ClangTidyError *LHS, const ClangTidyError *RHS) const {
     const ClangTidyMessage &M1 = LHS->Message;

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=207970&r1=207969&r2=207970&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h Mon May  5 09:54:47 2014
@@ -10,6 +10,7 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
 
+#include "ClangTidyOptions.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Refactoring.h"
@@ -28,8 +29,6 @@ class CompilationDatabase;
 
 namespace tidy {
 
-struct ClangTidyOptions;
-
 /// \brief A message from a clang-tidy check.
 ///
 /// Note that this is independent of a \c SourceManager.
@@ -108,6 +107,7 @@ public:
   StringRef getCheckName(unsigned DiagnosticID) const;
 
   ChecksFilter &getChecksFilter() { return Filter; }
+  const ClangTidyOptions &getOptions() const { return Options; }
 
 private:
   friend class ClangTidyDiagnosticConsumer; // Calls storeError().
@@ -117,6 +117,7 @@ private:
 
   SmallVectorImpl<ClangTidyError> *Errors;
   DiagnosticsEngine *DiagEngine;
+  ClangTidyOptions Options;
   ChecksFilter Filter;
 
   llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
@@ -142,8 +143,10 @@ public:
 
 private:
   void finalizeLastError();
+  bool relatesToUserCode(SourceLocation Location);
 
   ClangTidyContext &Context;
+  llvm::Regex HeaderFilter;
   std::unique_ptr<DiagnosticsEngine> Diags;
   SmallVector<ClangTidyError, 8> Errors;
   bool LastErrorRelatesToUserCode;

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=207970&r1=207969&r2=207970&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h Mon May  5 09:54:47 2014
@@ -10,6 +10,8 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
 
+#include <string>
+
 namespace clang {
 namespace tidy {
 
@@ -18,6 +20,9 @@ struct ClangTidyOptions {
   ClangTidyOptions() : EnableChecksRegex(".*"), AnalyzeTemporaryDtors(false) {}
   std::string EnableChecksRegex;
   std::string DisableChecksRegex;
+  // Output warnings from headers matching this filter. Warnings from main files
+  // will always be displayed.
+  std::string HeaderFilterRegex;
   bool AnalyzeTemporaryDtors;
 };
 

Modified: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp?rev=207970&r1=207969&r2=207970&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Mon May  5 09:54:47 2014
@@ -39,6 +39,12 @@ static cl::opt<std::string> DisableCheck
              "|llvm-namespace-comment" // Not complete.
              "|google-.*)"),           // Doesn't apply to LLVM.
     cl::cat(ClangTidyCategory));
+static cl::opt<std::string> HeaderFilter(
+    "header-filter",
+    cl::desc("Regular expression matching the names of the headers to output\n"
+             "diagnostics from. Diagnostics from the main file of each\n"
+             "translation unit are always displayed."),
+    cl::init(""), cl::cat(ClangTidyCategory));
 static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
                          cl::init(false), cl::cat(ClangTidyCategory));
 
@@ -59,6 +65,7 @@ int main(int argc, const char **argv) {
   clang::tidy::ClangTidyOptions Options;
   Options.EnableChecksRegex = Checks;
   Options.DisableChecksRegex = DisableChecks;
+  Options.HeaderFilterRegex = HeaderFilter;
   Options.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
 
   // FIXME: Allow using --list-checks without positional arguments.

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header1.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header1.h?rev=207970&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header1.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header1.h Mon May  5 09:54:47 2014
@@ -0,0 +1 @@
+class A1 { A1(int); };

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header2.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header2.h?rev=207970&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header2.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/file-filter/header2.h Mon May  5 09:54:47 2014
@@ -0,0 +1 @@
+class A2 { A2(int); };

Added: clang-tools-extra/trunk/test/clang-tidy/file-filter.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/file-filter.cpp?rev=207970&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/file-filter.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/file-filter.cpp Mon May  5 09:54:47 2014
@@ -0,0 +1,22 @@
+// RUN: clang-tidy -checks=google-explicit-constructor -disable-checks='' -header-filter='' %s -- -I %S/Inputs/file-filter | FileCheck %s
+// RUN: clang-tidy -checks=google-explicit-constructor -disable-checks='' -header-filter='.*' %s -- -I %S/Inputs/file-filter | FileCheck --check-prefix=CHECK2 %s
+// RUN: clang-tidy -checks=google-explicit-constructor -disable-checks='' -header-filter='header2\.h' %s -- -I %S/Inputs/file-filter | FileCheck --check-prefix=CHECK3 %s
+
+#include "header1.h"
+// CHECK-NOT: warning:
+// CHECK2: header1.h:1:12: warning: Single-argument constructors must be explicit [google-explicit-constructor]
+// CHECK3-NOT: warning:
+
+#include "header2.h"
+// CHECK-NOT: warning:
+// CHECK2: header2.h:1:12: warning: Single-argument constructors {{.*}}
+// CHECK3: header2.h:1:12: warning: Single-argument constructors {{.*}}
+
+class A { A(int); };
+// CHECK: :[[@LINE-1]]:11: warning: Single-argument constructors {{.*}}
+// CHECK2: :[[@LINE-2]]:11: warning: Single-argument constructors {{.*}}
+// CHECK3: :[[@LINE-3]]:11: warning: Single-argument constructors {{.*}}
+
+// CHECK-NOT: warning:
+// CHECK2-NOT: warning:
+// CHECK3-NOT: warning:





More information about the cfe-commits mailing list