Skip to content

Commit bad598c

Browse files
author
Carlos Gálvez
committed
[clang-tidy] Fix broken HeaderFilterRegex when read from config file
PR llvm#91400 broke the usage of HeaderFilterRegex via config file, because it is now created at a different point in the execution and leads to a different value. The result of that is that using HeaderFilterRegex only in the config file does NOT work, in other words clang-tidy stops triggering warnings on header files, thereby losing a lot of coverage. This patch reverts the logic so that the header filter is created upon calling the getHeaderFilter() function. Additionally, this patch adds 2 unit tests to prevent regressions in the future: - One of them, "simple", tests the most basic use case with a single top-level .clang-tidy file. - The second one, "inheritance", demonstrates that the subfolder only gets warnings from headers within it, and not from parent headers. Fixes llvm#118009, llvm#121969, llvm#133453
1 parent 4e4cb43 commit bad598c

File tree

11 files changed

+41
-5
lines changed

11 files changed

+41
-5
lines changed

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,17 +571,30 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
571571
}
572572

573573
StringRef FileName(File->getName());
574-
LastErrorRelatesToUserCode =
575-
LastErrorRelatesToUserCode || Sources.isInMainFile(Location) ||
576-
(HeaderFilter &&
577-
(HeaderFilter->match(FileName) &&
578-
!(ExcludeHeaderFilter && ExcludeHeaderFilter->match(FileName))));
574+
LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
575+
Sources.isInMainFile(Location) ||
576+
(getHeaderFilter()->match(FileName) &&
577+
!getExcludeHeaderFilter()->match(FileName));
579578

580579
unsigned LineNumber = Sources.getExpansionLineNumber(Location);
581580
LastErrorPassesLineFilter =
582581
LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber);
583582
}
584583

584+
llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() {
585+
if (!HeaderFilter)
586+
HeaderFilter =
587+
std::make_unique<llvm::Regex>(*Context.getOptions().HeaderFilterRegex);
588+
return HeaderFilter.get();
589+
}
590+
591+
llvm::Regex *ClangTidyDiagnosticConsumer::getExcludeHeaderFilter() {
592+
if (!ExcludeHeaderFilter)
593+
ExcludeHeaderFilter = std::make_unique<llvm::Regex>(
594+
*Context.getOptions().ExcludeHeaderFilterRegex);
595+
return ExcludeHeaderFilter.get();
596+
}
597+
585598
void ClangTidyDiagnosticConsumer::removeIncompatibleErrors() {
586599
// Each error is modelled as the set of intervals in which it applies
587600
// replacements. To detect overlapping replacements, we use a sweep line

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
302302
/// context.
303303
llvm::Regex *getHeaderFilter();
304304

305+
/// Returns the \c ExcludeHeaderFilter constructed for the options set in the
306+
/// context.
307+
llvm::Regex *getExcludeHeaderFilter();
308+
305309
/// Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
306310
/// according to the diagnostic \p Location.
307311
void checkFilters(SourceLocation Location, const SourceManager &Sources);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HeaderFilterRegex: '.*'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: clang-tidy -checks=-*,google-explicit-constructor %s -- 2>&1 | FileCheck %s
2+
#include "foo.h"
3+
// CHECK: foo.h:1:12: warning: single-argument constructors must be marked explicit
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct X { X(int); };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
InheritParentConfig: true
2+
HeaderFilterRegex: 'subfolder/.*'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: clang-tidy -checks=-*,google-explicit-constructor %s -- -I $(dirname %S) 2>&1 | FileCheck %s
2+
#include "foo.h"
3+
// CHECK-NOT: foo.h:1:12: warning: single-argument constructors must be marked explicit
4+
5+
#include "bar.h"
6+
// CHECK: bar.h:1:13: warning: single-argument constructors must be marked explicit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct XX { XX(int); };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HeaderFilterRegex: '.*'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: clang-tidy -checks=-*,google-explicit-constructor %s -- 2>&1 | FileCheck %s
2+
#include "foo.h"
3+
// CHECK: foo.h:1:12: warning: single-argument constructors must be marked explicit
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct X { X(int); };

0 commit comments

Comments
 (0)