Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix #2888 (Allow defining sizes of base types)
  • Loading branch information
IOBYTE committed Sep 17, 2011
1 parent 9e8a3f3 commit b5d22fd
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 23 deletions.
32 changes: 32 additions & 0 deletions cli/cmdlineparser.cpp
Expand Up @@ -591,6 +591,29 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->checkConfiguration = true;
}

// Specify platform
else if (strncmp(argv[i], "--platform=", 11) == 0)
{
std::string platform(11+argv[i]);

if (platform == "win32")
_settings->platform(Settings::Win32);
else if (platform == "win64")
_settings->platform(Settings::Win64);
else if (platform == "unix32")
_settings->platform(Settings::Unix32);
else if (platform == "unix64")
_settings->platform(Settings::Unix64);
else
{
std::string message("cppcheck: error: unrecognized platform\"");
message += argv[i];
message += "\"";
PrintMessage(message);
return false;
}
}

// Print help
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
Expand Down Expand Up @@ -710,6 +733,15 @@ void CmdLineParser::PrintHelp()
" more comments, like: // cppcheck-suppress warningId\n"
" on the lines before the warning to suppress.\n"
" -j <jobs> Start [jobs] threads to do the checking simultaneously.\n"
" --platform=<type> Specifies platform specific types and sizes. The available platforms are:\n"
" * unix32\n"
" 32 bit unix variant\n"
" * unix64\n"
" 64 bit unix variant\n"
" * win32\n"
" 32 bit Windows\n"
" * win64\n"
" 64 bit Windows\n"
" -q, --quiet Only print error messages.\n"
" --report-progress Report progress messages while checking a file.\n"
#ifdef HAVE_RULES
Expand Down
80 changes: 80 additions & 0 deletions lib/settings.cpp
Expand Up @@ -50,6 +50,18 @@ Settings::Settings()
ifcfg = false;
checkConfiguration = false;
posix = false;

// This assumes the code you are checking is for the same architecture this is compiled on.
sizeof_bool = sizeof(bool);
sizeof_short = sizeof(short);
sizeof_int = sizeof(int);
sizeof_long = sizeof(long);
sizeof_long_long = sizeof(long long);
sizeof_float = sizeof(float);
sizeof_double = sizeof(double);
sizeof_long_double = sizeof(long double);
sizeof_size_t = sizeof(size_t);
sizeof_pointer = sizeof(void *);
}

std::string Settings::addEnabled(const std::string &str)
Expand Down Expand Up @@ -126,3 +138,71 @@ std::string Settings::append() const
{
return _append;
}

bool Settings::platform(PlatformType type)
{
switch (type)
{
case Host: // same as system this code was compile on
return true;
case Win32:
sizeof_bool = 1; // 4 in Visual C++ 4.2
sizeof_short = 2;
sizeof_int = 4;
sizeof_long = 4;
sizeof_long_long = 8;
sizeof_float = 4;
sizeof_double = 8;
sizeof_long_double = 8;
sizeof_size_t = 4;
sizeof_pointer = 4;
return true;
case Win64:
sizeof_bool = 1;
sizeof_short = 2;
sizeof_int = 4;
sizeof_long = 4;
sizeof_long_long = 8;
sizeof_float = 4;
sizeof_double = 8;
sizeof_long_double = 8;
sizeof_size_t = 8;
sizeof_pointer = 8;
return true;
case Unix32:
sizeof_bool = 1;
sizeof_short = 2;
sizeof_int = 4;
sizeof_long = 4;
sizeof_long_long = 8;
sizeof_float = 4;
sizeof_double = 8;
sizeof_long_double = 12;
sizeof_size_t = 4;
sizeof_pointer = 4;
return true;
case Unix64:
sizeof_bool = 1;
sizeof_short = 2;
sizeof_int = 4;
sizeof_long = 8;
sizeof_long_long = 8;
sizeof_float = 4;
sizeof_double = 8;
sizeof_long_double = 12;
sizeof_size_t = 8;
sizeof_pointer = 8;
return true;
}

// unsupported platform
return false;
}

