Skip to content

Commit 9710085

Browse files
[Lex] Avoid repeated hash lookups (NFC) (#107963)
MacroAnnotations has three std::optional fields. Functions makeDeprecation, makeRestrictExpansion, and makeFinal construct an instance of MacroAnnotations with one field initialized with a non-default value (that is, some value other than std::nullopt). Functions addMacroDeprecationMsg, addRestrictExpansionMsg, and addFinalLoc either create a new map entry with one field initialized with a non-default value or replaces one field of an existing map entry. We can do all this with a simple statement of the form: AnnotationInfos[II].FieldName = NonDefaultValue; which takes care of default initialization of the fields with std::nullopt when a requested map entry does not exist.
1 parent 19a2f17 commit 9710085

File tree

1 file changed

+5
-38
lines changed

1 file changed

+5
-38
lines changed

clang/include/clang/Lex/Preprocessor.h

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,22 +1053,6 @@ class Preprocessor {
10531053
std::optional<MacroAnnotationInfo> DeprecationInfo;
10541054
std::optional<MacroAnnotationInfo> RestrictExpansionInfo;
10551055
std::optional<SourceLocation> FinalAnnotationLoc;
1056-
1057-
static MacroAnnotations makeDeprecation(SourceLocation Loc,
1058-
std::string Msg) {
1059-
return MacroAnnotations{MacroAnnotationInfo{Loc, std::move(Msg)},
1060-
std::nullopt, std::nullopt};
1061-
}
1062-
1063-
static MacroAnnotations makeRestrictExpansion(SourceLocation Loc,
1064-
std::string Msg) {
1065-
return MacroAnnotations{
1066-
std::nullopt, MacroAnnotationInfo{Loc, std::move(Msg)}, std::nullopt};
1067-
}
1068-
1069-
static MacroAnnotations makeFinal(SourceLocation Loc) {
1070-
return MacroAnnotations{std::nullopt, std::nullopt, Loc};
1071-
}
10721056
};
10731057

10741058
/// Warning information for macro annotations.
@@ -2884,35 +2868,18 @@ class Preprocessor {
28842868

28852869
void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg,
28862870
SourceLocation AnnotationLoc) {
2887-
auto Annotations = AnnotationInfos.find(II);
2888-
if (Annotations == AnnotationInfos.end())
2889-
AnnotationInfos.insert(std::make_pair(
2890-
II,
2891-
MacroAnnotations::makeDeprecation(AnnotationLoc, std::move(Msg))));
2892-
else
2893-
Annotations->second.DeprecationInfo =
2894-
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
2871+
AnnotationInfos[II].DeprecationInfo =
2872+
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
28952873
}
28962874

28972875
void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg,
28982876
SourceLocation AnnotationLoc) {
2899-
auto Annotations = AnnotationInfos.find(II);
2900-
if (Annotations == AnnotationInfos.end())
2901-
AnnotationInfos.insert(
2902-
std::make_pair(II, MacroAnnotations::makeRestrictExpansion(
2903-
AnnotationLoc, std::move(Msg))));
2904-
else
2905-
Annotations->second.RestrictExpansionInfo =
2906-
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
2877+
AnnotationInfos[II].RestrictExpansionInfo =
2878+
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
29072879
}
29082880

29092881
void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) {
2910-
auto Annotations = AnnotationInfos.find(II);
2911-
if (Annotations == AnnotationInfos.end())
2912-
AnnotationInfos.insert(
2913-
std::make_pair(II, MacroAnnotations::makeFinal(AnnotationLoc)));
2914-
else
2915-
Annotations->second.FinalAnnotationLoc = AnnotationLoc;
2882+
AnnotationInfos[II].FinalAnnotationLoc = AnnotationLoc;
29162883
}
29172884

29182885
const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const {

0 commit comments

Comments
 (0)