ROSE  0.11.145.0
ud/Utility.h
1 #ifndef Rosebud_Utility_H
2 #define Rosebud_Utility_H
3 #include <Rosebud/Ast.h>
4 
5 #include <Sawyer/Graph.h>
6 #include <Sawyer/Message.h>
7 #include <Sawyer/Optional.h>
8 
9 #include <boost/filesystem.hpp>
10 
11 #include <regex>
12 #include <string>
13 #include <vector>
14 
15 #define THIS_LOCATION locationDirective(__LINE__, __FILE__)
16 
17 namespace Rosebud {
18 
20 enum class Access {
21  PRIVATE,
22  PROTECTED,
23  PUBLIC
24 };
25 
27 struct Settings {
28  std::string backend = "yaml";
29  std::string serializer = "boost";
30  bool showingWarnings = true;
31  bool showingLocations = true;
32  bool debugging = false;
34 };
35 
37 extern Settings settings;
38 
40 // String utilities
42 
48 std::string matching(const std::string&);
49 char matching(char);
57 std::vector<std::string> splitIntoLines(const std::string&);
58 
60 void eraseBlankLines(std::vector<std::string>&);
61 
68 void trimBlankLines(std::vector<std::string>&);
69 
73 std::string prefixLines(const std::string &s, const std::string &prefix);
74 void prefixLines(std::vector<std::string> &lines, const std::string &prefix);
78 size_t editDistance(const std::string &src, const std::string &tgt);
79 
83 double relativeDifference(const std::string &src, const std::string &tgt);
84 
88 std::string bestMatch(const std::vector<std::string> &candidates, const std::string &sample);
89 
91 std::string toString(Access);
92 
94 // Filesystem utilities
96 
102 boost::filesystem::path findRoseRootDir(const boost::filesystem::path&);
103 
108 boost::filesystem::path relativeToRoseSource(const boost::filesystem::path&);
109 
115 boost::filesystem::path toPath(const std::string &symbol, const std::string &extension);
116 
118 // Comment utilities
120 
130 std::string makeBlockComment(const std::string &text, const std::string &opening);
131 std::vector<std::string> makeBlockComment(const std::vector<std::string> &textLines, const std::string &opening);
139 std::string makeTitleComment(const std::string &multiLine, const std::string &prefix, char bar, size_t width);
140 std::vector<std::string> makeTitleComment(const std::vector<std::string> &lines, const std::string &prefix, char bar, size_t width);
148 std::string appendToDoxygen(const std::string &existingComment, const std::string &newText);
149 
151 // Diagnostic messages
153 
155 extern size_t nErrors;
156 
158 bool usingColor();
159 
163 std::string messageString(Sawyer::Message::Importance, const std::string &mesg);
164 
179 void message(Sawyer::Message::Importance, const Ast::FilePtr&, const Token&, const std::string &mesg);
180 void message(Sawyer::Message::Importance, const Ast::FilePtr&, const Token &begin, const Token &focus,
181  const Token &end, const std::string &mesg);
182 void message(Sawyer::Message::Importance, const Ast::FilePtr&, const std::vector<Token>&, const std::string &mesg);
183 void message(Sawyer::Message::Importance, const std::string &mesg);
184 void message(Sawyer::Message::Importance, const Ast::FilePtr&, const std::string &mesg);
187 // Class hierarchy utilities
190 
192 struct HierarchyKey {
194  std::string s;
195 
197  HierarchyKey(const Ast::ClassPtr &c) /*implicit*/
198  : s(c->name) {}
199 
201  HierarchyKey(const std::string &s) /*implicit*/
202  : s(s) {}
203 
205  bool operator<(const HierarchyKey &other) const {
206  return s < other.s;
207  }
208 };
209 
214 
216 using Classes = std::vector<Ast::ClassPtr>;
217 
222 
225 
228 
231 
234 
236 bool isBaseClass(const Ast::ClassPtr&, const Hierarchy&);
237 
239 std::vector<Ast::PropertyPtr> allConstructorArguments(const Ast::ClassPtr&, const Hierarchy&);
240 
242 std::string firstPublicBaseClass(const Ast::ClassPtr&);
243 
245 // Type utilities
247 
257 std::string constRef(const std::string &type);
258 
260 std::string removeVolatileMutable(const std::string &type);
261 
263 // C preprocessor utilities
265 
272 std::string locationDirective(size_t line, const std::string &file);
273 std::string locationDirective(const Ast::NodePtr&, const Token&);
279 std::string toCppSymbol(const std::string&);
280 
285 std::vector<std::string> extractCpp(std::string&, const std::regex&, size_t capture);
286 
287 } // namespace
288 #endif
std::string s
Class name is the key.
Definition: ud/Utility.h:194
Like C++ protected access.
double relativeDifference(const std::string &src, const std::string &tgt)
Compute the relative difference between two strings.
Graph containing user-defined vertices and edges.
Definition: Graph.h:625
std::vector< std::string > splitIntoLines(const std::string &)
Split a multi-line string into one string per line.
Hierarchy classHierarchy(const Classes &)
Generate the class hierarchy from the specified class definitions.
std::string removeVolatileMutable(const std::string &type)
Rmove "volatile" and "mutable" from the beginning of a type string.
Classes bottomUp(Hierarchy &)
Return all the class definitions so that derived classes appear before base classes.
std::string appendToDoxygen(const std::string &existingComment, const std::string &newText)
Append text to a Doxygen comment.
HierarchyKey(const std::string &s)
Construct key from name.
Definition: ud/Utility.h:201
Command-line settings for the rosebud tool.
Definition: ud/Utility.h:27
bool showingLocations
Output should show source location from whence it came.
Definition: ud/Utility.h:31
Sometimes do it.
Like C++ public access.
Rosebud is a tool to generate code for ROSE.
Definition: Ast.h:14
std::string makeBlockComment(const std::string &text, const std::string &opening)
Make a block comment.
Like C++ private access.
void eraseBlankLines(std::vector< std::string > &)
Remove lines that are empty or contain only white space.
Classes topDown(Hierarchy &)
Return all the class definitions so that base classes are before derived classes. ...
Settings settings
Command-line settings for the rosebud tool.
boost::filesystem::path toPath(const std::string &symbol, const std::string &extension)
Convert a qualified C++ name to a relative path.
std::string backend
Name of main backend code generator to use.
Definition: ud/Utility.h:28
std::vector< Ast::ClassPtr > Classes
Ordered sequence of classes.
Definition: ud/Utility.h:216
bool operator<(const HierarchyKey &other) const
Compare keys.
Definition: ud/Utility.h:205
When
When something should be done.
Definition: ud/BasicTypes.h:23
void message(Sawyer::Message::Importance, const Ast::FilePtr &, const Token &, const std::string &mesg)
Print a diagnostic message to standard error.
std::string bestMatch(const std::vector< std::string > &candidates, const std::string &sample)
Returns the best match.
bool debugging
Generate additional debugging output.
Definition: ud/Utility.h:32
Access
Kinds of access.
Definition: ud/Utility.h:20
std::string locationDirective(size_t line, const std::string &file)
Input location information.
bool usingColor()
True if we're using color output for diagnostics.
std::string makeTitleComment(const std::string &multiLine, const std::string &prefix, char bar, size_t width)
Make a title comment that spans the entire width of the source code.
size_t nErrors
Number of error messages reported.
size_t editDistance(const std::string &src, const std::string &tgt)
Compute the Damerau-Levenshtein edit distance between two strings.
Classes derivedClasses(const Ast::ClassPtr &, const Hierarchy &)
Return all known subclasses.
void checkClassHierarchy(Hierarchy &)
Check for problems such as cycles in the class hiearchy and report them as errors.
boost::filesystem::path findRoseRootDir(const boost::filesystem::path &)
Find the root of the ROSE source tree.
std::string prefixLines(const std::string &s, const std::string &prefix)
Add a prefix to every line.
void trimBlankLines(std::vector< std::string > &)
Trim leading, trailing, and internal blank lines and trailing white space.
std::string firstPublicBaseClass(const Ast::ClassPtr &)
Name of first public base class.
Importance
Level of importance for a message.
Definition: Message.h:313
Key for ordering classes in the class hierarchy.
Definition: ud/Utility.h:192
std::string constRef(const std::string &type)
Turn a type into a const reference to the type.
std::shared_ptr< File > FilePtr
Shared-ownership pointer to a File.
Definition: ud/BasicTypes.h:53
std::shared_ptr< Class > ClassPtr
Shared-ownership pointer to a Class.
Definition: ud/BasicTypes.h:51
HierarchyKey(const Ast::ClassPtr &c)
Construct key from class.
Definition: ud/Utility.h:197
std::vector< std::string > extractCpp(std::string &, const std::regex &, size_t capture)
Extract all matching C preprocessor directives from the text.
bool isBaseClass(const Ast::ClassPtr &, const Hierarchy &)
True if the class is a base class of some other class.
std::string matching(const std::string &)
Return the matching opening or closing construct.
std::string toCppSymbol(const std::string &)
Convert a C++ qualified name to a CPP symbol.
bool showingWarnings
Show warnings about the input.
Definition: ud/Utility.h:30
std::shared_ptr< Node > NodePtr
Shared-ownership pointer to a Node.
Definition: ud/BasicTypes.h:37
When usingColor
Use ANSI color escapes in the diagnostic output.
Definition: ud/Utility.h:33
boost::filesystem::path relativeToRoseSource(const boost::filesystem::path &)
Convert a file path to a ROSE-relative path.
std::string toString(Access)
Convert an access enum to a C++ string.
std::vector< Ast::PropertyPtr > allConstructorArguments(const Ast::ClassPtr &, const Hierarchy &)
Properties that form constructor arguments.
std::string serializer
Name of the serializer code generator to use.
Definition: ud/Utility.h:29
std::string messageString(Sawyer::Message::Importance, const std::string &mesg)
Convert an importance level and message to an error output string.