Skip to content

Commit 3a8c80e

Browse files
committed
Fix
1 parent 5c002ef commit 3a8c80e

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,7 @@ class DuplicateIncludeCallbacks : public PPCallbacks {
3636
public:
3737
DuplicateIncludeCallbacks(DuplicateIncludeCheck &Check,
3838
const SourceManager &SM,
39-
const std::vector<StringRef> &IgnoredList)
40-
: Check(Check), SM(SM) {
41-
// The main file doesn't participate in the FileChanged notification.
42-
Files.emplace_back();
43-
44-
AllowedRegexes.reserve(IgnoredList.size());
45-
for (const StringRef &It : IgnoredList) {
46-
if (!It.empty())
47-
AllowedRegexes.emplace_back(It);
48-
}
49-
}
39+
const std::vector<StringRef> &IgnoredList);
5040

5141
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
5242
SrcMgr::CharacteristicKind FileType,
@@ -67,26 +57,12 @@ class DuplicateIncludeCallbacks : public PPCallbacks {
6757
const MacroDirective *Undef) override;
6858

6959
private:
60+
bool isAllowedDuplicate(StringRef FileName, OptionalFileEntryRef File) const;
7061
// A list of included files is kept for each file we enter.
7162
SmallVector<FileList> Files;
7263
DuplicateIncludeCheck &Check;
7364
const SourceManager &SM;
7465
std::vector<llvm::Regex> AllowedRegexes;
75-
76-
bool isAllowedDuplicate(StringRef FileName, OptionalFileEntryRef File) const {
77-
if (llvm::any_of(AllowedRegexes,
78-
[&](const llvm::Regex &R) { return R.match(FileName); }))
79-
return true;
80-
81-
if (File) {
82-
const StringRef Resolved = File->getName();
83-
if (llvm::any_of(AllowedRegexes,
84-
[&](const llvm::Regex &R) { return R.match(Resolved); }))
85-
return true;
86-
}
87-
88-
return false;
89-
}
9066
};
9167

9268
} // namespace
@@ -97,6 +73,37 @@ DuplicateIncludeCheck::DuplicateIncludeCheck(StringRef Name,
9773
IgnoredFilesList(utils::options::parseStringList(
9874
Options.get("IgnoredFilesList", ""))) {}
9975

76+
DuplicateIncludeCallbacks::DuplicateIncludeCallbacks(
77+
DuplicateIncludeCheck &Check, const SourceManager &SM,
78+
const std::vector<StringRef> &IgnoredList)
79+
: Check(Check), SM(SM) {
80+
// The main file doesn't participate in the FileChanged notification.
81+
Files.emplace_back();
82+
83+
AllowedRegexes.reserve(IgnoredList.size());
84+
for (const StringRef &It : IgnoredList) {
85+
if (!It.empty())
86+
AllowedRegexes.emplace_back(It);
87+
}
88+
}
89+
90+
bool DuplicateIncludeCallbacks::isAllowedDuplicate(
91+
StringRef FileName, OptionalFileEntryRef File) const {
92+
if (llvm::any_of(AllowedRegexes, [&FileName](const llvm::Regex &R) {
93+
return R.match(FileName);
94+
}))
95+
return true;
96+
97+
if (File) {
98+
const StringRef Resolved = File->getName();
99+
return llvm::any_of(AllowedRegexes, [&Resolved](const llvm::Regex &R) {
100+
return R.match(Resolved);
101+
});
102+
}
103+
104+
return false;
105+
}
106+
100107
void DuplicateIncludeCallbacks::FileChanged(SourceLocation Loc,
101108
FileChangeReason Reason,
102109
SrcMgr::CharacteristicKind FileType,
@@ -117,9 +124,8 @@ void DuplicateIncludeCallbacks::InclusionDirective(
117124
FilenameRange.getEnd().isMacroID())
118125
return;
119126
if (llvm::is_contained(Files.back(), FileName)) {
120-
if (isAllowedDuplicate(FileName, File)) {
127+
if (isAllowedDuplicate(FileName, File))
121128
return;
122-
}
123129
// We want to delete the entire line, so make sure that [Start,End] covers
124130
// everything.
125131
const SourceLocation Start =

clang-tools-extra/docs/clang-tidy/checks/readability/duplicate-include.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ Looks for duplicate includes and removes them. The check maintains a list of
77
included files and looks for duplicates. If a macro is defined or undefined
88
then the list of included files is cleared.
99

10-
Options
11-
-------
12-
13-
.. option:: IgnoredFilesList
14-
15-
A semicolon-separated list of regular expressions or filenames that are
16-
allowed to be included multiple times without diagnostics. Matching is
17-
performed against the textual include name. If the header can be
18-
resolved, its full path is also matched against the patterns.
19-
Default is empty.
20-
2110
Examples:
2211

2312
.. code-block:: c++
@@ -44,3 +33,12 @@ Because of the intervening macro definitions, this code remains unchanged:
4433
#define NDEBUG
4534
#include "assertion.h"
4635
// ...code with assertions disabled
36+
37+
Options
38+
-------
39+
40+
.. option:: IgnoredFilesList
41+
42+
A semicolon-separated list of regular expressions or filenames that are
43+
allowed to be included multiple times without diagnostics. Matching is
44+
performed against the textual include name. Default is an empty string.
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
// RUN: %check_clang_tidy %s readability-duplicate-include %t -- \
2-
// RUN: -config="{CheckOptions: {readability-duplicate-include.IgnoredFilesList: 'pack_.*\\.h'}}" \
2+
// RUN: -config="{CheckOptions: {readability-duplicate-include.IgnoredFilesList: 'pack_.*\\.h'}}" -header-filter='' \
33
// RUN: -- -I %S/Inputs/duplicate-include
44

5+
int g;
6+
#include "duplicate-include.h"
7+
int h;
8+
#include "duplicate-include.h"
9+
int i;
10+
// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include [readability-duplicate-include]
11+
// CHECK-FIXES: int g;
12+
// CHECK-FIXES-NEXT: #include "duplicate-include.h"
13+
// CHECK-FIXES-NEXT: int h;
14+
// CHECK-FIXES-NEXT: int i;
15+
516
#include "pack_begin.h"
617
struct A { int x; };
718
#include "pack_end.h"
819

9-
// no warning
20+
#include "pack_begin.h"
21+
struct B { int x; };
22+
#include "pack_end.h"
23+
24+
// No warning here.

0 commit comments

Comments
 (0)