dump: refactor valueType dump

This commit is contained in:
Daniel Marjamäki 2017-04-16 09:11:20 +02:00
parent ccf078747a
commit 1045ece946
5 changed files with 129 additions and 18 deletions

View File

@ -37,6 +37,38 @@ class Directive:
self.file = element.get('file')
self.linenr = element.get('linenr')
class ValueType:
"""
ValueType class. Contains (promoted) type information for each node in the AST.
"""
type = None
sign = None
constness = 0
pointer = 0
typeScopeId = None
typeScope = None
originalTypeName = None
def __init__(self, element):
self.type = element.get('valueType-type')
self.sign = element.get('valueType-sign')
self.typeScopeId = element.get('valueType-typeScope')
self.originalTypeName = element.get('valueType-originalTypeName')
constness = element.get('valueType-constness')
if constness:
self.constness = int(constness)
else:
self.constness = 0
pointer = element.get('valueType-pointer')
if pointer:
self.pointer = int(pointer)
else:
self.pointer = 0
def setId(self, IdMap):
self.typeScope = IdMap[self.typeScopeId]
class Token:
"""
@ -181,7 +213,10 @@ class Token:
self.function = None
self.valuesId = element.get('values')
self.values = None
self.valueType = element.get('valueType')
if element.get('valueType-type'):
self.valueType = ValueType(element)
else:
self.valueType = None
self.typeScopeId = element.get('type-scope')
self.typeScope = None
self.astParentId = element.get('astParent')
@ -203,6 +238,8 @@ class Token:
self.astParent = IdMap[self.astParentId]
self.astOperand1 = IdMap[self.astOperand1Id]
self.astOperand2 = IdMap[self.astOperand2Id]
if self.valueType:
self.valueType.setId(IdMap)
def getValue(self, v):
"""

View File

@ -272,13 +272,10 @@ def misra_11_7(data):
continue
if not token.valueType or not token.astOperand1.valueType:
continue
res1 = re.match(r'^(const )?([A-Za-z_0-9]+) \*', token.valueType)
res2 = re.match(r'^(const )?([A-Za-z_0-9]+) \*', token.astOperand1.valueType)
if not res1 or not res2:
if token.valueType.pointer==0 or token.valueType.pointer==0:
continue
if res1.group(2) == res2.group(2):
continue
reportError(token, 11, 7)
if token.valueType.type == 'record' and token.valueType.type == 'record':
reportError(token, 11, 7)
def misra_11_8(data):
for token in data.tokenlist:
@ -286,11 +283,10 @@ def misra_11_8(data):
continue
if not token.valueType or not token.astOperand1.valueType:
continue
if token.valueType.startswith('const') or not token.astOperand1.valueType.startswith('const'):
if token.valueType.pointer==0 or token.valueType.pointer==0:
continue
if token.valueType.find('*')<0 or token.astOperand1.valueType.find('*')<0:
continue
reportError(token, 11, 8)
if token.valueType.constness==0 and token.astOperand1.valueType.constness>0:
reportError(token, 11, 8)
def misra_11_9(data):
for directive in data.directives:

View File

@ -5221,6 +5221,85 @@ bool ValueType::fromLibraryType(const std::string &typestr, const Settings *sett
return false;
}
std::string ValueType::dump() const
{
std::ostringstream ret;
switch (type) {
case UNKNOWN_TYPE:
return "";
case NONSTD:
ret << "valueType-type=\"nonstd\"";
break;
case RECORD:
ret << "valueType-type=\"record\"";
break;
case CONTAINER:
ret << "valueType-type=\"container\"";
break;
case ITERATOR:
ret << "valueType-type=\"iterator\"";
break;
case VOID:
ret << "valueType-type=\"void\"";
break;
case BOOL:
ret << "valueType-type=\"bool\"";
break;
case CHAR:
ret << "valueType-type=\"char\"";
break;
case SHORT:
ret << "valueType-type=\"short\"";
break;
case INT:
ret << "valueType-type=\"int\"";
break;
case LONG:
ret << "valueType-type=\"long\"";
break;
case LONGLONG:
ret << "valueType-type=\"long long\"";
break;
case UNKNOWN_INT:
ret << "valueType-type=\"unknown int\"";
break;
case FLOAT:
ret << "valueType-type=\"float\"";
break;
case DOUBLE:
ret << "valueType-type=\"double\"";
break;
case LONGDOUBLE:
ret << "valueType-type=\"long double\"";
break;
};
switch (sign) {
case Sign::UNKNOWN_SIGN:
break;
case Sign::SIGNED:
ret << " valueType-sign=\"signed\"";
break;
case Sign::UNSIGNED:
ret << " valueType-sign=\"unsigned\"";
break;
};
if (pointer > 0)
ret << " valueType-pointer=\"" << pointer << '\"';
if (constness > 0)
ret << " valueType-constness=\"" << constness << '\"';
if (typeScope)
ret << " valueType-typeScope=\"" << typeScope << '\"';
if (!originalTypeName.empty())
ret << " valueType-originalTypeName=\"" << originalTypeName << '\"';
return ret.str();
}
std::string ValueType::str() const
{
std::string ret;

View File

@ -1061,6 +1061,7 @@ public:
bool fromLibraryType(const std::string &typestr, const Settings *settings);
std::string str() const;
std::string dump() const;
};

View File

@ -3919,14 +3919,12 @@ void Tokenizer::dump(std::ostream &out) const
out << " astOperand1=\"" << tok->astOperand1() << '\"';
if (tok->astOperand2())
out << " astOperand2=\"" << tok->astOperand2() << '\"';
if (!tok->originalName().empty())
out << " originalName=\"" << tok->originalName() << '\"';
if (tok->valueType()) {
std::string s = tok->valueType()->str();
std::string::size_type pos = s.find("container(");
if (pos != std::string::npos)
s.erase(pos+9, s.find(")",pos)-pos-8);
if ((pos = s.find("iterator(")) != std::string::npos)
s.erase(pos+8, s.find(")",pos)-pos-7);
out << " valueType=\"" << s << '\"';
const std::string vt = tok->valueType()->dump();
if (!vt.empty())
out << ' ' << vt;
}
out << "/>" << std::endl;
}