Skip to content

Commit

Permalink
Preprocessor: several cleanups (#6829)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Sep 25, 2024
1 parent f0092bc commit e34b23b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
7 changes: 3 additions & 4 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
mSettings.supprs.nomsg.dump(oss);
dumpProlog += oss.str();
}
tokens1.removeComments();
preprocessor.removeComments();
preprocessor.removeComments(tokens1);

if (!mSettings.buildDir.empty()) {
// Get toolinfo
Expand Down Expand Up @@ -775,9 +774,9 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string

// Get directives
std::list<Directive> directives = preprocessor.createDirectives(tokens1);
preprocessor.simplifyPragmaAsm(&tokens1);
preprocessor.simplifyPragmaAsm(tokens1);

preprocessor.setPlatformInfo(&tokens1);
Preprocessor::setPlatformInfo(tokens1, mSettings);

// Get configurations..
std::set<std::string> configurations;
Expand Down
47 changes: 24 additions & 23 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,32 +777,33 @@ bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<
return !hasErrors(outputList);
}

void Preprocessor::removeComments()
void Preprocessor::removeComments(simplecpp::TokenList &tokens)
{
tokens.removeComments();
for (std::pair<const std::string, simplecpp::TokenList*>& tokenList : mTokenLists) {
if (tokenList.second)
tokenList.second->removeComments();
}
}

void Preprocessor::setPlatformInfo(simplecpp::TokenList *tokens) const
void Preprocessor::setPlatformInfo(simplecpp::TokenList &tokens, const Settings& settings)
{
tokens->sizeOfType["bool"] = mSettings.platform.sizeof_bool;
tokens->sizeOfType["short"] = mSettings.platform.sizeof_short;
tokens->sizeOfType["int"] = mSettings.platform.sizeof_int;
tokens->sizeOfType["long"] = mSettings.platform.sizeof_long;
tokens->sizeOfType["long long"] = mSettings.platform.sizeof_long_long;
tokens->sizeOfType["float"] = mSettings.platform.sizeof_float;
tokens->sizeOfType["double"] = mSettings.platform.sizeof_double;
tokens->sizeOfType["long double"] = mSettings.platform.sizeof_long_double;
tokens->sizeOfType["bool *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["short *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["int *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["long *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["long long *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["float *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["double *"] = mSettings.platform.sizeof_pointer;
tokens->sizeOfType["long double *"] = mSettings.platform.sizeof_pointer;
tokens.sizeOfType["bool"] = settings.platform.sizeof_bool;
tokens.sizeOfType["short"] = settings.platform.sizeof_short;
tokens.sizeOfType["int"] = settings.platform.sizeof_int;
tokens.sizeOfType["long"] = settings.platform.sizeof_long;
tokens.sizeOfType["long long"] = settings.platform.sizeof_long_long;
tokens.sizeOfType["float"] = settings.platform.sizeof_float;
tokens.sizeOfType["double"] = settings.platform.sizeof_double;
tokens.sizeOfType["long double"] = settings.platform.sizeof_long_double;
tokens.sizeOfType["bool *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["short *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["int *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["long *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["long long *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["float *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["double *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["long double *"] = settings.platform.sizeof_pointer;
}

simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool throwError)
Expand Down Expand Up @@ -986,18 +987,18 @@ std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, con
return (std::hash<std::string>{})(hashData);
}

void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList *tokenList) const
void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList &tokenList) const
{
Preprocessor::simplifyPragmaAsmPrivate(tokenList);
for (const std::pair<const std::string, simplecpp::TokenList*>& list : mTokenLists) {
Preprocessor::simplifyPragmaAsmPrivate(list.second);
Preprocessor::simplifyPragmaAsmPrivate(*list.second);
}
}

void Preprocessor::simplifyPragmaAsmPrivate(simplecpp::TokenList *tokenList)
void Preprocessor::simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList)
{
// assembler code..
for (simplecpp::Token *tok = tokenList->front(); tok; tok = tok->next) {
for (simplecpp::Token *tok = tokenList.front(); tok; tok = tok->next) {
if (tok->op != '#')
continue;
if (sameline(tok, tok->previousSkipComments()))
Expand Down Expand Up @@ -1032,7 +1033,7 @@ void Preprocessor::simplifyPragmaAsmPrivate(simplecpp::TokenList *tokenList)
const_cast<simplecpp::Token *>(tok3)->setstr(")");
const_cast<simplecpp::Token *>(tok4)->setstr(";");
while (tok4->next != endasm)
tokenList->deleteToken(tok4->next);
tokenList.deleteToken(tok4->next);
}
}

Expand Down
33 changes: 16 additions & 17 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
friend class TestUnusedVar;

public:

/**
* Include file types.
*/
enum HeaderTypes : std::uint8_t {
UserHeader = 1,
SystemHeader
};

/** character that is inserted in expanded macros */
static char macroChar;

Expand All @@ -128,13 +119,11 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {

std::vector<RemarkComment> getRemarkComments(const simplecpp::TokenList &tokens) const;

void handleErrors(const simplecpp::OutputList &outputList, bool throwError);

bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files);

void removeComments();
void removeComments(simplecpp::TokenList &tokens);

void setPlatformInfo(simplecpp::TokenList *tokens) const;
static void setPlatformInfo(simplecpp::TokenList &tokens, const Settings& settings);

simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool throwError = false);

Expand All @@ -149,7 +138,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
*/
std::size_t calculateHash(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const;

void simplifyPragmaAsm(simplecpp::TokenList *tokenList) const;
void simplifyPragmaAsm(simplecpp::TokenList &tokenList) const;

static void getErrorMessages(ErrorLogger &errorLogger, const Settings &settings);

Expand All @@ -158,12 +147,22 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
*/
void dump(std::ostream &out) const;

void reportOutput(const simplecpp::OutputList &outputList, bool showerror);

static bool hasErrors(const simplecpp::Output &output);

private:
static void simplifyPragmaAsmPrivate(simplecpp::TokenList *tokenList);
void handleErrors(const simplecpp::OutputList &outputList, bool throwError);

void reportOutput(const simplecpp::OutputList &outputList, bool showerror);

static void simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList);

/**
* Include file types.
*/
enum HeaderTypes : std::uint8_t {
UserHeader = 1,
SystemHeader
};

void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
Expand Down
5 changes: 2 additions & 3 deletions test/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& s
Preprocessor preprocessor(settings, errorlogger);
if (inlineSuppression)
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
tokens.removeComments();
preprocessor.simplifyPragmaAsm(&tokens);
preprocessor.removeComments();
preprocessor.removeComments(tokens);
preprocessor.simplifyPragmaAsm(tokens);

preprocessor.reportOutput(outputList, true);

Expand Down
4 changes: 2 additions & 2 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,16 @@ class TestPreprocessor : public TestFixture {
// preprocess code with unix32 platform..
{
const Settings settings = settingsBuilder().platform(Platform::Type::Unix32).build();
Preprocessor::setPlatformInfo(tokens, settings);
Preprocessor preprocessor(settings, *this);
preprocessor.setPlatformInfo(&tokens);
ASSERT_EQUALS("\n1", preprocessor.getcode(tokens, "", files, false));
}

// preprocess code with unix64 platform..
{
const Settings settings = settingsBuilder().platform(Platform::Type::Unix64).build();
Preprocessor::setPlatformInfo(tokens, settings);
Preprocessor preprocessor(settings, *this);
preprocessor.setPlatformInfo(&tokens);
ASSERT_EQUALS("\n\n\n2", preprocessor.getcode(tokens, "", files, false));
}
}
Expand Down

0 comments on commit e34b23b

Please sign in to comment.