Skip to content

Commit b611038

Browse files
committed
[readability-duplicate-include] Rename options, update docs, and format code
1 parent b29140a commit b611038

File tree

6 files changed

+23
-46
lines changed

6 files changed

+23
-46
lines changed

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ class DuplicateIncludeCallbacks : public PPCallbacks {
6666
const SourceManager &SM;
6767
std::vector<llvm::Regex> AllowedDuplicateRegex;
6868

69-
bool IsAllowedDuplicateInclude(StringRef TokenName, OptionalFileEntryRef File,
70-
StringRef RelativePath);
69+
bool IsAllowedDuplicateInclude(StringRef TokenName);
7170
};
7271

7372
} // namespace
@@ -105,7 +104,7 @@ void DuplicateIncludeCallbacks::InclusionDirective(
105104
return;
106105

107106
// if duplicate allowed, record and return
108-
if (IsAllowedDuplicateInclude(FileName, File, RelativePath)) {
107+
if (IsAllowedDuplicateInclude(FileName)) {
109108
Files.back().push_back(FileName);
110109
return;
111110
}
@@ -134,37 +133,24 @@ void DuplicateIncludeCallbacks::MacroUndefined(const Token &MacroNameTok,
134133
Files.back().clear();
135134
}
136135

137-
bool DuplicateIncludeCallbacks::IsAllowedDuplicateInclude(
138-
StringRef TokenName, OptionalFileEntryRef File, StringRef RelativePath) {
139-
SmallVector<StringRef, 3> matchArguments;
140-
matchArguments.push_back(TokenName);
141-
142-
if (!RelativePath.empty())
143-
matchArguments.push_back(llvm::sys::path::filename(RelativePath));
144-
145-
if (File) {
146-
StringRef RealPath = File->getFileEntry().tryGetRealPathName();
147-
if (!RealPath.empty())
148-
matchArguments.push_back(llvm::sys::path::filename(RealPath));
149-
}
150-
136+
bool DuplicateIncludeCallbacks::IsAllowedDuplicateInclude(StringRef FileName) {
151137
// try to match with each regex
152138
for (const llvm::Regex &reg : AllowedDuplicateRegex) {
153-
for (StringRef arg : matchArguments) {
154-
if (reg.match(arg))
155-
return true;
156-
}
139+
if (reg.match(FileName))
140+
return true;
157141
}
158142
return false;
159143
}
144+
} // namespace clang::tidy::readability
160145

146+
namespace clang::tidy::readability {
161147
DuplicateIncludeCheck::DuplicateIncludeCheck(StringRef Name,
162148
ClangTidyContext *Context)
163149
: ClangTidyCheck(Name, Context) {
164-
std::string Raw = Options.get("AllowedDuplicateIncludes", "").str();
150+
std::string Raw = Options.get("IgnoreHeaders", "").str();
165151
if (!Raw.empty()) {
166152
SmallVector<StringRef, 4> StringParts;
167-
StringRef(Raw).split(StringParts, ',', -1, false);
153+
StringRef(Raw).split(StringParts, ';', -1, false);
168154

169155
for (StringRef Part : StringParts) {
170156
Part = Part.trim();
@@ -181,7 +167,7 @@ void DuplicateIncludeCheck::registerPPCallbacks(
181167
}
182168

183169
void DuplicateIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
184-
Options.store(Opts, "AllowedDuplicateIncludes",
185-
llvm::join(AllowedDuplicateIncludes, ","));
170+
Options.store(Opts, "IgnoreHeaders",
171+
llvm::join(AllowedDuplicateIncludes, ";"));
186172
}
187173
} // namespace clang::tidy::readability

clang-tools-extra/docs/clang-tidy/checks/misc/include-cleaner.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Options
2828
-------
2929

3030
.. option:: IgnoreHeaders
31-
31+
3232
A semicolon-separated list of regexes to disable insertion/removal of header
3333
files that match this regex as a suffix. E.g., `foo/.*` disables
3434
insertion/removal for all headers under the directory `foo`. Default is an

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,12 @@ Because of the intervening macro definitions, this code remains unchanged:
3434
#include "assertion.h"
3535
// ...code with assertions disabled
3636

37-
Option: ``AllowedDuplicateIncludes``
38-
------------------------------------
39-
40-
Headers listed in this option are exempt from warnings. For example:
41-
42-
.. code-block:: c++
43-
44-
-config='{CheckOptions: [{key: readability-duplicate-include.AllowedDuplicateIncludes, value: "pack_begin.h,pack_end.h"}]}'
45-
46-
This allows regex matches with ``pack_begin.h`` and ``pack_end.h`` to be included multiple times
47-
without triggering diagnostics.
48-
49-
Notes
50-
-----
51-
52-
- Only direct includes in the current translation unit are checked.
53-
- Useful for removing redundant includes and improving compile times in large codebases.
37+
Options
38+
-------
39+
40+
.. option:: IgnoreHeaders
41+
42+
A semicolon-separated list of regexes to allow duplicate inclusion of header
43+
files that match this regex. E.g., `foo/.*` disables
44+
insertion/removal for all headers under the directory `foo`. Default is an
45+
empty string, no headers will be ignored.

clang-tools-extra/docs/clang-tidy/checks/readability/else-after-return.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Options
6363
-------
6464

6565
.. option:: WarnOnUnfixable
66-
6766
When `true`, emit a warning for cases where the check can't output a
6867
Fix-It. These can occur with declarations inside the ``else`` branch that
6968
would have an extended lifetime if the ``else`` branch was removed.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// dummy other.h (should warn on duplicate includes)
2-
#pragma once
2+
#pragma once

clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include-allowed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %check_clang_tidy %s readability-duplicate-include %t -- \
22
// RUN: -header-filter='' \
3-
// RUN: -config='{CheckOptions: [{key: readability-duplicate-include.AllowedDuplicateIncludes, value: "pack_begin.h,pack_end.h"}]}' \
3+
// RUN: -config='{CheckOptions: [{key: readability-duplicate-include.IgnoreHeaders, value: "pack_begin.h;pack_end.h"}]}' \
44
// RUN: -- -I %S/Inputs/duplicate-include-allowed
55
//
66
// This test lives in test/clang-tidy/checkers/

0 commit comments

Comments
 (0)