Skip to content

Commit

Permalink
[Lex] Avoid repeated hash lookups (NFC) (llvm#107963)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kazutakahirata authored Sep 10, 2024
1 parent 19a2f17 commit 9710085
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1053,22 +1053,6 @@ class Preprocessor {
std::optional<MacroAnnotationInfo> DeprecationInfo;
std::optional<MacroAnnotationInfo> RestrictExpansionInfo;
std::optional<SourceLocation> FinalAnnotationLoc;

static MacroAnnotations makeDeprecation(SourceLocation Loc,
std::string Msg) {
return MacroAnnotations{MacroAnnotationInfo{Loc, std::move(Msg)},
std::nullopt, std::nullopt};
}

static MacroAnnotations makeRestrictExpansion(SourceLocation Loc,
std::string Msg) {
return MacroAnnotations{
std::nullopt, MacroAnnotationInfo{Loc, std::move(Msg)}, std::nullopt};
}

static MacroAnnotations makeFinal(SourceLocation Loc) {
return MacroAnnotations{std::nullopt, std::nullopt, Loc};
}
};

/// Warning information for macro annotations.
Expand Down Expand Up @@ -2884,35 +2868,18 @@ class Preprocessor {

void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg,
SourceLocation AnnotationLoc) {
auto Annotations = AnnotationInfos.find(II);
if (Annotations == AnnotationInfos.end())
AnnotationInfos.insert(std::make_pair(
II,
MacroAnnotations::makeDeprecation(AnnotationLoc, std::move(Msg))));
else
Annotations->second.DeprecationInfo =
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
AnnotationInfos[II].DeprecationInfo =
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
}

void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg,
SourceLocation AnnotationLoc) {
auto Annotations = AnnotationInfos.find(II);
if (Annotations == AnnotationInfos.end())
AnnotationInfos.insert(
std::make_pair(II, MacroAnnotations::makeRestrictExpansion(
AnnotationLoc, std::move(Msg))));
else
Annotations->second.RestrictExpansionInfo =
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
AnnotationInfos[II].RestrictExpansionInfo =
MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
}

void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) {
auto Annotations = AnnotationInfos.find(II);
if (Annotations == AnnotationInfos.end())
AnnotationInfos.insert(
std::make_pair(II, MacroAnnotations::makeFinal(AnnotationLoc)));
else
Annotations->second.FinalAnnotationLoc = AnnotationLoc;
AnnotationInfos[II].FinalAnnotationLoc = AnnotationLoc;
}

const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const {
Expand Down

0 comments on commit 9710085

Please sign in to comment.