Skip to content

Commit f774e44

Browse files
committed
[clang-tidy][NFC] Do less unnecessary work in modernize-deprecated-headers
1 parent 2837370 commit f774e44

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ namespace {
2424
class IncludeModernizePPCallbacks : public PPCallbacks {
2525
public:
2626
explicit IncludeModernizePPCallbacks(
27-
std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts,
28-
const SourceManager &SM, bool CheckHeaderFile);
27+
std::vector<IncludeMarker> &IncludesToBeProcessed,
28+
const LangOptions &LangOpts, const SourceManager &SM,
29+
bool CheckHeaderFile);
2930

3031
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
3132
StringRef FileName, bool IsAngled,
@@ -37,8 +38,7 @@ class IncludeModernizePPCallbacks : public PPCallbacks {
3738

3839
private:
3940
std::vector<IncludeMarker> &IncludesToBeProcessed;
40-
LangOptions LangOpts;
41-
llvm::StringMap<std::string> CStyledHeaderToCxx;
41+
llvm::StringMap<StringRef> CStyledHeaderToCxx;
4242
llvm::StringSet<> DeleteHeaders;
4343
const SourceManager &SM;
4444
bool CheckHeaderFile;
@@ -131,48 +131,38 @@ void DeprecatedHeadersCheck::check(
131131
}
132132

133133
IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
134-
std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts,
135-
const SourceManager &SM, bool CheckHeaderFile)
136-
: IncludesToBeProcessed(IncludesToBeProcessed), LangOpts(LangOpts), SM(SM),
134+
std::vector<IncludeMarker> &IncludesToBeProcessed,
135+
const LangOptions &LangOpts, const SourceManager &SM, bool CheckHeaderFile)
136+
: IncludesToBeProcessed(IncludesToBeProcessed), SM(SM),
137137
CheckHeaderFile(CheckHeaderFile) {
138-
for (const auto &KeyValue :
139-
std::vector<std::pair<llvm::StringRef, std::string>>(
140-
{{"assert.h", "cassert"},
141-
{"complex.h", "complex"},
142-
{"ctype.h", "cctype"},
143-
{"errno.h", "cerrno"},
144-
{"float.h", "cfloat"},
145-
{"limits.h", "climits"},
146-
{"locale.h", "clocale"},
147-
{"math.h", "cmath"},
148-
{"setjmp.h", "csetjmp"},
149-
{"signal.h", "csignal"},
150-
{"stdarg.h", "cstdarg"},
151-
{"stddef.h", "cstddef"},
152-
{"stdio.h", "cstdio"},
153-
{"stdlib.h", "cstdlib"},
154-
{"string.h", "cstring"},
155-
{"time.h", "ctime"},
156-
{"wchar.h", "cwchar"},
157-
{"wctype.h", "cwctype"}})) {
138+
139+
static constexpr std::pair<StringRef, StringRef> CXX98Headers[] = {
140+
{"assert.h", "cassert"}, {"complex.h", "complex"},
141+
{"ctype.h", "cctype"}, {"errno.h", "cerrno"},
142+
{"float.h", "cfloat"}, {"limits.h", "climits"},
143+
{"locale.h", "clocale"}, {"math.h", "cmath"},
144+
{"setjmp.h", "csetjmp"}, {"signal.h", "csignal"},
145+
{"stdarg.h", "cstdarg"}, {"stddef.h", "cstddef"},
146+
{"stdio.h", "cstdio"}, {"stdlib.h", "cstdlib"},
147+
{"string.h", "cstring"}, {"time.h", "ctime"},
148+
{"wchar.h", "cwchar"}, {"wctype.h", "cwctype"},
149+
};
150+
for (const auto &KeyValue : CXX98Headers)
158151
CStyledHeaderToCxx.insert(KeyValue);
159-
}
160-
// Add C++11 headers.
161-
if (LangOpts.CPlusPlus11) {
162-
for (const auto &KeyValue :
163-
std::vector<std::pair<llvm::StringRef, std::string>>(
164-
{{"fenv.h", "cfenv"},
165-
{"stdint.h", "cstdint"},
166-
{"inttypes.h", "cinttypes"},
167-
{"tgmath.h", "ctgmath"},
168-
{"uchar.h", "cuchar"}})) {
152+
153+
static constexpr std::pair<StringRef, StringRef> CXX11Headers[] = {
154+
{"fenv.h", "cfenv"}, {"stdint.h", "cstdint"},
155+
{"inttypes.h", "cinttypes"}, {"tgmath.h", "ctgmath"},
156+
{"uchar.h", "cuchar"},
157+
};
158+
if (LangOpts.CPlusPlus11)
159+
for (const auto &KeyValue : CXX11Headers)
169160
CStyledHeaderToCxx.insert(KeyValue);
170-
}
171-
}
172-
for (const auto &Key :
173-
std::vector<std::string>({"stdalign.h", "stdbool.h", "iso646.h"})) {
161+
162+
static constexpr StringRef HeadersToDelete[] = {"stdalign.h", "stdbool.h",
163+
"iso646.h"};
164+
for (const auto &Key : HeadersToDelete)
174165
DeleteHeaders.insert(Key);
175-
}
176166
}
177167

178168
void IncludeModernizePPCallbacks::InclusionDirective(
@@ -205,7 +195,7 @@ void IncludeModernizePPCallbacks::InclusionDirective(
205195
} else if (DeleteHeaders.contains(FileName)) {
206196
IncludesToBeProcessed.emplace_back(
207197
// NOLINTNEXTLINE(modernize-use-emplace) - false-positive
208-
IncludeMarker{std::string{}, FileName,
198+
IncludeMarker{StringRef{}, FileName,
209199
SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc});
210200
}
211201
}

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DeprecatedHeadersCheck : public ClangTidyCheck {
4444
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
4545

4646
struct IncludeMarker {
47-
std::string Replacement;
47+
StringRef Replacement;
4848
StringRef FileName;
4949
SourceRange ReplacementRange;
5050
SourceLocation DiagLoc;

0 commit comments

Comments
 (0)