ROSE 0.11.145.147
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
17namespace Rosebud {
18
20enum class Access {
21 PRIVATE,
22 PROTECTED,
23 PUBLIC,
24 DEFAULT
25};
26
28struct Settings {
29 std::string backend = "yaml";
30 std::vector<std::string> serializers;
31 bool showingWarnings = true;
32 bool showingLocations = true;
33 bool debugging = false;
35};
36
38extern Settings settings;
39
41// String utilities
43
49std::string matching(const std::string&);
50char matching(char);
58std::vector<std::string> splitIntoLines(const std::string&);
59
61void eraseBlankLines(std::vector<std::string>&);
62
69void trimBlankLines(std::vector<std::string>&);
70
74std::string prefixLines(const std::string &s, const std::string &prefix);
75void prefixLines(std::vector<std::string> &lines, const std::string &prefix);
79size_t editDistance(const std::string &src, const std::string &tgt);
80
84double relativeDifference(const std::string &src, const std::string &tgt);
85
89std::string bestMatch(const std::vector<std::string> &candidates, const std::string &sample);
90
92std::string toString(Access);
93
95enum class CamelCase {
96 LOWER,
97 UPPER,
99};
100
103std::string camelCase(const std::string&, CamelCase = CamelCase::LOWER);
104
106std::string pascalCase(const std::string&);
107
111std::string cEscape(char ch, char context = '\'');
112std::string cEscape(const std::string&, char context = '"');
116std::string bourneEscape(const std::string&);
117
119std::string withLeadSpace(const std::string&);
120
122// C++ code generation
124
127
129// Filesystem utilities
131
137boost::filesystem::path findRoseRootDir(const boost::filesystem::path&);
138
143boost::filesystem::path relativeToRoseSource(const boost::filesystem::path&);
144
150boost::filesystem::path toPath(const std::string &symbol, const std::string &extension);
151
153// Comment utilities
155
165std::string makeBlockComment(const std::string &text, const std::string &opening);
166std::vector<std::string> makeBlockComment(const std::vector<std::string> &textLines, const std::string &opening);
174std::string makeTitleComment(const std::string &multiLine, const std::string &prefix, char bar, size_t width);
175std::vector<std::string> makeTitleComment(const std::vector<std::string> &lines, const std::string &prefix, char bar, size_t width);
183std::string appendToDoxygen(const std::string &existingComment, const std::string &newText);
184
186// Diagnostic messages
188
190extern size_t nErrors;
191
194
198std::string messageString(Sawyer::Message::Importance, const std::string &mesg);
199
214void message(Sawyer::Message::Importance, const Ast::FilePtr&, const Token&, const std::string &mesg);
215void message(Sawyer::Message::Importance, const Ast::FilePtr&, const Token &begin, const Token &focus,
216 const Token &end, const std::string &mesg);
217void message(Sawyer::Message::Importance, const Ast::FilePtr&, const std::vector<Token>&, const std::string &mesg);
218void message(Sawyer::Message::Importance, const std::string &mesg);
219void message(Sawyer::Message::Importance, const Ast::FilePtr&, const std::string &mesg);
223// Class hierarchy utilities
225
229 std::string s;
230
232 HierarchyKey(const Ast::ClassPtr &c) /*implicit*/
233 : s(c->name) {}
234
236 HierarchyKey(const std::string &s) /*implicit*/
237 : s(s) {}
238
240 bool operator<(const HierarchyKey &other) const {
241 return s < other.s;
242 }
243};
244
249
251using Classes = std::vector<Ast::ClassPtr>;
252
257
260
263
266
269
272
274std::vector<Ast::PropertyPtr> allConstructorArguments(const Ast::ClassPtr&, const Hierarchy&);
275
278
280// Type utilities
282
292std::string constRef(const std::string &type);
293
295std::string removeVolatileMutable(const std::string &type);
296
298// C preprocessor utilities
300
307std::string locationDirective(size_t line, const std::string &file);
308std::string locationDirective(const Ast::NodePtr&, const Token&);
315std::string toCppSymbol(const std::string&);
316
321std::vector<std::string> extractCpp(std::string&, const std::regex&, size_t capture);
322
323} // namespace
324#endif
Graph containing user-defined vertices and edges.
Definition Graph.h:634
std::shared_ptr< Class > ClassPtr
Shared-ownership pointer to a Class.
std::shared_ptr< Node > NodePtr
Shared-ownership pointer to a Node.
std::shared_ptr< File > FilePtr
Shared-ownership pointer to a File.
Rosebud is a tool to generate abstract syntax trees.
Definition Ast.h:14
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::vector< Ast::ClassPtr > Classes
Ordered sequence of classes.
Definition ud/Utility.h:251
size_t nErrors
Number of error messages reported.
std::string bourneEscape(const std::string &)
Quote string to make it shell safe.
std::string firstPublicBaseClass(const Ast::ClassPtr &)
Name of first public base class.
Classes bottomUp(Hierarchy &)
Return all the class definitions so that derived classes appear before base classes.
std::string camelCase(const std::string &, CamelCase=CamelCase::LOWER)
Convert snake case to camelCase.
size_t editDistance(const std::string &src, const std::string &tgt)
Compute the Damerau-Levenshtein edit distance between two strings.
std::string cEscape(char ch, char context='\'')
Escape as if in C single or double quotes.
std::string withLeadSpace(const std::string &)
String with one leading space.
bool isBaseClass(const Ast::ClassPtr &, const Hierarchy &)
True if the class is a base class of some other class.
CamelCase
What to do with the first letter of the return value.
Definition ud/Utility.h:95
@ UNCHANGED
Leave the first character as it is in the input.
@ UPPER
Make the first character upper case.
@ LOWER
Make the first character lower case.
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.
Access
Kinds of access.
Definition ud/Utility.h:20
@ PROTECTED
Like C++ protected access.
@ DEFAULT
Use whatever access is already present.
@ PRIVATE
Like C++ private access.
@ PUBLIC
Like C++ public access.
void eraseBlankLines(std::vector< std::string > &)
Remove lines that are empty or contain only white space.
void checkClassHierarchy(Hierarchy &)
Check for problems such as cycles in the class hiearchy and report them as errors.
std::string matching(const std::string &)
Return the matching opening or closing construct.
std::string appendToDoxygen(const std::string &existingComment, const std::string &newText)
Append text to a Doxygen comment.
When
When something should be done.
@ AUTO
Sometimes do it.
void message(Sawyer::Message::Importance, const Ast::FilePtr &, const Token &, const std::string &mesg)
Print a diagnostic message to standard error.
std::string locationDirective(size_t line, const std::string &file)
Input location information.
std::string removeVolatileMutable(const std::string &type)
Rmove "volatile" and "mutable" from the beginning of a type string.
std::string toString(Access)
Convert an access enum to a C++ string.
std::vector< std::string > extractCpp(std::string &, const std::regex &, size_t capture)
Extract all matching C preprocessor directives from the text.
boost::filesystem::path relativeToRoseSource(const boost::filesystem::path &)
Convert a file path to a ROSE-relative path.
std::string pascalCase(const std::string &)
Convert a symbol to PascalCase.
std::vector< Ast::PropertyPtr > allConstructorArguments(const Ast::ClassPtr &, const Hierarchy &)
Properties that form constructor arguments.
double relativeDifference(const std::string &src, const std::string &tgt)
Compute the relative difference between two strings.
std::string bestMatch(const std::vector< std::string > &candidates, const std::string &sample)
Returns the best match.
bool usingColor()
True if we're using color output for diagnostics.
void trimBlankLines(std::vector< std::string > &)
Trim leading, trailing, and internal blank lines and trailing white space.
Classes topDown(Hierarchy &)
Return all the class definitions so that base classes are before derived classes.
Classes derivedClasses(const Ast::ClassPtr &, const Hierarchy &)
Return all known subclasses.
boost::filesystem::path findRoseRootDir(const boost::filesystem::path &)
Find the root of the ROSE source tree.
boost::filesystem::path toPath(const std::string &symbol, const std::string &extension)
Convert a qualified C++ name to a relative path.
std::string constRef(const std::string &type)
Turn a type into a const reference to the type.
std::string messageString(Sawyer::Message::Importance, const std::string &mesg)
Convert an importance level and message to an error output string.
std::string accessSpecifier(Access)
Return an access specifier with colon and trailing linefeed.
std::string prefixLines(const std::string &s, const std::string &prefix)
Add a prefix to every line.
std::string toCppSymbol(const std::string &)
Convert a C++ qualified name to a CPP symbol.
Settings settings
Command-line settings for the rosebud tool.
std::string makeBlockComment(const std::string &text, const std::string &opening)
Make a block comment.
Importance
Level of importance for a message.
Definition Message.h:313
Key for ordering classes in the class hierarchy.
Definition ud/Utility.h:227
bool operator<(const HierarchyKey &other) const
Compare keys.
Definition ud/Utility.h:240
std::string s
Class name is the key.
Definition ud/Utility.h:229
HierarchyKey(const std::string &s)
Construct key from name.
Definition ud/Utility.h:236
HierarchyKey(const Ast::ClassPtr &c)
Construct key from class.
Definition ud/Utility.h:232
Command-line settings for the rosebud tool.
Definition ud/Utility.h:28
std::vector< std::string > serializers
Name of the serializer code generator to use.
Definition ud/Utility.h:30
bool debugging
Generate additional debugging output.
Definition ud/Utility.h:33
std::string backend
Name of main backend code generator to use.
Definition ud/Utility.h:29
When usingColor
Use ANSI color escapes in the diagnostic output.
Definition ud/Utility.h:34
bool showingLocations
Output should show source location from whence it came.
Definition ud/Utility.h:32
bool showingWarnings
Show warnings about the input.
Definition ud/Utility.h:31