r328041 - [format] Initialize regex lazily

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 20 13:43:13 PDT 2018


Author: d0k
Date: Tue Mar 20 13:43:12 2018
New Revision: 328041

URL: http://llvm.org/viewvc/llvm-project?rev=328041&view=rev
Log:
[format] Initialize regex lazily

No need to pay for this on program startup, and also no need to destroy
it on process end.

Modified:
    cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp

Modified: cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp?rev=328041&r1=328040&r2=328041&view=diff
==============================================================================
--- cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp (original)
+++ cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp Tue Mar 20 13:43:12 2018
@@ -27,13 +27,6 @@ namespace {
 // Short namespaces don't need an end comment.
 static const int kShortNamespaceMaxLines = 1;
 
-// Matches a valid namespace end comment.
-// Valid namespace end comments don't need to be edited.
-static llvm::Regex kNamespaceCommentPattern =
-    llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
-                "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
-                llvm::Regex::IgnoreCase);
-
 // Computes the name of a namespace given the namespace token.
 // Returns "" for anonymous namespace.
 std::string computeName(const FormatToken *NamespaceTok) {
@@ -67,8 +60,15 @@ bool hasEndComment(const FormatToken *RB
 bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) {
   assert(hasEndComment(RBraceTok));
   const FormatToken *Comment = RBraceTok->Next;
+
+  // Matches a valid namespace end comment.
+  // Valid namespace end comments don't need to be edited.
+  static llvm::Regex *const NamespaceCommentPattern =
+      new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
+                      "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
+                      llvm::Regex::IgnoreCase);
   SmallVector<StringRef, 7> Groups;
-  if (kNamespaceCommentPattern.match(Comment->TokenText, &Groups)) {
+  if (NamespaceCommentPattern->match(Comment->TokenText, &Groups)) {
     StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
     // Anonymous namespace comments must not mention a namespace name.
     if (NamespaceName.empty() && !NamespaceNameInComment.empty())




More information about the cfe-commits mailing list