From 97100855479027ccda30b17b1172ad49043b3338 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Tue, 10 Sep 2024 07:33:35 -0700 Subject: [PATCH] [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. --- clang/include/clang/Lex/Preprocessor.h | 43 +++----------------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 1307659e27d137..4643b0213815f8 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -1053,22 +1053,6 @@ class Preprocessor { std::optional DeprecationInfo; std::optional RestrictExpansionInfo; std::optional 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. @@ -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 {