Sanitize field names when generating STL functions

A sanitizing function was already present in FieldVisitor.cpp. I
refactored and moved it to Utilities.
Refs #429
This commit is contained in:
Derek Bankieris 2017-05-18 08:42:40 -05:00
parent 34c62c5aab
commit 05b4d09b2b
4 changed files with 12 additions and 19 deletions

View File

@ -252,22 +252,6 @@ bool FieldVisitor::VisitPointerType(clang::PointerType *p) {
return true;
}
static std::string mangle_string( std::string in_name ) {
// convert characters not valid in a function name to underscores
std::string mangled_name = in_name ;
// Create a mangled type name, some characters have to converted to underscores.
std::replace( mangled_name.begin(), mangled_name.end(), '<', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), '>', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), ' ', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), ',', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), ':', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), '*', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), ']', '_') ;
std::replace( mangled_name.begin(), mangled_name.end(), '[', '_') ;
return mangled_name ;
}
std::map < std::string , std::string > FieldVisitor::processed_templates ;
bool FieldVisitor::ProcessTemplate(std::string in_name , clang::CXXRecordDecl * crd ) {
@ -284,7 +268,7 @@ bool FieldVisitor::ProcessTemplate(std::string in_name , clang::CXXRecordDecl *
// Check to see if we've processed this template before
// If not we need to create attributes for this template
if ( processed_templates.find(in_name) == processed_templates.end() ) {
std::string mangled_name = mangle_string(in_name) ;
std::string mangled_name = sanitize(in_name) ;
// save off the mangled name of this template to be used if another variable is the same template type
processed_templates[in_name] = fdes->getContainerClass() + "_" +

View File

@ -160,7 +160,7 @@ void PrintFileContents10::print_field_init_attr_stmts( std::ostream & ostream ,
if ( fdes->isSTL() ) {
auto print = [&](const std::string& field) {
ostream << prefix << field << " = " << field << "_" << fullyQualifiedMangledClassNameUnderscores + "_" + fieldName + " ;\n";
ostream << prefix << field << " = " << field << "_" << fullyQualifiedMangledClassNameUnderscores + "_" + sanitize(fieldName) + " ;\n";
};
if ( fdes->isCheckpointable() ) {
@ -404,7 +404,7 @@ void PrintFileContents10::printEnumMapFooter( std::ostream & ostream ) {
void PrintFileContents10::printStlFunction(const std::string& name, const std::string& parameters, const std::string& call, std::ostream& ostream, FieldDescription& fieldDescription, ClassValues& classValues) {
const std::string typeName = fieldDescription.getTypeName();
const std::string functionName = name + "_stl";
ostream << "void " << functionName << "_" << classValues.getFullyQualifiedMangledTypeName("__") << "_" << fieldDescription.getName()
ostream << "void " << functionName << "_" << classValues.getFullyQualifiedMangledTypeName("__") << "_" << sanitize(fieldDescription.getName())
<< "(" << parameters << ") {" << std::endl
<< " " << typeName << "* stl = reinterpret_cast<" << typeName << "*>(start_address);" << std::endl
<< " " << call << ";" << std::endl

View File

@ -6,6 +6,14 @@
#include "Utilities.hh"
std::string sanitize(const std::string& text) {
std::string result = text;
for (char c : {'<', '>', ' ', ',', ':', '*', '[', ']'}) {
std::replace(result.begin(), result.end(), c, '_');
}
return result ;
}
// removes leading and trailing whitespace from a string
std::string trim(const std::string& str, const std::string& whitespace ) {
size_t strBegin = str.find_first_not_of(whitespace);

View File

@ -14,6 +14,7 @@ enum Color {
SKIP = 95
};
std::string sanitize(const std::string&);
std::string trim( const std::string& str, const std::string& whitespace = " \t\n\r" ) ;
bool isInUserCode( clang::CompilerInstance & ci , clang::SourceLocation sl , HeaderSearchDirs & hsd ) ;
bool isInUserOrTrickCode( clang::CompilerInstance & ci , clang::SourceLocation sl , HeaderSearchDirs & hsd ) ;