[clang-tidy] Fix relative path in header-filter.

Summary:
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

// b.h is in /src/b/b.h
...

// c.cpp is in /src/c.cpp

```

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.

Reviewers: alexfh, hokein, aaron.ballman, gribozavr

Reviewed By: gribozavr

Subscribers: MyDeveloperDay, xazax.hun, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D67501

Patch by Yubo Xie.

llvm-svn: 372388
This commit is contained in:
Dmitri Gribenko 2019-09-20 13:19:32 +00:00
parent 81aa62addf
commit 4a13c828f6
5 changed files with 87 additions and 3 deletions

View File

@ -551,7 +551,9 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
return;
}
StringRef FileName(File->getName());
StringRef FileName = File->tryGetRealPathName();
if (FileName.empty())
FileName = File->getName();
LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
Sources.isInMainFile(Location) ||
getHeaderFilter()->match(FileName);

View File

@ -0,0 +1,3 @@
#include "../subfolder_b/header_b.h"
class SA { SA(int); };

View File

@ -0,0 +1 @@
class SB { SB(int); };

View File

@ -0,0 +1 @@
class SC { SC(int); };

View File

@ -9,6 +9,12 @@
// file-filter\header*.h due to code order between '/' and '\\'.
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
#include "header1.h"
// CHECK-NOT: warning:
@ -19,6 +25,12 @@
// CHECK3-QUIET-NOT: warning:
// CHECK4: header1.h:1:12: warning: single-argument constructors
// CHECK4-QUIET: header1.h:1:12: warning: single-argument constructors
// CHECK5-NOT: warning:
// CHECK5-QUIET-NOT: warning:
// CHECK6-NOT: warning:
// CHECK6-QUIET-NOT: warning:
// CHECK7-NOT: warning:
// CHECK7-QUIET-NOT: warning:
#include "header2.h"
// CHECK-NOT: warning:
@ -29,6 +41,44 @@
// CHECK3-QUIET: header2.h:1:12: warning: single-argument constructors
// CHECK4: header2.h:1:12: warning: single-argument constructors
// CHECK4-QUIET: header2.h:1:12: warning: single-argument constructors
// CHECK5-NOT: warning:
// CHECK5-QUIET-NOT: warning:
// CHECK6-NOT: warning:
// CHECK6-QUIET-NOT: warning:
// CHECK7-NOT: warning:
// CHECK7-QUIET-NOT: warning:
#include "subfolder_a/header_a.h"
// CHECK-NOT: warning:
// CHECK-QUIET-NOT: warning:
// CHECK2: header_b.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK2-QUIET: header_b.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK3-NOT: warning:
// CHECK3-QUIET-NOT: warning:
// CHECK4: header_b.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK4-QUIET: header_b.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK5: header_a.h:3:12: warning: single-argument constructors must be marked explicit
// CHECK5-QUIET: header_a.h:3:12: warning: single-argument constructors must be marked explicit
// CHECK6: header_b.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK6-QUIET: header_b.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK7-NOT: warning:
// CHECK7-QUIET-NOT: warning:
#include "subfolder_c/header_c.h"
// CHECK-NOT: warning:
// CHECK-QUIET-NOT: warning:
// CHECK2: header_c.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK2-QUIET: header_c.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK3-NOT: warning:
// CHECK3-QUIET-NOT: warning:
// CHECK4: header_c.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK4-QUIET: header_c.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK5-NOT: warning:
// CHECK5-QUIET-NOT: warning:
// CHECK6-NOT: warning:
// CHECK6-QUIET-NOT: warning:
// CHECK7: header_c.h:1:12: warning: single-argument constructors must be marked explicit
// CHECK7-QUIET: header_c.h:1:12: warning: single-argument constructors must be marked explicit
#include <system-header.h>
// CHECK-NOT: warning:
@ -39,6 +89,12 @@
// CHECK3-QUIET-NOT: warning:
// CHECK4: system-header.h:1:12: warning: single-argument constructors
// CHECK4-QUIET: system-header.h:1:12: warning: single-argument constructors
// CHECK5-NOT: warning:
// CHECK5-QUIET-NOT: warning:
// CHECK6-NOT: warning:
// CHECK6-QUIET-NOT: warning:
// CHECK7-NOT: warning:
// CHECK7-QUIET-NOT: warning:
class A { A(int); };
// CHECK: :[[@LINE-1]]:11: warning: single-argument constructors
@ -49,6 +105,12 @@ class A { A(int); };
// CHECK3-QUIET: :[[@LINE-6]]:11: warning: single-argument constructors
// CHECK4: :[[@LINE-7]]:11: warning: single-argument constructors
// CHECK4-QUIET: :[[@LINE-8]]:11: warning: single-argument constructors
// CHECK5: :[[@LINE-9]]:11: warning: single-argument constructors
// CHECK5-QUIET: :[[@LINE-10]]:11: warning: single-argument constructors
// CHECK6: :[[@LINE-11]]:11: warning: single-argument constructors
// CHECK6-QUIET: :[[@LINE-12]]:11: warning: single-argument constructors
// CHECK7: :[[@LINE-13]]:11: warning: single-argument constructors
// CHECK7-QUIET: :[[@LINE-14]]:11: warning: single-argument constructors
// CHECK-NOT: warning:
// CHECK-QUIET-NOT: warning:
@ -58,16 +120,31 @@ class A { A(int); };
// CHECK3-QUIET-NOT: warning:
// CHECK4-NOT: warning:
// CHECK4-QUIET-NOT: warning:
// CHECK5-NOT: warning:
// CHECK5-QUIET-NOT: warning:
// CHECK6-NOT: warning:
// CHECK6-QUIET-NOT: warning:
// CHECK7-NOT: warning:
// CHECK7-QUIET-NOT: warning:
// CHECK: Suppressed 3 warnings (3 in non-user code)
// CHECK: Suppressed 6 warnings (6 in non-user code)
// CHECK: Use -header-filter=.* to display errors from all non-system headers.
// CHECK-QUIET-NOT: Suppressed
// CHECK2: Suppressed 1 warnings (1 in non-user code)
// CHECK2: Use -header-filter=.* {{.*}}
// CHECK2-QUIET-NOT: Suppressed
// CHECK3: Suppressed 2 warnings (2 in non-user code)
// CHECK3: Suppressed 5 warnings (5 in non-user code)
// CHECK3: Use -header-filter=.* {{.*}}
// CHECK3-QUIET-NOT: Suppressed
// CHECK4-NOT: Suppressed {{.*}} warnings
// CHECK4-NOT: Use -header-filter=.* {{.*}}
// CHECK4-QUIET-NOT: Suppressed
// CHECK5: Suppressed 5 warnings (5 in non-user code)
// CHECK5: Use -header-filter=.* to display errors from all non-system headers.
// CHECK5-QUIET-NOT: Suppressed
// CHECK6: Suppressed 5 warnings (5 in non-user code)
// CHECK6: Use -header-filter=.* to display errors from all non-system headers.
// CHECK6-QUIET-NOT: Suppressed
// CHECK7: Suppressed 5 warnings (5 in non-user code)
// CHECK7: Use -header-filter=.* to display errors from all non-system headers.
// CHECK7-QUIET-NOT: Suppressed