From 05b4d09b2bbb6350bda33f94f35d88545a67bb8c Mon Sep 17 00:00:00 2001 From: Derek Bankieris Date: Thu, 18 May 2017 08:42:40 -0500 Subject: [PATCH] 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 --- .../Interface_Code_Gen/FieldVisitor.cpp | 18 +----------------- .../Interface_Code_Gen/PrintFileContents10.cpp | 4 ++-- .../codegen/Interface_Code_Gen/Utilities.cpp | 8 ++++++++ .../codegen/Interface_Code_Gen/Utilities.hh | 1 + 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp b/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp index de315d8b..1223114b 100644 --- a/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp +++ b/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp @@ -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() + "_" + diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp index d2192a67..4ba5d12a 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp @@ -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 diff --git a/trick_source/codegen/Interface_Code_Gen/Utilities.cpp b/trick_source/codegen/Interface_Code_Gen/Utilities.cpp index 1a68baa8..bd9d2e0c 100644 --- a/trick_source/codegen/Interface_Code_Gen/Utilities.cpp +++ b/trick_source/codegen/Interface_Code_Gen/Utilities.cpp @@ -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); diff --git a/trick_source/codegen/Interface_Code_Gen/Utilities.hh b/trick_source/codegen/Interface_Code_Gen/Utilities.hh index d1772a0f..6c05cc04 100644 --- a/trick_source/codegen/Interface_Code_Gen/Utilities.hh +++ b/trick_source/codegen/Interface_Code_Gen/Utilities.hh @@ -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 ) ;