From 2f1ba895672bafe8c95da1ba2da391f0b0003fca Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 12 Dec 2014 22:16:20 +0100 Subject: [PATCH] Improve readability of Library::load() by caching often used node names. Also constify existing name caches. --- lib/library.cpp | 58 ++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/library.cpp b/lib/library.cpp index d0381bf76..43624b17f 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -117,7 +117,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) return Error(UNSUPPORTED_FORMAT); for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { - std::string nodename = node->Name(); + const std::string nodename = node->Name(); if (nodename == "memory" || nodename == "resource") { // get allocationId to use.. int allocationId = 0; @@ -131,7 +131,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) } } if (allocationId == 0) { - if (strcmp(node->Name(), "memory")==0) + if (nodename == "memory") while (!ismemory(++allocid)); else while (!isresource(++allocid)); @@ -140,18 +140,19 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) // add alloc/dealloc/use functions.. for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) { - if (strcmp(memorynode->Name(),"alloc")==0) { + const std::string memorynodename = memorynode->Name(); + if (memorynodename == "alloc") { _alloc[memorynode->GetText()] = allocationId; const char *init = memorynode->Attribute("init"); if (init && strcmp(init,"false")==0) { returnuninitdata.insert(memorynode->GetText()); } - } else if (strcmp(memorynode->Name(),"dealloc")==0) + } else if (memorynodename == "dealloc") _dealloc[memorynode->GetText()] = allocationId; - else if (strcmp(memorynode->Name(),"use")==0) + else if (memorynodename == "use") use.insert(memorynode->GetText()); else - return Error(BAD_ELEMENT, memorynode->Name()); + return Error(BAD_ELEMENT, memorynodename); } } @@ -176,7 +177,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) std::string name = name_char; for (const tinyxml2::XMLElement *functionnode = node->FirstChildElement(); functionnode; functionnode = functionnode->NextSiblingElement()) { - std::string functionnodename = functionnode->Name(); + const std::string functionnodename = functionnode->Name(); if (functionnodename == "noreturn") _noreturn[name] = (strcmp(functionnode->GetText(), "true") == 0); else if (functionnodename == "pure") @@ -199,7 +200,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) std::string& valid = argumentChecks[name][nr].valid; std::list& minsizes = argumentChecks[name][nr].minsizes; for (const tinyxml2::XMLElement *argnode = functionnode->FirstChildElement(); argnode; argnode = argnode->NextSiblingElement()) { - std::string argnodename = argnode->Name(); + const std::string argnodename = argnode->Name(); if (argnodename == "not-bool") notbool = true; else if (argnodename == "not-null") @@ -271,7 +272,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) } else - return Error(BAD_ATTRIBUTE, argnode->Name()); + return Error(BAD_ATTRIBUTE, argnodename); } argumentChecks[name][nr].notbool = notbool; argumentChecks[name][nr].notnull = notnull; @@ -285,7 +286,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) const tinyxml2::XMLAttribute* secure = functionnode->FindAttribute("secure"); _formatstr[name] = std::make_pair(scan && scan->BoolValue(), secure && secure->BoolValue()); } else - return Error(BAD_ELEMENT, functionnode->Name()); + return Error(BAD_ELEMENT, functionnodename); } } @@ -314,7 +315,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) _processAfterCode[extension] = (aftercode && strcmp(aftercode, "true") == 0); for (const tinyxml2::XMLElement *markupnode = node->FirstChildElement(); markupnode; markupnode = markupnode->NextSiblingElement()) { - std::string markupnodename = markupnode->Name(); + const std::string markupnodename = markupnode->Name(); if (markupnodename == "keywords") { for (const tinyxml2::XMLElement *librarynode = markupnode->FirstChildElement(); librarynode; librarynode = librarynode->NextSiblingElement()) { if (strcmp(librarynode->Name(), "keyword") == 0) { @@ -337,12 +338,13 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) return Error(MISSING_ATTRIBUTE, "prefix"); for (const tinyxml2::XMLElement *e = exporter->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (strcmp(e->Name(), "prefix") == 0) + const std::string ename = e->Name(); + if (ename == "prefix") _exporters[prefix].addPrefix(e->GetText()); - else if (strcmp(e->Name(), "suffix") == 0) + else if (ename == "suffix") _exporters[prefix].addSuffix(e->GetText()); else - return Error(BAD_ELEMENT, e->Name()); + return Error(BAD_ELEMENT, ename); } } } @@ -358,11 +360,12 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) else if (markupnodename == "codeblocks") { for (const tinyxml2::XMLElement *blocknode = markupnode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) { - if (strcmp(blocknode->Name(), "block") == 0) { + const std::string blocknodename = blocknode->Name(); + if (blocknodename == "block") { const char * blockName = blocknode->Attribute("name"); if (blockName) _executableblocks[extension].addBlock(blockName); - } else if (strcmp(blocknode->Name(), "structure") == 0) { + } else if (blocknodename == "structure") { const char * start = blocknode->Attribute("start"); if (start) _executableblocks[extension].setStart(start); @@ -375,12 +378,12 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) } else - return Error(BAD_ELEMENT, blocknode->Name()); + return Error(BAD_ELEMENT, blocknodename); } } else - return Error(BAD_ELEMENT, markupnode->Name()); + return Error(BAD_ELEMENT, markupnodename); } } @@ -409,25 +412,26 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) type._type = value; std::set platform; for (const tinyxml2::XMLElement *typenode = node->FirstChildElement(); typenode; typenode = typenode->NextSiblingElement()) { - if (strcmp(typenode->Name(), "platform") == 0) { + const std::string typenodename = typenode->Name(); + if (typenodename == "platform") { const char * const type_attribute = typenode->Attribute("type"); if (type_attribute == nullptr) return Error(MISSING_ATTRIBUTE, "type"); platform.insert(type_attribute); - } else if (strcmp(typenode->Name(), "signed") == 0) + } else if (typenodename == "signed") type._signed = true; - else if (strcmp(typenode->Name(), "unsigned") == 0) + else if (typenodename == "unsigned") type._unsigned = true; - else if (strcmp(typenode->Name(), "long") == 0) + else if (typenodename == "long") type._long = true; - else if (strcmp(typenode->Name(), "pointer") == 0) + else if (typenodename == "pointer") type._pointer= true; - else if (strcmp(typenode->Name(), "ptr_ptr") == 0) + else if (typenodename == "ptr_ptr") type._ptr_ptr = true; - else if (strcmp(typenode->Name(), "const_ptr") == 0) + else if (typenodename == "const_ptr") type._const_ptr = true; else - return Error(BAD_ELEMENT, typenode->Name()); + return Error(BAD_ELEMENT, typenodename); } if (platform.empty()) { const PlatformType * const type_ptr = platform_type(type_name, ""); @@ -452,7 +456,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) } else - return Error(BAD_ELEMENT, node->Name()); + return Error(BAD_ELEMENT, nodename); } return Error(OK); }