bool Settings::platformFile(const std::string &filename)
{
(void)filename;
/** @todo TBD */

return false;
}
27 changes: 27 additions & 0 deletions lib/settings.h
Expand Up @@ -190,6 +190,33 @@ class Settings

/** Code is posix - it is not compatible with non-posix environments */
bool posix;

/** size of standard types */
unsigned int sizeof_bool;
unsigned int sizeof_short;
unsigned int sizeof_int;
unsigned int sizeof_long;
unsigned int sizeof_long_long;
unsigned int sizeof_float;
unsigned int sizeof_double;
unsigned int sizeof_long_double;
unsigned int sizeof_size_t;
unsigned int sizeof_pointer;

enum PlatformType
{
Host, // whatever system this code was compiled on
Win32,
Win64,
Unix32,
Unix64
};

/** set the platform type for predefined platforms */
bool platform(PlatformType type);

/** set the platform type for user specified platforms */
bool platformFile(const std::string &filename);
};

/// @}
Expand Down
36 changes: 14 additions & 22 deletions lib/tokenize.cpp
Expand Up @@ -225,9 +225,9 @@ unsigned int Tokenizer::sizeOfType(const Token *type) const
else if (type->isLong())
{
if (type->str() == "double")
return sizeof(long double);
return _settings->sizeof_long_double;
else if (type->str() == "long")
return sizeof(long long);
return _settings->sizeof_long_long;
}

return it->second;
Expand Down Expand Up @@ -2146,6 +2146,18 @@ bool Tokenizer::tokenize(std::istream &code,
// make sure settings specified
assert(_settings);

// Fill the map _typeSize..
_typeSize.clear();
_typeSize["char"] = 1;
_typeSize["bool"] = _settings->sizeof_bool;
_typeSize["short"] = _settings->sizeof_short;
_typeSize["int"] = _settings->sizeof_int;
_typeSize["long"] = _settings->sizeof_long;
_typeSize["float"] = _settings->sizeof_float;
_typeSize["double"] = _settings->sizeof_double;
_typeSize["size_t"] = _settings->sizeof_size_t;
_typeSize["*"] = _settings->sizeof_pointer;

_configuration = configuration;

// The "_files" vector remembers what files have been tokenized..
Expand Down Expand Up @@ -3272,15 +3284,6 @@ void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
// TODO: this is a bit hardcoded. make a bit more generic
if (Token::Match(tok2, "%var% < sizeof ( %type% ) >") && tok2->tokAt(4)->isStandardType())
{
// make sure standard types have a known size..
_typeSize["char"] = sizeof(char);
_typeSize["short"] = sizeof(short);
_typeSize["int"] = sizeof(int);
_typeSize["long"] = sizeof(long);
_typeSize["float"] = sizeof(float);
_typeSize["double"] = sizeof(double);
_typeSize["size_t"] = sizeof(size_t);

Token * const tok3 = tok2->next();
const unsigned int sz = sizeOfType(tok3->tokAt(3));
Token::eraseTokens(tok3, tok3->tokAt(5));
Expand Down Expand Up @@ -4230,17 +4233,6 @@ bool Tokenizer::createLinks()

void Tokenizer::simplifySizeof()
{
// Fill the map _typeSize..
_typeSize.clear();
_typeSize["char"] = sizeof(char);
_typeSize["short"] = sizeof(short);
_typeSize["int"] = sizeof(int);
_typeSize["long"] = sizeof(long);
_typeSize["float"] = sizeof(float);
_typeSize["double"] = sizeof(double);
_typeSize["size_t"] = sizeof(size_t);
_typeSize["*"] = sizeof(void *);

for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "class|struct %var%"))
Expand Down

0 comments on commit b5d22fd

Please sign in to comment.