[clang] [Lex] Avoid repeated hash lookups (NFC) (PR #107963)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 9 20:47:46 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/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.


>From b54015726ee2573ef87d4dcbc5549860f870d17c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 9 Sep 2024 07:18:54 -0700
Subject: [PATCH] [Lex] Avoid repeated hash lookups (NFC)

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<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.
@@ -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 {



More information about the cfe-commits mailing list