Skip to content

Commit 814910e

Browse files
committed
Fix relative path in header-filter of clang-tidy.
Clang-tidy supports output diagnostics from header files if user specifies --header-filter. But it can't handle relative path well. For example, the folder structure of a project is: // a.h is in /src/a/a.h #include "../b/b.h" // b.h is in /src/b/b.h ... // c.cpp is in /src/c.cpp #include "a/a.h" Now, we set --header-filter as --header-filter=/a/. That means we only want to check header files under /src/a/ path, and ignore header files uder /src/b/ path, but in current implementation, clang-tidy will check /src/b/b.h also, because the name of b.h used in clang-tidy is /src/a/../b/b.h. This change tries to fix this issue.
1 parent cec0ba4 commit 814910e

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
551551
return;
552552
}
553553

554-
StringRef FileName(File->getName());
554+
StringRef FileName = File->tryGetRealPathName();
555+
if (FileName.empty()) {
556+
FileName = File->getName();
557+
}
555558
LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
556559
Sources.isInMainFile(Location) ||
557560
getHeaderFilter()->match(FileName);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "../subfolder_b/header.h"
2+
3+
class SA { SA(int); };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class SB { SB(int); };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class SC { SC(int); };

clang-tools-extra/test/clang-tidy/file-filter.cpp

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
// file-filter\header*.h due to code order between '/' and '\\'.
1010
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='.*' -system-headers %s -- -I %S/Inputs/file-filter/system/.. -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK4 %s
1111
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='.*' -system-headers -quiet %s -- -I %S/Inputs/file-filter/system/.. -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK4-QUIET %s
12+
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='subfolder_a' %s -- -I %S/Inputs/file-filter -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK5 %s
13+
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='subfolder_a' -quiet %s -- -I %S/Inputs/file-filter -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK5-QUIET %s
14+
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='subfolder_b' %s -- -I %S/Inputs/file-filter -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK6 %s
15+
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='subfolder_b' -quiet %s -- -I %S/Inputs/file-filter -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK6-QUIET %s
16+
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='subfolder_c' %s -- -I %S/Inputs/file-filter -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK7 %s
17+
// RUN: clang-tidy -checks='-*,google-explicit-constructor' -header-filter='subfolder_c' -quiet %s -- -I %S/Inputs/file-filter -isystem %S/Inputs/file-filter/system 2>&1 | FileCheck --check-prefix=CHECK7-QUIET %s
1218

1319
#include "header1.h"
1420
// CHECK-NOT: warning:
@@ -19,6 +25,12 @@
1925
// CHECK3-QUIET-NOT: warning:
2026
// CHECK4: header1.h:1:12: warning: single-argument constructors
2127
// CHECK4-QUIET: header1.h:1:12: warning: single-argument constructors
28+
// CHECK5-NOT: warning:
29+
// CHECK5-QUIET-NOT: warning:
30+
// CHECK6-NOT: warning:
31+
// CHECK6-QUIET-NOT: warning:
32+
// CHECK7-NOT: warning:
33+
// CHECK7-QUIET-NOT: warning:
2234

2335
#include "header2.h"
2436
// CHECK-NOT: warning:
@@ -29,6 +41,44 @@
2941
// CHECK3-QUIET: header2.h:1:12: warning: single-argument constructors
3042
// CHECK4: header2.h:1:12: warning: single-argument constructors
3143
// CHECK4-QUIET: header2.h:1:12: warning: single-argument constructors
44+
// CHECK5-NOT: warning:
45+
// CHECK5-QUIET-NOT: warning:
46+
// CHECK6-NOT: warning:
47+
// CHECK6-QUIET-NOT: warning:
48+
// CHECK7-NOT: warning:
49+
// CHECK7-QUIET-NOT: warning:
50+
51+
#include "subfolder_a/header.h"
52+
// CHECK-NOT: warning:
53+
// CHECK-QUIET-NOT: warning:
54+
// CHECK2: header.h:1:12: warning: single-argument constructors must be marked explicit
55+
// CHECK2-QUIET: header.h:1:12: warning: single-argument constructors must be marked explicit
56+
// CHECK3-NOT: warning:
57+
// CHECK3-QUIET-NOT: warning:
58+
// CHECK4: header.h:1:12: warning: single-argument constructors must be marked explicit
59+
// CHECK4-QUIET: header.h:1:12: warning: single-argument constructors must be marked explicit
60+
// CHECK5: header.h:3:12: warning: single-argument constructors must be marked explicit
61+
// CHECK5-QUIET: header.h:3:12: warning: single-argument constructors must be marked explicit
62+
// CHECK6: header.h:1:12: warning: single-argument constructors must be marked explicit
63+
// CHECK6-QUIET: header.h:1:12: warning: single-argument constructors must be marked explicit
64+
// CHECK7-NOT: warning:
65+
// CHECK7-QUIET-NOT: warning:
66+
67+
#include "subfolder_c/header.h"
68+
// CHECK-NOT: warning:
69+
// CHECK-QUIET-NOT: warning:
70+
// CHECK2: header.h:1:12: warning: single-argument constructors must be marked explicit
71+
// CHECK2-QUIET: header.h:1:12: warning: single-argument constructors must be marked explicit
72+
// CHECK3-NOT: warning:
73+
// CHECK3-QUIET-NOT: warning:
74+
// CHECK4: header.h:1:12: warning: single-argument constructors must be marked explicit
75+
// CHECK4-QUIET: header.h:1:12: warning: single-argument constructors must be marked explicit
76+
// CHECK5-NOT: warning:
77+
// CHECK5-QUIET-NOT: warning:
78+
// CHECK6-NOT: warning:
79+
// CHECK6-QUIET-NOT: warning:
80+
// CHECK7: header.h:1:12: warning: single-argument constructors must be marked explicit
81+
// CHECK7-QUIET: header.h:1:12: warning: single-argument constructors must be marked explicit
3282

