CMS 3D CMS Logo

FriendlyName.cc
Go to the documentation of this file.
1 /*
2  * friendlyName.cpp
3  * CMSSW
4  *
5  * Created by Chris Jones on 2/24/06.
6  *
7  */
8 #include <string>
9 #include <regex>
10 #include <iostream>
11 #include <cassert>
12 #include "tbb/concurrent_unordered_map.h"
13 
14 //NOTE: This should probably be rewritten so that we break the class name into a tree where the template arguments are the node. On the way down the tree
15 // we look for '<' or ',' and on the way up (caused by finding a '>') we can apply the transformation to the output string based on the class name for the
16 // templated class. Up front we'd register a class name to a transformation function (which would probably take a std::vector<std::string> which holds
17 // the results of the node transformations)
18 
19 namespace {
20  constexpr bool debug = false;
21  std::string prefix; // used only if debug == true
22 } // namespace
23 
24 namespace edm {
25  namespace friendlyname {
26  static std::regex const reBeginSpace("^ +");
27  static std::regex const reEndSpace(" +$");
28  static std::regex const reAllSpaces(" +");
29  static std::regex const reColons("::");
30  static std::regex const reComma(",");
31  static std::regex const reTemplateArgs("[^<]*<(.*)>$");
32  static std::regex const rePointer("\\*");
33  static std::regex const reArray("\\[\\]");
34  static std::regex const reUniquePtrDeleter("^std::unique_ptr< *(.*), *std::default_delete<\\1> *>");
35  static std::regex const reUniquePtr("^std::unique_ptr");
36  static std::string const emptyString("");
37 
38  std::string handleNamespaces(std::string const& iIn) { return std::regex_replace(iIn, reColons, emptyString); }
39 
41  return std::regex_replace(std::regex_replace(iIn, reBeginSpace, emptyString), reEndSpace, emptyString);
42  }
43 
44  std::string removeAllSpaces(std::string const& iIn) { return std::regex_replace(iIn, reAllSpaces, emptyString); }
45  static std::regex const reWrapper("edm::Wrapper<(.*)>");
46  static std::regex const reString("std::basic_string<char>");
47  static std::regex const reString2("std::string");
48  static std::regex const reString3("std::basic_string<char,std::char_traits<char> >");
49  //The c++11 abi for gcc internally uses a different namespace for standard classes
50  static std::regex const reCXX11("std::__cxx11::");
51  static std::regex const reSorted("edm::SortedCollection<(.*), *edm::StrictWeakOrdering<\\1 *> >");
52  static std::regex const reclangabi("std::__1::");
53  static std::regex const reULongLong("ULong64_t");
54  static std::regex const reLongLong("Long64_t");
55  static std::regex const reUnsigned("unsigned ");
56  static std::regex const reLong("long ");
57  static std::regex const reVector("std::vector");
58  static std::regex const reSharedPtr("std::shared_ptr");
59  static std::regex const reAIKR(
60  ", *edm::helper::AssociationIdenticalKeyReference"); //this is a default so can replaced with empty
61  //force first argument to also be the argument to edm::ClonePolicy so that if OwnVector is within
62  // a template it will not eat all the remaining '>'s
63  static std::regex const reOwnVector("edm::OwnVector<(.*), *edm::ClonePolicy<\\1 *> >");
64 
65  //NOTE: the '?' means make the smallest match. This may lead to problems where the template arguments themselves have commas
66  // but we are using it in the cases where edm::AssociationMap appears multiple times in template arguments
67  static std::regex const reOneToOne("edm::AssociationMap< *edm::OneToOne<(.*?),(.*?), *u[a-z]*> >");
68  static std::regex const reOneToMany("edm::AssociationMap< *edm::OneToMany<(.*?),(.*?), *u[a-z]*> >");
69  static std::regex const reOneToValue("edm::AssociationMap< *edm::OneToValue<(.*?),(.*?), *u[a-z]*> >");
70  static std::regex const reOneToManyWithQuality(
71  "edm::AssociationMap<edm::OneToManyWithQuality<(.*?), *(.*?), *(.*?), *u[a-z]*> >");
72  static std::regex const reToVector("edm::AssociationVector<(.*), *(.*), *edm::Ref.*,.*>");
73  //NOTE: if the item within a clone policy is a template, this substitution will probably fail
74  static std::regex const reToRangeMap("edm::RangeMap< *(.*), *(.*), *edm::ClonePolicy<([^>]*)> >");
75  //NOTE: If container is a template with one argument which is its 'type' then can simplify name
76  static std::regex const reToRefs1(
77  "edm::RefVector< *(.*)< *(.*) *>, *\\2 *, *edm::refhelper::FindUsingAdvance< *\\1< *\\2 *> *, *\\2 *> *>");
78  static std::regex const reToRefs2(
79  "edm::RefVector< *(.*) *, *(.*) *, *edm::refhelper::FindUsingAdvance< *\\1, *\\2 *> *>");
80  static std::regex const reToRefsAssoc(
81  "edm::RefVector< *Association(.*) *, *edm::helper(.*), *Association(.*)::Find>");
82 
84  using std::regex;
85  using std::regex_replace;
86  std::string name = regex_replace(iIn, reWrapper, "$1");
87  name = regex_replace(name, rePointer, "ptr");
88  name = regex_replace(name, reArray, "As");
89  name = regex_replace(name, reAIKR, "");
90  name = regex_replace(name, reclangabi, "std::");
91  name = regex_replace(name, reCXX11, "std::");
92  name = regex_replace(name, reString, "String");
93  name = regex_replace(name, reString2, "String");
94  name = regex_replace(name, reString3, "String");
95  name = regex_replace(name, reSorted, "sSorted<$1>");
96  name = regex_replace(name, reULongLong, "ull");
97  name = regex_replace(name, reLongLong, "ll");
98  name = regex_replace(name, reUnsigned, "u");
99  name = regex_replace(name, reLong, "l");
100  name = regex_replace(name, reVector, "s");
101  name = regex_replace(name, reSharedPtr, "SharedPtr");
102  name = regex_replace(name, reOwnVector, "sOwned<$1>");
103  name = regex_replace(name, reToVector, "AssociationVector<$1,To,$2>");
104  name = regex_replace(name, reOneToOne, "Association<$1,ToOne,$2>");
105  name = regex_replace(name, reOneToMany, "Association<$1,ToMany,$2>");
106  name = regex_replace(name, reOneToValue, "Association<$1,ToValue,$2>");
107  name = regex_replace(name, reOneToManyWithQuality, "Association<$1,ToMany,$2,WithQuantity,$3>");
108  name = regex_replace(name, reToRangeMap, "RangeMap<$1,$2>");
109  name = regex_replace(name, reToRefs1, "Refs<$1<$2>>");
110  name = regex_replace(name, reToRefs2, "Refs<$1,$2>");
111  name = regex_replace(name, reToRefsAssoc, "Refs<Association$1>");
112  //std::cout <<"standardRenames '"<<name<<"'"<<std::endl;
113  return name;
114  }
115 
118  using namespace std;
119  std::string result = removeExtraSpaces(iFullName);
120 
121  // temporarily remove leading const
122  std::string leadingConst;
123  if (std::string_view{result}.substr(0, 5) == "const") {
124  leadingConst = "const";
125  result = removeExtraSpaces(result.substr(5));
126  }
127 
128  if constexpr (debug) {
129  std::cout << prefix << "subFriendlyName iFullName " << iFullName << " result " << result << std::endl;
130  }
131  // Handle unique_ptr, which may contain the deleter (but handle only std::default_delete)
132  {
133  auto result2 =
134  regex_replace(result, reUniquePtrDeleter, "UniquePtr<$1>", std::regex_constants::format_first_only);
135  if (result2 == result) {
136  result2 = regex_replace(result, reUniquePtr, "UniquePtr", std::regex_constants::format_first_only);
137  }
138  result = std::move(result2);
139  }
140  // insert the leading const back if it was there
141  result = leadingConst + result;
142  if (smatch theMatch; regex_match(result, theMatch, reTemplateArgs)) {
143  //std::cout <<"found match \""<<theMatch.str(1) <<"\"" <<std::endl;
144  //static regex const templateClosing(">$");
145  //std::string aMatch = regex_replace(theMatch.str(1),templateClosing,"");
146  std::string aMatch = theMatch.str(1);
147  if constexpr (debug) {
148  prefix += " ";
149  }
150  std::string theSub = handleTemplateArguments(aMatch);
151  if constexpr (debug) {
152  prefix.pop_back();
153  prefix.pop_back();
154  std::cout << prefix << " aMatch " << aMatch << " theSub " << theSub << std::endl;
155  }
156  regex const eMatch(std::string("(^[^<]*)<") + aMatch + ">");
157  result = regex_replace(result, eMatch, theSub + "$1");
158  }
159  return removeAllSpaces(result);
160  }
161 
163  using namespace std;
165  if constexpr (debug) {
166  std::cout << prefix << "handleTemplateArguments " << iIn << " removeExtraSpaces " << result << std::endl;
167  }
168 
169  // Trick to have every full class name to end with comma to
170  // avoid treating the end as a special case
171  result += ",";
172 
173  std::string result2;
174  result2.reserve(iIn.size());
175  unsigned int openTemplate = 0;
176  bool hadTemplate = false;
177  size_t begin = 0;
178  for (size_t i = 0, size = result.size(); i < size; ++i) {
179  if (result[i] == '<') {
180  ++openTemplate;
181  hadTemplate = true;
182  continue;
183  } else if (result[i] == '>') {
184  --openTemplate;
185  }
186  // If we are not within the template arguments of a class template
187  // - encountering comma means that we are within a template
188  // argument of some other class template, and we've reached
189  // a point when we should translate the argument class name
190  // - encountering colon, but only if the class name so far
191  // itself was a template, we've reached a point when we
192  // should translate the class name
193  if (const bool hasComma = result[i] == ',', hasColon = hadTemplate and result[i] == ':';
194  openTemplate == 0 and (hasComma or hasColon)) {
195  std::string templateClass = result.substr(begin, i - begin);
196  if constexpr (debug) {
197  std::cout << prefix << " templateClass " << templateClass << std::endl;
198  }
199  if (hadTemplate) {
200  if constexpr (debug) {
201  prefix += " ";
202  }
203  std::string friendlierName = subFriendlyName(templateClass);
204  if constexpr (debug) {
205  prefix.pop_back();
206  prefix.pop_back();
207  std::cout << prefix << " friendlierName " << friendlierName << std::endl;
208  }
209  result2 += friendlierName;
210  } else {
211  result2 += templateClass;
212  }
213  if constexpr (debug) {
214  std::cout << prefix << " result2 " << result2 << std::endl;
215  }
216  // reset counters
217  hadTemplate = false;
218  begin = i + 1;
219  // With colon we need to eat the second colon as well
220  if (hasColon) {
221  assert(result[begin] == ':');
222  ++begin;
223  }
224  }
225  }
226 
227  result = regex_replace(result2, reComma, "");
228  if constexpr (debug) {
229  std::cout << prefix << " reComma " << result << std::endl;
230  }
231  return result;
232  }
234  if constexpr (debug) {
235  std::cout << "\nfriendlyName for " << iFullName << std::endl;
236  prefix = " ";
237  }
238  typedef tbb::concurrent_unordered_map<std::string, std::string> Map;
239  static Map s_fillToFriendlyName;
240  auto itFound = s_fillToFriendlyName.find(iFullName);
241  if (s_fillToFriendlyName.end() == itFound) {
242  itFound = s_fillToFriendlyName
243  .insert(Map::value_type(iFullName, handleNamespaces(subFriendlyName(standardRenames(iFullName)))))
244  .first;
245  }
246  if constexpr (debug) {
247  std::cout << "result " << itFound->second << std::endl;
248  }
249  return itFound->second;
250  }
251  } // namespace friendlyname
252 } // namespace edm
edm::friendlyname::rePointer
static const std::regex rePointer("\\*")
edm::friendlyname::removeExtraSpaces
std::string removeExtraSpaces(std::string const &iIn)
Definition: FriendlyName.cc:40
mps_fire.i
i
Definition: mps_fire.py:355
edm::friendlyname::reToRefs2
static const std::regex reToRefs2("edm::RefVector< *(.*) *, *(.*) *, *edm::refhelper::FindUsingAdvance< *\\1, *\\2 *> *>")
edm::friendlyname::reString
static const std::regex reString("std::basic_string<char>")
edm::friendlyname::reLongLong
static const std::regex reLongLong("Long64_t")
edm::friendlyname::reWrapper
static const std::regex reWrapper("edm::Wrapper<(.*)>")
edm
HLT enums.
Definition: AlignableModifier.h:19
gather_cfg.cout
cout
Definition: gather_cfg.py:144
edm::friendlyname::reOneToValue
static const std::regex reOneToValue("edm::AssociationMap< *edm::OneToValue<(.*?),(.*?), *u[a-z]*> >")
edm::friendlyname::emptyString
static const std::string emptyString("")
cms::cuda::assert
assert(be >=bs)
edm::friendlyname::subFriendlyName
std::string subFriendlyName(std::string const &iFullName)
Definition: FriendlyName.cc:117
edm::friendlyname::reToRangeMap
static const std::regex reToRangeMap("edm::RangeMap< *(.*), *(.*), *edm::ClonePolicy<([^>]*)> >")
edm::friendlyname::reArray
static const std::regex reArray("\\[\\]")
edm::friendlyname::removeAllSpaces
std::string removeAllSpaces(std::string const &iIn)
Definition: FriendlyName.cc:44
edm::friendlyname::reToRefs1
static const std::regex reToRefs1("edm::RefVector< *(.*)< *(.*) *>, *\\2 *, *edm::refhelper::FindUsingAdvance< *\\1< *\\2 *> *, *\\2 *> *>")
edm::friendlyname::reColons
static const std::regex reColons("::")
debug
#define debug
Definition: HDRShower.cc:19
edm::friendlyname::reLong
static const std::regex reLong("long ")
edm::friendlyname::reToVector
static const std::regex reToVector("edm::AssociationVector<(.*), *(.*), *edm::Ref.*,.*>")
edm::friendlyname::reOneToOne
static const std::regex reOneToOne("edm::AssociationMap< *edm::OneToOne<(.*?),(.*?), *u[a-z]*> >")
edm::friendlyname::reTemplateArgs
static const std::regex reTemplateArgs("[^<]*<(.*)>$")
edm::friendlyname::reString3
static const std::regex reString3("std::basic_string<char,std::char_traits<char> >")
edm::friendlyname::handleNamespaces
std::string handleNamespaces(std::string const &iIn)
Definition: FriendlyName.cc:38
edm::friendlyname::reAIKR
static const std::regex reAIKR(", *edm::helper::AssociationIdenticalKeyReference")
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::friendlyname::reUniquePtr
static const std::regex reUniquePtr("^std::unique_ptr")
edm::friendlyname::reUniquePtrDeleter
static const std::regex reUniquePtrDeleter("^std::unique_ptr< *(.*), *std::default_delete<\\1> *>")
edm::friendlyname::reSorted
static const std::regex reSorted("edm::SortedCollection<(.*), *edm::StrictWeakOrdering<\\1 *> >")
edm::friendlyname::friendlyName
std::string friendlyName(std::string const &iFullName)
Definition: FriendlyName.cc:233
edm::friendlyname::reUnsigned
static const std::regex reUnsigned("unsigned ")
edm::friendlyname::reComma
static const std::regex reComma(",")
edm::friendlyname::reToRefsAssoc
static const std::regex reToRefsAssoc("edm::RefVector< *Association(.*) *, *edm::helper(.*), *Association(.*)::Find>")
edm::friendlyname::reOneToMany
static const std::regex reOneToMany("edm::AssociationMap< *edm::OneToMany<(.*?),(.*?), *u[a-z]*> >")
edm::friendlyname::handleTemplateArguments
std::string handleTemplateArguments(std::string const &)
Definition: FriendlyName.cc:162
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
edm::friendlyname::reEndSpace
static const std::regex reEndSpace(" +$")
edm::friendlyname::reULongLong
static const std::regex reULongLong("ULong64_t")
edm::friendlyname::reOwnVector
static const std::regex reOwnVector("edm::OwnVector<(.*), *edm::ClonePolicy<\\1 *> >")
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::friendlyname::standardRenames
std::string standardRenames(std::string const &iIn)
Definition: FriendlyName.cc:83
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
or
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
edm::friendlyname::reCXX11
static const std::regex reCXX11("std::__cxx11::")
mps_fire.result
result
Definition: mps_fire.py:303
edm::friendlyname::reAllSpaces
static const std::regex reAllSpaces(" +")
edm::friendlyname::reBeginSpace
static const std::regex reBeginSpace("^ +")
edm::friendlyname::reclangabi
static const std::regex reclangabi("std::__1::")
edm::friendlyname::reVector
static const std::regex reVector("std::vector")
edm::friendlyname::reOneToManyWithQuality
static const std::regex reOneToManyWithQuality("edm::AssociationMap<edm::OneToManyWithQuality<(.*?), *(.*?), *(.*?), *u[a-z]*> >")
edm::friendlyname::reSharedPtr
static const std::regex reSharedPtr("std::shared_ptr")
edm::friendlyname::reString2
static const std::regex reString2("std::string")
begin
#define begin
Definition: vmac.h:32
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
ZMuMuAnalysisNtupler_cff.prefix
prefix
Definition: ZMuMuAnalysisNtupler_cff.py:14