Skip to content

Commit

Permalink
Add std aliases to library
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 15, 2024
1 parent 17bcbec commit eb617fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
24 changes: 12 additions & 12 deletions cfg/std.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8588,22 +8588,22 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<returnValue type="void *"/>
</function>
<memory>
<alloc init="false" buffer-size="malloc">malloc</alloc>
<alloc init="true" buffer-size="calloc">calloc</alloc>
<alloc init="false" buffer-size="malloc:2">aligned_alloc</alloc>
<realloc init="false" buffer-size="malloc:2">realloc</realloc>
<realloc init="false" buffer-size="calloc:2,3">reallocarray</realloc>
<dealloc>free</dealloc>
<alloc init="false" buffer-size="malloc">malloc,std::malloc</alloc>
<alloc init="true" buffer-size="calloc">calloc,std::calloc</alloc>
<alloc init="false" buffer-size="malloc:2">aligned_alloc,std::aligned_alloc</alloc>
<realloc init="false" buffer-size="malloc:2">realloc,std::realloc</realloc>
<realloc init="false" buffer-size="calloc:2,3">reallocarray,std::reallocarray</realloc>
<dealloc>free,std::free</dealloc>
</memory>
<memory>
<alloc init="true" buffer-size="strdup">strdup</alloc>
<dealloc>free</dealloc>
<alloc init="true" buffer-size="strdup">strdup,std::strdup</alloc>
<dealloc>free,std::free</dealloc>
</memory>
<resource>
<alloc init="true">fopen</alloc>
<alloc init="true">tmpfile</alloc>
<realloc init="true" realloc-arg="3">freopen</realloc>
<dealloc>fclose</dealloc>
<alloc init="true">fopen,std::fopen</alloc>
<alloc init="true">tmpfile,std::tmpfile</alloc>
<realloc init="true" realloc-arg="3">freopen,std::freopen</realloc>
<dealloc>fclose,std::fclose</dealloc>
</resource>
<container id="stdContainer" endPattern="&gt; !!::" opLessAllowed="false" itEndPattern="&gt; :: iterator|const_iterator|reverse_iterator|const_reverse_iterator" hasInitializerListConstructor="true">
<type templateParameter="0"/>
Expand Down
38 changes: 18 additions & 20 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
int allocationId = 0;
for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) {
if (strcmp(memorynode->Name(),"dealloc")==0) {
const std::map<std::string, AllocFunc>::const_iterator it = mDealloc.find(memorynode->GetText());
if (it != mDealloc.end()) {
allocationId = it->second.groupId;
break;
const auto names = getnames(memorynode->GetText());
for (const auto& n : names) {
const std::map<std::string, AllocFunc>::const_iterator it = mDealloc.find(n);
if (it != mDealloc.end())
allocationId = it->second.groupId;
}
break;
}
}
if (allocationId == 0) {
Expand All @@ -234,7 +236,8 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
// add alloc/dealloc/use functions..
for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) {
const std::string memorynodename = memorynode->Name();
if (memorynodename == "alloc" || memorynodename == "realloc") {
const auto names = getnames(memorynode->GetText());
if (memorynodename == "alloc" || memorynodename == "realloc") {
AllocFunc temp = {0};
temp.groupId = allocationId;

Expand Down Expand Up @@ -268,17 +271,18 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
if (memorynodename == "realloc")
temp.reallocArg = memorynode->IntAttribute("realloc-arg", 1);

if (memorynodename != "realloc")
mAlloc[memorynode->GetText()] = temp;
else
mRealloc[memorynode->GetText()] = temp;
auto& map = (memorynodename == "realloc") ? mRealloc : mAlloc;
for (const auto& n : names)
map[n] = temp;
} else if (memorynodename == "dealloc") {
AllocFunc temp = {0};
temp.groupId = allocationId;
temp.arg = memorynode->IntAttribute("arg", 1);
mDealloc[memorynode->GetText()] = temp;
for (const auto& n : names)
mDealloc[n] = temp;
} else if (memorynodename == "use")
functions[memorynode->GetText()].use = true;
for (const auto& n : names)
functions[n].use = true;
else
unknown_elements.insert(memorynodename);
}
Expand Down Expand Up @@ -1060,27 +1064,21 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h
/** get allocation info for function */
const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const
{
std::string funcname = getFunctionName(tok);
if (tok->isCpp() && startsWith(funcname, "std::"))
funcname.erase(0, 5);
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mAlloc, funcname);
}

/** get deallocation info for function */
const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const
{
std::string funcname = getFunctionName(tok);
if (tok->isCpp() && startsWith(funcname, "std::"))
funcname.erase(0, 5);
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mDealloc, funcname);
}

/** get reallocation info for function */
const Library::AllocFunc* Library::getReallocFuncInfo(const Token *tok) const
{
std::string funcname = getFunctionName(tok);
if (tok->isCpp() && startsWith(funcname, "std::"))
funcname.erase(0, 5);
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mRealloc, funcname);
}

Expand Down

0 comments on commit eb617fa

Please sign in to comment.