3383
#include <system-header.h>
3484
// CHECK-NOT: warning:
@@ -39,6 +89,12 @@
3989
// CHECK3-QUIET-NOT: warning:
4090
// CHECK4: system-header.h:1:12: warning: single-argument constructors
4191
// CHECK4-QUIET: system-header.h:1:12: warning: single-argument constructors
92+
// CHECK5-NOT: warning:
93+
// CHECK5-QUIET-NOT: warning:
94+
// CHECK6-NOT: warning:
95+
// CHECK6-QUIET-NOT: warning:
96+
// CHECK7-NOT: warning:
97+
// CHECK7-QUIET-NOT: warning:
4298

4399
class A { A(int); };
44100
// CHECK: :[[@LINE-1]]:11: warning: single-argument constructors
@@ -49,6 +105,12 @@ class A { A(int); };
49105
// CHECK3-QUIET: :[[@LINE-6]]:11: warning: single-argument constructors
50106
// CHECK4: :[[@LINE-7]]:11: warning: single-argument constructors
51107
// CHECK4-QUIET: :[[@LINE-8]]:11: warning: single-argument constructors
108+
// CHECK5: :[[@LINE-9]]:11: warning: single-argument constructors
109+
// CHECK5-QUIET: :[[@LINE-10]]:11: warning: single-argument constructors
110+
// CHECK6: :[[@LINE-11]]:11: warning: single-argument constructors
111+
// CHECK6-QUIET: :[[@LINE-12]]:11: warning: single-argument constructors
112+
// CHECK7: :[[@LINE-13]]:11: warning: single-argument constructors
113+
// CHECK7-QUIET: :[[@LINE-14]]:11: warning: single-argument constructors
52114

53115
// CHECK-NOT: warning:
54116
// CHECK-QUIET-NOT: warning:
@@ -58,16 +120,31 @@ class A { A(int); };
58120
// CHECK3-QUIET-NOT: warning:
59121
// CHECK4-NOT: warning:
60122
// CHECK4-QUIET-NOT: warning:
123+
// CHECK5-NOT: warning:
124+
// CHECK5-QUIET-NOT: warning:
125+
// CHECK6-NOT: warning:
126+
// CHECK6-QUIET-NOT: warning:
127+
// CHECK7-NOT: warning:
128+
// CHECK7-QUIET-NOT: warning:
61129

62-
// CHECK: Suppressed 3 warnings (3 in non-user code)
130+
// CHECK: Suppressed 6 warnings (6 in non-user code)
63131
// CHECK: Use -header-filter=.* to display errors from all non-system headers.
64132
// CHECK-QUIET-NOT: Suppressed
65133
// CHECK2: Suppressed 1 warnings (1 in non-user code)
66134
// CHECK2: Use -header-filter=.* {{.*}}
67135
// CHECK2-QUIET-NOT: Suppressed
68-
// CHECK3: Suppressed 2 warnings (2 in non-user code)
136+
// CHECK3: Suppressed 5 warnings (5 in non-user code)
69137
// CHECK3: Use -header-filter=.* {{.*}}
70138
// CHECK3-QUIET-NOT: Suppressed
71139
// CHECK4-NOT: Suppressed {{.*}} warnings
72140
// CHECK4-NOT: Use -header-filter=.* {{.*}}
73141
// CHECK4-QUIET-NOT: Suppressed
142+
// CHECK5: Suppressed 5 warnings (5 in non-user code)
143+
// CHECK5: Use -header-filter=.* to display errors from all non-system headers.
144+
// CHECK5-QUIET-NOT: Suppressed
145+
// CHECK6: Suppressed 5 warnings (5 in non-user code)
146+
// CHECK6: Use -header-filter=.* to display errors from all non-system headers.
147+
// CHECK6-QUIET-NOT: Suppressed
148+
// CHECK7: Suppressed 5 warnings (5 in non-user code)
149+
// CHECK7: Use -header-filter=.* to display errors from all non-system headers.
150+
// CHECK7-QUIET-NOT: Suppressed

0 commit comments

Comments
 (0)