Skip to content

Commit 403e3da

Browse files
localspookclingfei
authored andcommitted
[clang-tidy][NFC] Do less unnecessary work in modernize-deprecated-headers (llvm#160967)
- `IncludeModernizePPCallbacks` creates temporary vectors when all it needs is constant arrays - The header replacement strings are `std::string` when they can just be `StringRef` - `IncludeModernizePPCallbacks`'s constructor 1. Takes `LangOptions` by value (the thing is 832 bytes) 2. Stores it, but none of `IncludeModernizePPCallbacks`'s member functions use it
1 parent 796a888 commit 403e3da

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

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

Lines changed: 33 additions & 46 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,35 @@ 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"}})) {
158-
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"}})) {
169-
CStyledHeaderToCxx.insert(KeyValue);
170-
}
171-
}
172-
for (const auto &Key :
173-
std::vector<std::string>({"stdalign.h", "stdbool.h", "iso646.h"})) {
174-
DeleteHeaders.insert(Key);
175-
}
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+
CStyledHeaderToCxx.insert(std::begin(CXX98Headers), std::end(CXX98Headers));
151+
152+
static constexpr std::pair<StringRef, StringRef> CXX11Headers[] = {
153+
{"fenv.h", "cfenv"}, {"stdint.h", "cstdint"},
154+
{"inttypes.h", "cinttypes"}, {"tgmath.h", "ctgmath"},
155+
{"uchar.h", "cuchar"},
156+
};
157+
if (LangOpts.CPlusPlus11)
158+
CStyledHeaderToCxx.insert(std::begin(CXX11Headers), std::end(CXX11Headers));
159+
160+
static constexpr StringRef HeadersToDelete[] = {"stdalign.h", "stdbool.h",
161+
"iso646.h"};
162+
DeleteHeaders.insert_range(HeadersToDelete);
176163
}
177164

178165
void IncludeModernizePPCallbacks::InclusionDirective(
@@ -205,7 +192,7 @@ void IncludeModernizePPCallbacks::InclusionDirective(
205192
} else if (DeleteHeaders.contains(FileName)) {
206193
IncludesToBeProcessed.emplace_back(
207194
// NOLINTNEXTLINE(modernize-use-emplace) - false-positive
208-
IncludeMarker{std::string{}, FileName,
195+
IncludeMarker{StringRef{}, FileName,
209196
SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc});
210197
}
211198
}

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)